o
    ™¨Êh�O  ã                   @   s:  d dl mZmZmZ d dlZd dlZd dlZd dlmZm	Z	 d dl
mZ d dlmZmZmZmZmZmZmZmZ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 e  e	j!¡G d	d
„ d
e"ƒƒZ#e  e	j$¡G dd„ de"ƒƒZ%e  e	j&¡G dd„ de"ƒƒZ'e  e	j(¡G dd„ de"ƒƒZ)e  e	j*j+¡G dd„ de"ƒƒZ,dS )é    )Úabsolute_importÚdivisionÚprint_functionN)ÚutilsÚx509)ÚUnsupportedAlgorithm)
Ú_CERTIFICATE_EXTENSION_PARSERÚ$_CERTIFICATE_EXTENSION_PARSER_NO_SCTÚ_CRL_EXTENSION_PARSERÚ_CSR_EXTENSION_PARSERÚ%_REVOKED_CERTIFICATE_EXTENSION_PARSERÚ_asn1_integer_to_intÚ_asn1_string_to_bytesÚ_decode_x509_nameÚ_obj2txtÚ_parse_asn1_time)Ú_encode_asn1_int_gc)ÚhashesÚserialization)ÚdsaÚecÚrsac                   @   sÞ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Ze	dd„ ƒZ
e	dd„ ƒZe	dd„ ƒZdd„ Ze	dd„ ƒZe	dd„ ƒZe	dd„ ƒZe	dd„ ƒZe	dd„ ƒZe	dd „ ƒZejd!d"„ ƒZe	d#d$„ ƒZe	d%d&„ ƒZd'd(„ Zd)S )*Ú_Certificatec                 C   ó   || _ || _d S ©N)Ú_backendÚ_x509)ÚselfÚbackendr   © r   ú[/var/www/html/env/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/x509.pyÚ__init__   ó   
z_Certificate.__init__c                 C   s   d  | j¡S )Nz<Certificate(subject={0}, ...)>)ÚformatÚsubject©r   r   r   r    Ú__repr__    s   z_Certificate.__repr__c                 C   ó,   t |tjƒstS | jj | j|j¡}|dkS ©Nr   )Ú
isinstancer   ÚCertificateÚNotImplementedr   Ú_libÚX509_cmpr   ©r   ÚotherÚresr   r   r    Ú__eq__#   ó   z_Certificate.__eq__c                 C   ó
   | |k S r   r   ©r   r/   r   r   r    Ú__ne__*   ó   
