
    h1                        d 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 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#m$Z$ ddl%m&Z& ddl'm(Z(m)Z* ejV                  Z,ejZ                  Z-ej\                  Z.ej^                  Z/ e0edd      Z1dZ2dZ3dZ4ejj                  Z6ejn                  ejp                  ejr                  ejt                  ejv                  ejt                  ejx                  z  iZ= e>d e=j                         D              Z@d ZAej                  ej                  ej                  fZEd ZF G d dej                        ZH G d deI      ZJ G d deI      ZKy)zMA CPython compatible SSLContext implementation wrapping PyOpenSSL's
context.
    N)EINTR)
ip_address)SSL)verify_hostnameverify_ip_address)CertificateErrorVerificationError)default_backend)_unicode)r   )time)_load_trusted_ca_certs_ocsp_callback)
_OCSPCache)_errno_from_exceptionSocketCheckerOP_NO_RENEGOTIATIONTc              #   *   K   | ]  \  }}||f  y wN ).0keyvalues      \/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/pymongo/pyopenssl_context.py	<genexpr>r   F   s      8S%UCL8s   c                 X    	 t        t        |              y# t        t        f$ r Y yw xY w)NTF)_ip_addressr   
ValueErrorUnicodeError)addresss    r   _is_ip_addressr    I   s.    HW%&% s    ))c                      | j                   dk(  S )z<Return True if the OpenSSL.SSL.SysCallError is a ragged EOF.)zUnexpected EOF)args)excs    r   _ragged_eofr%   V   s    88---    c                   N     e Zd Z fdZd Z fdZ fdZ fdZd fd	Z xZ	S )_sslConnc                 Z    t               | _        || _        t        t        |   ||       y r   )_SocketCheckersocket_checkersuppress_ragged_eofssuperr(   __init__)selfctxsockr,   	__class__s       r   r.   z_sslConn.__init__`   s'    ,.$8!h&sD1r&   c                     | j                         }|r
t               }	 	  ||i |S # t        $ rH | j                  j	                  | dd|       |r%t               z
  |kD  rt        j                  d      Y Zw xY w)NTz	timed out)
gettimeout_time_RETRY_ERRORSr+   select_sockettimeout)r/   callr#   kwargsr9   starts         r   _callz_sslConn._calle   s~    //#GET,V,,  ##**$g/uw8!//+66s   ' AA87A8c                 H     | j                   t        t        |   g|i |S r   )r=   r-   r(   do_handshake)r/   r#   r;   r2   s      r   r?   z_sslConn.do_handshakes   s%    tzz%$<NtNvNNr&   c                     	  | j                   t        t        |   g|i |S # t        j
                  $ r"}| j                  rt        |      rY d }~y d }~ww xY w)Nr&   )r=   r-   r(   recv_SSLSysCallErrorr,   r%   r/   r#   r;   r$   r2   s       r   rA   z_sslConn.recvv   sV    	4::eHd8J4J6JJ   	(([-=		   !% AAAAc                     	  | j                   t        t        |   g|i |S # t        j
                  $ r"}| j                  rt        |      rY d }~y d }~ww xY wNr   )r=   r-   r(   	recv_intorB   rC   r,   r%   rD   s       r   rH   z_sslConn.recv_into   sV    	4::eHd=OOOO   	(([-=		rE   c                     t        |      }t        |      }d}d}||k  r?	 | j                  t        t        |   ||d  |      }|dk  rt        d      ||z  }||k  r>y y # t        t        f$ r}t        |      t        k(  rY d }~k d }~ww xY w)Nr   zConnection closed)
memoryviewlenr=   r-   r(   sendIOErrorOSErrorr   _EINTR	Exception)	r/   bufflagsviewtotal_length
total_sentsentr$   r2   s	           r   sendallz_sslConn.sendall   s    #3x
<'	zz(D.Z[0A5J qy 344$J <' W% (-7s   "A! !B0BBB)r   )
__name__
__module____qualname__r.   r=   r?   rA   rH   rW   __classcell__)r2   s   @r   r(   r(   ^   s'    2
O r&   r(   c                       e Zd ZdZd Zy)_CallbackDataz0Data class which is passed to the OCSP callback.c                 >    d | _         d | _        t               | _        y r   )trusted_ca_certscheck_ocsp_endpointr   ocsp_response_cacher/   s    r   r.   z_CallbackData.__init__   s     $#' #-< r&   N)rX   rY   rZ   __doc__r.   r   r&   r   r]   r]      s
    :0r&   r]   c                       e Zd ZdZdZd Zed        Zd Zd Z	 eee	      Z
d Zd Z eee      Zd	 Zd
 Z eee      Zd Zd Z eee      ZddZddZd Z	 	 	 	 ddZy)
SSLContextzUA CPython compatible SSLContext implementation wrapping PyOpenSSL's
    context.
    )	_protocol_ctx_callback_data_check_hostnamec                     || _         t        j                  | j                         | _        t	               | _        d| _        d| j
                  _        | j                  j                  t        | j
                         y )NT)callbackdata)
rf   rB   Contextrg   r]   rh   ri   r`   set_ocsp_client_callbackr   )r/   protocols     r   r.   zSSLContext.__init__   sa    !LL0	+o#
 37/		**#$*=*= 	+ 	?r&   c                     | j                   S )zhThe protocol version chosen when constructing the context.
        This attribute is read-only.
        )rf   rb   s    r   ro   zSSLContext.protocol   s    
 ~~r&   c                 D    t         | j                  j                            S )zWhether to try to verify other peers' certificates and how to
        behave if verification fails. This attribute must be one of
        ssl.CERT_NONE, ssl.CERT_OPTIONAL or ssl.CERT_REQUIRED.
        )_REVERSE_VERIFY_MAPrg   get_verify_moderb   s    r   __get_verify_modezSSLContext.__get_verify_mode   s    
 #499#<#<#>??r&   c                 P    d }| j                   j                  t        |   |       y)zSetter for verify_mode.c                     |S r   r   )connobjx509objerrnumerrdepthretcodes        r   _cbz)SSLContext.__set_verify_mode.<locals>._cb   s	    
 Nr&   N)rg   
