
    nh3B                       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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rddlm/Z/  ed      Z0	 ddl1Z1dZ2ejh                  Z5ejl                  Z6ejn                  Z7ejp                  Z8 e9edd      Z:dZ;dZ<ejz                  Z>ej~                  ej                  ej                  ej                  ej                  ej                  ej                  z  iZEeEj                         D  ci c]  \  } }|| 
 c}} ZGddZHej                  ej                  ej                  fZLej                  ZMej                  ZNej                  ZOddZP G d dej                        ZR G d d      ZS G d d      ZTy# e3$ r dZ2Y Bw xY wc c}} w )zA CPython compatible SSLContext implementation wrapping PyOpenSSL's
context.

Due to limitations of the CPython asyncio.Protocol implementation for SSL, the async API does not support PyOpenSSL.
    )annotationsN)EINTR)
ip_address)TYPE_CHECKINGAnyCallableOptionalTypeVarUnion)SSL)crypto)ConfigurationError)_CertificateError)
_OCSPCache)_load_trusted_ca_certs_ocsp_callback)SocketChecker)_errno_from_exception)validate_boolean)
VerifyMode_TTFOP_NO_RENEGOTIATIONc                F    	 t        |        y# t        t        f$ r Y yw xY w)NTF)_ip_address
ValueErrorUnicodeError)addresss    /var/www/html/SchoolMeal/SchoolMeal/pds_admin_SchoolMeal/Backend/venv/lib/python3.12/site-packages/pymongo/pyopenssl_context.py_is_ip_addressr   S   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$   c   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   r.   z_sslConn.__init__l   s'     -.$8!d#r%   c                d   | j                         }|rt        j                         }	 	  ||i |S # t        $ r}|dk(  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   r!   z	timed outz!Underlying socket has been closedF)
gettimeout_time	monotonicBLOCKING_IO_ERRORSfileno_sockettimeoutSSLError
isinstance_SSLWantReadErrorWantWriteErrorr+   select)	r/   callr"   kwargsr:   startr#   	want_read
want_writes	            r   _callz_sslConn._callv   s   //#OO%ET,V,,% a<I;;=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)   )rF   r-   do_handshake)r/   r"   rB   r2   s      r   rH   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%   )rF   r-   recvr=   SysCallErrorr,   r$   r/   r"   rB   r#   r2   s       r   rJ   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   )rF   r-   	recv_intor=   rK   r,   r$   rL   s       r   rP   z_sslConn.recv_into   sT    	4::eg/A$A&AA   	(([-=		rM   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lenrF   r-   sendOSErrorr   _EINTR)	r/   bufflagsviewtotal_length
total_sentsentr#   r2   s	           r   sendallz_sslConn.sendall   s    #3x
<'zz%',Z[0A5I qy122$J <'  (-7s   A 	B$A<;A<<B)r0   z_SSL.Contextr1   zOptional[_socket.socket]r,   bool)rA   zCallable[..., _T]r"   r   rB   r   returnr   )r"   r   rB   r   r_   None)r"   r   rB   r   r_   bytes)r"   r   rB   r   r_   int)r   )rW   ra   rX   rb   r_   r`   )
__name__
__module____qualname__r.   rF   rH   rJ   rP   r]   __classcell__)r2   s   @r   r'   r'   k   sC    $$ '$ #	$<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r/   s    r   r.   z_CallbackData.__init__   s    BF37 #-< r%   Nr_   r`   )rc   rd   re   __doc__r.    r%   r   rh   rh      s
    :0r%   rh   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)
rs   r=   Contextrt   rh   ru   rv   rk   set_ocsp_client_callbackr   )r/   protocols     r   r.   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.
        )rs   rm   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_MAPrt   get_verify_moderm   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)   )r^   )_connobj_x509obj_errnum	_errdepthretcodes        r   _cbz)SSLContext.__set_verify_mode.<locals>._cb   s     = r%   N)r   z_SSL.Connectionr   z_crypto.X509r   rb   r   rb   r   rb   r_   r^   )rt   
set_verify_VERIFY_MAP)r/   valuer   s      r   __set_verify_modezSSLContext.__set_verify_mode   sZ    	!%	!"	! 	! 		!
 	! 	!  			[/5r%   c                    | j                   S r)   )rv   rm   s    r   __get_check_hostnamezSSLContext.__get_check_hostname   s    ###r%   c                *    t        d|       || _        y )Ncheck_hostname)r   rv   r/   r   s     r   __set_check_hostnamezSSLContext.__set_check_hostname  s    )51$r%   c                .    | j                   j                  S r)   )ru   rk   rm   s    r   __get_check_ocsp_endpointz$SSLContext.__get_check_ocsp_endpoint  s    ""666r%   c                >    t        d|       || j                  _        y )N
