o
    9Bi,%                     @   s   d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZmZ G dd dejZG d	d
 d
ej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i Ze   ! D ]\Z"Z#e"dd dkroe"dd ee#< q[dS )z
Implements the SSH v2 key agent protocol.  This protocol is documented in the
SSH source code, in the file
U{PROTOCOL.agent<http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.agent>}.

Maintainer: Paul Swartz
    N)
ConchErrorMissingKeyStoreError)keys)NSgetMPgetNS)deferprotocolc                   @   sb   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdddZ	dd Z
dd Zdd Zdd ZdS )SSHAgentClientz
    The client side of the SSH agent protocol.  This is equivalent to
    ssh-add(1) and can be used with either ssh-agent(1) or the SSHAgentServer
    protocol, also in this package.
    c                 C   s   d| _ g | _d S N    )buf	deferredsself r   W/var/www/html/Trade-python/venv/lib/python3.10/site-packages/twisted/conch/ssh/agent.py__init__   s   
zSSHAgentClient.__init__c                 C   s   |  j |7  _ 	 t| j dkrd S td| j d d d }t| j d| k r)d S | j dd|  | j d| d  }| _ t|dd }| jd}|tkrW|t	d n|t
kra|d n|| q)N      !Lr   zagent failurer   )r   lenstructunpackordr   popAGENT_FAILUREerrbackr   AGENT_SUCCESScallback)r   datapackLenpacketreqTypedr   r   r   dataReceived    s    (
zSSHAgentClient.dataReceivedc                 C   s>   t dt|d || }| j| t }| j| |S Nz!LBr   )	r   packr   	transportwriter   Deferredr   append)r   r#   r    r'   r$   r   r   r   sendRequest2   s
   zSSHAgentClient.sendRequestc                 C   s   |  td}|| j |S )z
        @return: A L{Deferred} which will fire with a list of all keys found in
            the SSH agent. The list of keys is comprised of (public key blob,
            comment) tuples.
        r   )r,   AGENTC_REQUEST_IDENTITIESaddCallback_cbRequestIdentities)r   r$   r   r   r   requestIdentities9   s   z SSHAgentClient.requestIdentitiesc                 C   s   t |dd tkrtdt |dd  td|dd d }g }|dd }t|D ]}t|\}}t|\}}|||f q.|S )z}
        Unpack a collection of identities into a list of tuples comprised of
        public key blobs and comments.
        r   r   zunexpected response: %ir      N)r   AGENT_IDENTITIES_ANSWERr   r   r   ranger   r+   )r   r    numKeysresultiblobcommentr   r   r   r/   C   s   z#SSHAgentClient._cbRequestIdentitiesr   c                 C   s   |}|t |7 }| t|S )zK
        Add a private key blob to the agent's collection of keys.
        )r   r,   AGENTC_ADD_IDENTITY)r   r7   r8   reqr   r   r   addIdentityS   s   zSSHAgentClient.addIdentityc                 C   s0   t |}|t |7 }|d7 }| t|| jS )a  
        Request that the agent sign the given C{data} with the private key
        which corresponds to the public key given by C{blob}.  The private
        key should have been added to the agent already.

        @type blob: L{bytes}
        @type data: L{bytes}
        @return: A L{Deferred} which fires with a signature for given data
            created with the given key.
               )r   r,   AGENTC_SIGN_REQUESTr.   _cbSignData)r   r7   r    r:   r   r   r   signData[   s   zSSHAgentClient.signDatac                 C   sD   t |dd tkrtdt |dd  t|dd  d }|S )Nr   r   zunexpected data: %i)r   AGENT_SIGN_RESPONSEr   r   )r   r    	signaturer   r   r   r>   k   s   zSSHAgentClient._cbSignDatac                 C   s   t |}| t|S )zp
        Remove the private key corresponding to the public key in blob from the
        running agent.
        )r   r,   AGENTC_REMOVE_IDENTITY)r   r7   r:   r   r   r   removeIdentityq   s   zSSHAgentClient.removeIdentityc                 C   s   |  tdS )z9
        Remove all keys from the running agent.
        r   )r,   AGENTC_REMOVE_ALL_IDENTITIESr   r   r   r   removeAllIdentitiesy   s   z"SSHAgentClient.removeAllIdentitiesN)r   )__name__
__module____qualname____doc__r   r%   r,   r0   r/   r;   r?   r>   rC   rE   r   r   r   r   r
      s    

r
   c                   @   sh   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )SSHAgentServerz
    The server side of the SSH agent protocol.  This is equivalent to
    ssh-agent(1) and can be used with either ssh-add(1) or the SSHAgentClient
    protocol, also in this package.
    c                 C   s
   d| _ d S r   )r   r   r   r   r   r      s   
