o
    cKH                     @   sT  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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   6lib/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REVOKEDZUNKNOWNr   r   r   r   r   5   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-   r/   r   r   r   r   r!   ;   s$    


	r!   c                   @   s   e Zd ZeejdefddZeejdefddZeejde	j
fddZeejdefdd	Zejd
ejdefddZe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   r4   z-
        The hash of the issuer name
        Nr   r6   r   r   r   issuer_name_hash   r8   zOCSPRequest.issuer_name_hashc                 C   r4   zK
        The hash algorithm used in the issuer name and key hashes
        Nr   r6   r   r   r   hash_algorithm   r8   zOCSPRequest.hash_algorithmc                 C   r4   zM
        The serial number of the cert whose status is being checked
        Nr   r6   r   r   r   serial_number   r8   zOCSPRequest.serial_numberencodingc                 C   r4   )z/
        Serializes the request to DER
        Nr   r.   r?   r   r   r   public_bytes   r8   zOCSPRequest.public_bytesc                 C   r4   )zP
        The list of request extensions. Not single request extensions.
        Nr   r6   r   r   r   
extensions   r8   zOCSPRequest.extensionsN)r   r   r   propertyabcabstractmethodbytesr7   r:   r   r0   r<   intr>   r   EncodingrA   r   
ExtensionsrB   r   r   r   r   r3      s$    r3   )	metaclassc                   @   s   e Zd ZeejdefddZeejdej	e
j
 fddZeejdej	ej fddZeejde
j
fdd	Zeejdej	e
j
 fd
