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__HASHNAME r   r   C/opt/certbot/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   
SUCCESSFULMALFORMED_REQUESTINTERNAL_ERROR	TRY_LATERSIG_REQUIRED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   GOODREVOKED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$   datetime_cert_issuer
_algorithm_this_update_next_updater%   r'   r#   r	   r   ReasonFlags_cert_status_revocation_time_revocation_reason)	selfr*   r+   r   r,   r-   r.   r/   r0   r   r   r   __init__<   s\   




z_SingleResponse.__init__N)r   r   r   r   r2   r   HashAlgorithmr%   r4   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   rD   z-
        The hash of the issuer name
        Nr   rF   r   r   r   issuer_name_hash   rH   zOCSPRequest.issuer_name_hashc                 C   rD   zK
        The hash algorithm used in the issuer name and key hashes
        Nr   rF   r   r   r   hash_algorithm   rH   zOCSPRequest.hash_algorithmc                 C   rD   zM
        The serial number of the cert whose status is being checked
        Nr   rF   r   r   r   serial_number   rH   zOCSPRequest.serial_numberencodingc                 C   rD   )z/
        Serializes the request to DER
        Nr   r>   rO   r   r   r   public_bytes   rH   zOCSPRequest.public_bytesc                 C   rD   )zP
        The list of request extensions. Not single request extensions.
        Nr   rF   r   r   r   
extensions   rH   zOCSPRequest.extensionsN)r   r   r   propertyabcabstractmethodbytesrG   rJ   r   r@   rL   intrN   r   EncodingrQ   r   
ExtensionsrR   r   r   r   r   rC      s$    rC   )	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   rD   zY
        The status of the certificate (an element from the OCSPCertStatus enum)
        Nr   rF   r   r   r   certificate_status   rH   z%OCSPSingleResponse.certificate_statusc                 C   rD   z^
        The date of when the certificate was revoked or None if not
        revoked.
        Nr   rF   r   r   r   r/      rH   z"OCSPSingleResponse.revocation_timec                 C   rD   zi
        The reason the certificate was revoked or None if not specified or
        not revoked.
        Nr   rF   r   r   r   r0      rH   z$OCSPSingleResponse.revocation_reasonc                 C   rD   z
        The most recent time at which the status being indicated is known by
        the responder to have been correct
        Nr   rF   r   r   r   r-      rH   zOCSPSingleResponse.this_updatec                 C   rD   zC
        The time when newer information will be available
        Nr   rF   r   r   r   r.      rH   zOCSPSingleResponse.next_updatec                 C   rD   rE   r   rF   r   r   r   rG      rH   z"OCSPSingleResponse.issuer_key_hashc                 C   rD   rI   r   rF   r   r   r   rJ      rH   z#OCSPSingleResponse.issuer_name_hashc                 C   rD   rK   r   rF   r   r   r   rL      rH   z!OCSPSingleResponse.hash_algorithmc                 C   rD   rM   r   rF   r   r   r   rN      rH   z OCSPSingleResponse.serial_numberN)r   r   r   rS   rT   rU   r%   r]   rA   rB   r4   r/   r   r:   r0   r-   r.   rV   rG   rJ   r   r@   rL   rW   rN   r   r   r   r   r[      s8    r[   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   rD   )z_
        An iterator over the individual SINGLERESP structures in the
        response
        Nr   rF   r   r   r   	responses   rH   zOCSPResponse.responsesc                 C   rD   )zm
        The status of the response. This is a value from the OCSPResponseStatus
        enumeration
        Nr   rF   r   r   r   response_status   rH   zOCSPResponse.response_statusc                 C   rD   )zA
        The ObjectIdentifier of the signature algorithm
        Nr   rF   r   r   r   signature_algorithm_oid  rH   z$OCSPResponse.signature_algorithm_oidc                 C   rD   )zX
        Returns a HashAlgorithm corresponding to the type of the digest signed
        Nr   rF   r   r   r   signature_hash_algorithm  rH   z%OCSPResponse.signature_hash_algorithmc                 C   rD   )z%
        The signature bytes
        Nr   rF   r   r   r   	signature  rH   zOCSPResponse.signaturec                 C   rD   )z+
        The tbsResponseData bytes
        Nr   rF   r   r   r   tbs_response_bytes  rH   zOCSPResponse.tbs_response_bytesc                 C   rD   )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   rF   r   r   r   certificates  rH   zOCSPResponse.certificatesc                 C   rD   )z2
        The responder's key hash or None
        Nr   rF   r   r   r   responder_key_hash(  rH   zOCSPResponse.responder_key_hashc                 C   rD   )z.
        The responder's Name or None
        Nr   rF   r   r   r   responder_name/  rH   zOCSPResponse.responder_namec                 C   rD   )z4
        The time the response was produced
        Nr   rF   r   r   r   produced_at6  rH   zOCSPResponse.produced_atc                 C   rD   r\   r   rF   r   r   r   r]   =  rH   zOCSPResponse.certificate_statusc                 C   rD   r^   r   rF   r   r   r   r/   D  rH   zOCSPResponse.revocation_timec                 C   rD   r_   r   rF   r   r   r   r0   L  rH   zOCSPResponse.revocation_reasonc                 C   rD   r`   r   rF   r   r   r   r-   T  rH   zOCSPResponse.this_updatec                 C   rD   ra   r   rF   r   r   r   r.   \  rH   zOCSPResponse.next_updatec                 C   rD   rE   r   rF   r   r   r   rG   c  rH   zOCSPResponse.issuer_key_hashc                 C   rD   rI   r   rF   r   r   r   rJ   j  rH   zOCSPResponse.issuer_name_hashc                 C   rD   rK   r   rF   r   r   r   rL   q  rH   zOCSPResponse.hash_algorithmc                 C   rD   rM   r   rF   r   r   r   rN   x  rH   zOCSPResponse.serial_numberc                 C   rD   )zR
        The list of response extensions. Not single response extensions.
        Nr   rF   r   r   r   rR     rH   zOCSPResponse.extensionsc                 C   rD   )zR
        The list of single response extensions. Not response extensions.
        Nr   rF   r   r   r   single_extensions  rH   zOCSPResponse.single_extensionsrO   c                 C   rD   )z0
        Serializes the response to DER
        Nr   rP   r   r   r   rQ     rH   zOCSPResponse.public_bytesN)0r   r   r   rS   rT   rU   rA   Iteratorr[   rc   r   rd   r   ObjectIdentifierre   rB   r   r@   rf   rV   rg   rh   Listr2   ri   rj   Namerk   r4   rl   r%   r]   r/   r:   r0   r-   r.   rG   rJ   rL   rW   rN   rY   rR   rm   r   rX   rQ   r   r   r   r   rb      s    
rb   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_hashrR   r    c                 C   s   || _ || _|| _d S N)_request_request_hash_extensions)r>   rs   rt   rR   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 requestr1   )
rv   rw   r#   r$   r!   r   r2   r3   rr   rx   )r>   r*   r+   r   r   r   r   add_certificate  s   z"OCSPRequestBuilder.add_certificaterJ   rG   rN   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 )Nry   z serial_number must be an integerrJ   rG   z`issuer_name_hash and issuer_key_hash must be the same length as the digest size of the algorithm)rv   rw   r#   r!   rW   r3   r$   r   _check_bytesdigest_sizelenrr   rx   )r>   rJ   rG   rN   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   ExtensionTyper3   	Extensionoidr
   rx   rr   rv   rw   r>   r   r   	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)rv   rw   r#   r   create_ocsp_requestrF   r   r   r   build  s   