z_Certificate.__ne__c                 C   ó   t |  tjj¡ƒS r   ©ÚhashÚpublic_bytesr   ÚEncodingÚDERr%   r   r   r    Ú__hash__-   ó   z_Certificate.__hash__c                 C   s*   t  || j¡}| |  tjj¡¡ | ¡ S r   )	r   ÚHashr   Úupdater:   r   r;   r<   Úfinalize)r   Ú	algorithmÚhr   r   r    Úfingerprint0   s   z_Certificate.fingerprintc                 C   sB   | j j | j¡}|dkrtjjS |dkrtjjS t d 	|¡|¡‚)Nr   é   z{0} is not a valid X509 version)
r   r,   ÚX509_get_versionr   r   ÚVersionÚv1Úv3ÚInvalidVersionr#   ©r   Úversionr   r   r    rL   5   s   
ÿz_Certificate.versionc                 C   s   t jdtjdd� | jS )Nz<Certificate serial is deprecated, use serial_number instead.rE   )Ú
stacklevel)ÚwarningsÚwarnr   ÚPersistentlyDeprecatedÚserial_numberr%   r   r   r    ÚserialA   s   ýz_Certificate.serialc                 C   ó2   | j j | j¡}| j  || j jjk¡ t| j |ƒS r   )r   r,   ÚX509_get_serialNumberr   Úopenssl_assertÚ_ffiÚNULLr   ©r   Úasn1_intr   r   r    rQ   J   ó   z_Certificate.serial_numberc                 C   sR   | j j | j¡}|| j jjkr| j  ¡  tdƒ‚| j j || j jj	¡}| j  
|¡S )Nz,Certificate public key is of an unknown type)r   r,   ÚX509_get_pubkeyr   rV   rW   Ú_consume_errorsÚ
ValueErrorÚgcÚEVP_PKEY_freeÚ_evp_pkey_to_public_key©r   Úpkeyr   r   r    Ú
public_keyP   s   
z_Certificate.public_keyc                 C   ó   | j j | j¡}t| j |ƒS r   )r   r,   ÚX509_get_notBeforer   r   ©r   Ú	asn1_timer   r   r    Únot_valid_before[   ó   z_Certificate.not_valid_beforec                 C   rd   r   )r   r,   ÚX509_get_notAfterr   r   rf   r   r   r    Únot_valid_after`   ri   z_Certificate.not_valid_afterc                 C   rS   r   )r   r,   ÚX509_get_issuer_namer   rU   rV   rW   r   ©r   Úissuerr   r   r    rn   e   rZ   z_Certificate.issuerc                 C   rS   r   )r   r,   ÚX509_get_subject_namer   rU   rV   rW   r   ©r   r$   r   r   r    r$   k   rZ   z_Certificate.subjectc                 C   ó0   | j }ztj| W S  ty   td |¡ƒ‚w ©Nz*Signature algorithm OID:{0} not recognized©Úsignature_algorithm_oidr   Ú_SIG_OIDS_TO_HASHÚKeyErrorr   r#   ©r   Úoidr   r   r    Úsignature_hash_algorithmq   ó   ÿÿz%_Certificate.signature_hash_algorithmc                 C   s^   | j j d¡}| j j | j jj|| j¡ | j  |d | j jjk¡ t| j |d j	ƒ}t
 |¡S ©NzX509_ALGOR **r   )r   rV   Únewr,   ÚX509_get0_signaturerW   r   rU   r   rB   r   ÚObjectIdentifier©r   Úalgrx   r   r   r    rt   {   ó   ÿ
z$_Certificate.signature_algorithm_oidc                 C   s*   | j jjrt | j | j¡S t | j | j¡S r   )r   r,   Ú#CRYPTOGRAPHY_OPENSSL_110_OR_GREATERr   Úparser   r	   r%   r   r   r    Ú
extensions…   s   
ÿÿz_Certificate.extensionsc                 C   sR   | j j d¡}| j j || j jj| j¡ | j  |d | j jjk¡ t| j |d ƒS ©NzASN1_BIT_STRING **r   )	r   rV   r|   r,   r}   rW   r   rU   r   ©r   Úsigr   r   r    Ú	signature�   ó   ÿz_Certificate.signaturec                    ód   ˆ j j d¡}ˆ j j ˆ j|¡}ˆ j  |dk¡ ˆ j j |‡ fdd„¡}ˆ j j |d |¡d d … S )Núunsigned char **r   c                    ó   ˆ j j | d ¡S r(   ©r   r,   ÚOPENSSL_free©Úpointerr%   r   r    Ú<lambda>Ÿ   ó    z4_Certificate.tbs_certificate_bytes.<locals>.<lambda>)	r   rV   r|   r,   Úi2d_re_X509_tbsr   rU   r^   Úbuffer©r   Úppr0   r   r%   r    Útbs_certificate_bytes™   ó   ÿz"_Certificate.tbs_certificate_bytesc                 C   ón   | j  ¡ }|tjju r| j j || j¡}n|tjju r%| j j 	|| j¡}nt
dƒ‚| j  |dk¡ | j  |¡S ©Nz/encoding must be an item from the Encoding enumé   )r   Ú_create_mem_bio_gcr   r;   ÚPEMr,   ÚPEM_write_bio_X509r   r<   Úi2d_X509_bioÚ	TypeErrorrU   Ú_read_mem_bio©r   ÚencodingÚbior0   r   r   r    r:   £   s   
z_Certificate.public_bytesN)Ú__name__Ú
__module__Ú__qualname__r!   r&   r1   r5   r=   rD   ÚpropertyrL   rR   rQ   rc   rh   rk   rn   r$   ry   rt   r   Úcached_propertyr„   rˆ   r—   r:   r   r   r   r    r      sB    







	
	



	r   c                   @   s:   e Zd Zdd„ Zedd„ ƒZedd„ ƒZejdd„ ƒZ	d	S )
Ú_RevokedCertificatec                 C   ó   || _ || _|| _d S r   )r   Ú_crlÚ_x509_revoked)r   r   ÚcrlÚx509_revokedr   r   r    r!   ²   s   
z_RevokedCertificate.__init__c                 C   rS   r   )r   r,   ÚX509_REVOKED_get0_serialNumberr­   rU   rV   rW   r   rX   r   r   r    rQ   ¾   s
   ÿz!_RevokedCertificate.serial_numberc                 C   s   t | j| jj | j¡ƒS r   )r   r   r,   Ú X509_REVOKED_get0_revocationDater­   r%   r   r   r    Úrevocation_dateÆ   s   ÿþz#_RevokedCertificate.revocation_datec                 C   ó   t  | j| j¡S r   )r   rƒ   r   r­   r%   r   r   r    r„   Ï   s   ÿz_RevokedCertificate.extensionsN)
r¥   r¦   r§   r!   r¨   rQ   r²   r   r©   r„   r   r   r   r    rª   °   s    

rª   c                   @   sÔ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zejd	d
„ ƒZ	dd„ Z
edd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zejd%d&„ ƒZd'd(„ Zd)S )*Ú_CertificateRevocationListc                 C   r   r   )r   Ú	_x509_crl)r   r   Úx509_crlr   r   r    r!   Ø   r"   z#_CertificateRevocationList.__init__c                 C   r'   r(   )r)   r   ÚCertificateRevocationListr+   r   r,   ÚX509_CRL_cmprµ   r.   r   r   r    r1   Ü   r2   z!_CertificateRevocationList.__eq__c                 C   r3   r   r   r4   r   r   r    r5   ã   r6   z!_CertificateRevocationList.__ne__c                 C   sX   t  || j¡}| j ¡ }| jj || j¡}| j |dk¡ | j |¡}| 	|¡ | 
¡ S )Nr›   )r   r?   r   rœ   r,   Úi2d_X509_CRL_biorµ   rU   r¡   r@   rA   )r   rB   rC   r¤   r0   Úderr   r   r    rD   æ   s   
ÿ
z&_CertificateRevocationList.fingerprintc                 C   s@   | j j | j¡}| j  || j jjk¡ | j j || j jj¡}|S r   )	r   r,   ÚX509_CRL_duprµ   rU   rV   rW   r^   ÚX509_CRL_free)r   Údupr   r   r    Ú_sorted_crlñ   s   z&_CertificateRevocationList._sorted_crlc                 C   sh   | j j d¡}t| j |ƒ}| j j | j||¡}|dkrd S | j  |d | j jjk¡ t	| j | j|d ƒS )NzX509_REVOKED **r   )
r   rV   r|   r   r,   ÚX509_CRL_get0_by_serialr¾   rU   rW   rª   )r   rQ   ÚrevokedrY   r0   r   r   r    Ú(get_revoked_certificate_by_serial_numberû   s   ÿÿÿzC_CertificateRevocationList.get_revoked_certificate_by_serial_numberc                 C   rq   rr   rs   rw   r   r   r    ry     rz   z3_CertificateRevocationList.signature_hash_algorithmc                 C   ó^   | j j d¡}| j j | j| j jj|¡ | j  |d | j jjk¡ t| j |d j	ƒ}t
 |¡S r{   )r   rV   r|   r,   ÚX509_CRL_get0_signaturerµ   rW   rU   r   rB   r   r~   r   r   r   r    rt     r�   z2_CertificateRevocationList.signature_algorithm_oidc                 C   rS   r   )r   r,   ÚX509_CRL_get_issuerrµ   rU   rV   rW   r   rm   r   r   r    rn     rZ   z!_CertificateRevocationList.issuerc                 C   rS   r   )r   r,   ÚX509_CRL_get_nextUpdaterµ   rU   rV   rW   r   )r   Únur   r   r    Únext_update%  rZ   z&_CertificateRevocationList.next_updatec                 C   rS   r   )r   r,   ÚX509_CRL_get_lastUpdaterµ   rU   rV   rW   r   )r   Úlur   r   r    Úlast_update+  rZ   z&_CertificateRevocationList.last_updatec                 C   óR   | j j d¡}| j j | j|| j jj¡ | j  |d | j jjk¡ t| j |d ƒS r…   )	r   rV   r|   r,   rÃ   rµ   rW   rU   r   r†   r   r   r    rˆ   1  r‰   z$_CertificateRevocationList.signaturec                    rŠ   )Nr‹   r   c                    rŒ   r(   r�   r�   r%   r   r    r‘   @  r’   z?_CertificateRevocationList.tbs_certlist_bytes.<locals>.<lambda>)	r   rV   r|   r,   Úi2d_re_X509_CRL_tbsrµ   rU   r^   r”   r•   r   r%   r    Útbs_certlist_bytes:  r˜   z-_CertificateRevocationList.tbs_certlist_bytesc                 C   r™   rš   )r   rœ   r   r;   r�   r,   ÚPEM_write_bio_X509_CRLrµ   r<   r¹   r    rU   r¡   r¢   r   r   r    r:   D  ó   
ÿz'_CertificateRevocationList.public_bytesc                 C   sD   | j j | j¡}| j j ||¡}| j  || j jjk¡ t| j | |ƒS r   )	r   r,   ÚX509_CRL_get_REVOKEDrµ   Úsk_X509_REVOKED_valuerU   rV   rW   rª   )r   ÚidxrÀ   Úrr   r   r    Ú_revoked_certR  s   z(_CertificateRevocationList._revoked_certc                 c   s$   � t t| ƒƒD ]}|  |¡V  qd S r   )ÚrangeÚlenrÔ   )r   Úir   r   r    Ú__iter__X  s   €ÿz#_CertificateRevocationList.__iter__c                    s€   t |tƒr| tˆ ƒ¡\}}}‡ fdd„t|||ƒD ƒS t |¡}|dk r+|tˆ ƒ7 }d|  kr8tˆ ƒk s;t‚ t‚ˆ  |¡S )Nc                    s   g | ]}ˆ   |¡‘qS r   )rÔ   )Ú.0r×   r%   r   r    Ú
<listcomp>_  s    z:_CertificateRevocationList.__getitem__.<locals>.<listcomp>r   )	r)   ÚsliceÚindicesrÖ   rÕ   ÚoperatorÚindexÚ
IndexErrorrÔ   )r   rÒ   ÚstartÚstopÚstepr   r%   r    Ú__getitem__\  s   

ÿ
z&_CertificateRevocationList.__getitem__c                 C   s0   | j j | j¡}|| j jjkrdS | j j |¡S r(   )r   r,   rÐ   rµ   rV   rW   Úsk_X509_REVOKED_num)r   rÀ   r   r   r    Ú__len__h  s   z"_CertificateRevocationList.__len__c                 C   r³   r   )r
   rƒ   r   rµ   r%   r   r   r    r„   o  s   z%_CertificateRevocationList.extensionsc                 C   sL   t |tjtjtjfƒstdƒ‚| jj	 
| j|j¡}|dkr$| j ¡  dS dS )NzGExpecting one of DSAPublicKey, RSAPublicKey, or EllipticCurvePublicKey.r›   FT)r)   r   ÚDSAPublicKeyr   ÚRSAPublicKeyr   ÚEllipticCurvePublicKeyr    r   r,   ÚX509_CRL_verifyrµ   Ú	_evp_pkeyr\   )r   rc   r0   r   r   r    Úis_signature_valids  s   ÿÿ
z-_CertificateRevocationList.is_signature_validN)r¥   r¦   r§   r!   r1   r5   rD   r   r©   r¾   rÁ   r¨   ry   rt   rn   rÇ   rÊ   rˆ   rÍ   r:   rÔ   rØ   rã   rå   r„   rë   r   r   r   r    r´   Ö   s<    
	
	
	




	
r´   c                   @   s’   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zedd„ ƒZ	edd„ ƒZ
edd„ ƒZejdd„ ƒZdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZdS )Ú_CertificateSigningRequestc                 C   r   r   )r   Ú	_x509_req)r   r   Úx509_reqr   r   r    r!   …  r"   z#_CertificateSigningRequest.__init__c                 C   s2   t |tƒstS |  tjj¡}| tjj¡}||kS r   )r)   rì   r+   r:   r   r;   r<   )r   r/   Ú
self_bytesÚother_bytesr   r   r    r1   ‰  s
   
z!_CertificateSigningRequest.__eq__c                 C   r3   r   r   r4   r   r   r    r5   ‘  r6   z!_CertificateSigningRequest.__ne__c                 C   r7   r   r8   r%   r   r   r    r=   ”  r>   z#_CertificateSigningRequest.__hash__c                 C   sH   | j j | j¡}| j  || j jjk¡ | j j || j jj¡}| j  	|¡S r   )
r   r,   ÚX509_REQ_get_pubkeyrí   rU   rV   rW   r^   r_   r`   ra   r   r   r    rc   —  s   z%_CertificateSigningRequest.public_keyc                 C   rS   r   )r   r,   ÚX509_REQ_get_subject_namerí   rU   rV   rW   r   rp   r   r   r    r$   �  rZ   z"_CertificateSigningRequest.subjectc                 C   rq   rr   rs   rw   r   r   r    ry   £  rz   z3_CertificateSigningRequest.signature_hash_algorithmc                 C   rÂ   r{   )r   rV   r|   r,   ÚX509_REQ_get0_signaturerí   rW   rU   r   rB   r   r~   r   r   r   r    rt   ­  r�   z2_CertificateSigningRequest.signature_algorithm_oidc                    s6   ˆ j j ˆ j¡}ˆ j j |‡ fdd„¡}t ˆ j |¡S )Nc                    s"   ˆ j j | ˆ j j ˆ j jjd¡¡S )NÚX509_EXTENSION_free)r   r,   Úsk_X509_EXTENSION_pop_freerV   Ú	addressofÚ_original_lib)Úxr%   r   r    r‘   ¼  s
    

