o
    b9                     @   s:  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 d dlm	Z	m
Z
 d dlmZmZmZmZ G dd dejZG d	d
 d
ejZe	je	je	je	je	jfZ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Z G dd deZ!de"defddZ#de"defddZ$dS )     N)utils)x509)ocsp)hashesserialization)PRIVATE_KEY_TYPES_EARLIEST_UTC_TIME_convert_to_naive_utc_time_reject_duplicate_extensionc                   @   s   e Zd ZdZdZdS )OCSPResponderEncodingzBy HashzBy NameN)__name__
__module____qualname__HASHNAME r   r   U/var/www/secure340b-portal/env/lib/python3.10/site-packages/cryptography/x509/ocsp.pyr      s    r   c                   @   s$   e Zd ZdZdZdZdZdZdZdS )OCSPResponseStatusr                  N)	r   r   r   
SUCCESSFULZMALFORMED_REQUESTINTERNAL_ERRORZ	TRY_LATERZSIG_REQUIREDUNAUTHORIZEDr   r   r   r   r      s    r   c                 C   s   t | ts	tdd S )Nz9Algorithm must be SHA1, SHA224, SHA256, SHA384, or SHA512)
isinstance_ALLOWED_HASHES
ValueError)	algorithmr   r   r   _verify_algorithm-   s
   
r    c                   @   s   e Zd ZdZdZdZdS )OCSPCertStatusr   r   r   N)r   r   r   ZGOODREVOKEDUNKNOWNr   r   r   r   r!   4   s    r!   c                   @   s   e Zd Zdd ZdS )_SingleResponsec	           	      C   s  t |tjrt |tjstdt| t |tjstd|d ur,t |tjs,td|| _|| _|| _|| _	|| _
t |tsDtd|tjurZ|d urQtd|d urYtdn$t |tjsdtdt|}|tk rptd|d ur~t |tjs~td	|| _|| _|| _d S )
N%cert and issuer must be a Certificatez%this_update must be a datetime objectz-next_update must be a datetime object or Nonez8cert_status must be an item from the OCSPCertStatus enumzBrevocation_time can only be provided if the certificate is revokedzDrevocation_reason can only be provided if the certificate is revokedz)revocation_time must be a datetime objectz7The revocation_time must be on or after 1950 January 1.zCrevocation_reason must be an item from the ReasonFlags enum or None)r   r   Certificate	TypeErrorr    datetimeZ_certZ_issuer
_algorithmZ_this_updateZ_next_updater!   r"   r   r	   r   ReasonFlagsZ_cert_statusZ_revocation_timeZ_revocation_reason)	selfcertissuerr   cert_statusthis_updatenext_updaterevocation_timerevocation_reasonr   r   r   __init__;   s\   




z_SingleResponse.__init__N)r   r   r   r3   r   r   r   r   r$   :   s    r$   c                   @   s   e Z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jdefddZejdejfddZdS )OCSPRequestreturnc                 C      dS z3
        The hash of the issuer public key
        Nr   r+   r   r   r   issuer_key_hash       zOCSPRequest.issuer_key_hashc                 C   r6   z-
        The hash of the issuer name
        Nr   r8   r   r   r   issuer_name_hash   r:   zOCSPRequest.issuer_name_hashc                 C   r6   zK
        The hash algorithm used in the issuer name and key hashes
        Nr   r8   r   r   r   hash_algorithm   r:   zOCSPRequest.hash_algorithmc                 C   r6   zM
        The serial number of the cert whose status is being checked
        Nr   r8   r   r   r   serial_number   r:   zOCSPRequest.serial_numberencodingc                 C   r6   )z/
        Serializes the request to DER
        Nr   r+   rA   r   r   r   public_bytes   r:   zOCSPRequest.public_bytesc                 C   r6   )zP
        The list of request extensions. Not single request extensions.
        Nr   r8   r   r   r   
