o
    C©ÌhB  ã                   @  sú  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ƒZ0zddl1Z1dZ2W n e3y•   dZ2Y nw ej4Z5ej6Z6ej7Z7ej8Z8e9eddƒZ:dZ;dZ<ej=Z>ej?ej@ejAejBejCejBejDB iZEdd„ eE F¡ D ƒZGd'dd„ZHejIejJejKfZLejIZMejJZNejKZOd(dd „ZPG d!d"„ d"ejQƒZRG d#d$„ d$ƒZSG d%d&„ d&ƒZTdS ))zÃA 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                 C  s   i | ]\}}||“qS © r   )Ú.0ÚkeyÚvaluer   r   úh/var/www/html/Moveonwheels/move_on_wheels/venv/lib/python3.10/site-packages/pymongo/pyopenssl_context.pyÚ
<dictcomp>N   s    r   Úaddressr   ÚreturnÚboolc              	   C  s(   zt | ƒ W dS  ttfy   Y dS w )NTF)Ú_ip_addressÚ
ValueErrorÚUnicodeError)r   r   r   r   Ú_is_ip_addressS   s   ÿr$   ÚexcÚBaseExceptionc                 C  s
   | j dkS )z<Return True if the OpenSSL.SSL.SysCallError is a ragged EOF.)éÿÿÿÿzUnexpected EOF)Úargs)r%   r   r   r   Ú_ragged_eofc   s   
r)   c                      sb   e Zd Zd ‡ fdd„Zd!dd„Zd"‡ fdd„Zd#‡ fdd„Zd$‡ fdd„Zd%d&‡ fdd„Z‡  Z	S )'Ú_sslConnÚctxú_SSL.ContextÚsockúOptional[_socket.socket]Úsuppress_ragged_eofsr    c                   s    t ƒ | _|| _tƒ  ||¡ d S ©N)Ú_SocketCheckerÚsocket_checkerr/   ÚsuperÚ__init__)Úselfr+   r-   r/   ©Ú	__class__r   r   r4   l   s   z_sslConn.__init__ÚcallúCallable[..., _T]r(   r   Úkwargsr   r   c           	   
   O  sô   |   ¡ }|r
t ¡ }	 z||i |¤ŽW S  tyy } zZ|dkr!|‚|  ¡ dkr<|r7t ¡ | |kr7t d¡d ‚tdƒd ‚t|t	j
ƒrGd}d}nt|t	jƒrRd}d}nd}d}| j | |||¡ |rot ¡ | |krot d¡d ‚W Y d }~q
d }~ww )NTr   r'   z	timed outz!Underlying socket has been closedF)Ú
gettimeoutÚ_timeÚ	monotonicÚBLOCKING_IO_ERRORSÚfilenoÚ_socketÚtimeoutÚSSLErrorÚ
isinstanceÚ_SSLÚWantReadErrorÚWantWriteErrorr2   Úselect)	r5   r8   r(   r:   rA   Ústartr%   Ú	want_readÚ
want_writer   r   r   Ú_callv   s6   
€ëz_sslConn._callÚNonec                   s   | j tƒ jg|¢R i |¤ŽS r0   )rK   r3   Údo_handshake)r5   r(   r:   r6   r   r   rM   ”   s   z_sslConn.do_handshakeÚbytesc              
     óX   z| j tƒ jg|¢R i |¤ŽW S  tjy+ } z| jr&t|ƒr&W Y d }~dS ‚ d }~ww )Nó    )rK   r3   ÚrecvrD   ÚSysCallErrorr/   r)   ©r5   r(   r:   r%   r6   r   r   rQ   —   ó   €üz_sslConn.recvÚintc              
     rO   ©Nr   )rK   r3   Ú	recv_intorD   rR   r/   r)   rS   r6   r   r   rW       rT   z_sslConn.recv_intor   ÚbufÚflagsc              
     s–   t |ƒ}t|ƒ}d}||k rIz|  tƒ j||d … |¡}W n ty6 } zt|ƒtkr1W Y d }~q
‚ d }~ww |dkr?tdƒ‚||7 }||k sd S d S )Nr   zconnection closed)Ú
memoryviewÚlenrK   r3   ÚsendÚOSErrorr   Ú_EINTR)r5   rX   rY   ÚviewÚtotal_lengthÚ
total_sentÚsentr%   r6   r   r   Úsendall©   s    €ýòz_sslConn.sendall)r+   r,   r-   r.   r/   r    )r8   r9   r(   r   r:   r   r   r   )r(   r   r:   r   r   rL   )r(   r   r:   r   r   rN   )r(   r   r:   r   r   rU   )r   )rX   rN   rY   rU   r   rL   )
Ú__name__Ú
__module__Ú__qualname__r4   rK   rM   rQ   rW   rc   Ú__classcell__r   r   r6   r   r*   k   s    

		r*   c                   @  s   e Zd ZdZddd„ZdS )Ú_CallbackDataz0Data class which is passed to the OCSP callback.r   rL   c                 C  s   d | _ d | _tƒ | _d S r0   )Útrusted_ca_certsÚcheck_ocsp_endpointr   Úocsp_response_cache©r5   r   r   r   r4   Á   s   z_CallbackData.__init__N©r   rL   )rd   re   rf   Ú__doc__r4   r   r   r   r   rh   ¾   s    rh   c                   @  s   e Zd ZdZdZdDdd„ZedEdd	„ƒZdFdd„ZdGdd„Z	eee	ƒZ