ÿÿz7_CertificateSigningRequest.extensions.<locals>.<lambda>)r   r,   ÚX509_REQ_get_extensionsrí   rV   r^   r   rƒ   )r   Ú	x509_extsr   r%   r    r„   ·  s   
þz%_CertificateSigningRequest.extensionsc                 C   r™   rš   )r   rœ   r   r;   r�   r,   ÚPEM_write_bio_X509_REQrí   r<   Úi2d_X509_REQ_bior    rU   r¡   r¢   r   r   r    r:   Ä  rÏ   z'_CertificateSigningRequest.public_bytesc                    rŠ   )Nr‹   r   c                    rŒ   r(   r�   r�   r%   r   r    r‘   Ø  r’   zB_CertificateSigningRequest.tbs_certrequest_bytes.<locals>.<lambda>)	r   rV   r|   r,   Úi2d_re_X509_REQ_tbsrí   rU   r^   r”   r•   r   r%   r    Útbs_certrequest_bytesÒ  r˜   z0_CertificateSigningRequest.tbs_certrequest_bytesc                 C   rË   r…   )	r   rV   r|   r,   ró   rí   rW   rU   r   r†   r   r   r    rˆ   Ü  r‰   z$_CertificateSigningRequest.signaturec                 C   sh   | j j | j¡}| j  || j jjk¡ | j j || j jj¡}| j j 	| j|¡}|dkr2| j  
¡  dS dS )Nr›   FT)r   r,   rñ   rí   rU   rV   rW   r^   r_   ÚX509_REQ_verifyr\   )r   rb   r0   r   r   r    rë   å  s   
z-_CertificateSigningRequest.is_signature_validN)r¥   r¦   r§   r!   r1   r5   r=   rc   r¨   r$   ry   rt   r   r©   r„   r:   rþ   rˆ   rë   r   r   r   r    rì   ƒ  s*    

	
	

	
rì   c                   @   sh   e Zd Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZed	d
„ ƒZedd„ ƒZ	dd„ Z
dd„ Zdd„ ZdS )Ú_SignedCertificateTimestampc                 C   r«   r   )r   Ú	_sct_listÚ_sct)r   r   Úsct_listÚsctr   r   r    r!   ÷  s   
z$_SignedCertificateTimestamp.__init__c                 C   ó,   | j j | j¡}|| j jjksJ ‚tjjjS r   )	r   r,   ÚSCT_get_versionr  ÚSCT_VERSION_V1r   Úcertificate_transparencyrG   rH   rK   r   r   r    rL   ý  s   
z#_SignedCertificateTimestamp.versionc                 C   sH   | j j d¡}| j j | j|¡}|dksJ ‚| j j |d |¡d d … S ©Nr‹   r   )r   rV   r|   r,   ÚSCT_get0_log_idr  r”   )r   ÚoutÚlog_id_lengthr   r   r    Úlog_id  s   z"_SignedCertificateTimestamp.log_idc                 C   s4   | j j | j¡}|d }tj |d ¡j|d d�S )Niè  )Úmicrosecond)r   r,   ÚSCT_get_timestampr  ÚdatetimeÚutcfromtimestampÚreplace)r   Ú	timestampÚmillisecondsr   r   r    r  
  s   ÿþz%_SignedCertificateTimestamp.timestampc                 C   r  r   )	r   r,   ÚSCT_get_log_entry_typer  ÚCT_LOG_ENTRY_TYPE_PRECERTr   r  ÚLogEntryTypeÚPRE_CERTIFICATE)r   Ú
