U
    •uÄc;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k
r”   edƒZY nX zd dl
mZ d	ZW n  ek
rÊ   d
ZedƒZY nX z,d dlmZ d dlmZ d dlmZ d	ZW n ek
�r   d
ZY nX dZddd„Zeeef e dœdd„Z!e�s`de"e#ee  ee$ dœdd„Z%nd e"e#ee  ee$ dœdd„Z%e�s¢e"eee ef  dœdd„Z&neeee ef  dœdd„Z&eeef e#edœ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)Útwisted.internetr   r   Z	callLaterÚcallback)Údelayr   Úd© r   úW/var/www/html/NKSPORTS/school/venv/lib/python3.8/site-packages/autobahn/twisted/util.pyr   D   s
    r   )Ú	transportÚreturnc                 C   s²   t  | ¡rd | j¡}n–t | ¡rª|  ¡ }t|tƒrFd |j|j	¡}q®t
rft|tƒrfd |j|j	¡}q®t|tƒrŠ|jr„d |j¡}q¨d}q®t|tƒr¤d t ¡ ¡}q®d}n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}Úunknown)r
   Ú
providedByÚformatÚpidr	   ZgetPeerÚ
isinstancer   ÚhostÚportÚ	_HAS_IPV6r   r   Únamer   ÚosÚgetppid)r   ÚresÚaddrr   r   r   r   X   s"    




r   )r   Ú	is_serverÚchannel_id_typer   c                 C   s   |d krdS t d |¡ƒ‚d S )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krdS |dkr"t d |¡ƒ‚t| tƒs@t d |t| ƒ¡ƒ‚|  ¡ }|rVt|tƒsZt‚d}|dkr¨||krx| ¡ }n| 	¡ }|dkrŒdS t
 ¡ }| |¡ | ¡ S ntd |¡ƒ‚dS )	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 {}Tr1   z7should not arrive here (unhandled channel_id_type "{}"))r0   r"   r$   r   ÚtypeZ	getHandler   ÚAssertionErrorZget_peer_finishedZget_finishedÚhashlibÚsha256ÚupdateÚdigestÚNotImplementedError)r   r-   r.   Z
connectionZis_not_resumedZtls_finished_msgÚmr   r   r   r   †   s0    
 ÿÿ


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 ]H}| |¡}d || ¡ ƒ¡d ||ƒ¡| ¡ d
kdœ}|d  |¡ qÊd| ¡ fd| ¡ ffD ]R\}}i ||< | ¡ D ]6\}	}
||	ƒ}	||
ƒ}
d |
¡|| d |	¡ ¡ < �qD�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 d S )NÚutf8)r$   ÚbytesÚdecode)Ú_valuer   r   r   Úmaybe_bytesá   s    

z-extract_peer_certificate.<locals>.maybe_bytesz{}Úmd5Úsha1r5   )r@   rA   r5   ZexpiredÚhashÚserialZsignature_algorithmÚversionZ
not_beforeZ	not_afterÚ
extensionsr   )r(   ÚvalueÚcriticalrE   ÚsubjectZissuer)r   r!   Úhasattrr:   r"   r7   Ú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(   ÚkeyrF   r   r   r   r   Ñ   s>    







õ

ý $)r   r-   r   c                 C   sš   t | ƒ}t ¡ }ttdƒr$t ¡ }nt ¡ }d}trbt 	| ¡rbd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éÿÿÿÿr1   TNF)
Úchannel_typeÚchannel_framingÚpeerr-   Úown_pidÚown_tidÚown_fdÚ	is_secureÚ
channel_idÚ	peer_cert)r   r)   ÚgetpidrI   Ú	threadingrX   Ú	get_identÚ_HAS_TLSr   r!   r   r   ZCHANNEL_TYPE_TLSr   ZCHANNEL_TYPE_TCPZCHANNEL_FRAMING_WEBSOCKET)r   r-   r\   r]   r^   r_   ra   rZ   rb   r`   r[   Útdr   r   r   r     s8    

 
þ     þr   )N)N)N)(r)   r4   rd   Ú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   ÚImportErrorr2   r   r'   r   Ztwisted.protocols.tlsr   ZOpenSSL.SSLr   rf   Z__allr   Ústrr   ÚobjectrK   r<   r   r   r   r   r   r   r   Ú<module>   sD   
	
' A	7