dHdd„ZdIdd„ZeeeƒZdJdd„ZdKdd„ZeeeƒZdEdd„ZdLdd„ZeeeƒZ	 	 dMdNd'd(„Z	 dMdOd+d,„ZdPd-d.„ZdQd1d2„ZdPd3d4„ZdPd5d6„Z	7	8	8	 	 dRdSdBdC„Zd S )TÚ
SSLContextzUA CPython compatible SSLContext implementation wrapping PyOpenSSL's
    context.
    )Ú	_protocolÚ_ctxÚ_callback_dataÚ_check_hostnameÚprotocolrU   c                 C  s@   || _ t | j ¡| _tƒ | _d| _d| j_| jjt	| jd� d S )NT)ÚcallbackÚdata)
rp   rD   ÚContextrq   rh   rr   rs   rj   Úset_ocsp_client_callbackr   )r5   rt   r   r   r   r4   Î   s   zSSLContext.__init__r   c                 C  ó   | j S )zhThe protocol version chosen when constructing the context.
        This attribute is read-only.
        )rp   rl   r   r   r   rt   Ú   s   zSSLContext.protocolr   c                 C  s   t | j ¡  S )zÉWhether 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_MAPrq   Úget_verify_moderl   r   r   r   Ú__get_verify_modeá   s   zSSLContext.__get_verify_moder   rL   c                 C  s    ddd„}| j  t| |¡ dS )zSetter for verify_mode.Ú_connobjú_SSL.ConnectionÚ_x509objú_crypto.X509Ú_errnumrU   Ú	_errdepthÚretcoder   r    c                 S  s   t |ƒS r0   )r    )r}   r   r�   r‚   rƒ   r   r   r   Ú_cbë   s   z)SSLContext.__set_verify_mode.<locals>._cbN)r}   r~   r   r€   r�   rU   r‚   rU   rƒ   rU   r   r    )rq   Ú
set_verifyÚ_VERIFY_MAP)r5   r   r„   r   r   r   Ú__set_verify_modeè   s   
zSSLContext.__set_verify_moder    c                 C  ry   r0   )rs   rl   r   r   r   Ú__get_check_hostnameÿ   s   zSSLContext.__get_check_hostnamer   c                 C  s   t d|ƒ || _d S )NÚcheck_hostname)r   rs   ©r5   r   r   r   r   Ú__set_check_hostname  s   

zSSLContext.__set_check_hostnameúOptional[bool]c                 C  s   | j jS r0   )rr   rj   rl   r   r   r   Ú__get_check_ocsp_endpoint  s   z$SSLContext.__get_check_ocsp_endpointc                 C  s   t d|ƒ || j_d S )NÚ
check_ocsp)r   rr   rj   rŠ   r   r   r   Ú__set_check_ocsp_endpoint  s   
z$SSLContext.__set_check_ocsp_endpointc                 C  s   | j  d¡S rV   )rq   Úset_optionsrl   r   r   r   Ú__get_options  s   zSSLContext.__get_optionsc                 C  s   | j  t|ƒ¡ d S r0   )rq   r�   rU   rŠ   r   r   r   Ú__set_options  s   zSSLContext.__set_optionsNÚcertfileúUnion[str, bytes]ÚkeyfileúUnion[str, bytes, None]ÚpasswordúOptional[str]c                   sH   ˆ rd‡ fd	d
„}| j  |¡ | j  |¡ | j  |p|¡ | j  ¡  dS )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.
        Ú_max_lengthrU   Ú_prompt_twicer    Ú
_user_dataúOptional[bytes]r   rN   c                   s   ˆ d usJ ‚ˆ   d¡S )Nzutf-8)Úencode)r™   rš   r›   ©r—   r   r   Ú_pwcb1  s   
z)SSLContext.load_cert_chain.<locals>._pwcbN)r™   rU   rš   r    r›   rœ   r   rN   )rq   Úset_passwd_cbÚuse_certificate_chain_fileÚuse_privatekey_fileÚcheck_privatekey)r5   r“   r•   r—   rŸ   r   rž   r   Úload_cert_chain  s   zSSLContext.load_cert_chainÚcafileÚcapathc                 C  s:   | j  ||¡ ttjdƒs|dusJ ‚t|ƒ| j_dS dS )z­Load 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)rq   Úload_verify_locationsÚhasattrrD   Ú
Connectionr   rr   ri   )r5   r¥   r¦   r   r   r   r¨   =  s
   þz SSLContext.load_verify_locationsc                 C  s   t r|  t ¡ ¡ dS tdƒ‚)z&Attempt to load CA certs from certifi.z±tlsAllowInvalidCertificates 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Ú_ConfigurationErrorrl   r   r   r   Ú_load_certifiJ  s
   ÿzSSLContext._load_certifiÚstoreÚstrc                 C  sj   | j  ¡ }|dusJ ‚tjjj}t |¡D ]\}}}|dkr2|du s&||v r2| tj	 