zOCSPRequestBuilder.build)r   r   r   rA   rB   Tupler   r2   r   r@   rV   rW   rp   r   r   r?   rz   r~   boolr   rC   r   r   r   r   r   rr     s^    




rr   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certsrR   c                 C   s   || _ || _|| _|| _d S ru   )	_response_responder_id_certsrx   )r>   r   r   r   rR   r   r   r   r?     s   	
zOCSPResponseBuilder.__init__r*   r+   r   r,   r-   r.   r/   r0   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)   r   r   r   rx   )
r>   r*   r+   r   r,   r-   r.   r/   r0   
singlerespr   r   r   add_response  s$   

z OCSPResponseBuilder.add_responserO   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   r2   r3   r   r   r   r   rx   )r>   rO   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 ru   )r!   r   r2   ).0xr   r   r   	<genexpr>3  s    z3OCSPResponseBuilder.certificates.<locals>.<genexpr>z$certs must be a list of Certificates)
r   r#   listr}   allr3   r   r   r   rx   )r>   r   r   r   r   ri   +  s   
z OCSPResponseBuilder.certificatesr   r   c                 C   sL   t |tjs
tdt|j||}t|| j t| j	| j
| j| j|g S r   )r!   r   r   r3   r   r   r
   rx   r   r   r   r   r   r   r   r   r   <  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.signrd   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   r3   r   r#   r   r   )clsrd   r   r   r   build_unsuccessfulZ  s   

z&OCSPResponseBuilder.build_unsuccessful)r   r   r   rA   rB   r)   r   r   r2   r   rp   r   r   r?   r   r@   r%   r4   r:   r   r   Iterableri   r   r   r   rb   r   classmethodr   r   r   r   r   r   r     s    



	







r   datac                 C   
   t | S ru   )r   load_der_ocsp_requestr   r   r   r   r   h     
r   c                 C   r   ru   )r   load_der_ocsp_responser   r   r   r   r   l  r   r   )'rT   r4   rA   cryptographyr   r   "cryptography.hazmat.bindings._rustr   cryptography.hazmat.primitivesr   r   /cryptography.hazmat.primitives.asymmetric.typesr   cryptography.x509.baser   r	   r
   Enumr   r   SHA1SHA224SHA256SHA384SHA512r"   r@   r$   r%   r)   ABCMetarC   r[   rb   rr   r   rV   r   r   r   r   r   r   <module>   s6   
	F+D %V~