
    Bwg              
          d dl mZmZmZmZ d dlZd dlZd dlZd dlZ	d dl
Z
d dlZd dlZd dlZddlmZmZmZmZ ddlmZmZmZ ddlmZmZmZ ddlmZ dd	lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z, dd
l-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZM ddlNmOZOmZ ddlPmQZQ ej                  dk  reSZTej                  dk  rej                  ZVnej                  ZVddgZWej                  ej                  ej                  ej                  ej                  dZ]ej                  dej                  dej                  dej                  dej                  diZ^ ej                  d      Z` ej                  d      Za ej                         Zci Zdd Zed Zfd Zg e#ed ee      Zh e#ed!eg      Zi G d" dej      Zk G d# dej      Zly)$    )unicode_literalsdivisionabsolute_importprint_functionN   )Securityosx_version_infohandle_sec_errorSecurityConst)CoreFoundationhandle_cf_error	CFHelpers   )Certificateint_to_bytestimezone)pretty_message)array_from_pointer	array_setbuffer_from_bytesbytes_from_buffercallbackcastderefnewnullpointer_setstructstruct_bytesunwrapwrite_to_buffer)	type_namestr_clsbyte_cls	int_types)CIPHER_SUITE_MAP)
rand_bytes)TLSErrorTLSDisconnectErrorTLSGracefulDisconnectError)detect_client_auth_requestdetect_other_protocolextract_chainget_dh_params_lengthparse_session_inforaise_client_authraise_dh_paramsraise_disconnectionraise_expired_not_yet_validraise_handshakeraise_hostnameraise_lifetime_too_longraise_no_issuerraise_protocol_errorraise_protocol_versionraise_revokedraise_self_signedraise_verificationraise_weak_signature)load_certificater   )parse_certificate)   )r@      
TLSSession	TLSSocket)SSLv2SSLv3TLSv1TLSv1.1TLSv1.2rD   rE   rF   rG   rH   s   (
||
)zGanon|PSK|SEED|RC4|MD5|NULL|CAMELLIA|ARIA|SRP|KRB5|EXPORT|(?<!3)DES|IDEAc                    d}	 t         j                  |       }|st        j                  |       }n|j                  }|s|syt	        |      }|j                         }d}d}	 t        |      |k  r|T|dkD  rOt        j                  |gg g |      \  }	}
}
t        |	      dk(  r$t        j                  t        j                  d      |j                  |t        |      z
        }||z  }|dk(  r1t        |      dk(  r"|t        j                  S t        j                  S nt        |      |k  r|Y|t        j                  k7  rF|t        j                   k(  s|t        j"                  k(  rt        j                  S t        j                  S |r|j$                  st        |      dk\  rqt        |j&                        dk(  rY|dd t)        dd	g      v }|dd t)        g d
      v }|r|s1|xj&                  |t+        |      z   z  c_        t        j,                  S |xj&                  |z  c_        t/        ||       t1        |t        |             t        |      |k7  rt        j2                  S y# t        j                  $ r}|j                  }Y d}~bd}~ww xY w# t4        $ r#}|r||_        t        j                  cY d}~S d}~ww xY w)a  
    Callback called by Secure Transport to actually read the socket

    :param connection_id:
        An integer identifying the connection

    :param data_buffer:
        A char pointer FFI type to write the data to

    :param data_length_pointer:
        A size_t pointer FFI type of the amount of data to read. Will be
        overwritten with the amount of data read on return.

    :return:
        An integer status code of the result - 0 for success
    Nr               z	timed outr@   r         )s    s   s   s   s   )_connection_refsget_socket_refs_socketr   
gettimeoutlenselectsocket_errorerrnoEAGAINrecvr   errSSLClosedNoNotifyerrSSLClosedAbort
ECONNRESETEPIPE_done_handshake_server_helloset_read_remainingerrSSLProtocolr!   r   errSSLWouldBlockKeyboardInterrupt
_exception)connection_iddata_bufferdata_length_pointerselfsocketbytes_requestedtimeoutrV   data
read_ready_chunkevalid_record_typevalid_protocol_versions                  H/var/www/horilla/myenv/lib/python3.12/site-packages/oscrypto/_mac/tls.py_read_callbackru   f   s   $ DE/##M2!%%m4F\\FF 34##%	d)o- &7S='-}}fXr2w'O$J1:!+%mmELL+FFOc$i$?@C<4yA~"?#0#E#EE,>>> d)o-$ %,,!6(((EU[[,@$999 222,, 4yA~#d&8&8"9Q">$(1Igw5G1H$H!)-ac ; 7 *& )0F&&$1H*HH&(777$&T*'T3t9' 111E  	GGE	F  /DO.../so   =J J "B*I* I* I* -A
J 8J BJ AJ *J=J	J JJ 	K J>8K>Kc                     d}| j                         }	 | j                  d       || j                  d      z  }| j                  |       |S # t        j                  $ r Y (w xY w# | j                  |       w xY w)z
    Reads everything available from the socket - used for debugging when there
    is a protocol error

    :param socket:
        The socket to read from

    :return:
        A byte string of the remaining data
    rJ   rK       )rR   
settimeoutrY   rU   rV   )rj   outputold_timeouts      rt   ra   ra      s{     F##%K'#&++d## 	+&M	 MM  	+&s#   %A A"A% !A""A% %A8c                    	 t         j                  |       }|st        j                  |       }n|j                  }|s|syt	        |      }t        ||      }|r!|j                  s|xj                  |z  c_        d}	 |j                  |      }|Y|t        j                  k7  rF|t        j                  k(  s|t        j                  k(  rt        j                   S t        j"                  S |k7  rt%        ||       t        j&                  S y# t        j                  $ r}	|	j                  }Y d}	~	d}	~	ww xY w# t(        $ r!}	|	_        t        j,                  cY d}	~	S d}	~	ww xY w)a  
    Callback called by Secure Transport to actually write to the socket

    :param connection_id:
        An integer identifying the connection

    :param data_buffer:
        A char pointer FFI type containing the data to write

    :param data_length_pointer:
        A size_t pointer FFI type of the amount of data to write. Will be
        overwritten with the amount of data actually written on return.

    :return:
        An integer status code of the result - 0 for success
    r   N)rN   rO   rP   rQ   r   r   r^   _client_hellosendrU   rV   rW   rX   r\   r]   r   rZ   r[   r   rc   rd   re   errSSLPeerUserCancelled)
rf   rg   rh   ri   rj   data_lengthrm   rV   sentrq   s
             rt   _write_callbackr      s/   $"5##M2!%%m4F\\FF/0 k:,,$&	;;t$D %,,!6(((EU[[,@$999 222;+T2 111  	GGE	  54445sZ   =D7  <D7 =D A
D7 D7 ) D7 D4D/*D7 /D44D7 7	E! EE!E!SSLReadFuncSSLWriteFuncc                   ,    e Zd ZdZdZdZdZdZdZddZ	y)rB   zj
    A TLS session object that multiple TLSSocket objects can share for the
    sake of session reuse
    Nc           	      j   t        |t              st        t        dt	        |                  || _        |t        g d      }t        |t              rt        |g      }n.t        |t              st        t        dt	        |                  |t        g d      z
  }|rt        t        dt        |                  || _
        g | _        |r|D ]  }t        |t              r|j                  }nt        |t              rt        |      }nmt        |t              r/t!        |d      5 }t        |j#                               }ddd       n.t        |t$              st        t        dt	        |                  | j                  j'                  |        t)        d	      | _        y# 1 sw Y   7xY w)
a]  
        :param protocol:
            A unicode string or set of unicode strings representing allowable
            protocols to negotiate with the server:

             - "TLSv1.2"
             - "TLSv1.1"
             - "TLSv1"
             - "SSLv3"

            Default is: {"TLSv1", "TLSv1.1", "TLSv1.2"}

        :param manual_validation:
            If certificate and certificate path validation should be skipped
            and left to the developer to implement

        :param extra_trust_roots:
            A list containing one or more certificates to be treated as trust
            roots, in one of the following formats:
             - A byte string of the DER encoded certificate
             - A unicode string of the certificate filename
             - An asn1crypto.x509.Certificate object
             - An oscrypto.asymmetric.Certificate object

        :raises:
            ValueError - when any of the parameters contain an invalid value
            TypeError - when any of the parameters are of the wrong type
            OSError - when an error is returned by the OS crypto library
        zM
                manual_validation must be a boolean, not %s
                N)rF   rG   rH   zu
                protocol must be a unicode string or set of unicode strings,
                not %s
                )rE   rF   rG   rH   z
                protocol must contain only the unicode strings "SSLv3", "TLSv1",
                "TLSv1.1", "TLSv1.2", not %s
                rbz
                        extra_trust_roots must be a list of byte strings, unicode
                        strings, asn1crypto.x509.Certificate objects or
                        oscrypto.asymmetric.Certificate objects, not %s
                           )
isinstancebool	TypeErrorr   r"   _manual_validationr`   r#   
ValueErrorrepr
_protocols_extra_trust_rootsr   asn1r$   r?   openreadAsn1Certificateappendr'   _peer_id)ri   protocolmanual_validationextra_trust_rootsunsupported_protocolsextra_trust_rootfs          rt   __init__zTLSSession.__init__   s   > +T2N +,	   #4:;Hh(H:HHc*N (#   !)3/W+X X ^ *+   #"$$5 A .<'7'<'<$ 0(;'89I'J$ 0':.5 G+<QVVX+F(G G#$4oF#N
 ""23%   ''../?@#A& #1G Gs   *F))F2	)NFN)
__name__
__module____qualname____doc__r   _ciphersr   r   r   r    rJ   rt   rB   rB     s*    
 JHHX&rJ   c                      e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZdZdZdZdZdZdZdZed d       Zd!dZd Zd Zd dZd	 Zd
 Zd Zd Z d Z!d dZ"d Z#d Z$d Z%d Z&d Z'e(d        Z)e(d        Z*e(d        Z+e(d        Z,e(d        Z-e(d        Z.e(d        Z/e(d        Z0e(d        Z1e(d        Z2e(d        Z3d Z4y)"rC   z8
    A wrapper around a socket.socket that adds TLS
    NFc                    t        |t        j                        st        t	        dt        |                  t        |t              st        t	        dt        |                  |.t        |t              st        t	        dt        |                   | dd|      }||_        ||_	        |j                          |S )az  
        Takes an existing socket and adds TLS

        :param socket:
            A socket.socket object to wrap with TLS

        :param hostname:
            A unicode string of the hostname or IP the socket is connected to

        :param session:
            An existing TLSSession object to allow for session reuse, specific
            protocol or manual certificate validation

        :raises:
            ValueError - when any of the parameters contain an invalid value
            TypeError - when any of the parameters are of the wrong type
            OSError - when an error is returned by the OS crypto library
        zU
                socket must be an instance of socket.socket, not %s
                zK
                hostname must be a unicode string, not %s
                N`
                session must be an instance of oscrypto.tls.TLSSession, not %s
                )session)r   rU   rj   r   r   r"   r#   rB   rQ   	_hostname
_handshake)clsrj   hostnamer   
new_sockets        rt   wrapzTLSSocket.wrap  s    * &'..1N &!	   (G,N (#	   z':'FN '"	   tW5
#
'
rJ   c                    d| _         d| _        d| _        d| _        |
|d| _        nt        |t              st        t        dt        |                  t        |t              st        t        dt        |                  |8t        |t        j                        st        t        dt        |                  t        j                  ||f|      | _        | j                  j                  |       |t!               }n.t        |t               st        t        dt        |                  || _        | j                  r|| _        | j'                          yy)a  
        :param address:
            A unicode string of the domain name or IP address to connect to

        :param port:
            An integer of the port number to connect to

        :param timeout:
            An integer timeout to use for the socket

        :param session:
            An oscrypto.tls.TLSSession object to allow for session reuse and
            controlling the protocols and validation performed
        FrJ   NzR
                    address must be a unicode string, not %s
                    zI
                    port must be an integer, not %s
                    zJ
                    timeout must be a number, not %s
                    r   )r^   r_   r|   _decrypted_bytesrQ   r   r#   r   r   r"   r%   numbersNumberrU   create_connectionrx   rB   _sessionr   r   )ri   addressportrl   r   s        rt   r   zTLSSocket.__init__  sT      %   #?t|DL gw/ g&	!   dI. dO	!   ":gw~~+N g&	!   #44gt_gNDLLL##G,? lGGZ0N '"	    <<$DNOO rJ   c           	      H&   d}d}d}d}d}d}d}d}	 t         dk  r=t        t        d      }	t        j                  d|	      }
t	        |
       t        |	      }n;t        j                  t               t        j                  t        j                        }t        j                  |t        t              }
t	        |
       t        |       dz  | _        | t         | j                  <   | j"                  t$        | j                  <   t        j&                  || j                        }
t	        |
       | j(                  j+                  d      }t        j,                  ||t/        |            }
t	        |
       t         dk\  r^| j0                  j2                  xs | j0                  j4                  }| j0                  j2                   xr | j0                  j4                  }nd}| j0                  j2                   }t         dk  rnd	D ]E  }t6        |   }|| j0                  j8                  v }t        j:                  |||      }
t	        |
       G |rt        j<                  |d      }
t	        |
       n| j0                  j8                  D cg c]  }t6        |    }}t?        |      }tA        |      }t        jB                  ||      }
t	        |
       t        jD                  ||      }
t	        |
       |r0t        jF                  |t        jH                  d      }
t	        |
       t        t        d
      }t        jJ                  ||      }
t	        |
       tM        |      }tO        |dz        }tQ        t        d|      }t        jR                  |||      }
t	        |
       tM        |      }tU        t        d||      }g }|D ]P  }tW        |d      }tY        jZ                  ||      }t\        j_                  |      du }|s@|ja                  |       R t/        |      }t        t        d|      }tc        ||       tQ        t        d|      } t        jd                  || |      }
t	        |
       | j0                  jf                  | j(                  j+                  d      z   }!t        jh                  ||!t/        |!            }
t	        |
       t        jj                  |      }"| jl                  | jl                  }#d| _6        |#|"t        jn                  k(  rJt        jj                  |      }"| jl                  | jl                  }#d| _6        |#|"t        jn                  k(  rJt         dk  rt         dk\  r
|xr |"dk(  }$n|xr |"t        jp                  k(  }$|$rt        t        d      }%t        jr                  ||%      }
t	        |
       t        |%      }tu        jv                  | j(                        }&t        jx                  d|&      }t{        j|                  |&      }
t        |
       t        t        d      }'t        |'      }(t/        t        j                        |(_B        tO        t        j                        })tQ        t        d|)      |(_C        t        t        d      }*t        j                  t        j                  |'t               |*      }
t	        |
       t        |*      }t        t        d      }+t        j                  ||+      }
t	        |
       t        |+      }t        t        d      },t        |,      }-t        j                  |-_H        t        j                  t        j                  z  |-_K        t        |,      }.t        t        d      }/t        |/      }0t/        |.      |0_B        tO        |.      }1tQ        t        d|1      |0_C        t        j                  ||/      }
t	        |
       t        t        d      }2t        |2      }3t/        t        j                        |3_B        tO        t        j                        }4tQ        t        d|4      |3_C        t        t        d      }5t        j                  t        j                  |2t               |5      }
t	        |
       t        |5      }t        t        d      }6t        j                  ||6      }
t	        |
       t        |6      }t        t        d      }7t        |7      }8t        j                  |8_H        d|8_P        t        |7      }9t        t        d      }/t        |/      }0t/        |9      |0_B        tO        |9      }:tQ        t        d|:      |0_C        t        j                  ||/      }
t	        |
       tu        j                  |||g      }t        j                  ||      }
t	        |
       | j0                  j4                  rg };g }<| j0                  j4                  D ]9  }=t        |=      }>|<ja                  |>       |;ja                  |>j                         ; t        j                  |d      }
t	        |
       tu        j                  |;      }?t        j                  ||?      }
t	        |
       t        t        d      }@t        j                  ||@      }
t	        |
       tM        |@      }At        t        j                  t        j                  g      }B|A|Bvrt        j                  }"nQt        jj                  |      }"|"t        jn                  k(  r)t        jj                  |      }"|"t        jn                  k(  r)d| _\        t        t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  g      }C|"|Cv rn|rt{        j|                  |       d}t        t        d      }%t        jr                  ||%      }
t	        |
       t        |%      }t        t        d      }Dt        j                  ||D      }
tM        |D      }Et        | j                        }Fd}Gd}Hd}Id}Jd}Kd}=d}L|FrFd   }=t        |=      }M|Mj                  }GEt        j                  k(  }H|G xr Et        j                  k(  }KEt        j                  k(  }I|Et        j                  k(  }J|Et        j                  k(  }L|Et        j                  k(  }Nt         dk\  rq|=d   d   }O|Od    j                  j                  }P|Od!   j                  j                  }Qt        j                  j                  t        j                        }R|Q|Rk  }I|P|RkD  }JFr*Fd   j                  t        d"d#g      v rt        Fd          Hrt        |=       Lrt        |=| j(                         n9IsJrt        |=       n)Krt        |=       nGrt        |=       nNrt        |=       t        | j                        r
t                t        |=       |"t        j                  k(  r)t        | j                        r
t                t                |"t        j                   k(  rt                |"t        j                  k(  rt                |"t        t        j                  t        j
                  g      v r?| xj                  t        | j"                        z  c_e        t        | j                         |"t        t        j                  t        j                  g      v rl| j                  s)| xj                  t        | j"                        z  c_e        t        | j                        rt        | j                         t                t         dk  r(t        | j                        }S|SSd$k  rt                |"t        jn                  k(  }T|"t        jp                  k(  }U| j0                  j2                  xr U}VTsVst	        |"t               || _        t        t        d%      }Wt        j                  ||W      }
t	        |
       tM        |W      }t         |   | _        t        t        d&      }Xt        j$                  ||X      }
t	        |
       tM        |X      }YtW        |Yd      }ZtY        jZ                  |Z|Z      | _        t)        | j                  | j*                        }[|[d'   | _        |[d(   | _        |[d)   | _        	 |r"t{        j|                  |      }
t        |
       d}|r"t{        j|                  |      }
t        |
       d}|r"t{        j|                  |      }
t        |
       d}|r"t{        j|                  |      }
t        |
       d}|rt{        j|                  |       d}yyc c}w # t2        t4        j6                  f$ rg |rKt         dk  r"t        j8                  |      }
t	        |
       n t{        j|                  |      }
t        |
       d| _        | j;                           w xY w# |r"t{        j|                  |      }
t        |
       d}|r"t{        j|                  |      }
t        |
       d}|r"t{        j|                  |      }
t        |
       d}|r"t{        j|                  |      }
t        |
       d}|rt{        j|                  |       d}w w xY w)*z2
        Perform an initial TLS handshake
        N
   r   zSSLContextRef *Fizutf-8)r   r   T)rD   rE   rF   size_t *   z
uint32_t *uint32_tr   )widthz
uint32_t[])r   rA   r   SecTrustRef *CSSM_OIDzchar *zSecPolicySearchRef *zSecPolicyRef *CSSM_APPLE_TP_OCSP_OPTIONS	CSSM_DATACSSM_APPLE_TP_CRL_OPTIONSzSecTrustResultType *z
OSStatus *)r      tbs_certificatevalidity
not_before	not_aftermd5md2i   zSSLProtocol *zSSLCipherSuite *compression
session_idsession_ticket)r	   r   r   SSLNewContextr
   r    SSLCreateContextr   r   kSSLClientSidekSSLStreamTypeSSLSetIOFuncs_read_callback_pointer_write_callback_pointerid_connection_idrN   rQ   rP   SSLSetConnectionr   encodeSSLSetPeerDomainNamerS   r   r   r   _PROTOCOL_STRING_CONST_MAPr   SSLSetProtocolVersionEnabledSSLSetEnableCertVerifyminmaxSSLSetProtocolVersionMinSSLSetProtocolVersionMaxSSLSetSessionOption"kSSLSessionOptionBreakOnServerAuthSSLGetNumberSupportedCiphersr   r   r   SSLGetSupportedCiphersr   r   r&   rO   _cipher_blacklist_regexsearchr   r   SSLSetEnabledCiphersr   SSLSetPeerIDSSLHandshakere   rc   errSSLServerAuthCompletedSSLCopyPeerTrustr   cf_string_from_unicodeSecPolicyCreateSSLr   	CFReleaser   r   APPLE_TP_REVOCATION_OCSPLengthDataSecPolicySearchCreateCSSM_CERT_X_509v3SecPolicySearchCopyNextCSSM_APPLE_TP_OCSP_OPTS_VERSIONVersionCSSM_TP_ACTION_OCSP_DISABLE_NET&CSSM_TP_ACTION_OCSP_CACHE_READ_DISABLEFlagsr   SecPolicySetValueAPPLE_TP_REVOCATION_CRLCSSM_APPLE_TP_CRL_OPTS_VERSIONCrlFlagscf_array_from_listSecTrustSetPoliciesr>   sec_certificate_ref!SecTrustSetAnchorCertificatesOnlySecTrustSetAnchorCertificatesSecTrustEvaluater`   kSecTrustResultProceedkSecTrustResultUnspecifiederrSSLXCertChainInvalidr^   errSSLCertExpirederrSSLCertNotYetValiderrSSLUnknownRootCerterrSSLNoRootCerterrSSLHostNameMismatcherrSSLInternalSecTrustGetCssmResultCoder-   r_   self_signedCSSMERR_TP_CERT_REVOKEDCSSMERR_TP_NOT_TRUSTEDCSSMERR_TP_CERT_EXPIREDCSSMERR_TP_CERT_NOT_VALID_YET!CSSMERR_APPLETP_HOSTNAME_MISMATCHCSSMERR_TP_CERT_SUSPENDEDchosennativedatetimenowr   utc	hash_algor=   r:   r5   r3   r7   r;   r6   r+   r0   r<   errSSLPeerHandshakeFailr4   errSSLWeakPeerEphemeralDHKeyr1   errSSLPeerProtocolVersionr9   errSSLRecordOverflowrb   ra   r8   rZ   r[   r,   r2   r.   r(   _session_contextSSLGetNegotiatedProtocolVersion_PROTOCOL_CONST_STRING_MAP	_protocolSSLGetNegotiatedCipher_cipher_suiter/   r|   _compression_session_id_session_ticketOSErrorrU   rV   SSLDisposeContextclose)\ri   session_contextssl_policy_refcrl_search_refcrl_policy_refocsp_search_refocsp_policy_refpolicy_array_ref	trust_refsession_context_pointerresultutf8_domaindisable_auto_validationexplicit_validationr   protocol_constenabledprotocol_constsmin_protocolmax_protocolsupported_ciphers_pointersupported_cipherscipher_buffersupported_cipher_suites_pointersupported_cipher_suitesgood_cipherssupported_cipher_suitecipher_suitecipher_suite_namegood_ciphernum_good_ciphersgood_ciphers_arraygood_ciphers_pointerpeer_idhandshake_result	exceptiondo_validationtrust_ref_pointercf_string_hostnameocsp_oid_pointerocsp_oidocsp_oid_bufferocsp_search_ref_pointerocsp_policy_ref_pointerocsp_struct_pointerocsp_structocsp_struct_bytescssm_data_pointer	cssm_dataocsp_struct_buffercrl_oid_pointercrl_oidcrl_oid_buffercrl_search_ref_pointercrl_policy_ref_pointercrl_struct_pointer
crl_structcrl_struct_bytescrl_struct_bufferca_cert_refsca_certscertca_cert	array_refresult_pointertrust_result_codeinvalid_chain_error_codeshandshake_error_codesresult_code_pointerresult_codechainr  revokedexpirednot_yet_valid	no_issuerbad_hostnameoscrypto_certvalidity_too_longr   r   r   utcnowdh_params_lengthwould_blockserver_auth_completer   protocol_const_pointercipher_int_pointer
cipher_intcipher_bytessession_infos\                                                                                               rt   r   zTLSSocket._handshake  s   
 	X	!')*-h8I*J'!//7NO ("()@"A #+";";F!00!00# ++&'F
 V$"$T(Z"7D48T00104L,,-..@S@STFV$..//8K22K F
 V$8+*.--*J*J*ndmmNnNn'+/==+K+K'K&qQUQ^Q^QqQq#*.'*.--*J*J&J#  ') ; -H%?%IN&$--*B*BBG%BB'&F
 %V,- +%<<_eTF$V, Y]XeXeXpXp"qH#=h#G"q"q"?3"?3!::#  !(!::#  !(*%99'%HHF
 %V, ),Hj(A%::?LefFV$ %&? @-.?!.CDM.28\=.Y+44/)F
 V$ %&? @&8/!	'# L*A @&+,B!L$4$8$8|$T!5<<=NOSWW ''(>?@  #<0!$X|=M!N(,7#',@R#S 22$ F
 V$
 mm,,t~~/D/DW/MMG**?GS\RFV$'44_E* OO	"&"m&D&DD#+#8#8#I ??. $I&*DO#O #m&D&DD  ').>'.I 3 M8HA8M 3 s8HMLsLs8s$'/$B!!22#% !("#45	%.%E%Ednn%U"!)!<!<TCU!V'112DE' $*(J#? !"23"%m&L&L"M"3M4Z4Z"[ $Xx I*-h8N*O'!77!33$F+	 !("()@"A*-h8H*I'!99/Kbc ("()@"A&,X7S&T#$%89&3&S&S#!AA!HHI ! %11D$E!$*8[$A!"#45	#&'8#9	 %67H%I"!%h:L!M	!33OEVW ( #):"> 1!$]%J%J!K!2=3X3X!Y#HhG),X7M)N&!77!33#F*	 !(!'(>!?),X7G)H&!99.J`a (!'(>!?%+H6Q%R"#$67
