o
    èó·i;5  ã                
   @   sæ  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	 d dl
mZmZ d dlmZmZ d dlmZ zd dlmZ W n eyI   edƒZY nw z
d dl
mZ d	ZW n eyc   d
ZedƒZY nw zd dlmZ d dlmZ d dlmZ d	ZW n ey…   d
ZY nw dZddd„Zdeeef de fdd„Z!es¯dde"de#dee  dee$ fdd„Z%ndde"de#dee  dee$ fdd„Z%esÓde"deee ef  fdd„Z&ndedeee ef  fdd„Z&deeef de#defdd„Z'dS ) é    N)ÚOptionalÚUnionÚDictÚAny)ÚDeferred)ÚIPv4AddressÚUNIXAddress)Ú
ITransportÚIProcessTransport)ÚTransportDetails)ÚPipeAddress)ÚIPv6AddressTF)ÚISSLTransport)ÚTLSMemoryBIOProtocol)Ú
Connection)ÚsleepÚpeer2strÚtransport_channel_idÚextract_peer_certificateÚcreate_transport_detailsc                 C   s*   |sddl m} tƒ }| | |jd¡ |S )aD  
    Inline sleep for use in co-routines (Twisted ``inlineCallback`` decorated functions).

    .. seealso::
       * `twisted.internet.defer.inlineCallbacks <http://twistedmatrix.com/documents/current/api/twisted.internet.defer.html#inlineCallbacks>`__
       * `twisted.internet.interfaces.IReactorTime <http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IReactorTime.html>`__

    :param delay: Time to sleep in seconds.
    :type delay: float
    :param reactor: The Twisted reactor to use.
    :type reactor: None or provider of ``IReactorTime``.
    r   )ÚreactorN)Ztwisted.internetr   r   Z	callLaterÚcallback)Údelayr   Úd© r   ú[C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\autobahn/twisted/util.pyr   D   s
   r   Ú	transportÚreturnc                 C   sÀ   t  | ¡rd | j¡}|S t | ¡r\|  ¡ }t|tƒr%d |j|j	¡}|S t
r6t|tƒr6d |j|j	¡}|S t|tƒrJ|jrFd |j¡}|S d}|S t|tƒrXd t ¡ ¡}|S d}|S d}|S )	a•  
    Return a *peer descriptor* given a Twisted transport, for example:

    * ``tcp4:127.0.0.1:52914``: a TCPv4 socket
    * ``unix:/tmp/server.sock``: a Unix domain socket
    * ``process:142092``: a Pipe originating from a spawning (parent) process
    * ``pipe``: a Pipe terminating in a spawned (child) process

    :returns: Returns a string representation of the peer of the Twisted transport.
    z
process:{}ztcp4:{0}:{1}ztcp6:{0}:{1}zunix:{0}Úunixzprocess:{0}ÚunknownN)r
   Ú
providedByÚformatÚpidr	   ZgetPeerÚ
isinstancer   ÚhostÚportÚ	_HAS_IPV6r   r   Únamer   ÚosÚgetppid)r   ÚresÚaddrr   r   r   r   X   s0   

ë
ï
ñõ
öüÿr   Ú	is_serverÚchannel_id_typec                 C   s   |d u rdS t d |¡ƒ‚)Nó                                    zUcannot determine TLS channel ID of type "{}" when TLS is not available on this system)ÚRuntimeErrorr!   )r   r,   r-   r   r   r   r   €   s   r   c                 C   s´   |du rdS |dvrt d |¡ƒ‚t| tƒs t d |t| ƒ¡ƒ‚|  ¡ }|r+t|tƒs-J ‚d}|dkrS||kr<| ¡ }n| ¡ }|du rFdS t	 
¡ }| |¡ | ¡ S td |¡ƒ‚)	a(  
        Return TLS channel ID of WAMP transport of the given TLS channel ID type.

        Application-layer user authentication protocols are vulnerable to generic credential forwarding attacks,
        where an authentication credential sent by a client C to a server M may then be used by M to impersonate C at
        another server S.
        To prevent such credential forwarding attacks, modern authentication protocols rely on channel bindings.
        For example, WAMP-cryptosign can use the tls-unique channel identifier provided by the TLS layer to strongly
        bind authentication credentials to the underlying channel, so that a credential received on one TLS channel
        cannot be forwarded on another.

        :param transport: The Twisted TLS transport to extract the TLS channel ID from. If the transport isn't
            TLS based, and non-empty ``channel_id_type`` is requested, ``None`` will be returned. If the transport
            is indeed TLS based, an empty ``channel_id_type`` of ``None`` is requested, 32 NUL bytes will be returned.
        :param is_server: Flag indicating that the transport is a server transport.
        :param channel_id_type: TLS channel ID type, if set currently only ``"tls-unique"`` is supported.
        :returns: The TLS channel ID (32 bytes).
        Nr.   )ú
