
    FCf@                       d Z ddlmZ ddlZ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mZmZmZ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" ddl#m$Z$ ddl%m&Z&m'Z' ddl(m)Z* ddl(m+Z+ ddl,m-Z-  ed      Z. ed      Z/ ed      Z0erddlm1Z1 ddl2m3Z3  ed      Z4	 ddl5Z5dZ6ejp                  Z9ejt                  Z:ejv                  Z;ejx                  Z< e=edd      Z>dZ?dZ@ej                  ZBej                  ej                  ej                  ej                  ej                  ej                  ej                  z  iZIeIj                         D  ci c]  \  } }|| 
 c}} ZKd"dZLej                  ej                  ej                  fZPd#dZQ G d dej                        ZS G d d      ZT G d  d!      ZUy# e7$ r dZ6Y w xY wc c}} w )$zMA CPython compatible SSLContext implementation wrapping PyOpenSSL's
context.
    )annotationsN)EINTR)
ip_address)TYPE_CHECKINGAnyCallableOptionalTypeVarUnion)SSL)crypto)lazy_import)ConfigurationError)_CertificateError)
_OCSPCache)_load_trusted_ca_certs_ocsp_callback)SocketChecker)_errno_from_exception)validate_booleanzcryptography.x509service_identityzservice_identity.pyopenssl)
VerifyMode)Certificate_TTFOP_NO_RENEGOTIATIONc                F    	 t        |        y# t        t        f$ r Y yw xY w)NTF)_ip_address
ValueErrorUnicodeError)addresss    Y/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/pymongo/pyopenssl_context.py_is_ip_addressr"   U   s)    G% 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'   b   s    88---    c                  f     e Zd Z	 	 	 	 	 	 d fdZddZd	 fdZd
 fdZd fdZdd fdZ xZ	S )_sslConnc                R    t               | _        || _        t        |   ||       y N)_SocketCheckersocket_checkersuppress_ragged_eofssuper__init__)selfctxsockr/   	__class__s       r!   r1   z_sslConn.__init__k   s'     -.$8!d#r(   c                T   | j                         }|rt        j                         }	 	  ||i |S # t        $ r}| j	                         dk(  r>|r0t        j                         z
  |kD  rt        j                  d      d t        d      d t        |t        j                        rd}d}n#t        |t        j                        rd}d}nd}d}| j                  j                  | |||       |r0t        j                         z
  |kD  rt        j                  d      d Y d }~d }~ww xY w)NTr$   z	timed outz!Underlying socket has been closedF)
gettimeout_time	monotonicBLOCKING_IO_ERRORSfileno_sockettimeoutSSLError
isinstance_SSLWantReadErrorWantWriteErrorr.   select)	r2   callr%   kwargsr=   startr&   	want_read
want_writes	            r!   _callz_sslConn._callr   s
   //#OO%ET,V,,% ;;=B&5??#4u#<w#F%ook:D"#FGTQc4#5#56 $I!&JT%8%89 %I!%J $I!%J##**4JPu0587B!//+6D@%s   1 	D'C#D""D'c                @     | j                   t        |   g|i |S r,   )rI   r0   do_handshake)r2   r%   rE   r5   s      r!   rK   z_sslConn.do_handshake   s#    tzz%'.@@@@r(   c                    	  | j                   t        |   g|i |S # t        j                  $ r"}| j
                  rt        |      rY d }~y d }~ww xY w)Nr(   )rI   r0   recvr@   SysCallErrorr/   r'   r2   r%   rE   r&   r5   s       r!   rM   z_sslConn.recv   sS    	4::egl<T<V<<   	(([-=		   ! AAAAc                    	  | j                   t        |   g|i |S # t        j                  $ r"}| j
                  rt        |      rY d }~y d }~ww xY wNr   )rI   r0   	recv_intor@   rN   r/   r'   rO   s       r!   rS   z_sslConn.recv_into   sT    	4::eg/A$A&AA   	(([-=		rP   c                   t        |      }t        |      }d}||k  r;	 | j                  t        |   ||d  |      }|dk  rt        d      ||z  }||k  r:y y # t
        $ r}t        |      t        k(  rY d }~a d }~ww xY w)Nr   zconnection closed)