extensions   r:   zOCSPRequest.extensionsN)r   r   r   abcabstractpropertybytesr9   r<   r   HashAlgorithmr>   intr@   abstractmethodr   EncodingrC   r   
ExtensionsrD   r   r   r   r   r4      s    r4   )	metaclassc                   @   s  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
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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
jej 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
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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*j+defd+d,Z,d-S ).OCSPResponser5   c                 C   r6   )zm
        The status of the response. This is a value from the OCSPResponseStatus
        enumeration
        Nr   r8   r   r   r   response_status   r:   zOCSPResponse.response_statusc                 C   r6   )zA
        The ObjectIdentifier of the signature algorithm
        Nr   r8   r   r   r   signature_algorithm_oid   r:   z$OCSPResponse.signature_algorithm_oidc                 C   r6   )zX
        Returns a HashAlgorithm corresponding to the type of the digest signed
        Nr   r8   r   r   r   signature_hash_algorithm   r:   z%OCSPResponse.signature_hash_algorithmc                 C   r6   )z%
        The signature bytes
        Nr   r8   r   r   r   	signature   r:   zOCSPResponse.signaturec                 C   r6   )z+
        The tbsResponseData bytes
        Nr   r8   r   r   r   tbs_response_bytes   r:   zOCSPResponse.tbs_response_bytesc                 C   r6   )z
        A list of certificates used to help build a chain to verify the OCSP
        response. This situation occurs when the OCSP responder uses a delegate
        certificate.
        Nr   r8   r   r   r   certificates   r:   zOCSPResponse.certificatesc                 C   r6   )z2
        The responder's key hash or None
        Nr   r8   r   r   r   responder_key_hash   r:   zOCSPResponse.responder_key_hashc                 C   r6   )z.
        The responder's Name or None
        Nr   r8   r   r   r   responder_name   r:   zOCSPResponse.responder_namec                 C   r6   )z4
        The time the response was produced
        Nr   r8   r   r   r   produced_at   r:   zOCSPResponse.produced_atc                 C   r6   )zY
        The status of the certificate (an element from the OCSPCertStatus enum)
        Nr   r8   r   r   r   certificate_status   r:   zOCSPResponse.certificate_statusc                 C   r6   )z^
        The date of when the certificate was revoked or None if not
        revoked.
        Nr   r8   r   r   r   r1      r:   zOCSPResponse.revocation_timec                 C   r6   )zi
        The reason the certificate was revoked or None if not specified or
        not revoked.
        Nr   r8   r   r   r   r2      r:   zOCSPResponse.revocation_reasonc                 C   r6   )z
        The most recent time at which the status being indicated is known by
        the responder to have been correct
        Nr   r8   r   r   r   r/      r:   zOCSPResponse.this_updatec                 C   r6   )zC
        The time when newer information will be available
        Nr   r8   r   r   r   r0      r:   zOCSPResponse.next_updatec                 C   r6   r7   r   r8   r   r   r   r9     r:   zOCSPResponse.issuer_key_hashc                 C   r6   r;   r   r8   r   r   r   r<   	  r:   zOCSPResponse.issuer_name_hashc                 C   r6   r=   r   r8   r   r   r   r>     r:   zOCSPResponse.hash_algorithmc                 C   r6   r?   r   r8   r   r   r   r@     r:   zOCSPResponse.serial_numberc                 C   r6   )zR
        The list of response extensions. Not single response extensions.
        Nr   r8   r   r   r   rD     r:   zOCSPResponse.extensionsc                 C   r6   )zR
        The list of single response extensions. Not response extensions.
        Nr   r8   r   r   r   single_extensions!  r:   zOCSPResponse.single_extensionsrA   c                 C   r6   )z0
        Serializes the response to DER
        Nr   rB   r   r   r   rC   '  r:   zOCSPResponse.public_bytesN)-r   r   r   rE   rF   r   rO   r   ZObjectIdentifierrP   typingOptionalr   rH   rQ   rG   rR   rS   Listr&   rT   rU   NamerV   r(   rW   r!   rX   r1   r*   r2   r/   r0   r9   r<   r>   rI   r@   rL   rD   rY   rJ   r   rK   rC   r   r   r   r   rN      sZ    
rN   c                   @   s   e Zd Zdg fdejejejejej	f  dej
ejej  ddfddZdejdejd	ej	dd fd
dZdejdedd fddZdefddZdS )OCSPRequestBuilderNrequestrD   r5   c                 C   s   || _ || _d S N)_request_extensions)r+   r_   rD   r   r   r   r3   /  s   	
zOCSPRequestBuilder.__init__r,   r-   r   c                 C   sL   | j d ur	tdt| t|tjrt|tjstdt|||f| jS )Nz.Only one certificate can be added to a requestr%   )	ra   r   r    r   r   r&   r'   r^   rb   )r+   r,   r-   r   r   r   r   add_certificate;  s   
z"OCSPRequestBuilder.add_certificateextvalcriticalc                 C   sD   t |tjs
tdt|j||}t|| j t| j	| j|g S Nz"extension must be an ExtensionType)
r   r   ExtensionTyper'   	Extensionoidr
   rb   r^   ra   r+   rd   re   	extensionr   r   r   add_extensionL  s   z OCSPRequestBuilder.add_extensionc                 C   s(   ddl m} | jd u rtd|| S )Nr   backendz*You must add a certificate before building),cryptography.hazmat.backends.openssl.backendrn   ra   r   Zcreate_ocsp_request)r+   rn   r   r   r   buildY  s   

