<s> """<STR_LIT>""" <EOL> import struct <EOL> from twisted . conch . ssh . common import NS , getNS , getMP <EOL> from twisted . conch . error import ConchError , MissingKeyStoreError <EOL> from twisted . conch . ssh import keys <EOL> from twisted . internet import defer , protocol <EOL> class SSHAgentClient ( protocol . Protocol ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . buf = '<STR_LIT>' <EOL> self . deferreds = [ ] <EOL> def dataReceived ( self , data ) : <EOL> self . buf += data <EOL> while <NUM_LIT:1> : <EOL> if len ( self . buf ) <= <NUM_LIT:4> : <EOL> return <EOL> packLen = struct . unpack ( '<STR_LIT>' , self . buf [ : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> if len ( self . buf ) < <NUM_LIT:4> + packLen : <EOL> return <EOL> packet , self . buf = self . buf [ <NUM_LIT:4> : <NUM_LIT:4> + packLen ] , self . buf [ <NUM_LIT:4> + packLen : ] <EOL> reqType = ord ( packet [ <NUM_LIT:0> ] ) <EOL> d = self . deferreds . pop ( <NUM_LIT:0> ) <EOL> if reqType == AGENT_FAILURE : <EOL> d . errback ( ConchError ( '<STR_LIT>' ) ) <EOL> elif reqType == AGENT_SUCCESS : <EOL> d . callback ( '<STR_LIT>' ) <EOL> else : <EOL> d . callback ( packet ) <EOL> def sendRequest ( self , reqType , data ) : <EOL> pack = struct . pack ( '<STR_LIT>' , len ( data ) + <NUM_LIT:1> , reqType ) + data <EOL> self . transport . write ( pack ) <EOL> d = defer . Deferred ( ) <EOL> self . deferreds . append ( d ) <EOL> return d <EOL> def requestIdentities ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . sendRequest ( AGENTC_REQUEST_IDENTITIES , '<STR_LIT>' ) <EOL> d . addCallback ( self . _cbRequestIdentities ) <EOL> return d <EOL> def _cbRequestIdentities ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if ord ( data [ <NUM_LIT:0> ] ) != AGENT_IDENTITIES_ANSWER : <EOL> raise ConchError ( '<STR_LIT>' % ord ( data [ <NUM_LIT:0> ] ) ) <EOL> numKeys = struct . unpack ( '<STR_LIT>' , data [ <NUM_LIT:1> : <NUM_LIT:5> ] ) [ <NUM_LIT:0> ] <EOL> keys = [ ] <EOL> data = data [ <NUM_LIT:5> : ] <EOL> for i in range ( numKeys ) : <EOL> blob , data = getNS ( data ) <EOL> comment , data = getNS ( data ) <EOL> keys . append ( ( blob , comment ) ) <EOL> return keys <EOL> def addIdentity ( self , blob , comment = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> req = blob <EOL> req += NS ( comment ) <EOL> return self . sendRequest ( AGENTC_ADD_IDENTITY , req ) <EOL> def signData ( self , blob , data ) : <EOL> """<STR_LIT>""" <EOL> req = NS ( blob ) <EOL> req += NS ( data ) <EOL> req += '<STR_LIT>' <EOL> return self . sendRequest ( AGENTC_SIGN_REQUEST , req ) . addCallback ( self . _cbSignData ) <EOL> def _cbSignData ( self , data ) : <EOL> if ord ( data [ <NUM_LIT:0> ] ) != AGENT_SIGN_RESPONSE : <EOL> raise ConchError ( '<STR_LIT>' % ord ( data [ <NUM_LIT:0> ] ) ) <EOL> signature = getNS ( data [ <NUM_LIT:1> : ] ) [ <NUM_LIT:0> ] <EOL> return signature <EOL> def removeIdentity ( self , blob ) : <EOL> """<STR_LIT>""" <EOL> req = NS ( blob ) <EOL> return self . sendRequest ( AGENTC_REMOVE_IDENTITY , req ) <EOL> def removeAllIdentities ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sendRequest ( AGENTC_REMOVE_ALL_IDENTITIES , '<STR_LIT>' ) <EOL> class SSHAgentServer ( protocol . Protocol ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . buf = '<STR_LIT>' <EOL> def dataReceived ( self , data ) : <EOL> self . buf += data <EOL> while <NUM_LIT:1> : <EOL> if len ( self . buf ) <= <NUM_LIT:4> : <EOL> return <EOL> packLen = struct . unpack ( '<STR_LIT>' , self . buf [ : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> if len ( self . buf ) < <NUM_LIT:4> + packLen : <EOL> return <EOL> packet , self . buf = self . buf [ <NUM_LIT:4> : <NUM_LIT:4> + packLen ] , self . buf [ <NUM_LIT:4> + packLen : ] <EOL> reqType = ord ( packet [ <NUM_LIT:0> ] ) <EOL> reqName = messages . get ( reqType , None ) <EOL> if not reqName : <EOL> self . sendResponse ( AGENT_FAILURE , '<STR_LIT>' ) <EOL> else : <EOL> f = getattr ( self , '<STR_LIT>' % reqName ) <EOL> if getattr ( self . factory , '<STR_LIT>' , None ) is None : <EOL> self . sendResponse ( AGENT_FAILURE , '<STR_LIT>' ) <EOL> raise MissingKeyStoreError ( ) <EOL> f ( packet [ <NUM_LIT:1> : ] ) <EOL> def sendResponse ( self , reqType , data ) : <EOL> pack = struct . pack ( '<STR_LIT>' , len ( data ) + <NUM_LIT:1> , reqType ) + data <EOL> self . transport . write ( pack ) <EOL> def agentc_REQUEST_IDENTITIES ( self , data ) : <EOL> """<STR_LIT>""" <EOL> assert data == '<STR_LIT>' <EOL> numKeys = len ( self . factory . keys ) <EOL> resp = [ ] <EOL> resp . append ( struct . pack ( '<STR_LIT>' , numKeys ) ) <EOL> for key , comment in self . factory . keys . itervalues ( ) : <EOL> resp . append ( NS ( key . blob ( ) ) ) <EOL> resp . append ( NS ( comment ) ) <EOL> self . sendResponse ( AGENT_IDENTITIES_ANSWER , '<STR_LIT>' . join ( resp ) ) <EOL> def agentc_SIGN_REQUEST ( self , data ) : <EOL> """<STR_LIT>""" <EOL> blob , data = getNS ( data ) <EOL> if blob not in self . factory . keys : <EOL> return self . sendResponse ( AGENT_FAILURE , '<STR_LIT>' ) <EOL> signData , data = getNS ( data ) <EOL> assert data == '<STR_LIT>' <EOL> self . sendResponse ( AGENT_SIGN_RESPONSE , NS ( self . factory . keys [ blob ] [ <NUM_LIT:0> ] . sign ( signData ) ) ) <EOL> def agentc_ADD_IDENTITY ( self , data ) : <EOL> """<STR_LIT>""" <EOL> keyType , rest = getNS ( data ) <EOL> if keyType == '<STR_LIT>' : <EOL> nmp = <NUM_LIT:6> <EOL> elif keyType == '<STR_LIT>' : <EOL> nmp = <NUM_LIT:5> <EOL> else : <EOL> raise keys . BadKeyError ( '<STR_LIT>' % keyType ) <EOL> rest = getMP ( rest , nmp ) [ - <NUM_LIT:1> ] <EOL> comment , rest = getNS ( rest ) <EOL> k = keys . Key . fromString ( data , type = '<STR_LIT>' ) <EOL> self . factory . keys [ k . blob ( ) ] = ( k , comment ) <EOL> self . sendResponse ( AGENT_SUCCESS , '<STR_LIT>' ) <EOL> def agentc_REMOVE_IDENTITY ( self , data ) : <EOL> """<STR_LIT>""" <EOL> blob , _ = getNS ( data ) <EOL> k = keys . Key . fromString ( blob , type = '<STR_LIT>' ) <EOL> del self . factory . keys [ k . blob ( ) ] <EOL> self . sendResponse ( AGENT_SUCCESS , '<STR_LIT>' ) <EOL> def agentc_REMOVE_ALL_IDENTITIES ( self , data ) : <EOL> """<STR_LIT>""" <EOL> assert data == '<STR_LIT>' <EOL> self . factory . keys = { } <EOL> self . sendResponse ( AGENT_SUCCESS , '<STR_LIT>' ) <EOL> def agentc_REQUEST_RSA_IDENTITIES ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . sendResponse ( AGENT_RSA_IDENTITIES_ANSWER , struct . pack ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> def agentc_REMOVE_RSA_IDENTITY ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . sendResponse ( AGENT_SUCCESS , '<STR_LIT>' ) <EOL> def agentc_REMOVE_ALL_RSA_IDENTITIES ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . sendResponse ( AGENT_SUCCESS , '<STR_LIT>' ) <EOL> AGENTC_REQUEST_RSA_IDENTITIES = <NUM_LIT:1> <EOL> AGENT_RSA_IDENTITIES_ANSWER = <NUM_LIT:2> <EOL> AGENT_FAILURE = <NUM_LIT:5> <EOL> AGENT_SUCCESS = <NUM_LIT:6> <EOL> AGENTC_REMOVE_RSA_IDENTITY = <NUM_LIT:8> <EOL> AGENTC_REMOVE_ALL_RSA_IDENTITIES = <NUM_LIT:9> <EOL> AGENTC_REQUEST_IDENTITIES = <NUM_LIT:11> <EOL> AGENT_IDENTITIES_ANSWER = <NUM_LIT:12> <EOL> AGENTC_SIGN_REQUEST = <NUM_LIT> <EOL> AGENT_SIGN_RESPONSE = <NUM_LIT> <EOL> AGENTC_ADD_IDENTITY = <NUM_LIT> <EOL> AGENTC_REMOVE_IDENTITY = <NUM_LIT> <EOL> AGENTC_REMOVE_ALL_IDENTITIES = <NUM_LIT> <EOL> messages = { } <EOL> for name , value in locals ( ) . copy ( ) . items ( ) : <EOL> if name [ : <NUM_LIT:7> ] == '<STR_LIT>' : <EOL> messages [ value ] = name [ <NUM_LIT:7> : ] </s>