memoryviewlenrI   r0   sendOSErrorr   _EINTR)	r2   bufflagsviewtotal_length
total_sentsentr&   r5   s	           r!   sendallz_sslConn.sendall   s    #3x
<'zz%',Z[0A5I qy122$J <'  (-7s   A 	B$A<;A<<B)r3   z_SSL.Contextr4   zOptional[_socket.socket]r/   bool)rD   zCallable[..., _T]r%   r   rE   r   returnr   )r%   r   rE   r   rb   None)r%   r   rE   r   rb   bytes)r%   r   rE   r   rb   int)r   )rZ   rd   r[   re   rb   rc   )
__name__
__module____qualname__r1   rI   rK   rM   rS   r`   __classcell__)r5   s   @r!   r*   r*   j   s?    $$'?$W[$6A r(   r*   c                      e Zd ZdZd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r2   s    r!   r1   z_CallbackData.__init__   s    =A37 #-< r(   Nrb   rc   )rf   rg   rh   __doc__r1    r(   r!   rk   rk      s
    :0r(   rk   c                  6   e Zd ZdZdZddZedd       ZddZddZ	 eee	      Z
ddZddZ eee      Zdd	Zdd
Z eee      ZddZddZ eee      Z	 	 d	 	 	 	 	 	 	 d dZ	 d	 	 	 	 	 d!dZddZd"dZddZddZ	 	 	 	 	 d#	 	 	 	 	 	 	 	 	 	 	 	 	 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)
rv   r@   Contextrw   rk   rx   ry   rn   set_ocsp_client_callbackr   )r2   protocols     r!   r1   zSSLContext.__init__   s\    !LL0	+o#
 37/		**NI\I\*]r(   c                    | j                   S )zhThe protocol version chosen when constructing the context.
        This attribute is read-only.
        )rv   rp   s    r!   r   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_MAPrw   get_verify_moderp   s    r!   __get_verify_modezSSLContext.__get_verify_mode   s    
 #499#<#<#>??r(   c                j    	 	 	 	 	 	 	 	 	 	 	 	 dd}| j                   j                  t        |   |       y)zSetter for verify_mode.c                    t        |      S r,   )ra   )_connobj_x509obj_errnum	_errdepthretcodes        r!   _cbz)SSLContext.__set_verify_mode.<locals>._cb   s     = r(   N)r   z_SSL.Connectionr   z_crypto.X509r   re   r   re   r   re   rb   ra   )rw   
set_verify_VERIFY_MAP)r2   valuer   s      r!   __set_verify_modezSSLContext.__set_verify_mode   sZ    	!%	!"	! 	! 		!
 	! 	!  			[/5r(   c                    | j                   S r,   )ry   rp   s    r!   __get_check_hostnamezSSLContext.__get_check_hostname   s    ###r(   c                *    t        d|       || _        y )Ncheck_hostname)r   ry   r2   r   s     r!   __set_check_hostnamezSSLContext.__set_check_hostname   s    )51$r(   c                .    | j                   j                  S r,   )rx   rn   rp   s    r!   __get_check_ocsp_endpointz$SSLContext.__get_check_ocsp_endpoint  s    ""666r(   c                >    t        d|       || j                  _        y )N
check_ocsp)r   rx   rn   r   s     r!   __set_check_ocsp_endpointz$SSLContext.__set_check_ocsp_endpoint  s    u-27/r(   c                8    | j                   j                  d      S rR   )rw   set_optionsrp   s    r!   __get_optionszSSLContext.__get_options
  s     yy$$Q''r(   c                L    | j                   j                  t        |             y r,   )rw   r   re   r   s     r!   __set_optionszSSLContext.__set_options  s     			c%j)r(   Nc                    r!d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 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)r   re   r   ra   r   rd   rb   rd   )rw   set_passwd_cbuse_certificate_chain_fileuse_privatekey_filecheck_privatekey)r2   certfilekeyfiler   r   s      ` r!   load_cert_chainzSSLContext.load_cert_chain  sZ      0 II##E*		,,X6		%%g&9:		""$r(   c                    | j                   j                  ||       t        t        j                  d      s|J t        |      | j                  _        yy)zLoad a set of "certification authority"(CA) certificates used to
        validate other peers' certificates when `~verify_mode` is other than
        ssl.CERT_NONE.
        get_verified_chainN)rw   load_verify_locationshasattrr@   
Connectionr   rx   rm   )r2   cafilecapaths      r!   r   z SSLContext.load_verify_locations6  sK     			''7t(<=%%%3I&3QD0 >r(   c                l    t         r$| j                  t        j                                yt	        d      )z&Attempt to load CA certs from certifi.ztlsAllowInvalidCertificates is False but no system CA certificates could be loaded. Please install the certifi package, or provide a path to a CA file using the tlsCAFile optionN)_HAVE_CERTIFIr   certifiwhere_ConfigurationErrorrp   s    r!   _load_certifizSSLContext._load_certifiC  s+    &&w}}7%' r(   c                \   | j                   j                         }t        j                  j                  j
                  }t        j                  |      D ]V  \  }}}|dk(  s|du s||v s|j                  t        j                  j                  t        j                  |                   X y)z2Attempt to load CA certs from Windows trust store.x509_asnTN)rw   get_cert_store