t |¡¡¡ qdS )z2Attempt to load CA certs from Windows trust store.NÚx509_asnT)rq   Úget_cert_storeÚ
_stdlibsslÚPurposeÚSERVER_AUTHÚoidÚenum_certificatesÚadd_certÚ_cryptoÚX509Úfrom_cryptographyÚx509Úload_der_x509_certificate)r5   r°   Ú
cert_storer·   ÚcertÚencodingÚtrustr   r   r   Ú_load_wincertsV  s   

ÿ€ýzSSLContext._load_wincertsc                 C  s^   t jdkrzdD ]}|  |¡ qW n ty   |  ¡  Y n
w t jdkr(|  ¡  | j ¡  dS )z7A PyOpenSSL version of load_default_certs from CPython.Úwin32)ÚCAÚROOTÚdarwinN)Ú_sysÚplatformrÃ   ÚPermissionErrorr¯   rq   Úset_default_verify_paths)r5   Ú	storenamer   r   r   Úload_default_certsc  s   
ÿþ
zSSLContext.load_default_certsc                 C  s   | j  ¡  dS )zmSpecify that the platform provided CA certificates are to be used
        for verification purposes.
        N)rq   rË   rl   r   r   r   rË   s  s   z#SSLContext.set_default_verify_pathsFTr-   ú_socket.socketÚserver_sideÚdo_handshake_on_connectr/   Úserver_hostnameÚsessionúOptional[_SSL.Session]r*   c           
   
   C  sì   t | j||ƒ}|r| |¡ |du r| ¡  n|r%t|ƒs%| | d¡¡ | jtj	kr/| 
¡  | ¡  |rt| ¡  | jrt|durtddlm} zt|ƒrT| ||¡ W |S | ||¡ W |S  tjtjfys }	 ztt|	ƒƒd‚d}	~	ww |S )zZWrap an existing Python socket connection and return a TLS socket
        object.
        TÚidnaNr   )Ú	pyopenssl)r*   rq   Úset_sessionÚset_accept_stater$   Úset_tlsext_host_namer�   Úverify_moder´   Ú	CERT_NONEÚrequest_ocspÚset_connect_staterM   r‰   Úservice_identityrÕ   Úverify_ip_addressÚverify_hostnameÚCertificateErrorÚVerificationErrorr   r±   )
r5   r-   rÏ   rÐ   r/   rÑ   rÒ   Ússl_connrÕ   r%   r   r   r   Úwrap_socket{  s8   

úûþ€üzSSLContext.wrap_socket)rt   rU   )r   rU   )r   r   )r   r   r   rL   )r   r    )r   r   r   rL   )r   rŒ   )r   r    r   rL   )r   rU   r   rL   )NN)r“   r”   r•   r–   r—   r˜   r   rL   )r¥   r˜   r¦   r˜   r   rL   rm   )r°   r±   r   rL   )FTTNN)r-   rÎ   rÏ   r    rÐ   r    r/   r    rÑ   r˜   rÒ   rÓ   r   r*   )rd   re   rf   rn   Ú	__slots__r4   Úpropertyrt   Ú_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_endpointrj   Ú_SSLContext__get_optionsÚ_SSLContext__set_optionsÚoptionsr¤   r¨   r¯   rÃ   rÍ   rË   rã   r   r   r   r   ro   Ç   sB    












üÿ



ùro   )r   r   r   r    )r%   r&   r   r    )Urn   Ú
__future__r   Úsocketr@   Ússlr´   ÚsysrÈ   Útimer<   Úerrnor   r^   Ú	ipaddressr   r!   Útypingr   r   r   r   r	   r
   Úcryptography.x509r½   rÝ   ÚOpenSSLr   rD   r   rº   Úpymongo.errorsr   r®   r   Úpymongo.ocsp_cacher   Úpymongo.ocsp_supportr   r   Úpymongo.socket_checkerr   r1   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ÚErrorrB   rÚ   ÚVERIFY_NONEÚCERT_OPTIONALÚVERIFY_PEERÚCERT_REQUIREDÚVERIFY_FAIL_IF_NO_PEER_CERTr†   Úitemsrz   r$   rE   rF   ÚWantX509LookupErrorr>   ÚBLOCKING_IO_READ_ERRORÚBLOCKING_IO_WRITE_ERRORÚBLOCKING_IO_LOOKUP_ERRORr)   rª   r*   rh   ro   r   r   r   r   Ú<module>   sf    ÿý


S	