%2%Q%Q
"&'
##/0B#C $*8[$A!"#45	#&'7#8	 $56F$G!!%h:K!L	!33NDUV (#,#?#?""#A $  "55iAQR (==33#%L!H $ @ @ I"24"8 0$++G,G,GHI
 &GG	SXYF$V, ) < <\ JI%CCIyYF$V,!$X/E!F!229nM ($).$9!,/!88!<<1 -) %,EE'4'L'L$'/'<'<_'M$*m.L.LL+3+@+@+Q( +m.L.LL $(D $'55//3333..44,,) %!  #88",,Y7 $I$'/$B!!22#% !("#45	&)(L&A#!;;IGZ[#$78%d&8&89# %!	$ 8D$4T$:M"/";";K)]-R-RRG$/ gK=CgCg4gI)]-R-RRG$/=3^3^$^M#.-2a2a#aL(3}7^7^(^% (83#'(9#::#F%-l%;%B%B%I%I
$,[$9$@$@$G$G	!)!2!2!6!6x||!D"+f"4(2V(;U1X//3u~3FF(q2!$'"48/5#D) %d+&+D1-d.@.@A%'"4(=#H#HH-d.@.@A%'!=#M#MM!=#J#JJ&(3(J(JMLhLh'i#jj""odll&CC"$T%7%783(J(JMLkLk'l#mm++&&/$,,*GG&(););<(););<#%(*#78J8J#K #/4Dt4K#%*m.L.LLK#3}7^7^#^  $ @ @ YEY'8 !18<$3D!%(?%C"==&F V$"#9:N7GDN!$X/A!B44"F V$12J'
!<L!1!5!5lL!QD-""""L !-] ;D+L9D#/0@#AD ( '11.A'!%'11.A'!%'11/B'"&'112BC'#' ((3 	 w #rj ' 	#g-%77HF$V,+55oFF#F+$(D!JJL	$ '11.A'!%'11.A'!%'11/B'"&'112BC'#' ((3 	 sP   I.AG/  AG*E)AG/ :EAG/ W:AG/ W+AG/ G*AG/ G/BAI2I2AI5 I5B,AL!c                    t        |t              st        t        dt	        |                  | j
                  4| j                  dk7  r| j                  }d| _        |S | j                          t        | j                        }||k\  r%| j                  d| }| j                  |d | _        |S |dkD  r&| j                  d      s| j                  }d| _        |S |t        | j                        z
  }t        |      }t        t        d      }t        j                  | j
                  |||      }| j                  | j                  }d| _        ||r;|t        t         j"                  t         j$                  g      vrt'        |t(               |r;|t         j$                  k(  r(d| _        | j-                  d       | j                          t/        |      }	| j                  t1        ||	      z   }||d | _        |d| S )a  
        Reads data from the TLS-wrapped socket

        :param max_length:
            The number of bytes to read - output may be less than this

        :raises:
            socket.socket - when a non-TLS socket error occurs
            oscrypto.errors.TLSError - when a TLS-related error occurs
            oscrypto.errors.TLSDisconnectError - when the connection disconnects
            oscrypto.errors.TLSGracefulDisconnectError - when the remote end gracefully closed the connection
            ValueError - when any of the parameters contain an invalid value
            TypeError - when any of the parameters are of the wrong type
            OSError - when an error is returned by the OS crypto library

        :return:
            A byte string of the data read
        zG
                max_length must be an integer, not %s
                NrJ   r   r   TF)r   r%   r   r   r"   r  r   _raise_closedrS   select_readr   r   r   SSLReadre   r`   r   rc   errSSLClosedGracefulr
   r(   _gracefully_closed	_shutdownr   r   )
ri   
max_lengthry   buffered_lengthto_readread_bufferprocessed_pointerr1  rI  
bytes_reads
             rt   r   zTLSSocket.read  s   ( *i0N *%	     ( $$+..(+% d334 j(**1Z8F$($9$9*+$FD!M Qt'7'7':**F$'D!M s4#8#899'0*5!!!!	
 ??&I"DOOfC)G)GIkIk(l$mmVX.f B BB&*D#NN5! ,-
&&):;
)SS &z{ 3a
##rJ   c                     t        | j                        dkD  ryt        j                  | j                  gg g |      \  }}}t        |      dkD  S )aZ  
        Blocks until the socket is ready to be read from, or the timeout is hit

        :param timeout:
            A float - the period of time to wait for data to be read. None for
            no time limit.

        :return:
            A boolean - if data is ready to be read. Will only be False if
            timeout is not None.
        r   T)rS   r   rT   rQ   )ri   rl   rn   ro   s       rt   r  zTLSSocket.select_readQ  sJ     t$$%)!==$,,RI
Aq:""rJ   c                 n   t        |t              s.t        |t              st        t	        dt        |                  d}t        |t              }	 t        | j                        dkD  r| j                  }d| _        n%| j                         xs d}| j                  |      }t        |      }||z  }|r$|j                  |      }|R|j                         }nBt        d|t        |      z
  dz
        }	|j                  ||	      }|dk7  r|t        |      z   }n||d | j                  z   | _        |d| S )a  
        Reads data from the socket until a marker is found. Data read includes
        the marker.

        :param marker:
            A byte string or regex object from re.compile(). Used to determine
            when to stop reading. Regex objects are more inefficient since
            they must scan the entire byte string of read data each time data
            is read off the socket.

        :return:
            A byte string of the data read, including the marker
        z_
                marker must be a byte string or compiled regex object, not %s
                rJ   r   rw   Nr   )r   r$   Patternr   r   r"   rS   r   _os_buffered_sizer   r   endr   find)
ri   markerry   is_regexrp   r  offsetmatchr  starts
             rt   
read_untilzTLSSocket.read_untile  s>    &(+Jvw4ON &!	   fg.4(()A---(+%002:d		'*[FeOFf-$))+C
 AvF3a78FE2B;#f+-C1 4 !'stt/D/D Da}rJ   c                     t        t        d      }t        j                  | j                  |      }t	        |       t        |      S )a  
        Returns the number of bytes of decrypted data stored in the Secure
        Transport read buffer. This amount of data can be read from SSLRead()
        without calling self._socket.recv().

        :return:
            An integer - the number of available bytes
        r   )r   r   SSLGetBufferedReadSizer  r
   r   )ri   num_bytes_pointerr1  s      rt   r  zTLSSocket._os_buffered_size  sC      *500!!
 	 &''rJ   c                 ,    | j                  t              S )z
        Reads a line from the socket, including the line ending of "\r\n", "\r",
        or "\n"

        :return:
            A byte string of the next line from the socket
        )r  _line_regexri   s    rt   	read_linezTLSSocket.read_line  s     {++rJ   c                 h    d}|}|dkD  r(|| j                  |      z  }|t        |      z
  }|dkD  r(|S )z
        Reads exactly the specified number of bytes from the socket

        :param num_bytes:
            An integer - the exact number of bytes to read

        :return:
            A byte string of the data that was read
        rJ   r   )r   rS   )ri   	num_bytesry   	remainings       rt   read_exactlyzTLSSocket.read_exactly  sF     	!mdii	**F!CK/I !m rJ   c                    | j                   | j                          t        t        d      }t	        |      }|rt        |      }t        j                  | j                   |||      }| j                  | j                  }d| _        |t        |t               t        |      }||d }t	        |      }|dkD  r| j                          |ryy)a  
        Writes data to the TLS-wrapped socket

        :param data:
            A byte string to write to the socket

        :raises:
            socket.socket - when a non-TLS socket error occurs
            oscrypto.errors.TLSError - when a TLS-related error occurs
            oscrypto.errors.TLSDisconnectError - when the connection disconnects
            oscrypto.errors.TLSGracefulDisconnectError - when the remote end gracefully closed the connection
            ValueError - when any of the parameters contain an invalid value
            TypeError - when any of the parameters are of the wrong type
            OSError - when an error is returned by the OS crypto library
        Nr   r   )r  r~  r   r   rS   r   SSLWritere   r
   r(   r   select_write)ri   rm   r  data_lenwrite_bufferr1  rI  bytes_writtens           rt   writezTLSSocket.write  s    "   ( *5t9,T2L&&%%!	F * OO	"&VX.!"34M'D4yH!|!!#% rJ   c                 l    t        j                   g | j                  gg |      \  }}}t        |      dkD  S )aw  
        Blocks until the socket is ready to be written to, or the timeout is hit

        :param timeout:
            A float - the period of time to wait for the socket to be ready to
            written to. None for no time limit.

        :return:
            A boolean - if the socket is ready for writing. Will only be False
            if timeout is not None.
        r   )rT   rQ   rS   )ri   rl   ro   write_readys       rt   r  zTLSSocket.select_write  s5     #MM"t||nb'J;;!##rJ   c                    | j                   yt        j                  | j                         }t        dk  r+t        j                  | j                         }t        |       n*t        j                  | j                         }t        |       d| _         |rd| _	        	 | j                  j                  t        j                         y# t        j                  $ r Y yw xY w)z
        Shuts down the TLS session and then shuts down the underlying socket

        :param manual:
            A boolean if the connection was manually shutdown
        Nr   T)r  r   SSLCloser	   r&  r
   r   r   r   _local_closedrQ   shutdownrU   	SHUT_RDWRrV   )ri   manualr1  s      rt   r  zTLSSocket._shutdown	  s       ( ""4#8#89g%//0E0EFFV$#--d.C.CDFF# $!%D	LL!!'"3"34 		s   )C CCc                 &    | j                  d       y)zV
        Shuts down the TLS session and then shuts down the underlying socket
        TN)r  r  s    rt   r  zTLSSocket.shutdown(  s    
 	trJ   c                    	 | j                          | j                  r"	 | j                  j                          d| _        | j
                  t        v rt        | j
                  = yy# t        j                  $ r Y Aw xY w# | j                  r<	 | j                  j                          n# t        j                  $ r Y nw xY wd| _        | j
                  t        v rt        | j
                  = w w xY w)zN
        Shuts down the TLS session and socket and forcibly closes it
        N)r  rQ   r'  rU   rV   r   rP   r  s    rt   r'  zTLSSocket.close/  s    
	6MMO ||LL&&(  $""l2 !4!45 3	    ||LL&&( #""l2 !4!45 3s@   A> A% %A;:A;>C,B'&C,'B=:C,<B==/C,c                 V   d}d}d}	 t        t        d      }t        j                  | j                  |      }t	        |       t        |      }t        j                  |      }g | _        t        d|      D ]  }t        j                  ||      }t        j                  |      }t        j                  |      }t        j                  |      }t        |       d}t!        j"                  |      }	|dk(  r|	| _        | j                  j'                  |	        	 |r t        j                  |      }t        |       |r!t        j                  |      }t        |       yy# |r t        j                  |      }t        |       |r!t        j                  |      }t        |       w w xY w)zh
        Reads end-entity and intermediate certificate information from the
        TLS session
        Nr   r   )r   r   r   r  r
   r    SecTrustGetCertificateCount_intermediatesrangeSecTrustGetCertificateAtIndexSecCertificateCopyDatar   cf_data_to_bytesr   r   r   r   load_certificater   )
ri   r/  cf_data_refr1  rK  number_certsindexr   	cert_datarc  s
             rt   _read_certificateszTLSSocket._read_certificatesB  s    	(	( #Ho >..%%!F V$01I#??	JL"$Dq,/ 5&.&L&L'# '==>QR%66{C	'11+>'"&++I6A:(,D%''..t4%5* '11)<''11+>'  '11)<''11+>' s   DE! !AF(c                 t    | j                   rt        d      | j                  rt        d      t        d      )zi
        Raises an exception describing if the local or remote end closed the
        connection
        z!The connection was already closedz$The remote end closed the connectionzThe connection was closed)r  r)   r  r*   r  s    rt   r~  zTLSSocket._raise_closedv  s:     $%HII$$,-STT$%@AArJ   c                     | j                   | j                          | j                  | j                          | j                  S )zu
        An asn1crypto.x509.Certificate object of the end-entity certificate
        presented by the server
        )r  r~  r  r  r  s    rt   certificatezTLSSocket.certificate  s@       ( $##%   rJ   c                     | j                   | j                          | j                  | j                          | j                  S )zz
        A list of asn1crypto.x509.Certificate objects that were presented as
        intermediates by the server
        )r  r~  r  r  r  r  s    rt   intermediateszTLSSocket.intermediates  s@       ( $##%"""rJ   c                     | j                   S )zg
        A unicode string of the IANA cipher suite name of the negotiated
        cipher suite
        )r!  r  s    rt   rA  zTLSSocket.cipher_suite  s     !!!rJ   c                     | j                   S )zM
        A unicode string of: "TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3"
        )r  r  s    rt   r   zTLSSocket.protocol       ~~rJ   c                     | j                   S )z5
        A boolean if compression is enabled
        )r"  r  s    rt   r   zTLSSocket.compression  s        rJ   c                     | j                   S zM
        A unicode string of "new" or "reused" or None for no ticket
        )r#  r  s    rt   r   zTLSSocket.session_id  s     rJ   c                     | j                   S r  )r$  r  s    rt   r   zTLSSocket.session_ticket  s     ###rJ   c                     | j                   S )zM
        The oscrypto.tls.TLSSession object used for this connection
        )r   r  s    rt   r   zTLSSocket.session  s     }}rJ   c                     | j                   S )zN
        A unicode string of the TLS server domain name or IP address
        )r   r  s    rt   r   zTLSSocket.hostname  r  rJ   c                 <    | j                   j                         d   S )zJ
        An integer of the port number the socket is connected to
        r   )rj   getpeernamer  s    rt   r   zTLSSocket.port  s     {{&&(++rJ   c                 R    | j                   | j                          | j                  S )z9
        The underlying socket.socket connection
        )r  r~  rQ   r  s    rt   rj   zTLSSocket.socket  s&       ( ||rJ   c                 $    | j                          y N)r'  r  s    rt   __del__zTLSSocket.__del__  s    

rJ   r  )r   N)5r   r   r   r   rQ   r   re   r  r   r   r  r  r  r!  r"  r#  r$  r^   r_   r|   r  r  r   classmethodr   r   r   r   r  r  r  r  r  r  r  r  r  r'  r  r~  propertyr  r  rA  r   r   r   r   r   r   r   rj   r  r   rJ   rt   rC   rC   {  s    GHJILNIMLKOOMMMN1 1fDLf!PQ$f#(6p(&	,&)$V$ >6&2(hB ! ! # # " "   ! !     $ $     , ,  rJ   )m
__future__r   r   r   r   r  sysrerj   rU   rT   r   rW   weakref	_securityr   r	   r
   r   _core_foundationr   r   r   _asn1r   r   r   r   _errorsr   _ffir   r   r   r   r   r   r   r   r   r   r   r   r    r!   _typesr"   r#   r$   r%   _cipher_suitesr&   utilr'   errorsr(   r)   r*   _tlsr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   
asymmetricr>   keysr?   version_infoxranger  _pattern_typer  __all__kSSLProtocol2kSSLProtocol3kTLSProtocol1kTLSProtocol11kTLSProtocol12r   r  compiler  r   WeakValueDictionaryrN   rP   ru   ra   r   r   r   objectrB   rC   r   rJ   rt   <module>r     s   R R  
 	      R R H H 
 %     = < -  M M     * 6 $dEfGjjG  ((((((++++    )  )  bjj)$"**%no .7..0 X/v045n "(M>J "8^_M d& d&Ns srJ   