o
    c3A                     @   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 d dl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e	jddfddZG dd dejZG dd 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!G dd dZ"G dd d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)CERTIFICATE_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__ZHASHNAME r   r   :usr/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_REQUESTZINTERNAL_ERRORZ	TRY_LATERZSIG_REQUIREDZUNAUTHORIZEDr   r   r   r   r      s    r   	algorithmreturnc                 C   s   t | ts	tdd S )Nz9Algorithm must be SHA1, SHA224, SHA256, SHA384, or SHA512)
isinstance_ALLOWED_HASHES
ValueError)r   r   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   6   s    r   c                   @   sV   e Z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 fd	d
ZdS )_SingleResponsecertissuerr   cert_statusthis_updatenext_updaterevocation_timerevocation_reasonc	           	      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_issuerZ
_algorithmZ_this_updateZ_next_updater   r    r   r	   r   ReasonFlagsZ_cert_statusZ_revocation_timeZ_revocation_reason)	selfr#   r$   r   r%   r&   r'   r(   r)   r   r   r   __init__=   s\   




z_SingleResponse.__init__N)r   r   r   r   r+   r   HashAlgorithmr   r-   typingOptionalr.   r0   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 )OCSPRequestr   c                 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   r5   z-
        The hash of the issuer name
        Nr   r7   r   r   r   issuer_name_hash   r9   zOCSPRequest.issuer_name_hashc                 C   r5   zK
        The hash algorithm used in the issuer name and key hashes
        Nr   r7   r   r   r   hash_algorithm   r9   zOCSPRequest.hash_algorithmc                 C   r5   zM
        The serial number of the cert whose status is being checked
        Nr   r7   r   r   r   serial_number   r9   zOCSPRequest.serial_numberencodingc                 C   r5   )z/
        Serializes the request to DER
        Nr   r/   r@   r   r   r   public_bytes   r9   zOCSPRequest.public_bytesc                 C   r5   )zP
        The list of request extensions. Not single request extensions.
        Nr   r7   r   r   r   
extensions   r9   zOCSPRequest.extensionsN)r   r   r   abcabstractpropertybytesr8   r;   r   r1   r=   intr?   abstractmethodr   EncodingrB   r   
ExtensionsrC   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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dS )OCSPSingleResponser   c                 C   r5   zY
        The status of the certificate (an element from the OCSPCertStatus enum)
        Nr   r7   r   r   r   certificate_status   r9   z%OCSPSingleResponse.certificate_statusc                 C   r5   z^
        The date of when the certificate was revoked or None if not
        revoked.
        Nr   r7   r   r   r   r(      r9   z"OCSPSingleResponse.revocation_timec                 C   r5   zi
        The reason the certificate was revoked or None if not specified or
        not revoked.
        Nr   r7   r   r   r   r)      r9   z$OCSPSingleResponse.revocation_reasonc                 C   r5   z
        The most recent time at which the status being indicated is known by
        the responder to have been correct
        Nr   r7   r   r   r   r&      r9   zOCSPSingleResponse.this_updatec                 C   r5   zC
        The time when newer information will be available
        Nr   r7   r   r   r   r'      r9   zOCSPSingleResponse.next_updatec                 C   r5   r6   r   r7   r   r   r   r8      r9   z"OCSPSingleResponse.issuer_key_hashc                 C   r5   r:   r   r7   r   r   r   r;      r9   z#OCSPSingleResponse.issuer_name_hashc                 C   r5   r<   r   r7   r   r   r   r=      r9   z!OCSPSingleResponse.hash_algorithmc                 C   r5   r>   r   r7   r   r   r   r?      r9   z OCSPSingleResponse.serial_numberN)r   r   r   rD   rE   r   rN   r2   r3   r-   r(   r   r.   r)   r&   r'   rF   r8   r;   r   r1   r=   rG   r?   r   r   r   r   rL      s&    rL   c                   @   s  e Zd Zejdej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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 )0OCSPResponser   c                 C   r5   )z_
        An iterator over the individual SINGLERESP structures in the
        response
        Nr   r7   r   r   r   	responses   r9   zOCSPResponse.responsesc                 C   r5   )zm
        The status of the response. This is a value from the OCSPResponseStatus
        enumeration
        Nr   r7   r   r   r   response_status   r9   zOCSPResponse.response_statusc                 C   r5   )zA
        The ObjectIdentifier of the signature algorithm
        Nr   r7   r   r   r   signature_algorithm_oid   r9   z$OCSPResponse.signature_algorithm_oidc                 C   r5   )zX
        Returns a HashAlgorithm corresponding to the type of the digest signed
        Nr   r7   r   r   r   signature_hash_algorithm   r9   z%OCSPResponse.signature_hash_algorithmc                 C   r5   )z%
        The signature bytes
        Nr   r7   r   r   r   	signature   r9   zOCSPResponse.signaturec                 C   r5   )z+
        The tbsResponseData bytes
        Nr   r7   r   r   r   tbs_response_bytes  r9   zOCSPResponse.tbs_response_bytesc                 C   r5   )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   r7   r   r   r   certificates  r9   zOCSPResponse.certificatesc                 C   r5   )z2
        The responder's key hash or None
        Nr   r7   r   r   r   responder_key_hash  r9   zOCSPResponse.responder_key_hashc                 C   r5   )z.
        The responder's Name or None
        Nr   r7   r   r   r   responder_name  r9   zOCSPResponse.responder_namec                 C   r5   )z4
        The time the response was produced
        Nr   r7   r   r   r   produced_at   r9   zOCSPResponse.produced_atc                 C   r5   rM   r   r7   r   r   r   rN   &  r9   zOCSPResponse.certificate_statusc                 C   r5   rO   r   r7   r   r   r   r(   ,  r9   zOCSPResponse.revocation_timec                 C   r5   rP   r   r7   r   r   r   r)   3  r9   zOCSPResponse.revocation_reasonc                 C   r5   rQ   r   r7   r   r   r   r&   :  r9   zOCSPResponse.this_updatec                 C   r5   rR   r   r7   r   r   r   r'   A  r9   zOCSPResponse.next_updatec                 C   r5   r6   r   r7   r   r   r   r8   G  r9   zOCSPResponse.issuer_key_hashc                 C   r5   r:   r   r7   r   r   r   r;   M  r9   zOCSPResponse.issuer_name_hashc                 C   r5   r<   r   r7   r   r   r   r=   S  r9   zOCSPResponse.hash_algorithmc                 C   r5   r>   r   r7   r   r   r   r?   Y  r9   zOCSPResponse.serial_numberc                 C   r5   )zR
        The list of response extensions. Not single response extensions.
        Nr   r7   r   r   r   rC   _  r9   zOCSPResponse.extensionsc                 C   r5   )zR
        The list of single response extensions. Not response extensions.
        Nr   r7   r   r   r   single_extensionse  r9   zOCSPResponse.single_extensionsr@   c                 C   r5   )z0
        Serializes the response to DER
        Nr   rA   r   r   r   rB   k  r9   zOCSPResponse.public_bytesN)0r   r   r   rD   rE   r2   IteratorrL   rT   r   rU   r   ZObjectIdentifierrV   r3   r   r1   rW   rF   rX   rY   Listr+   rZ   r[   Namer\   r-   r]   r   rN   r(   r.   r)   r&   r'   r8   r;   r=   rG   r?   rJ   rC   r^   rH   r   rI   rB   r   r   r   r   rS      s^    