zOCSPRequestBuilder.build)r   r   r   rZ   r[   Tupler   r&   r   rH   r\   rh   rg   r3   rc   boolrl   r4   rp   r   r   r   r   r^   .  s>    


r^   c                   @   s0  e Zd Zdddg fdeje dejejeje	f  dejej
ej  dej
ejej  fddZdejd	ejd
ejdedejdejej dejej dejej dd fddZde	dejdd fddZdejej dd fddZdejdedd fddZded
ejej defddZed edefd!d"ZdS )#OCSPResponseBuilderNresponseresponder_idcertsrD   c                 C   s   || _ || _|| _|| _d S r`   )	_response_responder_id_certsrb   )r+   rt   ru   rv   rD   r   r   r   r3   c  s   	
zOCSPResponseBuilder.__init__r,   r-   r   r.   r/   r0   r1   r2   r5   c	           
   	   C   s<   | j d ur	tdt||||||||}	t|	| j| j| jS )Nz#Only one response per OCSPResponse.)rw   r   r$   rs   rx   ry   rb   )
r+   r,   r-   r   r.   r/   r0   r1   r2   Z
singlerespr   r   r   add_responseq  s$   

z OCSPResponseBuilder.add_responserA   responder_certc                 C   sP   | j d ur	tdt|tjstdt|tstdt| j||f| j	| j
S )Nz!responder_id can only be set oncez$responder_cert must be a Certificatez6encoding must be an element from OCSPResponderEncoding)rx   r   r   r   r&   r'   r   rs   rw   ry   rb   )r+   rA   r{   r   r   r   ru     s   

z OCSPResponseBuilder.responder_idc                 C   s\   | j d ur	tdt|}t|dkrtdtdd |D s$tdt| j| j|| j	S )Nz!certificates may only be set oncer   zcerts must not be an empty listc                 s   s    | ]	}t |tjV  qd S r`   )r   r   r&   ).0xr   r   r   	<genexpr>  s    z3OCSPResponseBuilder.certificates.<locals>.<genexpr>z$certs must be a list of Certificates)
ry   r   listlenallr'   rs   rw   rx   rb   )r+   rv   r   r   r   rT     s   
z OCSPResponseBuilder.certificatesrd   re   c                 C   sL   t |tjs
tdt|j||}t|| j t| j	| j
| j| j|g S rf   )r   r   rg   r'   rh   ri   r
   rb   rs   rw   rx   ry   rj   r   r   r   rl     s   
z!OCSPResponseBuilder.add_extensionprivate_keyc                 C   sB   ddl m} | jd u rtd| jd u rtd|tj| ||S )Nr   rm   z&You must add a response before signingz*You must add a responder_id before signing)ro   rn   rw   r   rx   create_ocsp_responser   r   )r+   r   r   rn   r   r   r   sign  s   


zOCSPResponseBuilder.signrO   c                 C   s@   ddl m} t|tstd|tju rtd||d d d S )Nr   rm   z7response_status must be an item from OCSPResponseStatusz$response_status cannot be SUCCESSFUL)ro   rn   r   r   r'   r   r   r   )clsrO   rn   r   r   r   build_unsuccessful  s   

z&OCSPResponseBuilder.build_unsuccessful)r   r   r   rZ   r[   r$   rq   r   r&   r   r\   rh   rg   r3   r   rH   r!   r(   r*   rz   ru   IterablerT   rr   rl   r   rN   r   classmethodr   r   r   r   r   r   rs   b  s    



	







rs   datar5   c                 C   
   t | S r`   )r   load_der_ocsp_requestr   r   r   r   r        
r   c                 C   r   r`   )r   load_der_ocsp_responser   r   r   r   r     r   r   )%rE   r(   rZ   Zcryptographyr   r   Z"cryptography.hazmat.bindings._rustr   Zcryptography.hazmat.primitivesr   r   Zcryptography.x509.baser   r   r	   r
   Enumr   r   SHA1SHA224SHA256SHA384SHA512r   r    r!   objectr$   ABCMetar4   rN   r^   rs   rG   r   r   r   r   r   r   <module>   s6   
	F& 	4 