zSSHAgentServer.__init__c                 C   s   |  j |7  _ 	 t| j dkrd S td| j d d d }t| j d| k r)d S | j dd|  | j d| d  }| _ t|dd }t|d }|sT| td n!t	| d| }t	| j
dd d u rm| td t ||dd   q)Nr   r   r   r   r   z	agentc_%sr   )r   r   r   r   r   messagesgetsendResponser   getattrfactoryr   )r   r    r!   r"   r#   reqNamefr   r   r   r%      s$   (zSSHAgentServer.dataReceivedc                 C   s*   t dt|d || }| j| d S r&   )r   r'   r   r(   r)   )r   r#   r    r'   r   r   r   rM      s   zSSHAgentServer.sendResponsec                 C   sz   |dksJ t | jj}g }|td| | jj D ]\}}|t|  |t| q| 	t
d| dS )zQ
        Return all of the identities that have been added to the server
        r   r   N)r   rO   r   r+   r   r'   valuesr   r7   rM   r2   join)r   r    r4   respkeyr8   r   r   r   agentc_REQUEST_IDENTITIES   s   z(SSHAgentServer.agentc_REQUEST_IDENTITIESc                 C   sb   t |\}}|| jjvr| tdS t |\}}|dksJ | tt| jj| d | dS )z
        Data is a structure with a reference to an already added key object and
        some data that the clients wants signed with that key.  If the key
        object wasn't loaded, return AGENT_FAILURE, else return the signature.
        r   r<   r   N)r   rO   r   rM   r   r@   r   sign)r   r    r7   r?   r   r   r   agentc_SIGN_REQUEST   s   z"SSHAgentServer.agentc_SIGN_REQUESTc                 C   s   t |\}}|dkrd}n|dkrd}ntd| t||d }t |\}}tjj|dd}||f| jj| < | t	d	 d
S )z
        Adds a private key to the agent's collection of identities.  On
        subsequent interactions, the private key can be accessed using only the
        corresponding public key.
        s   ssh-rsa   s   ssh-dssr1   zunknown blob type: %sprivate_blobtyper   N)
r   r   BadKeyErrorr   Key
fromStringrO   r7   rM   r   )r   r    keyTyperestnmpr8   kr   r   r   agentc_ADD_IDENTITY   s   z"SSHAgentServer.agentc_ADD_IDENTITYc                 C   s:   t |\}}tjj|dd}| jj| = | td dS )zR
        Remove a specific key from the agent's collection of identities.
        r7   r\   r   N)r   r   r_   r`   rO   r7   rM   r   )r   r    r7   _rd   r   r   r   agentc_REMOVE_IDENTITY   s   z%SSHAgentServer.agentc_REMOVE_IDENTITYc                 C   s$   |dksJ i | j _| td dS )zL
        Remove all keys from the agent's collection of identities.
        r   N)rO   r   rM   r   r   r    r   r   r   agentc_REMOVE_ALL_IDENTITIES   s   z+SSHAgentServer.agentc_REMOVE_ALL_IDENTITIESc                 C   s   |  ttdd dS )z
        v1 message for listing RSA1 keys; superseded by
        agentc_REQUEST_IDENTITIES, which handles different key types.
        r   r   N)rM   AGENT_RSA_IDENTITIES_ANSWERr   r'   rh   r   r   r   agentc_REQUEST_RSA_IDENTITIES   s   z,SSHAgentServer.agentc_REQUEST_RSA_IDENTITIESc                 C      |  td dS )z
        v1 message for removing RSA1 keys; superseded by
        agentc_REMOVE_IDENTITY, which handles different key types.
        r   NrM   r   rh   r   r   r   agentc_REMOVE_RSA_IDENTITY      z)SSHAgentServer.agentc_REMOVE_RSA_IDENTITYc                 C   rl   )z
        v1 message for removing all RSA1 keys; superseded by
        agentc_REMOVE_ALL_IDENTITIES, which handles different key types.
        r   Nrm   rh   r   r   r    agentc_REMOVE_ALL_RSA_IDENTITIES   ro   z/SSHAgentServer.agentc_REMOVE_ALL_RSA_IDENTITIESN)rF   rG   rH   rI   r   r%   rM   rV   rX   re   rg   ri   rk   rn   rp   r   r   r   r   rJ      s    	rJ   r      r1   rY      	                           AGENTC_)$rI   r   twisted.conch.errorr   r   twisted.conch.sshr   twisted.conch.ssh.commonr   r   r   twisted.internetr   r	   Protocolr
   rJ   AGENTC_REQUEST_RSA_IDENTITIESrj   r   r   AGENTC_REMOVE_RSA_IDENTITY AGENTC_REMOVE_ALL_RSA_IDENTITIESr-   r2   r=   r@   r9   rB   rD   rK   localscopyitemsnamevaluer   r   r   r   <module>   s8   	k 