tls-uniquez*invalid TLS channel ID type "{}" requestedz[cannot determine TLS channel ID of type "{}" when TLS is not available on this transport {}Tr0   z7should not arrive here (unhandled channel_id_type "{}"))r/   r!   r#   r   ÚtypeZ	getHandler   Zget_peer_finishedZget_finishedÚhashlibÚsha256ÚupdateÚdigestÚNotImplementedError)r   r,   r-   Ú
connectionZis_not_resumedZtls_finished_msgÚmr   r   r   r   †   s.   
ÿÿ

c                 C   s   dS )z~
        Dummy when no TLS is available.

        :param transport: Ignored.
        :return: Always return ``None``.
        Nr   )r   r   r   r   r   È   s   r   c                 C   s‚  t  | ¡r
t| dƒsdS |  ¡ }|r¿dd„ }d || d¡ƒ¡ ¡ d || d¡ƒ¡ ¡ d || d¡ƒ¡ ¡ t| ¡ ƒ|| 	¡ ƒt
| ¡ ƒ|| ¡ ƒt
| ¡ ƒ|| ¡ ƒ|| ¡ ƒg d	œ}t| ¡ ƒD ]$}| |¡}d || ¡ ƒ¡d ||ƒ¡| ¡ d
kdœ}|d  |¡ qdd| ¡ fd| ¡ ffD ]'\}}i ||< | ¡ D ]\}	}
||	ƒ}	||
ƒ}
d |
¡|| d |	¡ ¡ < q¡q•|S dS )a«  
        Extract TLS x509 client certificate information from a Twisted stream transport, and
        return a dict with x509 TLS client certificate information (if the client provided a
        TLS client certificate).

        :param transport: The secure transport from which to extract the peer certificate (if present).
        :returns: If the peer provided a certificate, the parsed certificate information set.
        ÚgetPeerCertificateNc                 S   s   t | tƒr
|  d¡S | S )NÚutf8)r#   ÚbytesÚdecode)Ú_valuer   r   r   Úmaybe_bytesá   s   

z-extract_peer_certificate.<locals>.maybe_bytesz{}Úmd5Úsha1r3   )r?   r@   r3   ZexpiredÚhashÚserialZsignature_algorithmÚversionZ
not_beforeZ	not_afterÚ
extensionsr   )r'   ÚvalueÚcriticalrD   ÚsubjectZissuer)r   r    Úhasattrr9   r!   r5   ÚupperÚboolZhas_expiredZsubject_name_hashÚintZget_serial_numberZget_signature_algorithmÚget_versionZget_notBeforeZget_notAfterÚrangeZget_extension_countÚget_extensionZget_short_nameZget_criticalÚappendZget_subjectZ
get_issuerZget_componentsÚlower)r   Úcertr>   ÚresultÚiÚextZext_infoÚentityr'   ÚkeyrE   r   r   r   r   Ñ   sB   







õ

ý ýÚc                 C   sš   t | ƒ}t ¡ }ttdƒrt ¡ }nt ¡ }d}tr1t 	| ¡r1dt
| |dƒi}tj}t| ƒ}d}	n	i }tj}d}d}	tj}
t||
||||||	||d�
}|S )aB  
    Create transport details from Twisted transport.

    :param transport: The Twisted transport to extract information from.
    :param is_server: Flag indicating whether this transport side is a "server" (as in TCP server).
    :return: Transport details object filled with information from the Twisted transport.
    Úget_native_idéÿÿÿÿr0   TNF)
Úchannel_typeÚchannel_framingÚpeerr,   Úown_pidÚown_tidÚown_fdÚ	is_secureÚ
channel_idÚ	peer_cert)r   r(   ÚgetpidrH   Ú	threadingrW   Ú	get_identÚ_HAS_TLSr   r    r   r   ZCHANNEL_TYPE_TLSr   ZCHANNEL_TYPE_TCPZCHANNEL_FRAMING_WEBSOCKET)r   r,   r[   r\   r]   r^   r`   rY   ra   r_   rZ   Útdr   r   r   r     s,   

þþr   )N)(r(   r2   rc   Útypingr   r   r   r   Ztwisted.internet.deferr   Ztwisted.internet.addressr   r   Ztwisted.internet.interfacesr	   r
   Zautobahn.wamp.typesr   Ztwisted.internet.stdior   ÚImportErrorr1   r   r&   r   Ztwisted.protocols.tlsr   ZOpenSSL.SSLr   re   Z__allr   Ústrr   ÚobjectrJ   r;   r   r   r   r   r   r   r   Ú<module>   sL   þþÿ
	'&$A 	"7