o ¯b,%ã@sädZddlZddlmZmZddlmZddlmZm Z m Z ddl m Z m Z Gdd„de jƒZGd d „d e jƒZd Zd Zd ZdZdZdZdZdZdZdZdZdZdZiZeƒ  ¡ !¡D]\Z"Z#e"dd…dkroe"dd…ee#<q[dS)zÞ Implements the SSH v2 key agent protocol. This protocol is documented in the SSH source code, in the file U{PROTOCOL.agent}. Maintainer: Paul Swartz éN)Ú ConchErrorÚMissingKeyStoreError)Úkeys)ÚNSÚgetMPÚgetNS)ÚdeferÚprotocolc@sbeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zdd d„Z dd„Z dd„Z dd„Z dd„Z dS)Ú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. cCsd|_g|_dS©Nó)ÚbufÚ deferreds©Úself©rú9/usr/lib/python3/dist-packages/twisted/conch/ssh/agent.pyÚ__init__s zSSHAgentClient.__init__cCsÎ|j|7_ t|jƒdkrdSt d|jdd…¡d}t|jƒd|kr)dS|jdd|…|jd|d…}|_t|dd…ƒ}|j d¡}|tkrW| t dƒ¡n|t kra|  d¡n|  |¡q)Nééú!Lrz agent failurer ) r ÚlenÚstructÚunpackÚordrÚpopÚ AGENT_FAILUREÚerrbackrÚ AGENT_SUCCESSÚcallback)rÚdataÚpackLenÚpacketÚreqTypeÚdrrrÚ dataReceived s (   òzSSHAgentClient.dataReceivedcCs>t dt|ƒd|¡|}|j |¡t ¡}|j |¡|S©Nz!LBr) rÚpackrÚ transportÚwriterÚDeferredrÚappend)rr#r r'r$rrrÚ sendRequest2s   zSSHAgentClient.sendRequestcCs| td¡}| |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)rr$rrrÚrequestIdentities9s  z SSHAgentClient.requestIdentitiescCsŒt|dd…ƒtkrtdt|dd…ƒƒ‚t d|dd…¡d}g}|dd…}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. rrzunexpected response: %iréN)rÚAGENT_IDENTITIES_ANSWERrrrÚrangerr+)rr ÚnumKeysÚresultÚiÚblobÚcommentrrrr/Cs    z#SSHAgentClient._cbRequestIdentitiesr cCs|}|t|ƒ7}| t|¡S)zK Add a private key blob to the agent's collection of keys. )rr,ÚAGENTC_ADD_IDENTITY)rr7r8ÚreqrrrÚ addIdentitySs  zSSHAgentClient.addIdentitycCs0t|ƒ}|t|ƒ7}|d7}| 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. ó)rr,ÚAGENTC_SIGN_REQUESTr.Ú _cbSignData)rr7r r:rrrÚsignData[s zSSHAgentClient.signDatacCsDt|dd…ƒtkrtdt|dd…ƒƒ‚t|dd…ƒd}|S)Nrrzunexpected data: %i)rÚAGENT_SIGN_RESPONSErr)rr Ú signaturerrrr>kszSSHAgentClient._cbSignDatacCst|ƒ}| t|¡S)zp Remove the private key corresponding to the public key in blob from the running agent. )rr,ÚAGENTC_REMOVE_IDENTITY)rr7r:rrrÚremoveIdentityqs zSSHAgentClient.removeIdentitycCs | td¡S)z9 Remove all keys from the running agent. r )r,ÚAGENTC_REMOVE_ALL_IDENTITIESrrrrÚremoveAllIdentitiesys z"SSHAgentClient.removeAllIdentitiesN)r )Ú__name__Ú __module__Ú __qualname__Ú__doc__rr%r,r0r/r;r?r>rCrErrrrr s  r c@sheZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„ZdS)Ú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. cCs d|_dSr )r rrrrr‡s zSSHAgentServer.__init__cCsì|j|7_ t|jƒdkrdSt d|jdd…¡d}t|jƒd|kr)dS|jdd|…|jd|d…}|_t|dd…ƒ}t |d¡}|sT| td¡n!t |d|ƒ}t |j ddƒdurm| td¡t ƒ‚||dd…ƒq)Nrrrrr z agentc_%sr) r rrrrÚmessagesÚgetÚ sendResponserÚgetattrÚfactoryr)rr r!r"r#ÚreqNameÚfrrrr%Šs$(  ðzSSHAgentServer.dataReceivedcCs*t dt|ƒd|¡|}|j |¡dSr&)rr'rr(r))rr#r r'rrrrMžszSSHAgentServer.sendResponsecCsz|dksJ‚t|jjƒ}g}| t d|¡¡|jj ¡D]\}}| t| ¡ƒ¡| t|ƒ¡q|  t d  |¡¡dS)zQ Return all of the identities that have been added to the server r rN) rrOrr+rr'Úvaluesrr7rMr2Újoin)rr r4ÚrespÚkeyr8rrrÚagentc_REQUEST_IDENTITIES¢s  z(SSHAgentServer.agentc_REQUEST_IDENTITIEScCsbt|ƒ\}}||jjvr| td¡St|ƒ\}}|dksJ‚| tt|jj|d |¡ƒ¡dS)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<rN)rrOrrMrr@rÚsign)rr r7r?rrrÚagentc_SIGN_REQUEST°s     ÿz"SSHAgentServer.agentc_SIGN_REQUESTcCs„t|ƒ\}}|dkr d}n|dkrd}nt d|¡‚t||ƒd}t|ƒ\}}tjj|dd}||f|jj| ¡<| 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. sssh-rsaésssh-dssr1zunknown blob type: %séÿÿÿÿÚ private_blob©Útyper N) rrÚ BadKeyErrorrÚKeyÚ fromStringrOr7rMr)rr ÚkeyTypeÚrestÚnmpr8ÚkrrrÚagentc_ADD_IDENTITY¿s ÿ z"SSHAgentServer.agentc_ADD_IDENTITYcCs:t|ƒ\}}tjj|dd}|jj| ¡=| td¡dS)zR Remove a specific key from the agent's collection of identities. r7r\r N)rrr_r`rOr7rMr)rr r7Ú_rdrrrÚagentc_REMOVE_IDENTITYØs z%SSHAgentServer.agentc_REMOVE_IDENTITYcCs$|dksJ‚i|j_| td¡dS)zL Remove all keys from the agent's collection of identities. r N)rOrrMr©rr rrrÚagentc_REMOVE_ALL_IDENTITIESás z+SSHAgentServer.agentc_REMOVE_ALL_IDENTITIEScCs| tt dd¡¡dS)z‡ v1 message for listing RSA1 keys; superseded by agentc_REQUEST_IDENTITIES, which handles different key types. rrN)rMÚAGENT_RSA_IDENTITIES_ANSWERrr'rhrrrÚagentc_REQUEST_RSA_IDENTITIESísz,SSHAgentServer.agentc_REQUEST_RSA_IDENTITIEScCó| td¡dS)z… v1 message for removing RSA1 keys; superseded by agentc_REMOVE_IDENTITY, which handles different key types. r N©rMrrhrrrÚagentc_REMOVE_RSA_IDENTITYôóz)SSHAgentServer.agentc_REMOVE_RSA_IDENTITYcCrl)z v1 message for removing all RSA1 keys; superseded by agentc_REMOVE_ALL_IDENTITIES, which handles different key types. r NrmrhrrrÚ agentc_REMOVE_ALL_RSA_IDENTITIESûroz/SSHAgentServer.agentc_REMOVE_ALL_RSA_IDENTITIESN)rFrGrHrIrr%rMrVrXrergrirkrnrprrrrrJ€s   rJrér1rYéé é é é éééééÚAGENTC_)$rIrÚtwisted.conch.errorrrÚtwisted.conch.sshrÚtwisted.conch.ssh.commonrrrÚtwisted.internetrr ÚProtocolr rJÚAGENTC_REQUEST_RSA_IDENTITIESrjrrÚAGENTC_REMOVE_RSA_IDENTITYÚ AGENTC_REMOVE_ALL_RSA_IDENTITIESr-r2r=r@r9rBrDrKÚlocalsÚcopyÚitemsÚnameÚvaluerrrrÚs8  k€þ