set_verify_VERIFY_MAP)r/   r   r|   s      r   __set_verify_modezSSLContext.__set_verify_mode   s"    	 			[/5r&   c                     | j                   S r   )ri   rb   s    r   __get_check_hostnamezSSLContext.__get_check_hostname   s    ###r&   c                 H    t        |t              st        d      || _        y )Nz$check_hostname must be True or False)
isinstancebool	TypeErrorri   r/   r   s     r   __set_check_hostnamezSSLContext.__set_check_hostname   s     %&BCC$r&   c                 .    | j                   j                  S r   )rh   r`   rb   s    r   __get_check_ocsp_endpointz$SSLContext.__get_check_ocsp_endpoint   s    ""666r&   c                 \    t        |t              st        d      || j                  _        y )Nz check_ocsp must be True or False)r   r   r   rh   r`   r   s     r   __set_check_ocsp_endpointz$SSLContext.__set_check_ocsp_endpoint   s&    %&>??27/r&   c                 8    | j                   j                  d      S rG   )rg   set_optionsrb   s    r   __get_optionszSSLContext.__get_options   s     yy$$Q''r&   c                 L    | j                   j                  t        |             y r   )rg   r   intr   s     r   __set_optionszSSLContext.__set_options   s     			c%j)r&   Nc                     r fd}| j                   j                  |       | j                   j                  |       | j                   j                  |xs |       | j                   j	                          y)a  Load a private key and the corresponding certificate. The certfile
        string must be the path to a single file in PEM format containing the
        certificate as well as any number of CA certificates needed to
        establish the certificate's authenticity. The keyfile string, if
        present, must point to a file containing the private key. Otherwise
        the private key will be taken from certfile as well.
        c                 &    j                  d      S )Nzutf-8)encode)
