_e[c@s dZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z m Z mZmZmZmZmZddlmZmZddlmZmZddlmZd Zd efd YZd Zd efdYZ dS(s Packet handling iN(tHMAC(tutil(t linefeed_bytet cr_byte_valuetasbytest MSG_NAMEStDEBUGt xfffffffft zero_byte(tutbyte_ord(t SSHExceptiontProxyCommandFailure(tMessagecCst|||jS(N(Rtdigest(tkeytmessaget digest_class((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt compute_hmac)stNeedRekeyExceptioncBseZdZRS(s1 Exception indicating a rekey is needed. (t__name__t __module__t__doc__(((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyR-scCsDd}t|jtkr@t|jdkr@|jd}n|S(Ni(tNonettypetargsttupletlen(tetarg((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt first_arg4s*t PacketizercBsReZdZeddZeddZeddZeddZdZe dZ dZ e dZ dZdZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZe dZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%RS(s9 Implementation of the base SSH packet protocol. iicCsT||_d|_t|_t|_t|_d|_t|_ d|_ d|_ d|_ d|_ d|_d|_d|_d|_d|_d|_d|_d|_t|_d|_d|_t|_t|_d|_d|_d|_d|_tj |_!d|_"t#j#|_$d|_%d|_&t|_'t|_(dS(Nii()t_Packetizer__socketRt_Packetizer__loggertFalset_Packetizer__closedt_Packetizer__dump_packetst_Packetizer__need_rekeyt_Packetizer__init_counttbytest_Packetizer__remaindert_Packetizer__sent_bytest_Packetizer__sent_packetst_Packetizer__received_bytest_Packetizer__received_packetst$_Packetizer__received_bytes_overflowt&_Packetizer__received_packets_overflowt_Packetizer__block_size_outt_Packetizer__block_size_int_Packetizer__mac_size_outt_Packetizer__mac_size_int_Packetizer__block_engine_outt_Packetizer__block_engine_int_Packetizer__sdctr_outt_Packetizer__mac_engine_outt_Packetizer__mac_engine_int_Packetizer__mac_key_outt_Packetizer__mac_key_int _Packetizer__compress_engine_outt_Packetizer__compress_engine_int _Packetizer__sequence_number_outt_Packetizer__sequence_number_int threadingtRLockt_Packetizer__write_lockt_Packetizer__keepalive_intervalttimet_Packetizer__keepalive_lastt_Packetizer__keepalive_callbackt_Packetizer__timert_Packetizer__handshake_completet_Packetizer__timer_expired(tselftsocket((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt__init__KsF                                cCs|jS(N(R#(RH((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytclosedyscCs ||_dS(s? Set the Python log object to use for logging. N(R!(RHtlog((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytset_log}scCs||_||_||_||_||_||_d|_d|_|jdO_|jdkr{d|_t |_ ndS(s. Switch outbound data cipher. iiiN( R3R5R/R6R1R8R)R*R&R"R%(RHt block_enginet block_sizet mac_enginetmac_sizetmac_keytsdctr((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytset_outbound_ciphers         cCs||_||_||_||_||_d|_d|_d|_d|_|j dO_ |j dkrd|_ t |_ ndS(s- Switch inbound data cipher. iiiN( R4R0R7R2R9R+R,R-R.R&R"R%(RHRNRORPRQRR((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytset_inbound_ciphers          cCs ||_dS(N(R:(RHt compressor((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytset_outbound_compressorscCs ||_dS(N(R;(RHRV((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytset_inbound_compressorscCst|_|jjdS(N(tTrueR#R tclose(RH((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyRZs cCs ||_dS(N(R$(RHthexdump((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt set_hexdumpscCs|jS(N(R$(RH((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt get_hexdumpscCs|jS(N(R2(RH((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytget_mac_size_inscCs|jS(N(R1(RH((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytget_mac_size_outscCs|jS(s Returns ``True`` if a new set of keys needs to be negotiated. This will be triggered during a packet read or write, so it should be checked after every read or write, or at least after every few. (R%(RH((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt need_rekeyscCs%||_||_tj|_dS(s Turn on/off the callback keepalive. If ``interval`` seconds pass with no data read from or written to the socket, the callback will be executed and the timer will be reset. N(RARDRBRC(RHtintervaltcallback((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt set_keepalives  cCs t|_dS(N(RYRG(RH((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt read_timerscCs;|js7tjt||j|_|jjndS(s Tells `Packetizer` that the handshake process started. Starts a book keeping timer that can signal a timeout in the handshake process. :param float timeout: amount of seconds to wait before timing out N(RER>tTimertfloatRdtstart(RHttimeout((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytstart_handshakes cCs!|js tS|jrtS|jS(sR Checks if the handshake has timed out. If `start_handshake` wasn't called before the call to this function, the return value will always be `False`. If the handshake completed before a timeout was reached, the return value will be `False` :return: handshake time out status, as a `bool` (RER"RFRG(RH((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pythandshake_timed_outs  cCs/|jr+|jjt|_t|_ndS(sF Tells `Packetizer` that the handshake has completed. N(REtcancelR"RGRYRF(RH((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytcomplete_handshakes   cCst}t|jdkrN|j| }|j||_|t|8}nxQ|dkrt}|jr{tnyN|jj|}t|dkrtn||7}|t|8}Wn}tj k rt }ndtj k rH}t |}|t jkrt }qI|t jkr-qI|jrBtqInX|rQ|jrdtn|rt|dkr|jrtn|jqQqQW|S(s& Read as close to N bytes as possible, blocking as long as necessary. :param int n: number of bytes to read :return: the data read, as a `str` :raises: ``EOFError`` -- if the socket was closed before all the bytes could be read i(R'RR(R"RjtEOFErrorR trecvRIRhRYterrorRterrnotEAGAINtEINTRR#R%Rt_check_keepalive(RHtnt check_rekeytoutt got_timeouttxRR((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytread_allsB             ! cCsotj|_d}xSt|dkrjt}y|jj|}Wntjk rbt}ntj k r}t |}|t j krt}q|t j krt}qd}n*tk rntk rd}nX|rd}|jr/d}q/n+|dkr%|dkr%d}n|d7}|dkrGtn|t|kr]Pn||}qWdS(Niii i(RBRCRR"R tsendRIRhRYRoRRpRqRrR t ExceptionR#Rm(RHRvt#iteration_with_zero_as_return_valuet retry_writeRtRR((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt write_all/s@              cCs|j}x#t|kr.||j|7}q W|jt}||d|_|| }t|dkr|dtkr|d }nt|S(s Read a line from the socket. We assume no data is pending after the line, so it's okay to attempt large reads. iii(R(Rt _read_timeouttindexRRR (RHRhtbufRt((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pytreadlineZs  " c Cs"t|}t|d}|tkr5t|}ndj|}t|}|jjz|jdk r|j|}n|j |}|j r|j t dj|||j t t j|dn|jdk r|jj|}n|}|jdk rLtjd|j|}|t|j||j|j 7}n|jdt@|_|j||jt|7_|jd7_|j|jkp|j|jk}|r |j r d} |j t | j|j|jd|_d|_ |j!nWd|jj"XdS( sR Write a block of data using the current cipher, as an SSH block. is${:x}sWrite packet <{}>, length {}sOUT: s>Iis(Rekeying (hit {} packets, {} bytes sent)N(#RR RtformatRR@tacquireR:Rt _build_packetR$t_logRRt format_binaryR3tupdatetstructtpackR<RR8R6R1RR~R)R*t REKEY_PACKETSt REKEY_BYTESR%R-R.t_trigger_rekeytrelease( RHtdatatcmdtcmd_nametorig_lentpacketRvtpayloadt sent_too_muchtmsg((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt send_messageisP         cCs|j|jdt}|jdk r<|jj|}n|jrd|jtt j |dnt j d|d d}|d}|t ||jdkrtdn|j||jt |}||t | }||t |}|jdk r |jj|}n|jrH|jtt j |dn||}|jdkr||j }t jd|j||}t|j||j|j } t j| |stdqnt|d} |d || !} |jr|jtd j|| n|jdk r7|j| } nt| d } |j| _|jd t@|_||jd} |j| 7_|jd 7_|jr|j| 7_|j d 7_ |j |j!ks|j|j"kr^td q^nk|j|j#ks|j|j$kr^d }|jt|j|j|jd|_d|_ |j%nt| d}|t&krt&|}nd j|}|jr|jtdj|t | n|| fS(s Only one thread should ever be in this function (no other locking is done). :raises: `.SSHException` -- if the packet is mangled :raises: `.NeedRekeyException` -- if the transport should rekey RusIN: s>IiisInvalid packet blockings>IIsMismatched MACis"Got payload ({} bytes, {} padding)s+Remote transport is ignoring rekey requestss,Rekeying (hit {} packets, {} bytes received)s${:x}sRead packet <{}>, length {}N('RyR0RYR4RRR$RRRRRtunpackRR R2RR=RR9R7tconstant_time_bytes_eqR RR;R tseqnoRR+R,R%R-R.tREKEY_PACKETS_OVERFLOW_MAXtREKEY_BYTES_OVERFLOW_MAXRRRR(RHtheadert packet_sizetleftoverRRt post_packettmact mac_payloadtmy_mactpaddingRRtraw_packet_sizeterrRR((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyt read_messages                 cCsf|jdkrdStt|trOx7|D]}|jj||q/Wn|jj||dS(N(R!Rt issubclassRtlistRL(RHtlevelRtm((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyRs  cCs]|j s|j s|jr!dStj}||j|jkrY|j||_ndS(N(RAR3R%RBRCRD(RHtnow((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyRs s     cCstj}xtry5|jjd}t|dkrHtnPWnAtjk r`n.tk r}t |t j krqnX|j rtntj}|||krtjqqW|S(Nii( RBRYR RnRRmRIRhtEnvironmentErrorRRpRrR#(RHRhRgRxRR((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyRs&      cCs|j}d|t|d|}tjdt||d|}||7}|jsj|jdkr{|t|7}n|tj |7}|S(Niis>IBi( R/RRRR5R3RRtosturandom(RHRtbsizeRR((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyR.s # cCs t|_dS(N(RYR%(RH((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyR=s(&RRRtpowRRRRRJtpropertyRKRMR"RTRURWRXRZR\R]R^R_R`RcRdRiRjRlRyR~RRRRRsRRR(((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyR;sB .              3 +  9 `  (!RRpRRIRR>RBthmacRtparamikoRtparamiko.commonRRRRRRRtparamiko.py3compatR R tparamiko.ssh_exceptionR R tparamiko.messageR RR{RRtobjectR(((s3/tmp/pip-install-KP2Jbq/paramiko/paramiko/packet.pyts       4