rS   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requestrC   r   c                 C   s   || _ || _d S N)_request_extensions)r/   rc   rC   r   r   r   r0   s  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*   )	re   r   r   r   r   r+   r,   rb   rf   )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
   rf   rb   re   r/   rh   ri   	extensionr   r   r   add_extension  s   z OCSPRequestBuilder.add_extensionc                 C   s   | j d u r	tdt| S )Nz*You must add a certificate before building)re   r   r   Zcreate_ocsp_requestr7   r   r   r   build  s   

zOCSPRequestBuilder.build)r   r   r   r2   r3   Tupler   r+   r   r1   r`   rl   rk   r0   rg   boolrp   r4   rq   r   r   r   r   rb   r  s>    


rb   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certsrC   c                 C   s   || _ || _|| _|| _d S rd   )	_response_responder_id_certsrf   )r/   ru   rv   rw   rC   r   r   r   r0     s   	
zOCSPResponseBuilder.__init__r#   r$   r   r%   r&   r'   r(   r)   r   c	           
   	   C   s<   | j d ur	tdt||||||||}	t|	| j| j| jS )Nz#Only one response per OCSPResponse.)rx   r   r"   rt   ry   rz   rf   )
r/   r#   r$   r   r%   r&   r'   r(   r)   Z
singlerespr   r   r   add_response  s$   

z OCSPResponseBuilder.add_responser@   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)ry   r   r   r   r+   r,   r   rt   rx   rz   rf   )r/   r@   r|   r   r   r   rv     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 rd   )r   r   r+   ).0xr   r   r   	<genexpr>  s    z3OCSPResponseBuilder.certificates.<locals>.<genexpr>z$certs must be a list of Certificates)
rz   r   listlenallr,   rt   rx   ry   rf   )r/   rw   r   r   r   rZ     s   
z OCSPResponseBuilder.certificatesrh   ri   c                 C   sL   t |tjs
tdt|j||}t|| j t| j	| j
| j| j|g S rj   )r   r   rk   r,   rl   rm   r
   rf   rt   rx   ry   rz   rn   r   r   r   rp     s   
z!OCSPResponseBuilder.add_extensionprivate_keyc                 C   s6   | j d u r	td| jd u rtdttj| ||S )Nz&You must add a response before signingz*You must add a responder_id before signing)rx   r   ry   r   create_ocsp_responser   r   )r/   r   r   r   r   r   sign  s   


zOCSPResponseBuilder.signrU   c                 C   s4   t |ts	td|tju rtdt|d d d S )Nz7response_status must be an item from OCSPResponseStatusz$response_status cannot be SUCCESSFUL)r   r   r,   r   r   r   r   )clsrU   r   r   r   build_unsuccessful  s   

z&OCSPResponseBuilder.build_unsuccessful)r   r   r   r2   r3   r"   rr   r   r+   r   r`   rl   rk   r0   r   r1   r   r-   r.   r{   rv   IterablerZ   rs   rp   r   rS   r   classmethodr   r   r   r   r   r   rt     s    



	







rt   datac                 C   
   t | S rd   )r   load_der_ocsp_requestr   r   r   r   r   "     
r   c                 C   r   rd   )r   load_der_ocsp_responser   r   r   r   r   &  r   r   )'rD   r-   r2   cryptographyr   r   Z"cryptography.hazmat.bindings._rustr   Zcryptography.hazmat.primitivesr   r   Z/cryptography.hazmat.primitives.asymmetric.typesr   Zcryptography.x509.baser   r	   r
   Enumr   r   ZSHA1ZSHA224ZSHA256ZSHA384ZSHA512r   r1   r   r   r"   ABCMetar4   rL   rS   rb   rt   rF   r   r   r   r   r   r   <module>   s8   
	F&; 2~