max_lengthprompt_twice	user_datapasswords      r   _pwcbz)SSLContext.load_cert_chain.<locals>._pwcb  s      w//r&   N)rg   set_passwd_cbuse_certificate_chain_fileuse_privatekey_filecheck_privatekey)r/   certfilekeyfiler   r   s      ` r   load_cert_chainzSSLContext.load_cert_chain   sZ     0
 II##E*		,,X6		%%g&9:		""$r&   c                 p    | j                   j                  ||       t        |      | j                  _        y)zLoad a set of "certification authority"(CA) certificates used to
        validate other peers' certificates when `~verify_mode` is other than
        ssl.CERT_NONE.
        N)rg   load_verify_locationsr   rh   r_   )r/   cafilecapaths      r   r   z SSLContext.load_verify_locations  s+    
 			''7/Ef/M,r&   c                 8    | j                   j                          y)zdSpecify that the platform provided CA certificates are to be used
        for verification purposes.N)rg   set_default_verify_pathsrb   s    r   r   z#SSLContext.set_default_verify_paths  s    
 			**,r&   c                 d   t        | j                  ||      }|r|j                  |       |du r|j                          nj|r+t	        |      s |j                  |j                  d             | j                  t        j                  k7  r|j                          |j                          |rY|j                          | j                  r=|;	 t	        |      rt        |t        |             |S t!        |t        |             	 |S |S # t"        t$        f$ r}t'        t)        |            d}~ww xY w)zTWrap an existing Python socket sock and return a TLS socket
        object.
        TidnaN)r(   rg   set_sessionset_accept_stater    set_tlsext_host_namer   verify_mode
_stdlibssl	CERT_NONErequest_ocspset_connect_stater?   check_hostname_verify_ip_addressr   _verify_hostname_SICertificateError_SIVerificationError_CertificateErrorstr)	r/   r1   server_sidedo_handshake_on_connectr,   server_hostnamesessionssl_connr$   s	            r   wrap_socketzSSLContext.wrap_socket  s    DIIt-AB  )$%%' ~o'F --o.D.DV.LM:#7#77%%'&&( # !!# ""'B6%o6*8Xo5NO
  )8O3LM x ,-AB 6+CH556s    D -D D/D**D/)NN)FTTNN)rX   rY   rZ   rc   	__slots__r.   propertyro   _SSLContext__get_verify_mode_SSLContext__set_verify_moder   _SSLContext__get_check_hostname_SSLContext__set_check_hostnamer   $_SSLContext__get_check_ocsp_endpoint$_SSLContext__set_check_ocsp_endpointr`   _SSLContext__get_options_SSLContext__set_optionsoptionsr   r   r   r   r   r&   r   re   re      s     KI?  @6 ,.?@K$%
 24HIN78
 ##<#<>(* }m4G%,N- -2,0)-26'r&   re   )Lrc   socketr8   sslr   errnor   rO   	ipaddressr   r   OpenSSLr   rB   service_identity.pyopensslr   r   r   r   service_identityr   r   r	   r   cryptography.hazmat.backendsr
   _default_backendbson.py3compatr   pymongo.errorsr   pymongo.monotonicr   r5   pymongo.ocsp_supportr   r   pymongo.ocsp_cacher   pymongo.socket_checkerr   r   r*   SSLv23_METHODPROTOCOL_SSLv23OP_NO_SSLv2OP_NO_SSLv3OP_NO_COMPRESSIONgetattrr   HAS_SNICHECK_HOSTNAME_SAFEIS_PYOPENSSLErrorSSLErrorr   VERIFY_NONECERT_OPTIONALVERIFY_PEERCERT_REQUIREDVERIFY_FAIL_IF_NO_PEER_CERTr~   dictitemsrr   r    WantReadErrorWantWriteErrorWantX509LookupErrorr6   r%   
Connectionr(   objectr]   re   r   r&   r   <module>r      sc     ! 0 -/ M # @ + *< $$** d$91=    :: $**d..d..1Q1QQ  8#.#4#4#68 8  	++T-E-EG.>t >B0F 0] ]r&   