_stdlibsslPurposeSERVER_AUTHoidenum_certificatesadd_cert_cryptoX509from_cryptography_x509load_der_x509_certificate)r2   store
cert_storer   certencodingtrusts          r!   _load_wincertszSSLContext._load_wincertsO  s    YY--/
  ,,00%/%A%A%%H 	!D(E:%D=C5L''66u7V7VW[7\]	r(   c                   t         j                  dk(  r	 dD ]  }| j                  |        n#t         j                  dk(  r| j	                          | j
                  j                          y# t        $ r | j	                          Y 6w xY w)z7A PyOpenSSL version of load_default_certs from CPython.win32)CAROOTdarwinN)_sysplatformr   PermissionErrorr   rw   set_default_verify_paths)r2   	storenames     r!   load_default_certszSSLContext.load_default_certsZ  sy    
 ==G#%!/ 3I''	23
 ]]h& 		**, # %""$%s   A, ,BBc                8    | j                   j                          y)zmSpecify that the platform provided CA certificates are to be used
        for verification purposes.
        N)rw   r   rp   s    r!   r   z#SSLContext.set_default_verify_pathsj  s     			**,r(   c                   t        | j                  ||      }|r|j                  |       |du r|j                          nj|r+t	        |      s |j                  |j                  d             | j                  t        j                  k7  r|j                          |j                          |r[|j                          | j                  r?|=	 t	        |      rt        j                  ||       |S t        j!                  ||       	 |S |S # t"        j$                  t"        j&                  f$ r}t)        t+        |            dd}~ww xY w)zZWrap an existing Python socket connection and return a TLS socket
        object.
        TidnaN)r*   rw   set_sessionset_accept_stater"   set_tlsext_host_namer   verify_moder   	CERT_NONErequest_ocspset_connect_staterK   r   _service_identity_pyopensslverify_ip_addressverify_hostname_service_identitySICertificateErrorSIVerificationErrorr   str)	r2   r4   server_sidedo_handshake_on_connectr/   server_hostnamesessionssl_connr&   s	            r!   wrap_socketzSSLContext.wrap_socketr  s0    DIIt-AB  )$%%' ~o'F --o.D.DV.LM:#7#77%%'&&( # !!# ""'B	@%o63EEhP_`  4CCHo^ x	 &88%99 @ ,CH54?	@s   !D	 .D	 	#E,EE)r   re   )rb   re   )rb   r   )r   r   rb   rc   )rb   ra   )r   r   rb   rc   )rb   zOptional[bool])r   ra   rb   rc   rq   )r   re   rb   rc   )NN)r   zUnion[str, bytes]r   zUnion[str, bytes, None]r   Optional[str]rb   rc   )r   r   r   r   rb   rc   )r   r   rb   rc   )FTTNN)r4   z_socket.socketr   ra   r   ra   r/   ra   r   r   r   zOptional[_SSL.Session]rb   r*   )rf   rg   rh   rr   	__slots__r1   propertyr   _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_endpointrn   _SSLContext__get_options_SSLContext__set_optionsoptionsr   r   r   r   r   r   r   rs   r(   r!   ru   ru      sZ    KI
^  @6* ,.?@K$% 24HIN78 ##<>WX(* }m4G
 ,0"&	%#% )%  	%
 
%> EIR#R4AR	R
	- - "(,%))-*.// / "&	/
 #/ '/ (/ 
/r(   ru   )r    r   rb   ra   )r&   BaseExceptionrb   ra   )Vrr   
__future__r   socketr<   sslr   sysr   timer8   errnor   rY   	ipaddressr   r   typingr   r   r   r	   r
   r   OpenSSLr   r@   r   r   pymongo._lazy_importr   pymongo.errorsr   r   r   pymongo.ocsp_cacher   pymongo.ocsp_supportr   r   pymongo.socket_checkerr   r-   r   pymongo.write_concernr   r   r   r   r   cryptography.x509r   r   r   r   ImportErrorSSLv23_METHODPROTOCOL_SSLv23OP_NO_SSLv2OP_NO_SSLv3OP_NO_COMPRESSIONgetattrr   HAS_SNIIS_PYOPENSSLErrorr>   r   VERIFY_NONECERT_OPTIONALVERIFY_PEERCERT_REQUIREDVERIFY_FAIL_IF_NO_PEER_CERTr   itemsr   r"   rA   rB   WantX509LookupErrorr:   r'   r   r*   rk   ru   )keyr   s   00r!   <module>r"     s   #     ! / I I  % , D , ) G B 8 2'( 23 )*FG -T]M $$** d$91=   :: $**d..d..1Q1QQ 5@4E4E4GHjc5uczH 
 (($*=*=t?W?WX .Jt JZ0 0a aS  M2 Is   "F< G
<GG