entry_typer   r   r    r    s   
z&_SignedCertificateTimestamp.entry_typec                 C   sf   | j j d¡}| j j | j|¡}| j  |dk¡ | j  |d | j jjk¡ | j j |d |¡d d … S r	  )	r   rV   r|   r,   ÚSCT_get0_signaturer  rU   rW   r”   )r   Úptrptrr0   r   r   r    Ú
_signature  s
   z&_SignedCertificateTimestamp._signaturec                 C   s
   t | jƒS r   )r9   r  r%   r   r   r    r=   "  r6   z$_SignedCertificateTimestamp.__hash__c                 C   s   t |tƒstS | j|jkS r   )r)   r   r+   r  r4   r   r   r    r1   %  s   
z"_SignedCertificateTimestamp.__eq__c                 C   r3   r   r   r4   r   r   r    r5   +  r6   z"_SignedCertificateTimestamp.__ne__N)r¥   r¦   r§   r!   r¨   rL   r  r  r  r  r=   r1   r5   r   r   r   r    r   ó  s    




r   )-Ú
__future__r   r   r   r  rÝ   rN   Úcryptographyr   r   Úcryptography.exceptionsr   Ú0cryptography.hazmat.backends.openssl.decode_asn1r   r	   r
   r   r   r   r   r   r   r   Ú0cryptography.hazmat.backends.openssl.encode_asn1r   Úcryptography.hazmat.primitivesr   r   Ú)cryptography.hazmat.primitives.asymmetricr   r   r   Úregister_interfacer*   Úobjectr   ÚRevokedCertificaterª   r·   r´   ÚCertificateSigningRequestrì   r  ÚSignedCertificateTimestampr   r   r   r   r    Ú<module>   s0   0
 

% 
-oÿ