dZeejdefddZeejdefddZeejdejfddZeejdefddZdS )OCSPSingleResponser   c                 C   r4   zY
        The status of the certificate (an element from the OCSPCertStatus enum)
        Nr   r6   r   r   r   certificate_status   r8   z%OCSPSingleResponse.certificate_statusc                 C   r4   z^
        The date of when the certificate was revoked or None if not
        revoked.
        Nr   r6   r   r   r   r'      r8   z"OCSPSingleResponse.revocation_timec                 C   r4   zi
        The reason the certificate was revoked or None if not specified or
        not revoked.
        Nr   r6   r   r   r   r(      r8   z$OCSPSingleResponse.revocation_reasonc                 C   r4   z
        The most recent time at which the status being indicated is known by
        the responder to have been correct
        Nr   r6   r   r   r   r%      r8   zOCSPSingleResponse.this_updatec                 C   r4   zC
        The time when newer information will be available
        Nr   r6   r   r   r   r&      r8   zOCSPSingleResponse.next_updatec                 C   r4   r5   r   r6   r   r   r   r7      r8   z"OCSPSingleResponse.issuer_key_hashc                 C   r4   r9   r   r6   r   r   r   r:      r8   z#OCSPSingleResponse.issuer_name_hashc                 C   r4   r;   r   r6   r   r   r   r<      r8   z!OCSPSingleResponse.hash_algorithmc                 C   r4   r=   r   r6   r   r   r   r>      r8   z OCSPSingleResponse.serial_numberN)r   r   r   rC   rD   rE   r   rM   r1   r2   r,   r'   r   r-   r(   r%   r&   rF   r7   r:   r   r0   r<   rG   r>   r   r   r   r   rK      s8    rK   c                   @   sf  e Zd Zeejdeje fddZ	eejde
fddZeejdejfddZeejdejej fdd	Zeejdefd
dZeejdefddZeejdejej fddZeejdeje fddZeejdejej fddZeejdejfddZeejdefddZeejdejej fddZ eejdejej! fddZ"eejdejfddZ#eejdejej fddZ$eejdefd d!Z%eejdefd"d#Z&eejdejfd$d%Z'eejde(fd&d'Z)eejdej*fd(d)Z+e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   r4   )z_
        An iterator over the individual SINGLERESP structures in the
        response
        Nr   r6   r   r   r   	responses   r8   zOCSPResponse.responsesc                 C   r4   )zm
        The status of the response. This is a value from the OCSPResponseStatus
        enumeration
        Nr   r6   r   r   r   response_status   r8   zOCSPResponse.response_statusc                 C   r4   )zA
        The ObjectIdentifier of the signature algorithm
        Nr   r6   r   r   r   signature_algorithm_oid  r8   z$OCSPResponse.signature_algorithm_oidc                 C   r4   )zX
        Returns a HashAlgorithm corresponding to the type of the digest signed
        Nr   r6   r   r   r   signature_hash_algorithm  r8   z%OCSPResponse.signature_hash_algorithmc                 C   r4   )z%
        The signature bytes
        Nr   r6   r   r   r   	signature  r8   zOCSPResponse.signaturec                 C   r4   )z+
        The tbsResponseData bytes
        Nr   r6   r   r   r   tbs_response_bytes  r8   zOCSPResponse.tbs_response_bytesc                 C   r4   )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   r6   r   r   r   certificates  r8   zOCSPResponse.certificatesc                 C   r4   )z2
        The responder's key hash or None
        Nr   r6   r   r   r   responder_key_hash(  r8   zOCSPResponse.responder_key_hashc                 C   r4   )z.
        The responder's Name or None
        Nr   r6   r   r   r   responder_name/  r8   zOCSPResponse.responder_namec                 C   r4   )z4
        The time the response was produced
        Nr   r6   r   r   r   produced_at6  r8   zOCSPResponse.produced_atc                 C   r4   rL   r   r6   r   r   r   rM   =  r8   zOCSPResponse.certificate_statusc                 C   r4   rN   r   r6   r   r   r   r'   D  r8   zOCSPResponse.revocation_timec                 C   r4   rO   r   r6   r   r   r   r(   L  r8   zOCSPResponse.revocation_reasonc                 C   r4   rP   r   r6   r   r   r   r%   T  r8   zOCSPResponse.this_updatec                 C   r4   rQ   r   r6   r   r   r   r&   \  r8   zOCSPResponse.next_updatec                 C   r4   r5   r   r6   r   r   r   r7   c  r8   zOCSPResponse.issuer_key_hashc                 C   r4   r9   r   r6   r   r   r   r:   j  r8   zOCSPResponse.issuer_name_hashc                 C   r4   r;   r   r6   r   r   r   r<   q  r8   zOCSPResponse.hash_algorithmc                 C   r4   r=   r   r6   r   r   r   r>   x  r8   zOCSPResponse.serial_numberc                 C   r4   )zR
        The list of response extensions. Not single response extensions.
        Nr   r6   r   r   r   rB     r8   zOCSPResponse.extensionsc                 C   r4   )zR
        The list of single response extensions. Not response extensions.
        Nr   r6   r   r   r   single_extensions  r8   zOCSPResponse.single_extensionsr?   c                 C   r4   )z0
        Serializes the response to DER
        Nr   r@   r   r   r   rA     r8   zOCSPResponse.public_bytesN)0r   r   r   rC   rD   rE   r1   IteratorrK   rS   r   rT   r   ZObjectIdentifierrU   r2   r   r0   rV   rF   rW   rX   Listr*   rY   rZ   Namer[   r,   r\   r   rM   r'   r-   r(   r%   r&   r7   r:   r<   rG   r>   rI   rB   r]   r   rH   rA   r   r   r   r   rR      s    
rR   c                
   @   s   e Zd Zddg fdejejejejej	f  dejeje
e
e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
de
de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requestrequest_hashrB   r   c                 C   s   || _ || _|| _d S N)_request_request_hash_extensions)r.   rb   rc   rB   r   r   r   r/     s   
zOCSPRequestBuilder.__init__r"   r#   r   c                 C   sZ   | j d us
| jd urtdt| t|tjrt|tjs"tdt|||f| j| j	S )N.Only one certificate can be added to a requestr)   )
re   rf   r   r   r   r   r*   r+   ra   rg   )r.   r"   r#   r   r   r   r   add_certificate  s   z"OCSPRequestBuilder.add_certificater:   r7   r>   c                 C   s   | j d us
| jd urtdt|tstdt| td| td| |j	t
|ks5|j	t
|kr9tdt| j ||||f| jS )Nrh   z serial_number must be an integerr:   r7   z`issuer_name_hash and issuer_key_hash must be the same length as the digest size of the algorithm)re   rf   r   r   rG   r+   r   r   _check_bytesZdigest_sizelenra   rg   )r.   r:   r7   r>   r   r   r   r   add_certificate_by_hash  s&   

z*OCSPRequestBuilder.add_certificate_by_hashextvalcriticalc                 C   sH   t |tjs
tdt|j||}t|| j t| j	| j
| j|g S Nz"extension must be an ExtensionType)r   r   ExtensionTyper+   	Extensionoidr
   rg   ra   re   rf   r.   rm   rn   	extensionr   r   r   add_extension  s   z OCSPRequestBuilder.add_extensionc                 C   s&   | j d u r| jd u rtdt| S )Nz*You must add a certificate before building)re   rf   r   r   Zcreate_ocsp_requestr6   r   r   r   build  s   
zOCSPRequestBuilder.build)r   r   r   r1   r2   Tupler   r*   r   r0   rF   rG   r_   rq   rp   r/   ri   rl   boolru   r3   rv   r   r   r   r   ra     s^    




ra   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certsrB   c                 C   s   || _ || _|| _|| _d S rd   )	_response_responder_id_certsrg   )r.   rz   r{   r|   rB   r   r   r   r/     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.)r}   r   r!   ry   r~   r   rg   )
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)r~   r   r   r   r*   r+   r   ry   r}   r   rg   )r.   r?   r   r   r   r   r{     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>3  s    z3OCSPResponseBuilder.certificates.<locals>.<genexpr>z$certs must be a list of Certificates)
r   r   listrk   allr+   ry   r}   r~   rg   )r.   r|   r   r   r   rY   +  s   
z OCSPResponseBuilder.certificatesrm   rn   c                 C   sL   t |tjs
tdt|j||}t|| j t| j	| j
| j| j|g S ro   )r   r   rp   r+   rq   rr   r
   rg   ry   r}   r~   r   rs   r   r   r   ru   <  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)r}   r   r~   r   create_ocsp_responser   r   )r.   r   r   r   r   r   signL  s   


zOCSPResponseBuilder.signrT   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   )clsrT   r   r   r   build_unsuccessfulZ  s   

z&OCSPResponseBuilder.build_unsuccessful)r   r   r   r1   r2   r!   rw   r   r*   r   r_   rq   rp   r/   r   r0   r   r,   r-   r   r{   IterablerY   rx   ru   r   rR   r   classmethodr   r   r   r   r   r   ry     s    



	







ry   datac                 C   
   t | S rd   )r   load_der_ocsp_requestr   r   r   r   r   h     
r   c                 C   r   rd   )r   load_der_ocsp_responser   r   r   r   r   l  r   r   )'rD   r,   r1   Z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   r0   r   r   r!   ABCMetar3   rK   rR   ra   ry   rF   r   r   r   r   r   r   <module>   s6   
	F+D %V~