check_ocsp)r   ru   rk   r   s     r   __set_check_ocsp_endpointz$SSLContext.__set_check_ocsp_endpoint  s    u-27/r%   c                8    | j                   j                  d      S rO   )rt   set_optionsrm   s    r   __get_optionszSSLContext.__get_options  s     yy$$Q''r%   c                L    | j                   j                  t        |             y r)   )rt   r   rb   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>._pwcb1  s       +++w//r%   N)r   rb   r   r^   r   zOptional[bytes]r_   ra   )rt   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                    | 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)rt   load_verify_locationshasattrr=   
Connectionr   ru   rj   )r/   cafilecapaths      r   r   z SSLContext.load_verify_locations=  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_ConfigurationErrorrm   s    r   _load_certifizSSLContext._load_certifiJ  s+    &&w}}7%' r%   c                d   | j                   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.Nx509_asnT)rt   get_cert_store
_stdlibsslPurposeSERVER_AUTHoidenum_certificatesadd_cert_cryptoX509from_cryptographyx509load_der_x509_certificate)r/   store
cert_storer   certencodingtrusts          r   _load_wincertszSSLContext._load_wincertsV  s    YY--/
%%%  ,,00%/%A%A%%H 	!D(E:%D=C5L''66t7U7UVZ7[\	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   rt   set_default_verify_paths)r/   	storenames     r   load_default_certszSSLContext.load_default_certsc  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)rt   r   rm   s    r   r   z#SSLContext.set_default_verify_pathss  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                          |rY|j                          | j                  r=|;ddlm} 	 t	        |      r|j!                  ||       |S |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idnaNr   )	pyopenssl)r'   rt   set_sessionset_accept_stater   set_tlsext_host_namer   verify_moder   	CERT_NONErequest_ocspset_connect_staterH   r   service_identityr   verify_ip_addressverify_hostnameSICertificateErrorSIVerificationErrorr   str)
r/   r1   server_sidedo_handshake_on_connectr,   server_hostnamesessionssl_connr   r#   s
             r   wrap_socketzSSLContext.wrap_socket{  s2    DIIt-AB  )$%%' ~o'F --o.D.DV.LM:#7#77%%'&&( # !!# ""'B6	@%o6!33HoN  "11(OL x	 %77$88 @ ,CH54?	@s   D 0D #E*D??E)r|   rb   )r_   rb   )r_   r   )r   r   r_   r`   )r_   r^   )r   r   r_   r`   )r_   zOptional[bool])r   r^   r_   r`   )r   rb   r_   r`   )NN)r   zUnion[str, bytes]r   zUnion[str, bytes, None]r   Optional[str]r_   r`   )r   r   r   r   r_   r`   rn   )r   r   r_   r`   )FTTNN)r1   z_socket.socketr   r^   r   r^   r,   r^   r   r   r   zOptional[_SSL.Session]r_   r'   )rc   rd   re   ro   	__slots__r.   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_endpointrk   _SSLContext__get_options_SSLContext__set_optionsoptionsr   r   r   r   r   r   r   rp   r%   r   rr   rr      sZ    KI
^  @6* ,.?@K$% 24HIN78 ##<>WX(* }m4G
 ,0"&	%#% )%  	%
 
%> EIR#R4AR	R
- - "(,%))-*.11 1 "&	1
 #1 '1 (1 
1r%   rr   )r   r   r_   r^   )r#   BaseExceptionr_   r^   )Uro   
__future__r   socketr9   sslr   sysr   timer5   errnor   rV   	ipaddressr   r   typingr   r   r   r	   r
   r   cryptography.x509r   r   OpenSSLr   r=   r   r   pymongo.errorsr   r   r   pymongo.ocsp_cacher   pymongo.ocsp_supportr   r   pymongo.socket_checkerr   r*   r   pymongo.write_concernr   r   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   r>   r?   WantX509LookupErrorr7   BLOCKING_IO_READ_ERRORBLOCKING_IO_WRITE_ERRORBLOCKING_IO_LOOKUP_ERRORr$   r   r'   rh   rr   )keyr   s   00r   <module>r!     s  
 #     ! / I I     % D , ) G B 8 2 T]M $$** d$91=   :: $**d..d..1Q1QQ 5@4E4E4GHjc5uczH 
 (($*=*=t?W?WX ++ -- 33 .Pt Pf0 0e ee  M2 Is   G <GGG