o
    Df                     @  s  d Z ddl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 ddlmZ ddlmZmZ dd	lmZ d
dlmZ erpddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ dZ%G dd dZ&G dd dZ'G dd de'Z(dS )zX.509 certificates.    )annotationsN)TYPE_CHECKINGIterator)default_backend)paddingrsa)load_pem_x509_certificate)bytes_to_strensure_bytes)SecurityError   )reraise_errors)DSAPublicKey)EllipticCurvePublicKey)Ed448PublicKey)Ed25519PublicKey)RSAPublicKey)	Prehashed)HashAlgorithm)Certificate	CertStoreFSCertStorec                   @  sV   e Zd ZdZdddZdd	d
ZdddZd ddZd!ddZd!ddZ	d"ddZ
dS )#r   zX.509 certificate.certstrreturnNonec                 C  sb   t dtfd  tt|t d| _t| j tj	stdW d    d S 1 s*w   Y  d S )NzInvalid certificate: {0!r})errors)backendz'Non-RSA certificates are not supported.)
r   
ValueErrorr   r
   r   _cert
isinstance
public_keyr   r   )selfr    r#   T/home/ubuntu/webapp/venv/lib/python3.10/site-packages/celery/security/certificate.py__init__"   s   
"zCertificate.__init__boolc                 C  s   t j t jj| jjkS )z%Check if the certificate has expired.)datetimenowtimezoneutcr   not_valid_afterr"   r#   r#   r$   has_expired,      zCertificate.has_expiredXDSAPublicKey | EllipticCurvePublicKey | Ed448PublicKey | Ed25519PublicKey | RSAPublicKeyc                 C  s
   | j  S N)r   r!   r,   r#   r#   r$   
get_pubkey0   s   
zCertificate.get_pubkeyintc                 C  s   | j jS )z,Return the serial number in the certificate.)r   serial_numberr,   r#   r#   r$   get_serial_number5   s   zCertificate.get_serial_numberc                 C  s   d dd | jjD S )zReturn issuer (CA) as a string. c                 s  s    | ]}|j V  qd S r0   )value).0xr#   r#   r$   	<genexpr>;   s    z)Certificate.get_issuer.<locals>.<genexpr>)joinr   issuerr,   r#   r#   r$   
get_issuer9   r.   zCertificate.get_issuerc                 C  s   |    d|   S )z<Serial number/issuer pair uniquely identifies a certificate.r5   )r<   r4   r,   r#   r#   r$   get_id=   s   zCertificate.get_iddatabytes	signaturedigestHashAlgorithm | Prehashedc                 C  s^   t d! tjt|tjjd}|  |t||| W d   dS 1 s(w   Y  dS )z,Verify signature for string containing data.zBad signature: {0!r})mgfsalt_lengthN)r   r   PSSMGF1
MAX_LENGTHr1   verifyr
   )r"   r>   r@   rA   padr#   r#   r$   rH   A   s   
"zCertificate.verifyN)r   r   r   r   )r   r&   )r   r/   )r   r2   )r   r   )r>   r?   r@   r?   rA   rB   r   r   )__name__
__module____qualname____doc__r%   r-   r1   r4   r<   r=   rH   r#   r#   r#   r$   r      s    






r   c                   @  s8   e Zd ZdZdddZdddZdddZdddZdS )r   z"Base class for certificate stores.r   r   c                 C  s
   i | _ d S r0   )_certsr,   r#   r#   r$   r%   O   s   
zCertStore.__init__Iterator[Certificate]c                 c  s    | j  E dH  dS )zReturn certificate iterator.N)rN   valuesr,   r#   r#   r$   	itercertsR   s   zCertStore.itercertsidr   r   c                 C  s.   z| j t| W S  ty   td|w )zGet certificate by id.zUnknown certificate: )rN   r	   KeyErrorr   )r"   rR   r#   r#   r$   __getitem__V   s
   zCertStore.__getitem__r   c                 C  s2   t | }|| jv rtdt|| j|< d S )NzDuplicate certificate: )r	   r=   rN   r   rR   )r"   r   cert_idr#   r#   r$   add_cert]   s   
zCertStore.add_certN)r   r   )r   rO   )rR   r   r   r   )r   r   r   r   )rJ   rK   rL   rM   r%   rQ   rT   rV   r#   r#   r#   r$   r   L   s    


r   c                      s"   e Zd ZdZd fddZ  ZS )	r   zFile system certificate store.pathr   r   r   c              	     s   t    tj|rtj|d}t|D ].}t| }t|	 }|
 r1td| | | W d    n1 s@w   Y  qd S )N*zExpired certificate: )superr%   osrW   isdirr:   globopenr   readr-   r   r=   rV   )r"   rW   pfr   	__class__r#   r$   r%   g   s   

zFSCertStore.__init__)rW   r   r   r   )rJ   rK   rL   rM   r%   __classcell__r#   r#   ra   r$   r   d   s    r   ))rM   
__future__r   r'   r\   rZ   typingr   r   cryptography.hazmat.backendsr   )cryptography.hazmat.primitives.asymmetricr   r   cryptography.x509r   kombu.utils.encodingr	   r
   celery.exceptionsr   utilsr   -cryptography.hazmat.primitives.asymmetric.dsar   ,cryptography.hazmat.primitives.asymmetric.ecr   /cryptography.hazmat.primitives.asymmetric.ed448r   1cryptography.hazmat.primitives.asymmetric.ed25519r   -cryptography.hazmat.primitives.asymmetric.rsar   /cryptography.hazmat.primitives.asymmetric.utilsr   %cryptography.hazmat.primitives.hashesr   __all__r   r   r   r#   r#   r#   r$   <module>   s0    -