Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

ssh.js 159KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301
  1. // TODO: * Automatic re-key every (configurable) n bytes or length of time
  2. // - RFC suggests every 1GB of transmitted data or 1 hour, whichever
  3. // comes sooner
  4. // * Filter control codes from strings
  5. // (as per http://tools.ietf.org/html/rfc4251#section-9.2)
  6. var crypto = require('crypto');
  7. var zlib = require('zlib');
  8. var TransformStream = require('stream').Transform;
  9. var inherits = require('util').inherits;
  10. var inspect = require('util').inspect;
  11. var StreamSearch = require('streamsearch');
  12. var readUInt32BE = require('./buffer-helpers').readUInt32BE;
  13. var writeUInt32BE = require('./buffer-helpers').writeUInt32BE;
  14. var consts = require('./constants');
  15. var utils = require('./utils');
  16. var iv_inc = utils.iv_inc;
  17. var readString = utils.readString;
  18. var readInt = utils.readInt;
  19. var DSASigBERToBare = utils.DSASigBERToBare;
  20. var ECDSASigASN1ToSSH = utils.ECDSASigASN1ToSSH;
  21. var sigSSHToASN1 = utils.sigSSHToASN1;
  22. var parseDERKey = require('./keyParser').parseDERKey;
  23. var CIPHER_INFO = consts.CIPHER_INFO;
  24. var HMAC_INFO = consts.HMAC_INFO;
  25. var MESSAGE = consts.MESSAGE;
  26. var DYNAMIC_KEXDH_MESSAGE = consts.DYNAMIC_KEXDH_MESSAGE;
  27. var KEXDH_MESSAGE = consts.KEXDH_MESSAGE;
  28. var ALGORITHMS = consts.ALGORITHMS;
  29. var DISCONNECT_REASON = consts.DISCONNECT_REASON;
  30. var CHANNEL_OPEN_FAILURE = consts.CHANNEL_OPEN_FAILURE;
  31. var SSH_TO_OPENSSL = consts.SSH_TO_OPENSSL;
  32. var TERMINAL_MODE = consts.TERMINAL_MODE;
  33. var SIGNALS = consts.SIGNALS;
  34. var EDDSA_SUPPORTED = consts.EDDSA_SUPPORTED;
  35. var BUGS = consts.BUGS;
  36. var BUGGY_IMPLS = consts.BUGGY_IMPLS;
  37. var BUGGY_IMPLS_LEN = BUGGY_IMPLS.length;
  38. var MODULE_VER = require('../package.json').version;
  39. var I = 0;
  40. var IN_INIT = I++;
  41. var IN_GREETING = I++;
  42. var IN_HEADER = I++;
  43. var IN_PACKETBEFORE = I++;
  44. var IN_PACKET = I++;
  45. var IN_PACKETDATA = I++;
  46. var IN_PACKETDATAVERIFY = I++;
  47. var IN_PACKETDATAAFTER = I++;
  48. var OUT_INIT = I++;
  49. var OUT_READY = I++;
  50. var OUT_REKEYING = I++;
  51. var MAX_SEQNO = 4294967295;
  52. var MAX_PACKET_SIZE = 35000;
  53. var MAX_PACKETS_REKEYING = 50;
  54. var EXP_TYPE_HEADER = 0;
  55. var EXP_TYPE_LF = 1;
  56. var EXP_TYPE_BYTES = 2; // Waits until n bytes have been seen
  57. var Z_PARTIAL_FLUSH = zlib.Z_PARTIAL_FLUSH;
  58. var ZLIB_OPTS = { flush: Z_PARTIAL_FLUSH };
  59. var RE_KEX_HASH = /-(.+)$/;
  60. var RE_GEX = /^gex-/;
  61. var RE_NULL = /\x00/g;
  62. var IDENT_PREFIX_BUFFER = Buffer.from('SSH-');
  63. var EMPTY_BUFFER = Buffer.allocUnsafe(0);
  64. var HMAC_COMPUTE = Buffer.allocUnsafe(9);
  65. var PING_PACKET = Buffer.from([
  66. MESSAGE.GLOBAL_REQUEST,
  67. // "keepalive@openssh.com"
  68. 0, 0, 0, 21,
  69. 107, 101, 101, 112, 97, 108, 105, 118, 101, 64, 111, 112, 101, 110, 115,
  70. 115, 104, 46, 99, 111, 109,
  71. // Request a reply
  72. 1
  73. ]);
  74. var NEWKEYS_PACKET = Buffer.from([MESSAGE.NEWKEYS]);
  75. var USERAUTH_SUCCESS_PACKET = Buffer.from([MESSAGE.USERAUTH_SUCCESS]);
  76. var REQUEST_SUCCESS_PACKET = Buffer.from([MESSAGE.REQUEST_SUCCESS]);
  77. var REQUEST_FAILURE_PACKET = Buffer.from([MESSAGE.REQUEST_FAILURE]);
  78. var NO_TERMINAL_MODES_BUFFER = Buffer.from([TERMINAL_MODE.TTY_OP_END]);
  79. var KEXDH_GEX_REQ_PACKET = Buffer.from([
  80. MESSAGE.KEXDH_GEX_REQUEST,
  81. // Minimal size in bits of an acceptable group
  82. 0, 0, 4, 0, // 1024, modp2
  83. // Preferred size in bits of the group the server will send
  84. 0, 0, 16, 0, // 4096, modp16
  85. // Maximal size in bits of an acceptable group
  86. 0, 0, 32, 0 // 8192, modp18
  87. ]);
  88. function DEBUG_NOOP(msg) {}
  89. function SSH2Stream(cfg) {
  90. if (typeof cfg !== 'object' || cfg === null)
  91. cfg = {};
  92. TransformStream.call(this, {
  93. highWaterMark: (typeof cfg.highWaterMark === 'number'
  94. ? cfg.highWaterMark
  95. : 32 * 1024)
  96. });
  97. this._needContinue = false;
  98. this.bytesSent = this.bytesReceived = 0;
  99. this.debug = (typeof cfg.debug === 'function' ? cfg.debug : DEBUG_NOOP);
  100. this.server = (cfg.server === true);
  101. this.maxPacketSize = (typeof cfg.maxPacketSize === 'number'
  102. ? cfg.maxPacketSize
  103. : MAX_PACKET_SIZE);
  104. // Bitmap that indicates any bugs the remote side has. This is determined
  105. // by the reported software version.
  106. this.remoteBugs = 0;
  107. if (this.server) {
  108. // TODO: Remove when we support group exchange for server implementation
  109. this.remoteBugs = BUGS.BAD_DHGEX;
  110. }
  111. this.readable = true;
  112. var self = this;
  113. var hostKeys = cfg.hostKeys;
  114. if (this.server && (typeof hostKeys !== 'object' || hostKeys === null))
  115. throw new Error('hostKeys must be an object keyed on host key type');
  116. this.config = {
  117. // Server
  118. hostKeys: hostKeys, // All keys supported by server
  119. // Client/Server
  120. ident: 'SSH-2.0-'
  121. + (cfg.ident
  122. || ('ssh2js' + MODULE_VER + (this.server ? 'srv' : ''))),
  123. algorithms: {
  124. kex: ALGORITHMS.KEX,
  125. kexBuf: ALGORITHMS.KEX_BUF,
  126. serverHostKey: ALGORITHMS.SERVER_HOST_KEY,
  127. serverHostKeyBuf: ALGORITHMS.SERVER_HOST_KEY_BUF,
  128. cipher: ALGORITHMS.CIPHER,
  129. cipherBuf: ALGORITHMS.CIPHER_BUF,
  130. hmac: ALGORITHMS.HMAC,
  131. hmacBuf: ALGORITHMS.HMAC_BUF,
  132. compress: ALGORITHMS.COMPRESS,
  133. compressBuf: ALGORITHMS.COMPRESS_BUF
  134. }
  135. };
  136. // RFC 4253 states the identification string must not contain NULL
  137. this.config.ident.replace(RE_NULL, '');
  138. if (this.config.ident.length + 2 /* Account for "\r\n" */ > 255)
  139. throw new Error('ident too long');
  140. if (typeof cfg.algorithms === 'object' && cfg.algorithms !== null) {
  141. var algos = cfg.algorithms;
  142. if (Array.isArray(algos.kex) && algos.kex.length > 0) {
  143. this.config.algorithms.kex = algos.kex;
  144. if (!Buffer.isBuffer(algos.kexBuf))
  145. algos.kexBuf = Buffer.from(algos.kex.join(','), 'ascii');
  146. this.config.algorithms.kexBuf = algos.kexBuf;
  147. }
  148. if (Array.isArray(algos.serverHostKey) && algos.serverHostKey.length > 0) {
  149. this.config.algorithms.serverHostKey = algos.serverHostKey;
  150. if (!Buffer.isBuffer(algos.serverHostKeyBuf)) {
  151. algos.serverHostKeyBuf = Buffer.from(algos.serverHostKey.join(','),
  152. 'ascii');
  153. }
  154. this.config.algorithms.serverHostKeyBuf = algos.serverHostKeyBuf;
  155. }
  156. if (Array.isArray(algos.cipher) && algos.cipher.length > 0) {
  157. this.config.algorithms.cipher = algos.cipher;
  158. if (!Buffer.isBuffer(algos.cipherBuf))
  159. algos.cipherBuf = Buffer.from(algos.cipher.join(','), 'ascii');
  160. this.config.algorithms.cipherBuf = algos.cipherBuf;
  161. }
  162. if (Array.isArray(algos.hmac) && algos.hmac.length > 0) {
  163. this.config.algorithms.hmac = algos.hmac;
  164. if (!Buffer.isBuffer(algos.hmacBuf))
  165. algos.hmacBuf = Buffer.from(algos.hmac.join(','), 'ascii');
  166. this.config.algorithms.hmacBuf = algos.hmacBuf;
  167. }
  168. if (Array.isArray(algos.compress) && algos.compress.length > 0) {
  169. this.config.algorithms.compress = algos.compress;
  170. if (!Buffer.isBuffer(algos.compressBuf))
  171. algos.compressBuf = Buffer.from(algos.compress.join(','), 'ascii');
  172. this.config.algorithms.compressBuf = algos.compressBuf;
  173. }
  174. }
  175. this.reset(true);
  176. // Common events
  177. this.on('end', function() {
  178. // Let GC collect any Buffers we were previously storing
  179. self.readable = false;
  180. self._state = undefined;
  181. self.reset();
  182. self._state.outgoing.bufSeqno = undefined;
  183. });
  184. this.on('DISCONNECT', function(reason, code, desc, lang) {
  185. onDISCONNECT(self, reason, code, desc, lang);
  186. });
  187. this.on('KEXINIT', function(init, firstFollows) {
  188. onKEXINIT(self, init, firstFollows);
  189. });
  190. this.on('NEWKEYS', function() { onNEWKEYS(self); });
  191. if (this.server) {
  192. // Server-specific events
  193. this.on('KEXDH_INIT', function(e) { onKEXDH_INIT(self, e); });
  194. } else {
  195. // Client-specific events
  196. this.on('KEXDH_REPLY', function(info) { onKEXDH_REPLY(self, info); })
  197. .on('KEXDH_GEX_GROUP',
  198. function(prime, gen) { onKEXDH_GEX_GROUP(self, prime, gen); });
  199. }
  200. if (this.server) {
  201. // Greeting displayed before the ssh identification string is sent, this is
  202. // usually ignored by most clients
  203. if (typeof cfg.greeting === 'string' && cfg.greeting.length) {
  204. if (cfg.greeting.slice(-2) === '\r\n')
  205. this.push(cfg.greeting);
  206. else
  207. this.push(cfg.greeting + '\r\n');
  208. }
  209. // Banner shown after the handshake completes, but before user
  210. // authentication begins
  211. if (typeof cfg.banner === 'string' && cfg.banner.length) {
  212. if (cfg.banner.slice(-2) === '\r\n')
  213. this.banner = cfg.banner;
  214. else
  215. this.banner = cfg.banner + '\r\n';
  216. }
  217. }
  218. this.debug('DEBUG: Local ident: ' + inspect(this.config.ident));
  219. this.push(this.config.ident + '\r\n');
  220. this._state.incoming.expectedPacket = 'KEXINIT';
  221. }
  222. inherits(SSH2Stream, TransformStream);
  223. SSH2Stream.prototype.__read = TransformStream.prototype._read;
  224. SSH2Stream.prototype._read = function(n) {
  225. if (this._needContinue) {
  226. this._needContinue = false;
  227. this.emit('continue');
  228. }
  229. return this.__read(n);
  230. };
  231. SSH2Stream.prototype.__push = TransformStream.prototype.push;
  232. SSH2Stream.prototype.push = function(chunk, encoding) {
  233. var ret = this.__push(chunk, encoding);
  234. this._needContinue = (ret === false);
  235. return ret;
  236. };
  237. SSH2Stream.prototype._cleanup = function(callback) {
  238. this.reset();
  239. this.debug('DEBUG: Parser: Malformed packet');
  240. callback && callback(new Error('Malformed packet'));
  241. };
  242. SSH2Stream.prototype._transform = function(chunk, encoding, callback, decomp) {
  243. var skipDecrypt = false;
  244. var decryptAuthMode = false;
  245. var state = this._state;
  246. var instate = state.incoming;
  247. var outstate = state.outgoing;
  248. var expect = instate.expect;
  249. var decrypt = instate.decrypt;
  250. var decompress = instate.decompress;
  251. var chlen = chunk.length;
  252. var chleft = 0;
  253. var debug = this.debug;
  254. var self = this;
  255. var i = 0;
  256. var p = i;
  257. var blockLen;
  258. var buffer;
  259. var buf;
  260. var r;
  261. this.bytesReceived += chlen;
  262. while (true) {
  263. if (expect.type !== undefined) {
  264. if (i >= chlen)
  265. break;
  266. if (expect.type === EXP_TYPE_BYTES) {
  267. chleft = (chlen - i);
  268. var pktLeft = (expect.buf.length - expect.ptr);
  269. if (pktLeft <= chleft) {
  270. chunk.copy(expect.buf, expect.ptr, i, i + pktLeft);
  271. i += pktLeft;
  272. buffer = expect.buf;
  273. expect.buf = undefined;
  274. expect.ptr = 0;
  275. expect.type = undefined;
  276. } else {
  277. chunk.copy(expect.buf, expect.ptr, i);
  278. expect.ptr += chleft;
  279. i += chleft;
  280. }
  281. continue;
  282. } else if (expect.type === EXP_TYPE_HEADER) {
  283. i += instate.search.push(chunk);
  284. if (expect.type !== undefined)
  285. continue;
  286. } else if (expect.type === EXP_TYPE_LF) {
  287. if (++expect.ptr + 4 /* Account for "SSH-" */ > 255) {
  288. this.reset();
  289. debug('DEBUG: Parser: Identification string exceeded 255 characters');
  290. return callback(new Error('Max identification string size exceeded'));
  291. }
  292. if (chunk[i] === 0x0A) {
  293. expect.type = undefined;
  294. if (p < i) {
  295. if (expect.buf === undefined)
  296. expect.buf = chunk.toString('ascii', p, i);
  297. else
  298. expect.buf += chunk.toString('ascii', p, i);
  299. }
  300. buffer = expect.buf;
  301. expect.buf = undefined;
  302. ++i;
  303. } else {
  304. if (++i === chlen && p < i) {
  305. if (expect.buf === undefined)
  306. expect.buf = chunk.toString('ascii', p, i);
  307. else
  308. expect.buf += chunk.toString('ascii', p, i);
  309. }
  310. continue;
  311. }
  312. }
  313. }
  314. if (instate.status === IN_INIT) {
  315. if (!this.readable)
  316. return callback();
  317. if (this.server) {
  318. // Retrieve what should be the start of the protocol version exchange
  319. if (!buffer) {
  320. debug('DEBUG: Parser: IN_INIT (waiting for identification begin)');
  321. expectData(this, EXP_TYPE_BYTES, 4);
  322. } else {
  323. if (buffer[0] === 0x53 // S
  324. && buffer[1] === 0x53 // S
  325. && buffer[2] === 0x48 // H
  326. && buffer[3] === 0x2D) { // -
  327. instate.status = IN_GREETING;
  328. debug('DEBUG: Parser: IN_INIT (waiting for rest of identification)');
  329. } else {
  330. this.reset();
  331. debug('DEBUG: Parser: Bad identification start');
  332. return callback(new Error('Bad identification start'));
  333. }
  334. }
  335. } else {
  336. debug('DEBUG: Parser: IN_INIT');
  337. // Retrieve any bytes that may come before the protocol version exchange
  338. var ss = instate.search = new StreamSearch(IDENT_PREFIX_BUFFER);
  339. ss.on('info', function onInfo(matched, data, start, end) {
  340. if (data) {
  341. if (instate.greeting === undefined)
  342. instate.greeting = data.toString('binary', start, end);
  343. else
  344. instate.greeting += data.toString('binary', start, end);
  345. }
  346. if (matched) {
  347. expect.type = undefined;
  348. instate.search.removeListener('info', onInfo);
  349. }
  350. });
  351. ss.maxMatches = 1;
  352. expectData(this, EXP_TYPE_HEADER);
  353. instate.status = IN_GREETING;
  354. }
  355. } else if (instate.status === IN_GREETING) {
  356. debug('DEBUG: Parser: IN_GREETING');
  357. instate.search = undefined;
  358. // Retrieve the identification bytes after the "SSH-" header
  359. p = i;
  360. expectData(this, EXP_TYPE_LF);
  361. instate.status = IN_HEADER;
  362. } else if (instate.status === IN_HEADER) {
  363. debug('DEBUG: Parser: IN_HEADER');
  364. if (buffer.charCodeAt(buffer.length - 1) === 13)
  365. buffer = buffer.slice(0, -1);
  366. var idxDash = buffer.indexOf('-');
  367. var idxSpace = buffer.indexOf(' ');
  368. var header = {
  369. // RFC says greeting SHOULD be utf8
  370. greeting: instate.greeting,
  371. identRaw: 'SSH-' + buffer,
  372. versions: {
  373. protocol: buffer.substr(0, idxDash),
  374. software: (idxSpace === -1
  375. ? buffer.substring(idxDash + 1)
  376. : buffer.substring(idxDash + 1, idxSpace))
  377. },
  378. comments: (idxSpace > -1 ? buffer.substring(idxSpace + 1) : undefined)
  379. };
  380. instate.greeting = undefined;
  381. if (header.versions.protocol !== '1.99'
  382. && header.versions.protocol !== '2.0') {
  383. this.reset();
  384. debug('DEBUG: Parser: protocol version not supported: '
  385. + header.versions.protocol);
  386. return callback(new Error('Protocol version not supported'));
  387. } else
  388. this.emit('header', header);
  389. if (instate.status === IN_INIT) {
  390. // We reset from an event handler, possibly due to an unsupported SSH
  391. // protocol version?
  392. return;
  393. }
  394. var identRaw = header.identRaw;
  395. var software = header.versions.software;
  396. this.debug('DEBUG: Remote ident: ' + inspect(identRaw));
  397. for (var j = 0, rule; j < BUGGY_IMPLS_LEN; ++j) {
  398. rule = BUGGY_IMPLS[j];
  399. if (typeof rule[0] === 'string') {
  400. if (software === rule[0])
  401. this.remoteBugs |= rule[1];
  402. } else if (rule[0].test(software))
  403. this.remoteBugs |= rule[1];
  404. }
  405. instate.identRaw = identRaw;
  406. // Adjust bytesReceived first otherwise it will have an incorrectly larger
  407. // total when we call back into this function after completing KEXINIT
  408. this.bytesReceived -= (chlen - i);
  409. KEXINIT(this, function() {
  410. if (i === chlen)
  411. callback();
  412. else
  413. self._transform(chunk.slice(i), encoding, callback);
  414. });
  415. instate.status = IN_PACKETBEFORE;
  416. return;
  417. } else if (instate.status === IN_PACKETBEFORE) {
  418. blockLen = (decrypt.instance ? decrypt.info.blockLen : 8);
  419. debug('DEBUG: Parser: IN_PACKETBEFORE (expecting ' + blockLen + ')');
  420. // Wait for the right number of bytes so we can determine the incoming
  421. // packet length
  422. expectData(this, EXP_TYPE_BYTES, blockLen, decrypt.buf);
  423. instate.status = IN_PACKET;
  424. } else if (instate.status === IN_PACKET) {
  425. debug('DEBUG: Parser: IN_PACKET');
  426. if (decrypt.instance) {
  427. decryptAuthMode = (decrypt.info.authLen > 0);
  428. if (!decryptAuthMode)
  429. buffer = decryptData(this, buffer);
  430. blockLen = decrypt.info.blockLen;
  431. } else {
  432. decryptAuthMode = false;
  433. blockLen = 8;
  434. }
  435. r = readInt(buffer, 0, this, callback);
  436. if (r === false)
  437. return;
  438. var hmacInfo = instate.hmac.info;
  439. var macSize;
  440. if (hmacInfo)
  441. macSize = hmacInfo.actualLen;
  442. else
  443. macSize = 0;
  444. var fullPacketLen = r + 4 + macSize;
  445. var maxPayloadLen = this.maxPacketSize;
  446. if (decompress.instance) {
  447. // Account for compressed payloads
  448. // This formula is taken from dropbear which derives it from zlib's
  449. // documentation. Explanation from dropbear:
  450. /* For exact details see http://www.zlib.net/zlib_tech.html
  451. * 5 bytes per 16kB block, plus 6 bytes for the stream.
  452. * We might allocate 5 unnecessary bytes here if it's an
  453. * exact multiple. */
  454. maxPayloadLen += (((this.maxPacketSize / 16384) + 1) * 5 + 6);
  455. }
  456. if (r > maxPayloadLen
  457. // TODO: Change 16 to "MAX(16, decrypt.info.blockLen)" when/if SSH2
  458. // adopts 512-bit ciphers
  459. || fullPacketLen < (16 + macSize)
  460. || ((r + (decryptAuthMode ? 0 : 4)) % blockLen) !== 0) {
  461. this.disconnect(DISCONNECT_REASON.PROTOCOL_ERROR);
  462. debug('DEBUG: Parser: Bad packet length (' + fullPacketLen + ')');
  463. return callback(new Error('Bad packet length'));
  464. }
  465. instate.pktLen = r;
  466. var remainLen = instate.pktLen + 4 - blockLen;
  467. if (decryptAuthMode) {
  468. decrypt.instance.setAAD(buffer.slice(0, 4));
  469. debug('DEBUG: Parser: pktLen:'
  470. + instate.pktLen
  471. + ',remainLen:'
  472. + remainLen);
  473. } else {
  474. instate.padLen = buffer[4];
  475. debug('DEBUG: Parser: pktLen:'
  476. + instate.pktLen
  477. + ',padLen:'
  478. + instate.padLen
  479. + ',remainLen:'
  480. + remainLen);
  481. }
  482. if (remainLen > 0) {
  483. if (decryptAuthMode)
  484. instate.pktExtra = buffer.slice(4);
  485. else
  486. instate.pktExtra = buffer.slice(5);
  487. // Grab the rest of the packet
  488. expectData(this, EXP_TYPE_BYTES, remainLen);
  489. instate.status = IN_PACKETDATA;
  490. } else if (remainLen < 0)
  491. instate.status = IN_PACKETBEFORE;
  492. else {
  493. // Entire message fit into one block
  494. skipDecrypt = true;
  495. instate.status = IN_PACKETDATA;
  496. continue;
  497. }
  498. } else if (instate.status === IN_PACKETDATA) {
  499. debug('DEBUG: Parser: IN_PACKETDATA');
  500. if (decrypt.instance) {
  501. decryptAuthMode = (decrypt.info.authLen > 0);
  502. if (!skipDecrypt) {
  503. if (!decryptAuthMode)
  504. buffer = decryptData(this, buffer);
  505. } else {
  506. skipDecrypt = false;
  507. }
  508. } else {
  509. decryptAuthMode = false;
  510. skipDecrypt = false;
  511. }
  512. var padStart = instate.pktLen - instate.padLen - 1;
  513. // TODO: Allocate a Buffer once that is slightly larger than maxPacketSize
  514. // (to accommodate for packet length field and MAC) and re-use that
  515. // instead
  516. if (instate.pktExtra) {
  517. buf = Buffer.allocUnsafe(instate.pktExtra.length + buffer.length);
  518. instate.pktExtra.copy(buf);
  519. buffer.copy(buf, instate.pktExtra.length);
  520. instate.payload = buf.slice(0, padStart);
  521. } else {
  522. // Entire message fit into one block
  523. if (decryptAuthMode)
  524. buf = buffer.slice(4);
  525. else
  526. buf = buffer.slice(5);
  527. instate.payload = buffer.slice(5, 5 + padStart);
  528. }
  529. if (instate.hmac.info !== undefined) {
  530. // Wait for hmac hash
  531. var inHMACSize = decrypt.info.authLen || instate.hmac.info.actualLen;
  532. debug('DEBUG: Parser: HMAC size:' + inHMACSize);
  533. expectData(this, EXP_TYPE_BYTES, inHMACSize, instate.hmac.buf);
  534. instate.status = IN_PACKETDATAVERIFY;
  535. instate.packet = buf;
  536. } else
  537. instate.status = IN_PACKETDATAAFTER;
  538. instate.pktExtra = undefined;
  539. buf = undefined;
  540. } else if (instate.status === IN_PACKETDATAVERIFY) {
  541. debug('DEBUG: Parser: IN_PACKETDATAVERIFY');
  542. // Verify packet data integrity
  543. if (hmacVerify(this, buffer)) {
  544. debug('DEBUG: Parser: IN_PACKETDATAVERIFY (Valid HMAC)');
  545. instate.status = IN_PACKETDATAAFTER;
  546. instate.packet = undefined;
  547. } else {
  548. this.reset();
  549. debug('DEBUG: Parser: IN_PACKETDATAVERIFY (Invalid HMAC)');
  550. return callback(new Error('Invalid HMAC'));
  551. }
  552. } else if (instate.status === IN_PACKETDATAAFTER) {
  553. if (decompress.instance) {
  554. if (!decomp) {
  555. debug('DEBUG: Parser: Decompressing');
  556. decompress.instance.write(instate.payload);
  557. var decompBuf = [];
  558. var decompBufLen = 0;
  559. decompress.instance.on('readable', function() {
  560. var buf;
  561. while (buf = this.read()) {
  562. decompBuf.push(buf);
  563. decompBufLen += buf.length;
  564. }
  565. }).flush(Z_PARTIAL_FLUSH, function() {
  566. decompress.instance.removeAllListeners('readable');
  567. if (decompBuf.length === 1)
  568. instate.payload = decompBuf[0];
  569. else
  570. instate.payload = Buffer.concat(decompBuf, decompBufLen);
  571. decompBuf = null;
  572. var nextSlice;
  573. if (i === chlen)
  574. nextSlice = EMPTY_BUFFER; // Avoid slicing a zero-length buffer
  575. else
  576. nextSlice = chunk.slice(i);
  577. self._transform(nextSlice, encoding, callback, true);
  578. });
  579. return;
  580. } else {
  581. // Make sure we reset this after this first time in the loop,
  582. // otherwise we could end up trying to interpret as-is another
  583. // compressed packet that is within the same chunk
  584. decomp = false;
  585. }
  586. }
  587. this.emit('packet');
  588. var ptype = instate.payload[0];
  589. if (debug !== DEBUG_NOOP) {
  590. var msgPacket = 'DEBUG: Parser: IN_PACKETDATAAFTER, packet: ';
  591. var kexdh = state.kexdh;
  592. var authMethod = state.authsQueue[0];
  593. var msgPktType = null;
  594. if (outstate.status === OUT_REKEYING
  595. && !(ptype <= 4 || (ptype >= 20 && ptype <= 49)))
  596. msgPacket += '(enqueued) ';
  597. if (ptype === MESSAGE.KEXDH_INIT) {
  598. if (kexdh === 'group')
  599. msgPktType = 'KEXDH_INIT';
  600. else if (kexdh[0] === 'e')
  601. msgPktType = 'KEXECDH_INIT';
  602. else
  603. msgPktType = 'KEXDH_GEX_REQUEST';
  604. } else if (ptype === MESSAGE.KEXDH_REPLY) {
  605. if (kexdh === 'group')
  606. msgPktType = 'KEXDH_REPLY';
  607. else if (kexdh[0] === 'e')
  608. msgPktType = 'KEXECDH_REPLY';
  609. else
  610. msgPktType = 'KEXDH_GEX_GROUP';
  611. } else if (ptype === MESSAGE.KEXDH_GEX_GROUP)
  612. msgPktType = 'KEXDH_GEX_GROUP';
  613. else if (ptype === MESSAGE.KEXDH_GEX_REPLY)
  614. msgPktType = 'KEXDH_GEX_REPLY';
  615. else if (ptype === 60) {
  616. if (authMethod === 'password')
  617. msgPktType = 'USERAUTH_PASSWD_CHANGEREQ';
  618. else if (authMethod === 'keyboard-interactive')
  619. msgPktType = 'USERAUTH_INFO_REQUEST';
  620. else if (authMethod === 'publickey')
  621. msgPktType = 'USERAUTH_PK_OK';
  622. else
  623. msgPktType = 'UNKNOWN PACKET 60';
  624. } else if (ptype === 61) {
  625. if (authMethod === 'keyboard-interactive')
  626. msgPktType = 'USERAUTH_INFO_RESPONSE';
  627. else
  628. msgPktType = 'UNKNOWN PACKET 61';
  629. }
  630. if (msgPktType === null)
  631. msgPktType = MESSAGE[ptype];
  632. // Don't write debug output for messages we custom make in parsePacket()
  633. if (ptype !== MESSAGE.CHANNEL_OPEN
  634. && ptype !== MESSAGE.CHANNEL_REQUEST
  635. && ptype !== MESSAGE.CHANNEL_SUCCESS
  636. && ptype !== MESSAGE.CHANNEL_FAILURE
  637. && ptype !== MESSAGE.CHANNEL_EOF
  638. && ptype !== MESSAGE.CHANNEL_CLOSE
  639. && ptype !== MESSAGE.CHANNEL_DATA
  640. && ptype !== MESSAGE.CHANNEL_EXTENDED_DATA
  641. && ptype !== MESSAGE.CHANNEL_WINDOW_ADJUST
  642. && ptype !== MESSAGE.DISCONNECT
  643. && ptype !== MESSAGE.USERAUTH_REQUEST
  644. && ptype !== MESSAGE.GLOBAL_REQUEST)
  645. debug(msgPacket + msgPktType);
  646. }
  647. // Only parse packet if we are not re-keying or the packet is not a
  648. // transport layer packet needed for re-keying
  649. if (outstate.status === OUT_READY
  650. || ptype <= 4
  651. || (ptype >= 20 && ptype <= 49)) {
  652. if (parsePacket(this, callback) === false)
  653. return;
  654. if (instate.status === IN_INIT) {
  655. // We were reset due to some error/disagreement ?
  656. return;
  657. }
  658. } else if (outstate.status === OUT_REKEYING) {
  659. if (instate.rekeyQueue.length === MAX_PACKETS_REKEYING) {
  660. debug('DEBUG: Parser: Max incoming re-key queue length reached');
  661. this.disconnect(DISCONNECT_REASON.PROTOCOL_ERROR);
  662. return callback(
  663. new Error('Incoming re-key queue length limit reached')
  664. );
  665. }
  666. // Make sure to record the sequence number in case we need it later on
  667. // when we drain the queue (e.g. unknown packet)
  668. var seqno = instate.seqno;
  669. if (++instate.seqno > MAX_SEQNO)
  670. instate.seqno = 0;
  671. instate.rekeyQueue.push([seqno, instate.payload]);
  672. }
  673. instate.status = IN_PACKETBEFORE;
  674. instate.payload = undefined;
  675. }
  676. if (buffer !== undefined)
  677. buffer = undefined;
  678. }
  679. callback();
  680. };
  681. SSH2Stream.prototype.reset = function(noend) {
  682. if (this._state) {
  683. var state = this._state;
  684. state.incoming.status = IN_INIT;
  685. state.outgoing.status = OUT_INIT;
  686. } else {
  687. this._state = {
  688. authsQueue: [],
  689. hostkeyFormat: undefined,
  690. kex: undefined,
  691. kexdh: undefined,
  692. incoming: {
  693. status: IN_INIT,
  694. expectedPacket: undefined,
  695. search: undefined,
  696. greeting: undefined,
  697. seqno: 0,
  698. pktLen: undefined,
  699. padLen: undefined,
  700. pktExtra: undefined,
  701. payload: undefined,
  702. packet: undefined,
  703. kexinit: undefined,
  704. identRaw: undefined,
  705. rekeyQueue: [],
  706. ignoreNext: false,
  707. expect: {
  708. amount: undefined,
  709. type: undefined,
  710. ptr: 0,
  711. buf: undefined
  712. },
  713. decrypt: {
  714. instance: false,
  715. info: undefined,
  716. iv: undefined,
  717. key: undefined,
  718. buf: undefined,
  719. type: undefined
  720. },
  721. hmac: {
  722. info: undefined,
  723. key: undefined,
  724. buf: undefined,
  725. type: false
  726. },
  727. decompress: {
  728. instance: false,
  729. type: false
  730. }
  731. },
  732. outgoing: {
  733. status: OUT_INIT,
  734. seqno: 0,
  735. bufSeqno: Buffer.allocUnsafe(4),
  736. rekeyQueue: [],
  737. kexinit: undefined,
  738. kexsecret: undefined,
  739. pubkey: undefined,
  740. exchangeHash: undefined,
  741. sessionId: undefined,
  742. sentNEWKEYS: false,
  743. encrypt: {
  744. instance: false,
  745. info: undefined,
  746. iv: undefined,
  747. key: undefined,
  748. type: undefined
  749. },
  750. hmac: {
  751. info: undefined,
  752. key: undefined,
  753. buf: undefined,
  754. type: false
  755. },
  756. compress: {
  757. instance: false,
  758. type: false,
  759. queue: null
  760. }
  761. }
  762. };
  763. }
  764. if (!noend) {
  765. if (this.readable)
  766. this.push(null);
  767. }
  768. };
  769. // Common methods
  770. // Global
  771. SSH2Stream.prototype.disconnect = function(reason) {
  772. /*
  773. byte SSH_MSG_DISCONNECT
  774. uint32 reason code
  775. string description in ISO-10646 UTF-8 encoding
  776. string language tag
  777. */
  778. var buf = Buffer.alloc(1 + 4 + 4 + 4);
  779. buf[0] = MESSAGE.DISCONNECT;
  780. if (DISCONNECT_REASON[reason] === undefined)
  781. reason = DISCONNECT_REASON.BY_APPLICATION;
  782. writeUInt32BE(buf, reason, 1);
  783. this.debug('DEBUG: Outgoing: Writing DISCONNECT ('
  784. + DISCONNECT_REASON[reason]
  785. + ')');
  786. send(this, buf);
  787. this.reset();
  788. return false;
  789. };
  790. SSH2Stream.prototype.ping = function() {
  791. this.debug('DEBUG: Outgoing: Writing ping (GLOBAL_REQUEST: keepalive@openssh.com)');
  792. return send(this, PING_PACKET);
  793. };
  794. SSH2Stream.prototype.rekey = function() {
  795. var status = this._state.outgoing.status;
  796. if (status === OUT_REKEYING)
  797. throw new Error('A re-key is already in progress');
  798. else if (status !== OUT_READY)
  799. throw new Error('Cannot re-key yet');
  800. this.debug('DEBUG: Outgoing: Starting re-key');
  801. return KEXINIT(this);
  802. };
  803. // 'ssh-connection' service-specific
  804. SSH2Stream.prototype.requestSuccess = function(data) {
  805. var buf;
  806. if (Buffer.isBuffer(data)) {
  807. buf = Buffer.allocUnsafe(1 + data.length);
  808. buf[0] = MESSAGE.REQUEST_SUCCESS;
  809. data.copy(buf, 1);
  810. } else
  811. buf = REQUEST_SUCCESS_PACKET;
  812. this.debug('DEBUG: Outgoing: Writing REQUEST_SUCCESS');
  813. return send(this, buf);
  814. };
  815. SSH2Stream.prototype.requestFailure = function() {
  816. this.debug('DEBUG: Outgoing: Writing REQUEST_FAILURE');
  817. return send(this, REQUEST_FAILURE_PACKET);
  818. };
  819. SSH2Stream.prototype.channelSuccess = function(chan) {
  820. // Does not consume window space
  821. var buf = Buffer.allocUnsafe(1 + 4);
  822. buf[0] = MESSAGE.CHANNEL_SUCCESS;
  823. writeUInt32BE(buf, chan, 1);
  824. this.debug('DEBUG: Outgoing: Writing CHANNEL_SUCCESS (' + chan + ')');
  825. return send(this, buf);
  826. };
  827. SSH2Stream.prototype.channelFailure = function(chan) {
  828. // Does not consume window space
  829. var buf = Buffer.allocUnsafe(1 + 4);
  830. buf[0] = MESSAGE.CHANNEL_FAILURE;
  831. writeUInt32BE(buf, chan, 1);
  832. this.debug('DEBUG: Outgoing: Writing CHANNEL_FAILURE (' + chan + ')');
  833. return send(this, buf);
  834. };
  835. SSH2Stream.prototype.channelEOF = function(chan) {
  836. // Does not consume window space
  837. var buf = Buffer.allocUnsafe(1 + 4);
  838. buf[0] = MESSAGE.CHANNEL_EOF;
  839. writeUInt32BE(buf, chan, 1);
  840. this.debug('DEBUG: Outgoing: Writing CHANNEL_EOF (' + chan + ')');
  841. return send(this, buf);
  842. };
  843. SSH2Stream.prototype.channelClose = function(chan) {
  844. // Does not consume window space
  845. var buf = Buffer.allocUnsafe(1 + 4);
  846. buf[0] = MESSAGE.CHANNEL_CLOSE;
  847. writeUInt32BE(buf, chan, 1);
  848. this.debug('DEBUG: Outgoing: Writing CHANNEL_CLOSE (' + chan + ')');
  849. return send(this, buf);
  850. };
  851. SSH2Stream.prototype.channelWindowAdjust = function(chan, amount) {
  852. // Does not consume window space
  853. var buf = Buffer.allocUnsafe(1 + 4 + 4);
  854. buf[0] = MESSAGE.CHANNEL_WINDOW_ADJUST;
  855. writeUInt32BE(buf, chan, 1);
  856. writeUInt32BE(buf, amount, 5);
  857. this.debug('DEBUG: Outgoing: Writing CHANNEL_WINDOW_ADJUST ('
  858. + chan
  859. + ', '
  860. + amount
  861. + ')');
  862. return send(this, buf);
  863. };
  864. SSH2Stream.prototype.channelData = function(chan, data) {
  865. var dataIsBuffer = Buffer.isBuffer(data);
  866. var dataLen = (dataIsBuffer ? data.length : Buffer.byteLength(data));
  867. var buf = Buffer.allocUnsafe(1 + 4 + 4 + dataLen);
  868. buf[0] = MESSAGE.CHANNEL_DATA;
  869. writeUInt32BE(buf, chan, 1);
  870. writeUInt32BE(buf, dataLen, 5);
  871. if (dataIsBuffer)
  872. data.copy(buf, 9);
  873. else
  874. buf.write(data, 9, dataLen, 'utf8');
  875. this.debug('DEBUG: Outgoing: Writing CHANNEL_DATA (' + chan + ')');
  876. return send(this, buf);
  877. };
  878. SSH2Stream.prototype.channelExtData = function(chan, data, type) {
  879. var dataIsBuffer = Buffer.isBuffer(data);
  880. var dataLen = (dataIsBuffer ? data.length : Buffer.byteLength(data));
  881. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 4 + dataLen);
  882. buf[0] = MESSAGE.CHANNEL_EXTENDED_DATA;
  883. writeUInt32BE(buf, chan, 1);
  884. writeUInt32BE(buf, type, 5);
  885. writeUInt32BE(buf, dataLen, 9);
  886. if (dataIsBuffer)
  887. data.copy(buf, 13);
  888. else
  889. buf.write(data, 13, dataLen, 'utf8');
  890. this.debug('DEBUG: Outgoing: Writing CHANNEL_EXTENDED_DATA (' + chan + ')');
  891. return send(this, buf);
  892. };
  893. SSH2Stream.prototype.channelOpenConfirm = function(remoteChan, localChan,
  894. initWindow, maxPacket) {
  895. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 4 + 4);
  896. buf[0] = MESSAGE.CHANNEL_OPEN_CONFIRMATION;
  897. writeUInt32BE(buf, remoteChan, 1);
  898. writeUInt32BE(buf, localChan, 5);
  899. writeUInt32BE(buf, initWindow, 9);
  900. writeUInt32BE(buf, maxPacket, 13);
  901. this.debug('DEBUG: Outgoing: Writing CHANNEL_OPEN_CONFIRMATION (r:'
  902. + remoteChan
  903. + ', l:'
  904. + localChan
  905. + ')');
  906. return send(this, buf);
  907. };
  908. SSH2Stream.prototype.channelOpenFail = function(remoteChan, reason, desc,
  909. lang) {
  910. if (typeof desc !== 'string')
  911. desc = '';
  912. if (typeof lang !== 'string')
  913. lang = '';
  914. var descLen = Buffer.byteLength(desc);
  915. var langLen = Buffer.byteLength(lang);
  916. var p = 9;
  917. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 4 + descLen + 4 + langLen);
  918. buf[0] = MESSAGE.CHANNEL_OPEN_FAILURE;
  919. writeUInt32BE(buf, remoteChan, 1);
  920. writeUInt32BE(buf, reason, 5);
  921. writeUInt32BE(buf, descLen, p);
  922. p += 4;
  923. if (descLen) {
  924. buf.write(desc, p, descLen, 'utf8');
  925. p += descLen;
  926. }
  927. writeUInt32BE(buf, langLen, p);
  928. if (langLen)
  929. buf.write(lang, p += 4, langLen, 'ascii');
  930. this.debug('DEBUG: Outgoing: Writing CHANNEL_OPEN_FAILURE ('
  931. + remoteChan
  932. + ')');
  933. return send(this, buf);
  934. };
  935. // Client-specific methods
  936. // Global
  937. SSH2Stream.prototype.service = function(svcName) {
  938. if (this.server)
  939. throw new Error('Client-only method called in server mode');
  940. var svcNameLen = Buffer.byteLength(svcName);
  941. var buf = Buffer.allocUnsafe(1 + 4 + svcNameLen);
  942. buf[0] = MESSAGE.SERVICE_REQUEST;
  943. writeUInt32BE(buf, svcNameLen, 1);
  944. buf.write(svcName, 5, svcNameLen, 'ascii');
  945. this.debug('DEBUG: Outgoing: Writing SERVICE_REQUEST (' + svcName + ')');
  946. return send(this, buf);
  947. };
  948. // 'ssh-connection' service-specific
  949. SSH2Stream.prototype.tcpipForward = function(bindAddr, bindPort, wantReply) {
  950. if (this.server)
  951. throw new Error('Client-only method called in server mode');
  952. var addrlen = Buffer.byteLength(bindAddr);
  953. var buf = Buffer.allocUnsafe(1 + 4 + 13 + 1 + 4 + addrlen + 4);
  954. buf[0] = MESSAGE.GLOBAL_REQUEST;
  955. writeUInt32BE(buf, 13, 1);
  956. buf.write('tcpip-forward', 5, 13, 'ascii');
  957. buf[18] = (wantReply === undefined || wantReply === true ? 1 : 0);
  958. writeUInt32BE(buf, addrlen, 19);
  959. buf.write(bindAddr, 23, addrlen, 'ascii');
  960. writeUInt32BE(buf, bindPort, 23 + addrlen);
  961. this.debug('DEBUG: Outgoing: Writing GLOBAL_REQUEST (tcpip-forward)');
  962. return send(this, buf);
  963. };
  964. SSH2Stream.prototype.cancelTcpipForward = function(bindAddr, bindPort,
  965. wantReply) {
  966. if (this.server)
  967. throw new Error('Client-only method called in server mode');
  968. var addrlen = Buffer.byteLength(bindAddr);
  969. var buf = Buffer.allocUnsafe(1 + 4 + 20 + 1 + 4 + addrlen + 4);
  970. buf[0] = MESSAGE.GLOBAL_REQUEST;
  971. writeUInt32BE(buf, 20, 1);
  972. buf.write('cancel-tcpip-forward', 5, 20, 'ascii');
  973. buf[25] = (wantReply === undefined || wantReply === true ? 1 : 0);
  974. writeUInt32BE(buf, addrlen, 26);
  975. buf.write(bindAddr, 30, addrlen, 'ascii');
  976. writeUInt32BE(buf, bindPort, 30 + addrlen);
  977. this.debug('DEBUG: Outgoing: Writing GLOBAL_REQUEST (cancel-tcpip-forward)');
  978. return send(this, buf);
  979. };
  980. SSH2Stream.prototype.openssh_streamLocalForward = function(socketPath,
  981. wantReply) {
  982. if (this.server)
  983. throw new Error('Client-only method called in server mode');
  984. var pathlen = Buffer.byteLength(socketPath);
  985. var buf = Buffer.allocUnsafe(1 + 4 + 31 + 1 + 4 + pathlen);
  986. buf[0] = MESSAGE.GLOBAL_REQUEST;
  987. writeUInt32BE(buf, 31, 1);
  988. buf.write('streamlocal-forward@openssh.com', 5, 31, 'ascii');
  989. buf[36] = (wantReply === undefined || wantReply === true ? 1 : 0);
  990. writeUInt32BE(buf, pathlen, 37);
  991. buf.write(socketPath, 41, pathlen, 'utf8');
  992. this.debug('DEBUG: Outgoing: Writing GLOBAL_REQUEST (streamlocal-forward@openssh.com)');
  993. return send(this, buf);
  994. };
  995. SSH2Stream.prototype.openssh_cancelStreamLocalForward = function(socketPath,
  996. wantReply) {
  997. if (this.server)
  998. throw new Error('Client-only method called in server mode');
  999. var pathlen = Buffer.byteLength(socketPath);
  1000. var buf = Buffer.allocUnsafe(1 + 4 + 38 + 1 + 4 + pathlen);
  1001. buf[0] = MESSAGE.GLOBAL_REQUEST;
  1002. writeUInt32BE(buf, 38, 1);
  1003. buf.write('cancel-streamlocal-forward@openssh.com', 5, 38, 'ascii');
  1004. buf[43] = (wantReply === undefined || wantReply === true ? 1 : 0);
  1005. writeUInt32BE(buf, pathlen, 44);
  1006. buf.write(socketPath, 48, pathlen, 'utf8');
  1007. this.debug('DEBUG: Outgoing: Writing GLOBAL_REQUEST (cancel-streamlocal-forward@openssh.com)');
  1008. return send(this, buf);
  1009. };
  1010. SSH2Stream.prototype.directTcpip = function(chan, initWindow, maxPacket, cfg) {
  1011. if (this.server)
  1012. throw new Error('Client-only method called in server mode');
  1013. var srclen = Buffer.byteLength(cfg.srcIP);
  1014. var dstlen = Buffer.byteLength(cfg.dstIP);
  1015. var p = 29;
  1016. var buf = Buffer.allocUnsafe(1 + 4 + 12 + 4 + 4 + 4 + 4 + srclen + 4 + 4
  1017. + dstlen + 4);
  1018. buf[0] = MESSAGE.CHANNEL_OPEN;
  1019. writeUInt32BE(buf, 12, 1);
  1020. buf.write('direct-tcpip', 5, 12, 'ascii');
  1021. writeUInt32BE(buf, chan, 17);
  1022. writeUInt32BE(buf, initWindow, 21);
  1023. writeUInt32BE(buf, maxPacket, 25);
  1024. writeUInt32BE(buf, dstlen, p);
  1025. buf.write(cfg.dstIP, p += 4, dstlen, 'ascii');
  1026. writeUInt32BE(buf, cfg.dstPort, p += dstlen);
  1027. writeUInt32BE(buf, srclen, p += 4);
  1028. buf.write(cfg.srcIP, p += 4, srclen, 'ascii');
  1029. writeUInt32BE(buf, cfg.srcPort, p += srclen);
  1030. this.debug('DEBUG: Outgoing: Writing CHANNEL_OPEN ('
  1031. + chan
  1032. + ', direct-tcpip)');
  1033. return send(this, buf);
  1034. };
  1035. SSH2Stream.prototype.openssh_directStreamLocal = function(chan, initWindow,
  1036. maxPacket, cfg) {
  1037. if (this.server)
  1038. throw new Error('Client-only method called in server mode');
  1039. var pathlen = Buffer.byteLength(cfg.socketPath);
  1040. var p = 47;
  1041. var buf = Buffer.allocUnsafe(1 + 4 + 30 + 4 + 4 + 4 + 4 + pathlen + 4 + 4);
  1042. buf[0] = MESSAGE.CHANNEL_OPEN;
  1043. writeUInt32BE(buf, 30, 1);
  1044. buf.write('direct-streamlocal@openssh.com', 5, 30, 'ascii');
  1045. writeUInt32BE(buf, chan, 35);
  1046. writeUInt32BE(buf, initWindow, 39);
  1047. writeUInt32BE(buf, maxPacket, 43);
  1048. writeUInt32BE(buf, pathlen, p);
  1049. buf.write(cfg.socketPath, p += 4, pathlen, 'utf8');
  1050. // reserved fields (string and uint32)
  1051. buf.fill(0, buf.length - 8);
  1052. this.debug('DEBUG: Outgoing: Writing CHANNEL_OPEN ('
  1053. + chan
  1054. + ', direct-streamlocal@openssh.com)');
  1055. return send(this, buf);
  1056. };
  1057. SSH2Stream.prototype.openssh_noMoreSessions = function(wantReply) {
  1058. if (this.server)
  1059. throw new Error('Client-only method called in server mode');
  1060. var buf = Buffer.allocUnsafe(1 + 4 + 28 + 1);
  1061. buf[0] = MESSAGE.GLOBAL_REQUEST;
  1062. writeUInt32BE(buf, 28, 1);
  1063. buf.write('no-more-sessions@openssh.com', 5, 28, 'ascii');
  1064. buf[33] = (wantReply === undefined || wantReply === true ? 1 : 0);
  1065. this.debug('DEBUG: Outgoing: Writing GLOBAL_REQUEST (no-more-sessions@openssh.com)');
  1066. return send(this, buf);
  1067. };
  1068. SSH2Stream.prototype.session = function(chan, initWindow, maxPacket) {
  1069. if (this.server)
  1070. throw new Error('Client-only method called in server mode');
  1071. // Does not consume window space
  1072. var buf = Buffer.allocUnsafe(1 + 4 + 7 + 4 + 4 + 4);
  1073. buf[0] = MESSAGE.CHANNEL_OPEN;
  1074. writeUInt32BE(buf, 7, 1);
  1075. buf.write('session', 5, 7, 'ascii');
  1076. writeUInt32BE(buf, chan, 12);
  1077. writeUInt32BE(buf, initWindow, 16);
  1078. writeUInt32BE(buf, maxPacket, 20);
  1079. this.debug('DEBUG: Outgoing: Writing CHANNEL_OPEN ('
  1080. + chan
  1081. + ', session)');
  1082. return send(this, buf);
  1083. };
  1084. SSH2Stream.prototype.windowChange = function(chan, rows, cols, height, width) {
  1085. if (this.server)
  1086. throw new Error('Client-only method called in server mode');
  1087. // Does not consume window space
  1088. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 13 + 1 + 4 + 4 + 4 + 4);
  1089. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1090. writeUInt32BE(buf, chan, 1);
  1091. writeUInt32BE(buf, 13, 5);
  1092. buf.write('window-change', 9, 13, 'ascii');
  1093. buf[22] = 0;
  1094. writeUInt32BE(buf, cols, 23);
  1095. writeUInt32BE(buf, rows, 27);
  1096. writeUInt32BE(buf, width, 31);
  1097. writeUInt32BE(buf, height, 35);
  1098. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1099. + chan
  1100. + ', window-change)');
  1101. return send(this, buf);
  1102. };
  1103. SSH2Stream.prototype.pty = function(chan, rows, cols, height,
  1104. width, term, modes, wantReply) {
  1105. if (this.server)
  1106. throw new Error('Client-only method called in server mode');
  1107. // Does not consume window space
  1108. if (!term || !term.length)
  1109. term = 'vt100';
  1110. if (modes
  1111. && !Buffer.isBuffer(modes)
  1112. && !Array.isArray(modes)
  1113. && typeof modes === 'object')
  1114. modes = modesToBytes(modes);
  1115. if (!modes || !modes.length)
  1116. modes = NO_TERMINAL_MODES_BUFFER;
  1117. var termLen = term.length;
  1118. var modesLen = modes.length;
  1119. var p = 21;
  1120. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 7 + 1 + 4 + termLen + 4 + 4 + 4 + 4
  1121. + 4 + modesLen);
  1122. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1123. writeUInt32BE(buf, chan, 1);
  1124. writeUInt32BE(buf, 7, 5);
  1125. buf.write('pty-req', 9, 7, 'ascii');
  1126. buf[16] = (wantReply === undefined || wantReply === true ? 1 : 0);
  1127. writeUInt32BE(buf, termLen, 17);
  1128. buf.write(term, 21, termLen, 'utf8');
  1129. writeUInt32BE(buf, cols, p += termLen);
  1130. writeUInt32BE(buf, rows, p += 4);
  1131. writeUInt32BE(buf, width, p += 4);
  1132. writeUInt32BE(buf, height, p += 4);
  1133. writeUInt32BE(buf, modesLen, p += 4);
  1134. p += 4;
  1135. if (Array.isArray(modes)) {
  1136. for (var i = 0; i < modesLen; ++i)
  1137. buf[p++] = modes[i];
  1138. } else if (Buffer.isBuffer(modes)) {
  1139. modes.copy(buf, p);
  1140. }
  1141. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1142. + chan
  1143. + ', pty-req)');
  1144. return send(this, buf);
  1145. };
  1146. SSH2Stream.prototype.shell = function(chan, wantReply) {
  1147. if (this.server)
  1148. throw new Error('Client-only method called in server mode');
  1149. // Does not consume window space
  1150. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 5 + 1);
  1151. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1152. writeUInt32BE(buf, chan, 1);
  1153. writeUInt32BE(buf, 5, 5);
  1154. buf.write('shell', 9, 5, 'ascii');
  1155. buf[14] = (wantReply === undefined || wantReply === true ? 1 : 0);
  1156. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1157. + chan
  1158. + ', shell)');
  1159. return send(this, buf);
  1160. };
  1161. SSH2Stream.prototype.exec = function(chan, cmd, wantReply) {
  1162. if (this.server)
  1163. throw new Error('Client-only method called in server mode');
  1164. // Does not consume window space
  1165. var cmdlen = (Buffer.isBuffer(cmd) ? cmd.length : Buffer.byteLength(cmd));
  1166. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 4 + 1 + 4 + cmdlen);
  1167. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1168. writeUInt32BE(buf, chan, 1);
  1169. writeUInt32BE(buf, 4, 5);
  1170. buf.write('exec', 9, 4, 'ascii');
  1171. buf[13] = (wantReply === undefined || wantReply === true ? 1 : 0);
  1172. writeUInt32BE(buf, cmdlen, 14);
  1173. if (Buffer.isBuffer(cmd))
  1174. cmd.copy(buf, 18);
  1175. else
  1176. buf.write(cmd, 18, cmdlen, 'utf8');
  1177. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1178. + chan
  1179. + ', exec)');
  1180. return send(this, buf);
  1181. };
  1182. SSH2Stream.prototype.signal = function(chan, signal) {
  1183. if (this.server)
  1184. throw new Error('Client-only method called in server mode');
  1185. // Does not consume window space
  1186. signal = signal.toUpperCase();
  1187. if (signal.slice(0, 3) === 'SIG')
  1188. signal = signal.substring(3);
  1189. if (SIGNALS.indexOf(signal) === -1)
  1190. throw new Error('Invalid signal: ' + signal);
  1191. var signalLen = signal.length;
  1192. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 6 + 1 + 4 + signalLen);
  1193. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1194. writeUInt32BE(buf, chan, 1);
  1195. writeUInt32BE(buf, 6, 5);
  1196. buf.write('signal', 9, 6, 'ascii');
  1197. buf[15] = 0;
  1198. writeUInt32BE(buf, signalLen, 16);
  1199. buf.write(signal, 20, signalLen, 'ascii');
  1200. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1201. + chan
  1202. + ', signal)');
  1203. return send(this, buf);
  1204. };
  1205. SSH2Stream.prototype.env = function(chan, key, val, wantReply) {
  1206. if (this.server)
  1207. throw new Error('Client-only method called in server mode');
  1208. // Does not consume window space
  1209. var keyLen = Buffer.byteLength(key);
  1210. var valLen = (Buffer.isBuffer(val) ? val.length : Buffer.byteLength(val));
  1211. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 3 + 1 + 4 + keyLen + 4 + valLen);
  1212. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1213. writeUInt32BE(buf, chan, 1);
  1214. writeUInt32BE(buf, 3, 5);
  1215. buf.write('env', 9, 3, 'ascii');
  1216. buf[12] = (wantReply === undefined || wantReply === true ? 1 : 0);
  1217. writeUInt32BE(buf, keyLen, 13);
  1218. buf.write(key, 17, keyLen, 'ascii');
  1219. writeUInt32BE(buf, valLen, 17 + keyLen);
  1220. if (Buffer.isBuffer(val))
  1221. val.copy(buf, 17 + keyLen + 4);
  1222. else
  1223. buf.write(val, 17 + keyLen + 4, valLen, 'utf8');
  1224. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1225. + chan
  1226. + ', env)');
  1227. return send(this, buf);
  1228. };
  1229. SSH2Stream.prototype.x11Forward = function(chan, cfg, wantReply) {
  1230. if (this.server)
  1231. throw new Error('Client-only method called in server mode');
  1232. // Does not consume window space
  1233. var protolen = Buffer.byteLength(cfg.protocol);
  1234. var cookielen = Buffer.byteLength(cfg.cookie);
  1235. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 7 + 1 + 1 + 4 + protolen + 4
  1236. + cookielen + 4);
  1237. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1238. writeUInt32BE(buf, chan, 1);
  1239. writeUInt32BE(buf, 7, 5);
  1240. buf.write('x11-req', 9, 7, 'ascii');
  1241. buf[16] = (wantReply === undefined || wantReply === true ? 1 : 0);
  1242. buf[17] = (cfg.single ? 1 : 0);
  1243. writeUInt32BE(buf, protolen, 18);
  1244. var bp = 22;
  1245. if (Buffer.isBuffer(cfg.protocol))
  1246. cfg.protocol.copy(buf, bp);
  1247. else
  1248. buf.write(cfg.protocol, bp, protolen, 'utf8');
  1249. bp += protolen;
  1250. writeUInt32BE(buf, cookielen, bp);
  1251. bp += 4;
  1252. if (Buffer.isBuffer(cfg.cookie))
  1253. cfg.cookie.copy(buf, bp);
  1254. else
  1255. buf.write(cfg.cookie, bp, cookielen, 'binary');
  1256. bp += cookielen;
  1257. writeUInt32BE(buf, (cfg.screen || 0), bp);
  1258. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1259. + chan
  1260. + ', x11-req)');
  1261. return send(this, buf);
  1262. };
  1263. SSH2Stream.prototype.subsystem = function(chan, name, wantReply) {
  1264. if (this.server)
  1265. throw new Error('Client-only method called in server mode');
  1266. // Does not consume window space
  1267. var nameLen = Buffer.byteLength(name);
  1268. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 9 + 1 + 4 + nameLen);
  1269. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1270. writeUInt32BE(buf, chan, 1);
  1271. writeUInt32BE(buf, 9, 5);
  1272. buf.write('subsystem', 9, 9, 'ascii');
  1273. buf[18] = (wantReply === undefined || wantReply === true ? 1 : 0);
  1274. writeUInt32BE(buf, nameLen, 19);
  1275. buf.write(name, 23, nameLen, 'ascii');
  1276. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1277. + chan
  1278. + ', subsystem: '
  1279. + name
  1280. + ')');
  1281. return send(this, buf);
  1282. };
  1283. SSH2Stream.prototype.openssh_agentForward = function(chan, wantReply) {
  1284. if (this.server)
  1285. throw new Error('Client-only method called in server mode');
  1286. // Does not consume window space
  1287. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 26 + 1);
  1288. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1289. writeUInt32BE(buf, chan, 1);
  1290. writeUInt32BE(buf, 26, 5);
  1291. buf.write('auth-agent-req@openssh.com', 9, 26, 'ascii');
  1292. buf[35] = (wantReply === undefined || wantReply === true ? 1 : 0);
  1293. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1294. + chan
  1295. + ', auth-agent-req@openssh.com)');
  1296. return send(this, buf);
  1297. };
  1298. // 'ssh-userauth' service-specific
  1299. SSH2Stream.prototype.authPassword = function(username, password) {
  1300. if (this.server)
  1301. throw new Error('Client-only method called in server mode');
  1302. var userLen = Buffer.byteLength(username);
  1303. var passLen = Buffer.byteLength(password);
  1304. var p = 0;
  1305. var buf = Buffer.allocUnsafe(1
  1306. + 4 + userLen
  1307. + 4 + 14 // "ssh-connection"
  1308. + 4 + 8 // "password"
  1309. + 1
  1310. + 4 + passLen);
  1311. buf[p] = MESSAGE.USERAUTH_REQUEST;
  1312. writeUInt32BE(buf, userLen, ++p);
  1313. buf.write(username, p += 4, userLen, 'utf8');
  1314. writeUInt32BE(buf, 14, p += userLen);
  1315. buf.write('ssh-connection', p += 4, 14, 'ascii');
  1316. writeUInt32BE(buf, 8, p += 14);
  1317. buf.write('password', p += 4, 8, 'ascii');
  1318. buf[p += 8] = 0;
  1319. writeUInt32BE(buf, passLen, ++p);
  1320. buf.write(password, p += 4, passLen, 'utf8');
  1321. this._state.authsQueue.push('password');
  1322. this.debug('DEBUG: Outgoing: Writing USERAUTH_REQUEST (password)');
  1323. return send(this, buf);
  1324. };
  1325. SSH2Stream.prototype.authPK = function(username, pubKey, cbSign) {
  1326. if (this.server)
  1327. throw new Error('Client-only method called in server mode');
  1328. var self = this;
  1329. var outstate = this._state.outgoing;
  1330. var keyType;
  1331. if (typeof pubKey.getPublicSSH === 'function') {
  1332. keyType = pubKey.type;
  1333. pubKey = pubKey.getPublicSSH();
  1334. } else {
  1335. keyType = pubKey.toString('ascii',
  1336. 4,
  1337. 4 + readUInt32BE(pubKey, 0));
  1338. }
  1339. var userLen = Buffer.byteLength(username);
  1340. var algoLen = Buffer.byteLength(keyType);
  1341. var pubKeyLen = pubKey.length;
  1342. var sesLen = outstate.sessionId.length;
  1343. var p = 0;
  1344. var buf = Buffer.allocUnsafe((cbSign ? 4 + sesLen : 0)
  1345. + 1
  1346. + 4 + userLen
  1347. + 4 + 14 // "ssh-connection"
  1348. + 4 + 9 // "publickey"
  1349. + 1
  1350. + 4 + algoLen
  1351. + 4 + pubKeyLen
  1352. );
  1353. if (cbSign) {
  1354. writeUInt32BE(buf, sesLen, p);
  1355. outstate.sessionId.copy(buf, p += 4);
  1356. buf[p += sesLen] = MESSAGE.USERAUTH_REQUEST;
  1357. } else {
  1358. buf[p] = MESSAGE.USERAUTH_REQUEST;
  1359. }
  1360. writeUInt32BE(buf, userLen, ++p);
  1361. buf.write(username, p += 4, userLen, 'utf8');
  1362. writeUInt32BE(buf, 14, p += userLen);
  1363. buf.write('ssh-connection', p += 4, 14, 'ascii');
  1364. writeUInt32BE(buf, 9, p += 14);
  1365. buf.write('publickey', p += 4, 9, 'ascii');
  1366. buf[p += 9] = (cbSign ? 1 : 0);
  1367. writeUInt32BE(buf, algoLen, ++p);
  1368. buf.write(keyType, p += 4, algoLen, 'ascii');
  1369. writeUInt32BE(buf, pubKeyLen, p += algoLen);
  1370. pubKey.copy(buf, p += 4);
  1371. if (!cbSign) {
  1372. this._state.authsQueue.push('publickey');
  1373. this.debug('DEBUG: Outgoing: Writing USERAUTH_REQUEST (publickey -- check)');
  1374. return send(this, buf);
  1375. }
  1376. cbSign(buf, function(signature) {
  1377. signature = convertSignature(signature, keyType);
  1378. if (signature === false)
  1379. throw new Error('Error while converting handshake signature');
  1380. var sigLen = signature.length;
  1381. var sigbuf = Buffer.allocUnsafe(1
  1382. + 4 + userLen
  1383. + 4 + 14 // "ssh-connection"
  1384. + 4 + 9 // "publickey"
  1385. + 1
  1386. + 4 + algoLen
  1387. + 4 + pubKeyLen
  1388. + 4 // 4 + algoLen + 4 + sigLen
  1389. + 4 + algoLen
  1390. + 4 + sigLen);
  1391. p = 0;
  1392. sigbuf[p] = MESSAGE.USERAUTH_REQUEST;
  1393. writeUInt32BE(sigbuf, userLen, ++p);
  1394. sigbuf.write(username, p += 4, userLen, 'utf8');
  1395. writeUInt32BE(sigbuf, 14, p += userLen);
  1396. sigbuf.write('ssh-connection', p += 4, 14, 'ascii');
  1397. writeUInt32BE(sigbuf, 9, p += 14);
  1398. sigbuf.write('publickey', p += 4, 9, 'ascii');
  1399. sigbuf[p += 9] = 1;
  1400. writeUInt32BE(sigbuf, algoLen, ++p);
  1401. sigbuf.write(keyType, p += 4, algoLen, 'ascii');
  1402. writeUInt32BE(sigbuf, pubKeyLen, p += algoLen);
  1403. pubKey.copy(sigbuf, p += 4);
  1404. writeUInt32BE(sigbuf, 4 + algoLen + 4 + sigLen, p += pubKeyLen);
  1405. writeUInt32BE(sigbuf, algoLen, p += 4);
  1406. sigbuf.write(keyType, p += 4, algoLen, 'ascii');
  1407. writeUInt32BE(sigbuf, sigLen, p += algoLen);
  1408. signature.copy(sigbuf, p += 4);
  1409. // Servers shouldn't send packet type 60 in response to signed publickey
  1410. // attempts, but if they do, interpret as type 60.
  1411. self._state.authsQueue.push('publickey');
  1412. self.debug('DEBUG: Outgoing: Writing USERAUTH_REQUEST (publickey)');
  1413. return send(self, sigbuf);
  1414. });
  1415. return true;
  1416. };
  1417. SSH2Stream.prototype.authHostbased = function(username, pubKey, hostname,
  1418. userlocal, cbSign) {
  1419. // TODO: Make DRY by sharing similar code with authPK()
  1420. if (this.server)
  1421. throw new Error('Client-only method called in server mode');
  1422. var self = this;
  1423. var outstate = this._state.outgoing;
  1424. var keyType;
  1425. if (typeof pubKey.getPublicSSH === 'function') {
  1426. keyType = pubKey.type;
  1427. pubKey = pubKey.getPublicSSH();
  1428. } else {
  1429. keyType = pubKey.toString('ascii',
  1430. 4,
  1431. 4 + readUInt32BE(pubKey, 0));
  1432. }
  1433. var userLen = Buffer.byteLength(username);
  1434. var algoLen = Buffer.byteLength(keyType);
  1435. var pubKeyLen = pubKey.length;
  1436. var sesLen = outstate.sessionId.length;
  1437. var hostnameLen = Buffer.byteLength(hostname);
  1438. var userlocalLen = Buffer.byteLength(userlocal);
  1439. var p = 0;
  1440. var buf = Buffer.allocUnsafe(4 + sesLen
  1441. + 1
  1442. + 4 + userLen
  1443. + 4 + 14 // "ssh-connection"
  1444. + 4 + 9 // "hostbased"
  1445. + 4 + algoLen
  1446. + 4 + pubKeyLen
  1447. + 4 + hostnameLen
  1448. + 4 + userlocalLen
  1449. );
  1450. writeUInt32BE(buf, sesLen, p);
  1451. outstate.sessionId.copy(buf, p += 4);
  1452. buf[p += sesLen] = MESSAGE.USERAUTH_REQUEST;
  1453. writeUInt32BE(buf, userLen, ++p);
  1454. buf.write(username, p += 4, userLen, 'utf8');
  1455. writeUInt32BE(buf, 14, p += userLen);
  1456. buf.write('ssh-connection', p += 4, 14, 'ascii');
  1457. writeUInt32BE(buf, 9, p += 14);
  1458. buf.write('hostbased', p += 4, 9, 'ascii');
  1459. writeUInt32BE(buf, algoLen, p += 9);
  1460. buf.write(keyType, p += 4, algoLen, 'ascii');
  1461. writeUInt32BE(buf, pubKeyLen, p += algoLen);
  1462. pubKey.copy(buf, p += 4);
  1463. writeUInt32BE(buf, hostnameLen, p += pubKeyLen);
  1464. buf.write(hostname, p += 4, hostnameLen, 'ascii');
  1465. writeUInt32BE(buf, userlocalLen, p += hostnameLen);
  1466. buf.write(userlocal, p += 4, userlocalLen, 'utf8');
  1467. cbSign(buf, function(signature) {
  1468. signature = convertSignature(signature, keyType);
  1469. if (signature === false)
  1470. throw new Error('Error while converting handshake signature');
  1471. var sigLen = signature.length;
  1472. var sigbuf = Buffer.allocUnsafe((buf.length - sesLen) + sigLen);
  1473. buf.copy(sigbuf, 0, 4 + sesLen);
  1474. writeUInt32BE(sigbuf, sigLen, sigbuf.length - sigLen - 4);
  1475. signature.copy(sigbuf, sigbuf.length - sigLen);
  1476. self._state.authsQueue.push('hostbased');
  1477. self.debug('DEBUG: Outgoing: Writing USERAUTH_REQUEST (hostbased)');
  1478. return send(self, sigbuf);
  1479. });
  1480. return true;
  1481. };
  1482. SSH2Stream.prototype.authKeyboard = function(username) {
  1483. if (this.server)
  1484. throw new Error('Client-only method called in server mode');
  1485. var userLen = Buffer.byteLength(username);
  1486. var p = 0;
  1487. var buf = Buffer.allocUnsafe(1
  1488. + 4 + userLen
  1489. + 4 + 14 // "ssh-connection"
  1490. + 4 + 20 // "keyboard-interactive"
  1491. + 4 // no language set
  1492. + 4 // no submethods
  1493. );
  1494. buf[p] = MESSAGE.USERAUTH_REQUEST;
  1495. writeUInt32BE(buf, userLen, ++p);
  1496. buf.write(username, p += 4, userLen, 'utf8');
  1497. writeUInt32BE(buf, 14, p += userLen);
  1498. buf.write('ssh-connection', p += 4, 14, 'ascii');
  1499. writeUInt32BE(buf, 20, p += 14);
  1500. buf.write('keyboard-interactive', p += 4, 20, 'ascii');
  1501. writeUInt32BE(buf, 0, p += 20);
  1502. writeUInt32BE(buf, 0, p += 4);
  1503. this._state.authsQueue.push('keyboard-interactive');
  1504. this.debug('DEBUG: Outgoing: Writing USERAUTH_REQUEST (keyboard-interactive)');
  1505. return send(this, buf);
  1506. };
  1507. SSH2Stream.prototype.authNone = function(username) {
  1508. if (this.server)
  1509. throw new Error('Client-only method called in server mode');
  1510. var userLen = Buffer.byteLength(username);
  1511. var p = 0;
  1512. var buf = Buffer.allocUnsafe(1
  1513. + 4 + userLen
  1514. + 4 + 14 // "ssh-connection"
  1515. + 4 + 4 // "none"
  1516. );
  1517. buf[p] = MESSAGE.USERAUTH_REQUEST;
  1518. writeUInt32BE(buf, userLen, ++p);
  1519. buf.write(username, p += 4, userLen, 'utf8');
  1520. writeUInt32BE(buf, 14, p += userLen);
  1521. buf.write('ssh-connection', p += 4, 14, 'ascii');
  1522. writeUInt32BE(buf, 4, p += 14);
  1523. buf.write('none', p += 4, 4, 'ascii');
  1524. this._state.authsQueue.push('none');
  1525. this.debug('DEBUG: Outgoing: Writing USERAUTH_REQUEST (none)');
  1526. return send(this, buf);
  1527. };
  1528. SSH2Stream.prototype.authInfoRes = function(responses) {
  1529. if (this.server)
  1530. throw new Error('Client-only method called in server mode');
  1531. var responsesLen = 0;
  1532. var p = 0;
  1533. var resLen;
  1534. var len;
  1535. var i;
  1536. if (responses) {
  1537. for (i = 0, len = responses.length; i < len; ++i)
  1538. responsesLen += 4 + Buffer.byteLength(responses[i]);
  1539. }
  1540. var buf = Buffer.allocUnsafe(1 + 4 + responsesLen);
  1541. buf[p++] = MESSAGE.USERAUTH_INFO_RESPONSE;
  1542. writeUInt32BE(buf, responses ? responses.length : 0, p);
  1543. if (responses) {
  1544. p += 4;
  1545. for (i = 0, len = responses.length; i < len; ++i) {
  1546. resLen = Buffer.byteLength(responses[i]);
  1547. writeUInt32BE(buf, resLen, p);
  1548. p += 4;
  1549. if (resLen) {
  1550. buf.write(responses[i], p, resLen, 'utf8');
  1551. p += resLen;
  1552. }
  1553. }
  1554. }
  1555. this.debug('DEBUG: Outgoing: Writing USERAUTH_INFO_RESPONSE');
  1556. return send(this, buf);
  1557. };
  1558. // Server-specific methods
  1559. // Global
  1560. SSH2Stream.prototype.serviceAccept = function(svcName) {
  1561. if (!this.server)
  1562. throw new Error('Server-only method called in client mode');
  1563. var svcNameLen = svcName.length;
  1564. var buf = Buffer.allocUnsafe(1 + 4 + svcNameLen);
  1565. buf[0] = MESSAGE.SERVICE_ACCEPT;
  1566. writeUInt32BE(buf, svcNameLen, 1);
  1567. buf.write(svcName, 5, svcNameLen, 'ascii');
  1568. this.debug('DEBUG: Outgoing: Writing SERVICE_ACCEPT (' + svcName + ')');
  1569. send(this, buf);
  1570. if (this.server && this.banner && svcName === 'ssh-userauth') {
  1571. /*
  1572. byte SSH_MSG_USERAUTH_BANNER
  1573. string message in ISO-10646 UTF-8 encoding
  1574. string language tag
  1575. */
  1576. var bannerLen = Buffer.byteLength(this.banner);
  1577. var packetLen = 1 + 4 + bannerLen + 4;
  1578. var packet = Buffer.allocUnsafe(packetLen);
  1579. packet[0] = MESSAGE.USERAUTH_BANNER;
  1580. writeUInt32BE(packet, bannerLen, 1);
  1581. packet.write(this.banner, 5, bannerLen, 'utf8');
  1582. packet.fill(0, packetLen - 4); // Empty language tag
  1583. this.debug('DEBUG: Outgoing: Writing USERAUTH_BANNER');
  1584. send(this, packet);
  1585. this.banner = undefined; // Prevent banner from being displayed again
  1586. }
  1587. };
  1588. // 'ssh-connection' service-specific
  1589. SSH2Stream.prototype.forwardedTcpip = function(chan, initWindow, maxPacket,
  1590. cfg) {
  1591. if (!this.server)
  1592. throw new Error('Server-only method called in client mode');
  1593. var boundAddrLen = Buffer.byteLength(cfg.boundAddr);
  1594. var remoteAddrLen = Buffer.byteLength(cfg.remoteAddr);
  1595. var p = 36 + boundAddrLen;
  1596. var buf = Buffer.allocUnsafe(1 + 4 + 15 + 4 + 4 + 4 + 4 + boundAddrLen + 4 + 4
  1597. + remoteAddrLen + 4);
  1598. buf[0] = MESSAGE.CHANNEL_OPEN;
  1599. writeUInt32BE(buf, 15, 1);
  1600. buf.write('forwarded-tcpip', 5, 15, 'ascii');
  1601. writeUInt32BE(buf, chan, 20);
  1602. writeUInt32BE(buf, initWindow, 24);
  1603. writeUInt32BE(buf, maxPacket, 28);
  1604. writeUInt32BE(buf, boundAddrLen, 32);
  1605. buf.write(cfg.boundAddr, 36, boundAddrLen, 'ascii');
  1606. writeUInt32BE(buf, cfg.boundPort, p);
  1607. writeUInt32BE(buf, remoteAddrLen, p += 4);
  1608. buf.write(cfg.remoteAddr, p += 4, remoteAddrLen, 'ascii');
  1609. writeUInt32BE(buf, cfg.remotePort, p += remoteAddrLen);
  1610. this.debug('DEBUG: Outgoing: Writing CHANNEL_OPEN ('
  1611. + chan
  1612. + ', forwarded-tcpip)');
  1613. return send(this, buf);
  1614. };
  1615. SSH2Stream.prototype.x11 = function(chan, initWindow, maxPacket, cfg) {
  1616. if (!this.server)
  1617. throw new Error('Server-only method called in client mode');
  1618. var addrLen = Buffer.byteLength(cfg.originAddr);
  1619. var p = 24 + addrLen;
  1620. var buf = Buffer.allocUnsafe(1 + 4 + 3 + 4 + 4 + 4 + 4 + addrLen + 4);
  1621. buf[0] = MESSAGE.CHANNEL_OPEN;
  1622. writeUInt32BE(buf, 3, 1);
  1623. buf.write('x11', 5, 3, 'ascii');
  1624. writeUInt32BE(buf, chan, 8);
  1625. writeUInt32BE(buf, initWindow, 12);
  1626. writeUInt32BE(buf, maxPacket, 16);
  1627. writeUInt32BE(buf, addrLen, 20);
  1628. buf.write(cfg.originAddr, 24, addrLen, 'ascii');
  1629. writeUInt32BE(buf, cfg.originPort, p);
  1630. this.debug('DEBUG: Outgoing: Writing CHANNEL_OPEN ('
  1631. + chan
  1632. + ', x11)');
  1633. return send(this, buf);
  1634. };
  1635. SSH2Stream.prototype.openssh_authAgent = function(chan, initWindow, maxPacket) {
  1636. if (!this.server)
  1637. throw new Error('Server-only method called in client mode');
  1638. var buf = Buffer.allocUnsafe(1 + 4 + 22 + 4 + 4 + 4);
  1639. buf[0] = MESSAGE.CHANNEL_OPEN;
  1640. writeUInt32BE(buf, 22, 1);
  1641. buf.write('auth-agent@openssh.com', 5, 22, 'ascii');
  1642. writeUInt32BE(buf, chan, 27);
  1643. writeUInt32BE(buf, initWindow, 31);
  1644. writeUInt32BE(buf, maxPacket, 35);
  1645. this.debug('DEBUG: Outgoing: Writing CHANNEL_OPEN ('
  1646. + chan
  1647. + ', auth-agent@openssh.com)');
  1648. return send(this, buf);
  1649. };
  1650. SSH2Stream.prototype.openssh_forwardedStreamLocal = function(chan, initWindow,
  1651. maxPacket, cfg) {
  1652. if (!this.server)
  1653. throw new Error('Server-only method called in client mode');
  1654. var pathlen = Buffer.byteLength(cfg.socketPath);
  1655. var buf = Buffer.allocUnsafe(1 + 4 + 33 + 4 + 4 + 4 + 4 + pathlen + 4);
  1656. buf[0] = MESSAGE.CHANNEL_OPEN;
  1657. writeUInt32BE(buf, 33, 1);
  1658. buf.write('forwarded-streamlocal@openssh.com', 5, 33, 'ascii');
  1659. writeUInt32BE(buf, chan, 38);
  1660. writeUInt32BE(buf, initWindow, 42);
  1661. writeUInt32BE(buf, maxPacket, 46);
  1662. writeUInt32BE(buf, pathlen, 50);
  1663. buf.write(cfg.socketPath, 54, pathlen, 'utf8');
  1664. writeUInt32BE(buf, 0, 54 + pathlen);
  1665. this.debug('DEBUG: Outgoing: Writing CHANNEL_OPEN ('
  1666. + chan
  1667. + ', forwarded-streamlocal@openssh.com)');
  1668. return send(this, buf);
  1669. };
  1670. SSH2Stream.prototype.exitStatus = function(chan, status) {
  1671. if (!this.server)
  1672. throw new Error('Server-only method called in client mode');
  1673. // Does not consume window space
  1674. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 11 + 1 + 4);
  1675. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1676. writeUInt32BE(buf, chan, 1);
  1677. writeUInt32BE(buf, 11, 5);
  1678. buf.write('exit-status', 9, 11, 'ascii');
  1679. buf[20] = 0;
  1680. writeUInt32BE(buf, status, 21);
  1681. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1682. + chan
  1683. + ', exit-status)');
  1684. return send(this, buf);
  1685. };
  1686. SSH2Stream.prototype.exitSignal = function(chan, name, coreDumped, msg) {
  1687. if (!this.server)
  1688. throw new Error('Server-only method called in client mode');
  1689. // Does not consume window space
  1690. var nameLen = Buffer.byteLength(name);
  1691. var msgLen = (msg ? Buffer.byteLength(msg) : 0);
  1692. var p = 25 + nameLen;
  1693. var buf = Buffer.allocUnsafe(1 + 4 + 4 + 11 + 1 + 4 + nameLen + 1 + 4 + msgLen
  1694. + 4);
  1695. buf[0] = MESSAGE.CHANNEL_REQUEST;
  1696. writeUInt32BE(buf, chan, 1);
  1697. writeUInt32BE(buf, 11, 5);
  1698. buf.write('exit-signal', 9, 11, 'ascii');
  1699. buf[20] = 0;
  1700. writeUInt32BE(buf, nameLen, 21);
  1701. buf.write(name, 25, nameLen, 'utf8');
  1702. buf[p++] = (coreDumped ? 1 : 0);
  1703. writeUInt32BE(buf, msgLen, p);
  1704. p += 4;
  1705. if (msgLen) {
  1706. buf.write(msg, p, msgLen, 'utf8');
  1707. p += msgLen;
  1708. }
  1709. writeUInt32BE(buf, 0, p);
  1710. this.debug('DEBUG: Outgoing: Writing CHANNEL_REQUEST ('
  1711. + chan
  1712. + ', exit-signal)');
  1713. return send(this, buf);
  1714. };
  1715. // 'ssh-userauth' service-specific
  1716. SSH2Stream.prototype.authFailure = function(authMethods, isPartial) {
  1717. if (!this.server)
  1718. throw new Error('Server-only method called in client mode');
  1719. var authsQueue = this._state.authsQueue;
  1720. if (!authsQueue.length)
  1721. throw new Error('No auth in progress');
  1722. var methods;
  1723. if (typeof authMethods === 'boolean') {
  1724. isPartial = authMethods;
  1725. authMethods = undefined;
  1726. }
  1727. if (authMethods) {
  1728. methods = [];
  1729. for (var i = 0, len = authMethods.length; i < len; ++i) {
  1730. if (authMethods[i].toLowerCase() === 'none')
  1731. continue;
  1732. methods.push(authMethods[i]);
  1733. }
  1734. methods = methods.join(',');
  1735. } else
  1736. methods = '';
  1737. var methodsLen = methods.length;
  1738. var buf = Buffer.allocUnsafe(1 + 4 + methodsLen + 1);
  1739. buf[0] = MESSAGE.USERAUTH_FAILURE;
  1740. writeUInt32BE(buf, methodsLen, 1);
  1741. buf.write(methods, 5, methodsLen, 'ascii');
  1742. buf[5 + methodsLen] = (isPartial === true ? 1 : 0);
  1743. this._state.authsQueue.shift();
  1744. this.debug('DEBUG: Outgoing: Writing USERAUTH_FAILURE');
  1745. return send(this, buf);
  1746. };
  1747. SSH2Stream.prototype.authSuccess = function() {
  1748. if (!this.server)
  1749. throw new Error('Server-only method called in client mode');
  1750. var authsQueue = this._state.authsQueue;
  1751. if (!authsQueue.length)
  1752. throw new Error('No auth in progress');
  1753. var state = this._state;
  1754. var outstate = state.outgoing;
  1755. var instate = state.incoming;
  1756. state.authsQueue.shift();
  1757. this.debug('DEBUG: Outgoing: Writing USERAUTH_SUCCESS');
  1758. var ret = send(this, USERAUTH_SUCCESS_PACKET);
  1759. if (outstate.compress.type === 'zlib@openssh.com') {
  1760. outstate.compress.instance = zlib.createDeflate(ZLIB_OPTS);
  1761. outstate.compress.queue = [];
  1762. }
  1763. if (instate.decompress.type === 'zlib@openssh.com')
  1764. instate.decompress.instance = zlib.createInflate(ZLIB_OPTS);
  1765. return ret;
  1766. };
  1767. SSH2Stream.prototype.authPKOK = function(keyAlgo, key) {
  1768. if (!this.server)
  1769. throw new Error('Server-only method called in client mode');
  1770. var authsQueue = this._state.authsQueue;
  1771. if (!authsQueue.length || authsQueue[0] !== 'publickey')
  1772. throw new Error('"publickey" auth not in progress');
  1773. var keyAlgoLen = keyAlgo.length;
  1774. var keyLen = key.length;
  1775. var buf = Buffer.allocUnsafe(1 + 4 + keyAlgoLen + 4 + keyLen);
  1776. buf[0] = MESSAGE.USERAUTH_PK_OK;
  1777. writeUInt32BE(buf, keyAlgoLen, 1);
  1778. buf.write(keyAlgo, 5, keyAlgoLen, 'ascii');
  1779. writeUInt32BE(buf, keyLen, 5 + keyAlgoLen);
  1780. key.copy(buf, 5 + keyAlgoLen + 4);
  1781. this._state.authsQueue.shift();
  1782. this.debug('DEBUG: Outgoing: Writing USERAUTH_PK_OK');
  1783. return send(this, buf);
  1784. };
  1785. SSH2Stream.prototype.authPasswdChg = function(prompt, lang) {
  1786. if (!this.server)
  1787. throw new Error('Server-only method called in client mode');
  1788. var promptLen = Buffer.byteLength(prompt);
  1789. var langLen = lang ? lang.length : 0;
  1790. var p = 0;
  1791. var buf = Buffer.allocUnsafe(1 + 4 + promptLen + 4 + langLen);
  1792. buf[p] = MESSAGE.USERAUTH_PASSWD_CHANGEREQ;
  1793. writeUInt32BE(buf, promptLen, ++p);
  1794. buf.write(prompt, p += 4, promptLen, 'utf8');
  1795. writeUInt32BE(buf, langLen, p += promptLen);
  1796. if (langLen)
  1797. buf.write(lang, p += 4, langLen, 'ascii');
  1798. this.debug('DEBUG: Outgoing: Writing USERAUTH_PASSWD_CHANGEREQ');
  1799. return send(this, buf);
  1800. };
  1801. SSH2Stream.prototype.authInfoReq = function(name, instructions, prompts) {
  1802. if (!this.server)
  1803. throw new Error('Server-only method called in client mode');
  1804. var promptsLen = 0;
  1805. var nameLen = name ? Buffer.byteLength(name) : 0;
  1806. var instrLen = instructions ? Buffer.byteLength(instructions) : 0;
  1807. var p = 0;
  1808. var promptLen;
  1809. var prompt;
  1810. var len;
  1811. var i;
  1812. for (i = 0, len = prompts.length; i < len; ++i)
  1813. promptsLen += 4 + Buffer.byteLength(prompts[i].prompt) + 1;
  1814. var buf = Buffer.allocUnsafe(1 + 4 + nameLen + 4 + instrLen + 4 + 4
  1815. + promptsLen);
  1816. buf[p++] = MESSAGE.USERAUTH_INFO_REQUEST;
  1817. writeUInt32BE(buf, nameLen, p);
  1818. p += 4;
  1819. if (name) {
  1820. buf.write(name, p, nameLen, 'utf8');
  1821. p += nameLen;
  1822. }
  1823. writeUInt32BE(buf, instrLen, p);
  1824. p += 4;
  1825. if (instructions) {
  1826. buf.write(instructions, p, instrLen, 'utf8');
  1827. p += instrLen;
  1828. }
  1829. writeUInt32BE(buf, 0, p);
  1830. p += 4;
  1831. writeUInt32BE(buf, prompts.length, p);
  1832. p += 4;
  1833. for (i = 0, len = prompts.length; i < len; ++i) {
  1834. prompt = prompts[i];
  1835. promptLen = Buffer.byteLength(prompt.prompt);
  1836. writeUInt32BE(buf, promptLen, p);
  1837. p += 4;
  1838. if (promptLen) {
  1839. buf.write(prompt.prompt, p, promptLen, 'utf8');
  1840. p += promptLen;
  1841. }
  1842. buf[p++] = (prompt.echo ? 1 : 0);
  1843. }
  1844. this.debug('DEBUG: Outgoing: Writing USERAUTH_INFO_REQUEST');
  1845. return send(this, buf);
  1846. };
  1847. // Shared incoming/parser functions
  1848. function onDISCONNECT(self, reason, code, desc, lang) { // Client/Server
  1849. if (code !== DISCONNECT_REASON.BY_APPLICATION) {
  1850. var err = new Error(desc || reason);
  1851. err.code = code;
  1852. self.emit('error', err);
  1853. }
  1854. self.reset();
  1855. }
  1856. function onKEXINIT(self, init, firstFollows) { // Client/Server
  1857. var state = self._state;
  1858. var outstate = state.outgoing;
  1859. if (outstate.status === OUT_READY) {
  1860. self.debug('DEBUG: Received re-key request');
  1861. outstate.status = OUT_REKEYING;
  1862. outstate.kexinit = undefined;
  1863. KEXINIT(self, check);
  1864. } else
  1865. check();
  1866. function check() {
  1867. if (check_KEXINIT(self, init, firstFollows) === true) {
  1868. var isGEX = RE_GEX.test(state.kexdh);
  1869. if (!self.server) {
  1870. if (isGEX)
  1871. KEXDH_GEX_REQ(self);
  1872. else
  1873. KEXDH_INIT(self);
  1874. } else {
  1875. if (isGEX)
  1876. state.incoming.expectedPacket = 'KEXDH_GEX_REQ';
  1877. else
  1878. state.incoming.expectedPacket = 'KEXDH_INIT';
  1879. }
  1880. }
  1881. }
  1882. }
  1883. function check_KEXINIT(self, init, firstFollows) {
  1884. var state = self._state;
  1885. var instate = state.incoming;
  1886. var outstate = state.outgoing;
  1887. var debug = self.debug;
  1888. var serverList;
  1889. var clientList;
  1890. var val;
  1891. var len;
  1892. var i;
  1893. debug('DEBUG: Comparing KEXINITs ...');
  1894. var algos = self.config.algorithms;
  1895. var kexList = algos.kex;
  1896. if (self.remoteBugs & BUGS.BAD_DHGEX) {
  1897. var copied = false;
  1898. for (var j = kexList.length - 1; j >= 0; --j) {
  1899. if (kexList[j].indexOf('group-exchange') !== -1) {
  1900. if (!copied) {
  1901. kexList = kexList.slice();
  1902. copied = true;
  1903. }
  1904. kexList.splice(j, 1);
  1905. }
  1906. }
  1907. }
  1908. debug('DEBUG: (local) KEX algorithms: ' + kexList);
  1909. debug('DEBUG: (remote) KEX algorithms: ' + init.algorithms.kex);
  1910. if (self.server) {
  1911. serverList = kexList;
  1912. clientList = init.algorithms.kex;
  1913. } else {
  1914. serverList = init.algorithms.kex;
  1915. clientList = kexList;
  1916. }
  1917. // Check for agreeable key exchange algorithm
  1918. for (i = 0, len = clientList.length;
  1919. i < len && serverList.indexOf(clientList[i]) === -1;
  1920. ++i);
  1921. if (i === len) {
  1922. // No suitable match found!
  1923. debug('DEBUG: No matching key exchange algorithm');
  1924. var err = new Error('Handshake failed: no matching key exchange algorithm');
  1925. err.level = 'handshake';
  1926. self.emit('error', err);
  1927. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  1928. return false;
  1929. }
  1930. var kex_algorithm = clientList[i];
  1931. debug('DEBUG: KEX algorithm: ' + kex_algorithm);
  1932. if (firstFollows
  1933. && (!init.algorithms.kex.length
  1934. || kex_algorithm !== init.algorithms.kex[0])) {
  1935. // Ignore next incoming packet, it was a wrong first guess at KEX algorithm
  1936. instate.ignoreNext = true;
  1937. }
  1938. debug('DEBUG: (local) Host key formats: ' + algos.serverHostKey);
  1939. debug('DEBUG: (remote) Host key formats: ' + init.algorithms.srvHostKey);
  1940. if (self.server) {
  1941. serverList = algos.serverHostKey;
  1942. clientList = init.algorithms.srvHostKey;
  1943. } else {
  1944. serverList = init.algorithms.srvHostKey;
  1945. clientList = algos.serverHostKey;
  1946. }
  1947. // Check for agreeable server host key format
  1948. for (i = 0, len = clientList.length;
  1949. i < len && serverList.indexOf(clientList[i]) === -1;
  1950. ++i);
  1951. if (i === len) {
  1952. // No suitable match found!
  1953. debug('DEBUG: No matching host key format');
  1954. var err = new Error('Handshake failed: no matching host key format');
  1955. err.level = 'handshake';
  1956. self.emit('error', err);
  1957. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  1958. return false;
  1959. }
  1960. state.hostkeyFormat = clientList[i];
  1961. debug('DEBUG: Host key format: ' + state.hostkeyFormat);
  1962. debug('DEBUG: (local) Client->Server ciphers: ' + algos.cipher);
  1963. debug('DEBUG: (remote) Client->Server ciphers: '
  1964. + init.algorithms.cs.encrypt);
  1965. if (self.server) {
  1966. serverList = algos.cipher;
  1967. clientList = init.algorithms.cs.encrypt;
  1968. } else {
  1969. serverList = init.algorithms.cs.encrypt;
  1970. clientList = algos.cipher;
  1971. }
  1972. // Check for agreeable client->server cipher
  1973. for (i = 0, len = clientList.length;
  1974. i < len && serverList.indexOf(clientList[i]) === -1;
  1975. ++i);
  1976. if (i === len) {
  1977. // No suitable match found!
  1978. debug('DEBUG: No matching Client->Server cipher');
  1979. var err = new Error('Handshake failed: no matching client->server cipher');
  1980. err.level = 'handshake';
  1981. self.emit('error', err);
  1982. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  1983. return false;
  1984. }
  1985. if (self.server)
  1986. val = instate.decrypt.type = clientList[i];
  1987. else
  1988. val = outstate.encrypt.type = clientList[i];
  1989. debug('DEBUG: Client->Server Cipher: ' + val);
  1990. debug('DEBUG: (local) Server->Client ciphers: ' + algos.cipher);
  1991. debug('DEBUG: (remote) Server->Client ciphers: '
  1992. + (init.algorithms.sc.encrypt));
  1993. if (self.server) {
  1994. serverList = algos.cipher;
  1995. clientList = init.algorithms.sc.encrypt;
  1996. } else {
  1997. serverList = init.algorithms.sc.encrypt;
  1998. clientList = algos.cipher;
  1999. }
  2000. // Check for agreeable server->client cipher
  2001. for (i = 0, len = clientList.length;
  2002. i < len && serverList.indexOf(clientList[i]) === -1;
  2003. ++i);
  2004. if (i === len) {
  2005. // No suitable match found!
  2006. debug('DEBUG: No matching Server->Client cipher');
  2007. var err = new Error('Handshake failed: no matching server->client cipher');
  2008. err.level = 'handshake';
  2009. self.emit('error', err);
  2010. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  2011. return false;
  2012. }
  2013. if (self.server)
  2014. val = outstate.encrypt.type = clientList[i];
  2015. else
  2016. val = instate.decrypt.type = clientList[i];
  2017. debug('DEBUG: Server->Client Cipher: ' + val);
  2018. debug('DEBUG: (local) Client->Server HMAC algorithms: ' + algos.hmac);
  2019. debug('DEBUG: (remote) Client->Server HMAC algorithms: '
  2020. + init.algorithms.cs.mac);
  2021. if (self.server) {
  2022. serverList = algos.hmac;
  2023. clientList = init.algorithms.cs.mac;
  2024. } else {
  2025. serverList = init.algorithms.cs.mac;
  2026. clientList = algos.hmac;
  2027. }
  2028. // Check for agreeable client->server hmac algorithm
  2029. for (i = 0, len = clientList.length;
  2030. i < len && serverList.indexOf(clientList[i]) === -1;
  2031. ++i);
  2032. if (i === len) {
  2033. // No suitable match found!
  2034. debug('DEBUG: No matching Client->Server HMAC algorithm');
  2035. var err = new Error('Handshake failed: no matching client->server HMAC');
  2036. err.level = 'handshake';
  2037. self.emit('error', err);
  2038. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  2039. return false;
  2040. }
  2041. if (self.server)
  2042. val = instate.hmac.type = clientList[i];
  2043. else
  2044. val = outstate.hmac.type = clientList[i];
  2045. debug('DEBUG: Client->Server HMAC algorithm: ' + val);
  2046. debug('DEBUG: (local) Server->Client HMAC algorithms: ' + algos.hmac);
  2047. debug('DEBUG: (remote) Server->Client HMAC algorithms: '
  2048. + init.algorithms.sc.mac);
  2049. if (self.server) {
  2050. serverList = algos.hmac;
  2051. clientList = init.algorithms.sc.mac;
  2052. } else {
  2053. serverList = init.algorithms.sc.mac;
  2054. clientList = algos.hmac;
  2055. }
  2056. // Check for agreeable server->client hmac algorithm
  2057. for (i = 0, len = clientList.length;
  2058. i < len && serverList.indexOf(clientList[i]) === -1;
  2059. ++i);
  2060. if (i === len) {
  2061. // No suitable match found!
  2062. debug('DEBUG: No matching Server->Client HMAC algorithm');
  2063. var err = new Error('Handshake failed: no matching server->client HMAC');
  2064. err.level = 'handshake';
  2065. self.emit('error', err);
  2066. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  2067. return false;
  2068. }
  2069. if (self.server)
  2070. val = outstate.hmac.type = clientList[i];
  2071. else
  2072. val = instate.hmac.type = clientList[i];
  2073. debug('DEBUG: Server->Client HMAC algorithm: ' + val);
  2074. debug('DEBUG: (local) Client->Server compression algorithms: '
  2075. + algos.compress);
  2076. debug('DEBUG: (remote) Client->Server compression algorithms: '
  2077. + init.algorithms.cs.compress);
  2078. if (self.server) {
  2079. serverList = algos.compress;
  2080. clientList = init.algorithms.cs.compress;
  2081. } else {
  2082. serverList = init.algorithms.cs.compress;
  2083. clientList = algos.compress;
  2084. }
  2085. // Check for agreeable client->server compression algorithm
  2086. for (i = 0, len = clientList.length;
  2087. i < len && serverList.indexOf(clientList[i]) === -1;
  2088. ++i);
  2089. if (i === len) {
  2090. // No suitable match found!
  2091. debug('DEBUG: No matching Client->Server compression algorithm');
  2092. var err = new Error('Handshake failed: no matching client->server '
  2093. + 'compression algorithm');
  2094. err.level = 'handshake';
  2095. self.emit('error', err);
  2096. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  2097. return false;
  2098. }
  2099. if (self.server)
  2100. val = instate.decompress.type = clientList[i];
  2101. else
  2102. val = outstate.compress.type = clientList[i];
  2103. debug('DEBUG: Client->Server compression algorithm: ' + val);
  2104. debug('DEBUG: (local) Server->Client compression algorithms: '
  2105. + algos.compress);
  2106. debug('DEBUG: (remote) Server->Client compression algorithms: '
  2107. + init.algorithms.sc.compress);
  2108. if (self.server) {
  2109. serverList = algos.compress;
  2110. clientList = init.algorithms.sc.compress;
  2111. } else {
  2112. serverList = init.algorithms.sc.compress;
  2113. clientList = algos.compress;
  2114. }
  2115. // Check for agreeable server->client compression algorithm
  2116. for (i = 0, len = clientList.length;
  2117. i < len && serverList.indexOf(clientList[i]) === -1;
  2118. ++i);
  2119. if (i === len) {
  2120. // No suitable match found!
  2121. debug('DEBUG: No matching Server->Client compression algorithm');
  2122. var err = new Error('Handshake failed: no matching server->client '
  2123. + 'compression algorithm');
  2124. err.level = 'handshake';
  2125. self.emit('error', err);
  2126. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  2127. return false;
  2128. }
  2129. if (self.server)
  2130. val = outstate.compress.type = clientList[i];
  2131. else
  2132. val = instate.decompress.type = clientList[i];
  2133. debug('DEBUG: Server->Client compression algorithm: ' + val);
  2134. switch (kex_algorithm) {
  2135. case 'diffie-hellman-group1-sha1':
  2136. state.kexdh = 'group';
  2137. state.kex = crypto.getDiffieHellman('modp2');
  2138. break;
  2139. case 'diffie-hellman-group14-sha1':
  2140. state.kexdh = 'group';
  2141. state.kex = crypto.getDiffieHellman('modp14');
  2142. break;
  2143. case 'ecdh-sha2-nistp256':
  2144. state.kexdh = 'ec-sha256';
  2145. state.kex = crypto.createECDH(SSH_TO_OPENSSL[kex_algorithm]);
  2146. break;
  2147. case 'ecdh-sha2-nistp384':
  2148. state.kexdh = 'ec-sha384';
  2149. state.kex = crypto.createECDH(SSH_TO_OPENSSL[kex_algorithm]);
  2150. break;
  2151. case 'ecdh-sha2-nistp521':
  2152. state.kexdh = 'ec-sha512';
  2153. state.kex = crypto.createECDH(SSH_TO_OPENSSL[kex_algorithm]);
  2154. break;
  2155. default:
  2156. if (kex_algorithm === 'diffie-hellman-group-exchange-sha1')
  2157. state.kexdh = 'gex-sha1';
  2158. else if (kex_algorithm === 'diffie-hellman-group-exchange-sha256')
  2159. state.kexdh = 'gex-sha256';
  2160. // Reset kex object if DH group exchange is selected on re-key and DH
  2161. // group exchange was used before the re-key. This ensures that we send
  2162. // the right DH packet after the KEXINIT exchange
  2163. state.kex = undefined;
  2164. }
  2165. if (state.kex) {
  2166. outstate.pubkey = state.kex.generateKeys();
  2167. var idx = 0;
  2168. len = outstate.pubkey.length;
  2169. while (outstate.pubkey[idx] === 0x00) {
  2170. ++idx;
  2171. --len;
  2172. }
  2173. if (outstate.pubkey[idx] & 0x80) {
  2174. var key = Buffer.allocUnsafe(len + 1);
  2175. key[0] = 0;
  2176. outstate.pubkey.copy(key, 1, idx);
  2177. outstate.pubkey = key;
  2178. }
  2179. }
  2180. return true;
  2181. }
  2182. function onKEXDH_GEX_GROUP(self, prime, gen) {
  2183. var state = self._state;
  2184. var outstate = state.outgoing;
  2185. state.kex = crypto.createDiffieHellman(prime, gen);
  2186. outstate.pubkey = state.kex.generateKeys();
  2187. var idx = 0;
  2188. var len = outstate.pubkey.length;
  2189. while (outstate.pubkey[idx] === 0x00) {
  2190. ++idx;
  2191. --len;
  2192. }
  2193. if (outstate.pubkey[idx] & 0x80) {
  2194. var key = Buffer.allocUnsafe(len + 1);
  2195. key[0] = 0;
  2196. outstate.pubkey.copy(key, 1, idx);
  2197. outstate.pubkey = key;
  2198. }
  2199. KEXDH_INIT(self);
  2200. }
  2201. function onKEXDH_INIT(self, e) { // Server
  2202. KEXDH_REPLY(self, e);
  2203. }
  2204. function onKEXDH_REPLY(self, info, verifiedHost) { // Client
  2205. var state = self._state;
  2206. var instate = state.incoming;
  2207. var outstate = state.outgoing;
  2208. var debug = self.debug;
  2209. var len;
  2210. var i;
  2211. if (verifiedHost === undefined) {
  2212. instate.expectedPacket = 'NEWKEYS';
  2213. outstate.sentNEWKEYS = false;
  2214. debug('DEBUG: Checking host key format');
  2215. // Ensure all host key formats agree
  2216. var hostkey_format = readString(info.hostkey, 0, 'ascii', self);
  2217. if (hostkey_format === false)
  2218. return false;
  2219. if (info.hostkey_format !== state.hostkeyFormat
  2220. || info.hostkey_format !== hostkey_format) {
  2221. // Expected and actual server host key format do not match!
  2222. debug('DEBUG: Host key format mismatch');
  2223. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  2224. self.reset();
  2225. var err = new Error('Handshake failed: host key format mismatch');
  2226. err.level = 'handshake';
  2227. self.emit('error', err);
  2228. return false;
  2229. }
  2230. debug('DEBUG: Checking signature format');
  2231. // Ensure signature formats agree
  2232. var sig_format = readString(info.sig, 0, 'ascii', self);
  2233. if (sig_format === false)
  2234. return false;
  2235. if (info.sig_format !== sig_format) {
  2236. debug('DEBUG: Signature format mismatch');
  2237. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  2238. self.reset();
  2239. var err = new Error('Handshake failed: signature format mismatch');
  2240. err.level = 'handshake';
  2241. self.emit('error', err);
  2242. return false;
  2243. }
  2244. }
  2245. // Verify the host fingerprint first if needed
  2246. if (outstate.status === OUT_INIT) {
  2247. if (verifiedHost === undefined) {
  2248. debug('DEBUG: Verifying host fingerprint');
  2249. var sync = true;
  2250. var emitted = self.emit('fingerprint', info.hostkey, function(permitted) {
  2251. // Prevent multiple calls to this callback
  2252. if (verifiedHost !== undefined)
  2253. return;
  2254. verifiedHost = !!permitted;
  2255. if (!sync) {
  2256. // Continue execution by re-entry
  2257. onKEXDH_REPLY(self, info, verifiedHost);
  2258. }
  2259. });
  2260. sync = false;
  2261. // Support async calling of verification callback
  2262. if (emitted && verifiedHost === undefined)
  2263. return;
  2264. }
  2265. if (verifiedHost === undefined)
  2266. debug('DEBUG: Host accepted by default (no verification)');
  2267. else if (verifiedHost === true)
  2268. debug('DEBUG: Host accepted (verified)');
  2269. else {
  2270. debug('DEBUG: Host denied via fingerprint verification');
  2271. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  2272. self.reset();
  2273. var err = new Error('Handshake failed: '
  2274. + 'host fingerprint verification failed');
  2275. err.level = 'handshake';
  2276. self.emit('error', err);
  2277. return false;
  2278. }
  2279. }
  2280. var slicepos = -1;
  2281. for (i = 0, len = info.pubkey.length; i < len; ++i) {
  2282. if (info.pubkey[i] === 0)
  2283. ++slicepos;
  2284. else
  2285. break;
  2286. }
  2287. if (slicepos > -1)
  2288. info.pubkey = info.pubkey.slice(slicepos + 1);
  2289. info.secret = tryComputeSecret(state.kex, info.pubkey);
  2290. if (info.secret instanceof Error) {
  2291. info.secret.message = 'Error while computing DH secret ('
  2292. + state.kexdh + '): '
  2293. + info.secret.message;
  2294. info.secret.level = 'handshake';
  2295. self.emit('error', info.secret);
  2296. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  2297. return false;
  2298. }
  2299. var hashAlgo;
  2300. if (state.kexdh === 'group')
  2301. hashAlgo = 'sha1';
  2302. else
  2303. hashAlgo = RE_KEX_HASH.exec(state.kexdh)[1];
  2304. var hash = crypto.createHash(hashAlgo);
  2305. var len_ident = Buffer.byteLength(self.config.ident);
  2306. var len_sident = Buffer.byteLength(instate.identRaw);
  2307. var len_init = outstate.kexinit.length;
  2308. var len_sinit = instate.kexinit.length;
  2309. var len_hostkey = info.hostkey.length;
  2310. var len_pubkey = outstate.pubkey.length;
  2311. var len_spubkey = info.pubkey.length;
  2312. var len_secret = info.secret.length;
  2313. var idx_pubkey = 0;
  2314. var idx_spubkey = 0;
  2315. var idx_secret = 0;
  2316. while (outstate.pubkey[idx_pubkey] === 0x00) {
  2317. ++idx_pubkey;
  2318. --len_pubkey;
  2319. }
  2320. while (info.pubkey[idx_spubkey] === 0x00) {
  2321. ++idx_spubkey;
  2322. --len_spubkey;
  2323. }
  2324. while (info.secret[idx_secret] === 0x00) {
  2325. ++idx_secret;
  2326. --len_secret;
  2327. }
  2328. if (outstate.pubkey[idx_pubkey] & 0x80)
  2329. ++len_pubkey;
  2330. if (info.pubkey[idx_spubkey] & 0x80)
  2331. ++len_spubkey;
  2332. if (info.secret[idx_secret] & 0x80)
  2333. ++len_secret;
  2334. var exchangeBufLen = len_ident
  2335. + len_sident
  2336. + len_init
  2337. + len_sinit
  2338. + len_hostkey
  2339. + len_pubkey
  2340. + len_spubkey
  2341. + len_secret
  2342. + (4 * 8); // Length fields for above values
  2343. // Group exchange-related
  2344. var isGEX = RE_GEX.test(state.kexdh);
  2345. var len_gex_prime = 0;
  2346. var len_gex_gen = 0;
  2347. var idx_gex_prime = 0;
  2348. var idx_gex_gen = 0;
  2349. var gex_prime;
  2350. var gex_gen;
  2351. if (isGEX) {
  2352. gex_prime = state.kex.getPrime();
  2353. gex_gen = state.kex.getGenerator();
  2354. len_gex_prime = gex_prime.length;
  2355. len_gex_gen = gex_gen.length;
  2356. while (gex_prime[idx_gex_prime] === 0x00) {
  2357. ++idx_gex_prime;
  2358. --len_gex_prime;
  2359. }
  2360. while (gex_gen[idx_gex_gen] === 0x00) {
  2361. ++idx_gex_gen;
  2362. --len_gex_gen;
  2363. }
  2364. if (gex_prime[idx_gex_prime] & 0x80)
  2365. ++len_gex_prime;
  2366. if (gex_gen[idx_gex_gen] & 0x80)
  2367. ++len_gex_gen;
  2368. exchangeBufLen += (4 * 3); // min, n, max values
  2369. exchangeBufLen += (4 * 2); // prime, generator length fields
  2370. exchangeBufLen += len_gex_prime;
  2371. exchangeBufLen += len_gex_gen;
  2372. }
  2373. var bp = 0;
  2374. var exchangeBuf = Buffer.allocUnsafe(exchangeBufLen);
  2375. writeUInt32BE(exchangeBuf, len_ident, bp);
  2376. bp += 4;
  2377. exchangeBuf.write(self.config.ident, bp, 'utf8'); // V_C
  2378. bp += len_ident;
  2379. writeUInt32BE(exchangeBuf, len_sident, bp);
  2380. bp += 4;
  2381. exchangeBuf.write(instate.identRaw, bp, 'utf8'); // V_S
  2382. bp += len_sident;
  2383. writeUInt32BE(exchangeBuf, len_init, bp);
  2384. bp += 4;
  2385. outstate.kexinit.copy(exchangeBuf, bp); // I_C
  2386. bp += len_init;
  2387. outstate.kexinit = undefined;
  2388. writeUInt32BE(exchangeBuf, len_sinit, bp);
  2389. bp += 4;
  2390. instate.kexinit.copy(exchangeBuf, bp); // I_S
  2391. bp += len_sinit;
  2392. instate.kexinit = undefined;
  2393. writeUInt32BE(exchangeBuf, len_hostkey, bp);
  2394. bp += 4;
  2395. info.hostkey.copy(exchangeBuf, bp); // K_S
  2396. bp += len_hostkey;
  2397. if (isGEX) {
  2398. KEXDH_GEX_REQ_PACKET.slice(1).copy(exchangeBuf, bp); // min, n, max
  2399. bp += (4 * 3); // Skip over bytes just copied
  2400. writeUInt32BE(exchangeBuf, len_gex_prime, bp);
  2401. bp += 4;
  2402. if (gex_prime[idx_gex_prime] & 0x80)
  2403. exchangeBuf[bp++] = 0;
  2404. gex_prime.copy(exchangeBuf, bp, idx_gex_prime); // p
  2405. bp += len_gex_prime - (gex_prime[idx_gex_prime] & 0x80 ? 1 : 0);
  2406. writeUInt32BE(exchangeBuf, len_gex_gen, bp);
  2407. bp += 4;
  2408. if (gex_gen[idx_gex_gen] & 0x80)
  2409. exchangeBuf[bp++] = 0;
  2410. gex_gen.copy(exchangeBuf, bp, idx_gex_gen); // g
  2411. bp += len_gex_gen - (gex_gen[idx_gex_gen] & 0x80 ? 1 : 0);
  2412. }
  2413. writeUInt32BE(exchangeBuf, len_pubkey, bp);
  2414. bp += 4;
  2415. if (outstate.pubkey[idx_pubkey] & 0x80)
  2416. exchangeBuf[bp++] = 0;
  2417. outstate.pubkey.copy(exchangeBuf, bp, idx_pubkey); // e
  2418. bp += len_pubkey - (outstate.pubkey[idx_pubkey] & 0x80 ? 1 : 0);
  2419. writeUInt32BE(exchangeBuf, len_spubkey, bp);
  2420. bp += 4;
  2421. if (info.pubkey[idx_spubkey] & 0x80)
  2422. exchangeBuf[bp++] = 0;
  2423. info.pubkey.copy(exchangeBuf, bp, idx_spubkey); // f
  2424. bp += len_spubkey - (info.pubkey[idx_spubkey] & 0x80 ? 1 : 0);
  2425. writeUInt32BE(exchangeBuf, len_secret, bp);
  2426. bp += 4;
  2427. if (info.secret[idx_secret] & 0x80)
  2428. exchangeBuf[bp++] = 0;
  2429. info.secret.copy(exchangeBuf, bp, idx_secret); // K
  2430. outstate.exchangeHash = hash.update(exchangeBuf).digest(); // H
  2431. var rawsig = readString(info.sig, info.sig._pos, self); // s
  2432. if (rawsig === false
  2433. || !(rawsig = sigSSHToASN1(rawsig, info.sig_format, self))) {
  2434. return false;
  2435. }
  2436. var hostPubKey = parseDERKey(info.hostkey, info.sig_format);
  2437. if (hostPubKey instanceof Error)
  2438. return false;
  2439. debug('DEBUG: Verifying signature');
  2440. if (!hostPubKey.verify(outstate.exchangeHash, rawsig)) {
  2441. debug('DEBUG: Signature verification failed');
  2442. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  2443. self.reset();
  2444. var err = new Error('Handshake failed: signature verification failed');
  2445. err.level = 'handshake';
  2446. self.emit('error', err);
  2447. return false;
  2448. }
  2449. if (outstate.sessionId === undefined)
  2450. outstate.sessionId = outstate.exchangeHash;
  2451. outstate.kexsecret = info.secret;
  2452. debug('DEBUG: Outgoing: Writing NEWKEYS');
  2453. if (outstate.status === OUT_REKEYING)
  2454. send(self, NEWKEYS_PACKET, undefined, true);
  2455. else
  2456. send(self, NEWKEYS_PACKET);
  2457. outstate.sentNEWKEYS = true;
  2458. if (verifiedHost !== undefined && instate.expectedPacket === undefined) {
  2459. // We received NEWKEYS while we were waiting for the fingerprint
  2460. // verification callback to be called. In this case we have to re-execute
  2461. // onNEWKEYS to finish the handshake.
  2462. onNEWKEYS(self);
  2463. }
  2464. }
  2465. function onNEWKEYS(self) { // Client/Server
  2466. var state = self._state;
  2467. var outstate = state.outgoing;
  2468. var instate = state.incoming;
  2469. instate.expectedPacket = undefined;
  2470. if (!outstate.sentNEWKEYS)
  2471. return;
  2472. var idx_secret = 0;
  2473. var len = outstate.kexsecret.length;
  2474. while (outstate.kexsecret[idx_secret] === 0x00) {
  2475. ++idx_secret;
  2476. --len;
  2477. }
  2478. var outCipherInfo = outstate.encrypt.info = CIPHER_INFO[outstate.encrypt.type];
  2479. var p = 0;
  2480. var dhHashAlgo;
  2481. if (state.kexdh === 'group')
  2482. dhHashAlgo = 'sha1';
  2483. else
  2484. dhHashAlgo = RE_KEX_HASH.exec(state.kexdh)[1];
  2485. var len_secret = (outstate.kexsecret[idx_secret] & 0x80 ? 1 : 0) + len;
  2486. var secret = Buffer.allocUnsafe(4 + len_secret);
  2487. var iv;
  2488. var key;
  2489. // Whenever the client sends a new authentication request, it is enqueued
  2490. // here. Once the request is resolved (success, fail, or PK_OK),
  2491. // dequeue. Whatever is at the front of the queue determines how we
  2492. // interpret packet type 60.
  2493. state.authsQueue = [];
  2494. writeUInt32BE(secret, len_secret, p);
  2495. p += 4;
  2496. if (outstate.kexsecret[idx_secret] & 0x80)
  2497. secret[p++] = 0;
  2498. outstate.kexsecret.copy(secret, p, idx_secret);
  2499. outstate.kexsecret = undefined;
  2500. if (!outCipherInfo.stream) {
  2501. iv = crypto.createHash(dhHashAlgo)
  2502. .update(secret)
  2503. .update(outstate.exchangeHash)
  2504. .update(!self.server ? 'A' : 'B', 'ascii')
  2505. .update(outstate.sessionId)
  2506. .digest();
  2507. while (iv.length < outCipherInfo.ivLen) {
  2508. iv = Buffer.concat([iv,
  2509. crypto.createHash(dhHashAlgo)
  2510. .update(secret)
  2511. .update(outstate.exchangeHash)
  2512. .update(iv)
  2513. .digest()]);
  2514. }
  2515. if (iv.length > outCipherInfo.ivLen)
  2516. iv = iv.slice(0, outCipherInfo.ivLen);
  2517. } else {
  2518. iv = EMPTY_BUFFER; // Streaming ciphers don't use an IV upfront
  2519. }
  2520. key = crypto.createHash(dhHashAlgo)
  2521. .update(secret)
  2522. .update(outstate.exchangeHash)
  2523. .update(!self.server ? 'C' : 'D', 'ascii')
  2524. .update(outstate.sessionId)
  2525. .digest();
  2526. while (key.length < outCipherInfo.keyLen) {
  2527. key = Buffer.concat([key,
  2528. crypto.createHash(dhHashAlgo)
  2529. .update(secret)
  2530. .update(outstate.exchangeHash)
  2531. .update(key)
  2532. .digest()]);
  2533. }
  2534. if (key.length > outCipherInfo.keyLen)
  2535. key = key.slice(0, outCipherInfo.keyLen);
  2536. if (outCipherInfo.authLen > 0) {
  2537. outstate.encrypt.iv = iv;
  2538. outstate.encrypt.key = key;
  2539. outstate.encrypt.instance = true;
  2540. } else {
  2541. var cipherAlgo = SSH_TO_OPENSSL[outstate.encrypt.type];
  2542. outstate.encrypt.instance = crypto.createCipheriv(cipherAlgo, key, iv);
  2543. outstate.encrypt.instance.setAutoPadding(false);
  2544. }
  2545. // And now for decrypting ...
  2546. var inCipherInfo = instate.decrypt.info = CIPHER_INFO[instate.decrypt.type];
  2547. if (!inCipherInfo.stream) {
  2548. iv = crypto.createHash(dhHashAlgo)
  2549. .update(secret)
  2550. .update(outstate.exchangeHash)
  2551. .update(!self.server ? 'B' : 'A', 'ascii')
  2552. .update(outstate.sessionId)
  2553. .digest();
  2554. while (iv.length < inCipherInfo.ivLen) {
  2555. iv = Buffer.concat([iv,
  2556. crypto.createHash(dhHashAlgo)
  2557. .update(secret)
  2558. .update(outstate.exchangeHash)
  2559. .update(iv)
  2560. .digest()]);
  2561. }
  2562. if (iv.length > inCipherInfo.ivLen)
  2563. iv = iv.slice(0, inCipherInfo.ivLen);
  2564. } else {
  2565. iv = EMPTY_BUFFER; // Streaming ciphers don't use an IV upfront
  2566. }
  2567. // Create a reusable buffer for decryption purposes
  2568. instate.decrypt.buf = Buffer.allocUnsafe(inCipherInfo.blockLen);
  2569. key = crypto.createHash(dhHashAlgo)
  2570. .update(secret)
  2571. .update(outstate.exchangeHash)
  2572. .update(!self.server ? 'D' : 'C', 'ascii')
  2573. .update(outstate.sessionId)
  2574. .digest();
  2575. while (key.length < inCipherInfo.keyLen) {
  2576. key = Buffer.concat([key,
  2577. crypto.createHash(dhHashAlgo)
  2578. .update(secret)
  2579. .update(outstate.exchangeHash)
  2580. .update(key)
  2581. .digest()]);
  2582. }
  2583. if (key.length > inCipherInfo.keyLen)
  2584. key = key.slice(0, inCipherInfo.keyLen);
  2585. var decipherAlgo = SSH_TO_OPENSSL[instate.decrypt.type];
  2586. instate.decrypt.instance = crypto.createDecipheriv(decipherAlgo, key, iv);
  2587. instate.decrypt.instance.setAutoPadding(false);
  2588. instate.decrypt.iv = iv;
  2589. instate.decrypt.key = key;
  2590. var emptyBuf;
  2591. if (outCipherInfo.discardLen > 0) {
  2592. emptyBuf = Buffer.alloc(outCipherInfo.discardLen);
  2593. outstate.encrypt.instance.update(emptyBuf);
  2594. }
  2595. if (inCipherInfo.discardLen > 0) {
  2596. if (!emptyBuf || emptyBuf.length !== inCipherInfo.discardLen)
  2597. emptyBuf = Buffer.alloc(outCipherInfo.discardLen);
  2598. instate.decrypt.instance.update(emptyBuf);
  2599. }
  2600. var outHMACInfo = outstate.hmac.info = HMAC_INFO[outstate.hmac.type];
  2601. var inHMACInfo = instate.hmac.info = HMAC_INFO[instate.hmac.type];
  2602. if (outCipherInfo.authLen === 0) {
  2603. key = crypto.createHash(dhHashAlgo)
  2604. .update(secret)
  2605. .update(outstate.exchangeHash)
  2606. .update(!self.server ? 'E' : 'F', 'ascii')
  2607. .update(outstate.sessionId)
  2608. .digest();
  2609. while (key.length < outHMACInfo.len) {
  2610. key = Buffer.concat([key,
  2611. crypto.createHash(dhHashAlgo)
  2612. .update(secret)
  2613. .update(outstate.exchangeHash)
  2614. .update(key)
  2615. .digest()]);
  2616. }
  2617. if (key.length > outHMACInfo.len)
  2618. key = key.slice(0, outHMACInfo.len);
  2619. outstate.hmac.key = key;
  2620. } else {
  2621. outstate.hmac.key = undefined;
  2622. }
  2623. if (inCipherInfo.authLen === 0) {
  2624. key = crypto.createHash(dhHashAlgo)
  2625. .update(secret)
  2626. .update(outstate.exchangeHash)
  2627. .update(!self.server ? 'F' : 'E', 'ascii')
  2628. .update(outstate.sessionId)
  2629. .digest();
  2630. while (key.length < inHMACInfo.len) {
  2631. key = Buffer.concat([key,
  2632. crypto.createHash(dhHashAlgo)
  2633. .update(secret)
  2634. .update(outstate.exchangeHash)
  2635. .update(key)
  2636. .digest()]);
  2637. }
  2638. if (key.length > inHMACInfo.len)
  2639. key = key.slice(0, inHMACInfo.len);
  2640. instate.hmac.key = key;
  2641. } else {
  2642. instate.hmac.key = undefined;
  2643. }
  2644. // Create a reusable buffer for message verification purposes
  2645. var inHMACSize = inCipherInfo.authLen || instate.hmac.info.actualLen;
  2646. if (!instate.hmac.buf
  2647. || instate.hmac.buf.length !== inHMACSize) {
  2648. instate.hmac.buf = Buffer.allocUnsafe(inHMACSize);
  2649. }
  2650. outstate.exchangeHash = undefined;
  2651. if (outstate.compress.type === 'zlib') {
  2652. outstate.compress.instance = zlib.createDeflate(ZLIB_OPTS);
  2653. outstate.compress.queue = [];
  2654. } else if (outstate.compress.type === 'none') {
  2655. outstate.compress.instance = false;
  2656. outstate.compress.queue = null;
  2657. }
  2658. if (instate.decompress.type === 'zlib')
  2659. instate.decompress.instance = zlib.createInflate(ZLIB_OPTS);
  2660. else if (instate.decompress.type === 'none')
  2661. instate.decompress.instance = false;
  2662. self.bytesSent = self.bytesReceived = 0;
  2663. if (outstate.status === OUT_REKEYING) {
  2664. outstate.status = OUT_READY;
  2665. // Empty our outbound buffer of any data we tried to send during the
  2666. // re-keying process
  2667. var queue = outstate.rekeyQueue;
  2668. var qlen = queue.length;
  2669. var q = 0;
  2670. outstate.rekeyQueue = [];
  2671. for (; q < qlen; ++q) {
  2672. if (Buffer.isBuffer(queue[q]))
  2673. send(self, queue[q]);
  2674. else
  2675. send(self, queue[q][0], queue[q][1]);
  2676. }
  2677. // Now empty our inbound buffer of any non-transport layer packets we
  2678. // received during the re-keying process
  2679. queue = instate.rekeyQueue;
  2680. qlen = queue.length;
  2681. q = 0;
  2682. instate.rekeyQueue = [];
  2683. var curSeqno = instate.seqno;
  2684. for (; q < qlen; ++q) {
  2685. instate.seqno = queue[q][0];
  2686. instate.payload = queue[q][1];
  2687. if (parsePacket(self) === false)
  2688. return;
  2689. if (instate.status === IN_INIT) {
  2690. // We were reset due to some error/disagreement ?
  2691. return;
  2692. }
  2693. }
  2694. instate.seqno = curSeqno;
  2695. } else {
  2696. outstate.status = OUT_READY;
  2697. if (instate.status === IN_PACKET) {
  2698. // Explicitly update incoming packet parser status in order to get the
  2699. // correct decipher, hmac, etc. states.
  2700. // We only get here if the host fingerprint callback was called
  2701. // asynchronously and the incoming packet parser is still expecting an
  2702. // unencrypted packet, etc.
  2703. self.debug('DEBUG: Parser: IN_PACKETBEFORE (update) (expecting '
  2704. + inCipherInfo.blockLen + ')');
  2705. // Wait for the right number of bytes so we can determine the incoming
  2706. // packet length
  2707. expectData(self,
  2708. EXP_TYPE_BYTES,
  2709. inCipherInfo.blockLen,
  2710. instate.decrypt.buf);
  2711. }
  2712. self.emit('ready');
  2713. }
  2714. }
  2715. function parsePacket(self, callback) {
  2716. var instate = self._state.incoming;
  2717. var outstate = self._state.outgoing;
  2718. var payload = instate.payload;
  2719. var seqno = instate.seqno;
  2720. var serviceName;
  2721. var lang;
  2722. var message;
  2723. var info;
  2724. var chan;
  2725. var data;
  2726. var srcIP;
  2727. var srcPort;
  2728. var sender;
  2729. var window;
  2730. var packetSize;
  2731. var recipient;
  2732. var description;
  2733. var socketPath;
  2734. if (++instate.seqno > MAX_SEQNO)
  2735. instate.seqno = 0;
  2736. if (instate.ignoreNext) {
  2737. self.debug('DEBUG: Parser: Packet ignored');
  2738. instate.ignoreNext = false;
  2739. return;
  2740. }
  2741. var type = payload[0];
  2742. if (type === undefined)
  2743. return false;
  2744. // If we receive a packet during handshake that is not the expected packet
  2745. // and it is not one of: DISCONNECT, IGNORE, UNIMPLEMENTED, or DEBUG, then we
  2746. // close the stream
  2747. if (outstate.status !== OUT_READY
  2748. && MESSAGE[type] !== instate.expectedPacket
  2749. && type < 1
  2750. && type > 4) {
  2751. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, expected: '
  2752. + instate.expectedPacket
  2753. + ' but got: '
  2754. + MESSAGE[type]);
  2755. // XXX: Potential issue where the module user decides to initiate a rekey
  2756. // via KEXINIT() (which sets `expectedPacket`) after receiving a packet
  2757. // and there is still another packet already waiting to be parsed at the
  2758. // time the KEXINIT is written. this will cause an unexpected disconnect...
  2759. self.disconnect(DISCONNECT_REASON.PROTOCOL_ERROR);
  2760. var err = new Error('Received unexpected packet');
  2761. err.level = 'protocol';
  2762. self.emit('error', err);
  2763. return false;
  2764. }
  2765. if (type === MESSAGE.CHANNEL_DATA) {
  2766. /*
  2767. byte SSH_MSG_CHANNEL_DATA
  2768. uint32 recipient channel
  2769. string data
  2770. */
  2771. chan = readInt(payload, 1, self, callback);
  2772. if (chan === false)
  2773. return false;
  2774. // TODO: MAX_CHAN_DATA_LEN here should really be dependent upon the
  2775. // channel's packet size. The ssh2 module uses 32KB, so we'll hard
  2776. // code this for now ...
  2777. data = readString(payload, 5, self, callback, 32768);
  2778. if (data === false)
  2779. return false;
  2780. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: CHANNEL_DATA ('
  2781. + chan
  2782. + ')');
  2783. self.emit('CHANNEL_DATA:' + chan, data);
  2784. } else if (type === MESSAGE.CHANNEL_EXTENDED_DATA) {
  2785. /*
  2786. byte SSH_MSG_CHANNEL_EXTENDED_DATA
  2787. uint32 recipient channel
  2788. uint32 data_type_code
  2789. string data
  2790. */
  2791. chan = readInt(payload, 1, self, callback);
  2792. if (chan === false)
  2793. return false;
  2794. var dataType = readInt(payload, 5, self, callback);
  2795. if (dataType === false)
  2796. return false;
  2797. data = readString(payload, 9, self, callback);
  2798. if (data === false)
  2799. return false;
  2800. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: '
  2801. + 'CHANNEL_EXTENDED_DATA ('
  2802. + chan
  2803. + ')');
  2804. self.emit('CHANNEL_EXTENDED_DATA:' + chan, dataType, data);
  2805. } else if (type === MESSAGE.CHANNEL_WINDOW_ADJUST) {
  2806. /*
  2807. byte SSH_MSG_CHANNEL_WINDOW_ADJUST
  2808. uint32 recipient channel
  2809. uint32 bytes to add
  2810. */
  2811. chan = readInt(payload, 1, self, callback);
  2812. if (chan === false)
  2813. return false;
  2814. var bytesToAdd = readInt(payload, 5, self, callback);
  2815. if (bytesToAdd === false)
  2816. return false;
  2817. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: '
  2818. + 'CHANNEL_WINDOW_ADJUST ('
  2819. + chan
  2820. + ', '
  2821. + bytesToAdd
  2822. + ')');
  2823. self.emit('CHANNEL_WINDOW_ADJUST:' + chan, bytesToAdd);
  2824. } else if (type === MESSAGE.CHANNEL_SUCCESS) {
  2825. /*
  2826. byte SSH_MSG_CHANNEL_SUCCESS
  2827. uint32 recipient channel
  2828. */
  2829. chan = readInt(payload, 1, self, callback);
  2830. if (chan === false)
  2831. return false;
  2832. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: CHANNEL_SUCCESS ('
  2833. + chan
  2834. + ')');
  2835. self.emit('CHANNEL_SUCCESS:' + chan);
  2836. } else if (type === MESSAGE.CHANNEL_FAILURE) {
  2837. /*
  2838. byte SSH_MSG_CHANNEL_FAILURE
  2839. uint32 recipient channel
  2840. */
  2841. chan = readInt(payload, 1, self, callback);
  2842. if (chan === false)
  2843. return false;
  2844. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: CHANNEL_FAILURE ('
  2845. + chan
  2846. + ')');
  2847. self.emit('CHANNEL_FAILURE:' + chan);
  2848. } else if (type === MESSAGE.CHANNEL_EOF) {
  2849. /*
  2850. byte SSH_MSG_CHANNEL_EOF
  2851. uint32 recipient channel
  2852. */
  2853. chan = readInt(payload, 1, self, callback);
  2854. if (chan === false)
  2855. return false;
  2856. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: CHANNEL_EOF ('
  2857. + chan
  2858. + ')');
  2859. self.emit('CHANNEL_EOF:' + chan);
  2860. } else if (type === MESSAGE.CHANNEL_OPEN) {
  2861. /*
  2862. byte SSH_MSG_CHANNEL_OPEN
  2863. string channel type in US-ASCII only
  2864. uint32 sender channel
  2865. uint32 initial window size
  2866. uint32 maximum packet size
  2867. .... channel type specific data follows
  2868. */
  2869. var chanType = readString(payload, 1, 'ascii', self, callback);
  2870. if (chanType === false)
  2871. return false;
  2872. sender = readInt(payload, payload._pos, self, callback);
  2873. if (sender === false)
  2874. return false;
  2875. window = readInt(payload, payload._pos += 4, self, callback);
  2876. if (window === false)
  2877. return false;
  2878. packetSize = readInt(payload, payload._pos += 4, self, callback);
  2879. if (packetSize === false)
  2880. return false;
  2881. var channel;
  2882. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: CHANNEL_OPEN ('
  2883. + sender
  2884. + ', '
  2885. + chanType
  2886. + ')');
  2887. if (chanType === 'forwarded-tcpip' // Server->Client
  2888. || chanType === 'direct-tcpip') { // Client->Server
  2889. /*
  2890. string address that was connected / host to connect
  2891. uint32 port that was connected / port to connect
  2892. string originator IP address
  2893. uint32 originator port
  2894. */
  2895. var destIP = readString(payload,
  2896. payload._pos += 4,
  2897. 'ascii',
  2898. self,
  2899. callback);
  2900. if (destIP === false)
  2901. return false;
  2902. var destPort = readInt(payload, payload._pos, self, callback);
  2903. if (destPort === false)
  2904. return false;
  2905. srcIP = readString(payload, payload._pos += 4, 'ascii', self, callback);
  2906. if (srcIP === false)
  2907. return false;
  2908. srcPort = readInt(payload, payload._pos, self, callback);
  2909. if (srcPort === false)
  2910. return false;
  2911. channel = {
  2912. type: chanType,
  2913. sender: sender,
  2914. window: window,
  2915. packetSize: packetSize,
  2916. data: {
  2917. destIP: destIP,
  2918. destPort: destPort,
  2919. srcIP: srcIP,
  2920. srcPort: srcPort
  2921. }
  2922. };
  2923. } else if (// Server->Client
  2924. chanType === 'forwarded-streamlocal@openssh.com'
  2925. // Client->Server
  2926. || chanType === 'direct-streamlocal@openssh.com') {
  2927. /*
  2928. string socket path
  2929. string reserved for future use
  2930. */
  2931. socketPath = readString(payload,
  2932. payload._pos += 4,
  2933. 'utf8',
  2934. self,
  2935. callback);
  2936. if (socketPath === false)
  2937. return false;
  2938. channel = {
  2939. type: chanType,
  2940. sender: sender,
  2941. window: window,
  2942. packetSize: packetSize,
  2943. data: {
  2944. socketPath: socketPath,
  2945. }
  2946. };
  2947. } else if (chanType === 'x11') { // Server->Client
  2948. /*
  2949. string originator address (e.g., "192.168.7.38")
  2950. uint32 originator port
  2951. */
  2952. srcIP = readString(payload, payload._pos += 4, 'ascii', self, callback);
  2953. if (srcIP === false)
  2954. return false;
  2955. srcPort = readInt(payload, payload._pos, self, callback);
  2956. if (srcPort === false)
  2957. return false;
  2958. channel = {
  2959. type: chanType,
  2960. sender: sender,
  2961. window: window,
  2962. packetSize: packetSize,
  2963. data: {
  2964. srcIP: srcIP,
  2965. srcPort: srcPort
  2966. }
  2967. };
  2968. } else {
  2969. // 'session' (Client->Server), 'auth-agent@openssh.com' (Server->Client)
  2970. channel = {
  2971. type: chanType,
  2972. sender: sender,
  2973. window: window,
  2974. packetSize: packetSize,
  2975. data: {}
  2976. };
  2977. }
  2978. self.emit('CHANNEL_OPEN', channel);
  2979. } else if (type === MESSAGE.CHANNEL_OPEN_CONFIRMATION) {
  2980. /*
  2981. byte SSH_MSG_CHANNEL_OPEN_CONFIRMATION
  2982. uint32 recipient channel
  2983. uint32 sender channel
  2984. uint32 initial window size
  2985. uint32 maximum packet size
  2986. .... channel type specific data follows
  2987. */
  2988. // "The 'recipient channel' is the channel number given in the
  2989. // original open request, and 'sender channel' is the channel number
  2990. // allocated by the other side."
  2991. recipient = readInt(payload, 1, self, callback);
  2992. if (recipient === false)
  2993. return false;
  2994. sender = readInt(payload, 5, self, callback);
  2995. if (sender === false)
  2996. return false;
  2997. window = readInt(payload, 9, self, callback);
  2998. if (window === false)
  2999. return false;
  3000. packetSize = readInt(payload, 13, self, callback);
  3001. if (packetSize === false)
  3002. return false;
  3003. info = {
  3004. recipient: recipient,
  3005. sender: sender,
  3006. window: window,
  3007. packetSize: packetSize
  3008. };
  3009. if (payload.length > 17)
  3010. info.data = payload.slice(17);
  3011. self.emit('CHANNEL_OPEN_CONFIRMATION:' + info.recipient, info);
  3012. } else if (type === MESSAGE.CHANNEL_OPEN_FAILURE) {
  3013. /*
  3014. byte SSH_MSG_CHANNEL_OPEN_FAILURE
  3015. uint32 recipient channel
  3016. uint32 reason code
  3017. string description in ISO-10646 UTF-8 encoding
  3018. string language tag
  3019. */
  3020. recipient = readInt(payload, 1, self, callback);
  3021. if (recipient === false)
  3022. return false;
  3023. var reasonCode = readInt(payload, 5, self, callback);
  3024. if (reasonCode === false)
  3025. return false;
  3026. description = readString(payload, 9, 'utf8', self, callback);
  3027. if (description === false)
  3028. return false;
  3029. lang = readString(payload, payload._pos, 'utf8', self, callback);
  3030. if (lang === false)
  3031. return false;
  3032. payload._pos = 9;
  3033. info = {
  3034. recipient: recipient,
  3035. reasonCode: reasonCode,
  3036. reason: CHANNEL_OPEN_FAILURE[reasonCode],
  3037. description: description,
  3038. lang: lang
  3039. };
  3040. self.emit('CHANNEL_OPEN_FAILURE:' + info.recipient, info);
  3041. } else if (type === MESSAGE.CHANNEL_CLOSE) {
  3042. /*
  3043. byte SSH_MSG_CHANNEL_CLOSE
  3044. uint32 recipient channel
  3045. */
  3046. chan = readInt(payload, 1, self, callback);
  3047. if (chan === false)
  3048. return false;
  3049. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: CHANNEL_CLOSE ('
  3050. + chan
  3051. + ')');
  3052. self.emit('CHANNEL_CLOSE:' + chan);
  3053. } else if (type === MESSAGE.IGNORE) {
  3054. /*
  3055. byte SSH_MSG_IGNORE
  3056. string data
  3057. */
  3058. } else if (type === MESSAGE.DISCONNECT) {
  3059. /*
  3060. byte SSH_MSG_DISCONNECT
  3061. uint32 reason code
  3062. string description in ISO-10646 UTF-8 encoding
  3063. string language tag
  3064. */
  3065. var reason = readInt(payload, 1, self, callback);
  3066. if (reason === false)
  3067. return false;
  3068. var reasonText = DISCONNECT_REASON[reason];
  3069. description = readString(payload, 5, 'utf8', self, callback);
  3070. if (description === false)
  3071. return false;
  3072. if (payload._pos < payload.length)
  3073. lang = readString(payload, payload._pos, 'ascii', self, callback);
  3074. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: DISCONNECT ('
  3075. + reasonText
  3076. + ')');
  3077. self.emit('DISCONNECT', reasonText, reason, description, lang);
  3078. } else if (type === MESSAGE.DEBUG) {
  3079. /*
  3080. byte SSH_MSG_DEBUG
  3081. boolean always_display
  3082. string message in ISO-10646 UTF-8 encoding
  3083. string language tag
  3084. */
  3085. message = readString(payload, 2, 'utf8', self, callback);
  3086. if (message === false)
  3087. return false;
  3088. lang = readString(payload, payload._pos, 'ascii', self, callback);
  3089. if (lang === false)
  3090. return false;
  3091. self.emit('DEBUG', message, lang);
  3092. } else if (type === MESSAGE.NEWKEYS) {
  3093. /*
  3094. byte SSH_MSG_NEW_KEYS
  3095. */
  3096. self.emit('NEWKEYS');
  3097. } else if (type === MESSAGE.SERVICE_REQUEST) {
  3098. /*
  3099. byte SSH_MSG_SERVICE_REQUEST
  3100. string service name
  3101. */
  3102. serviceName = readString(payload, 1, 'ascii', self, callback);
  3103. if (serviceName === false)
  3104. return false;
  3105. self.emit('SERVICE_REQUEST', serviceName);
  3106. } else if (type === MESSAGE.SERVICE_ACCEPT) {
  3107. /*
  3108. byte SSH_MSG_SERVICE_ACCEPT
  3109. string service name
  3110. */
  3111. serviceName = readString(payload, 1, 'ascii', self, callback);
  3112. if (serviceName === false)
  3113. return false;
  3114. self.emit('SERVICE_ACCEPT', serviceName);
  3115. } else if (type === MESSAGE.USERAUTH_REQUEST) {
  3116. /*
  3117. byte SSH_MSG_USERAUTH_REQUEST
  3118. string user name in ISO-10646 UTF-8 encoding [RFC3629]
  3119. string service name in US-ASCII
  3120. string method name in US-ASCII
  3121. .... method specific fields
  3122. */
  3123. var username = readString(payload, 1, 'utf8', self, callback);
  3124. if (username === false)
  3125. return false;
  3126. var svcName = readString(payload, payload._pos, 'ascii', self, callback);
  3127. if (svcName === false)
  3128. return false;
  3129. var method = readString(payload, payload._pos, 'ascii', self, callback);
  3130. if (method === false)
  3131. return false;
  3132. var methodData;
  3133. var methodDesc;
  3134. if (method === 'password') {
  3135. methodData = readString(payload,
  3136. payload._pos + 1,
  3137. 'utf8',
  3138. self,
  3139. callback);
  3140. if (methodData === false)
  3141. return false;
  3142. } else if (method === 'publickey' || method === 'hostbased') {
  3143. var pkSigned;
  3144. var keyAlgo;
  3145. var key;
  3146. var signature;
  3147. var blob;
  3148. var hostname;
  3149. var userlocal;
  3150. if (method === 'publickey') {
  3151. pkSigned = payload[payload._pos++];
  3152. if (pkSigned === undefined)
  3153. return false;
  3154. pkSigned = (pkSigned !== 0);
  3155. }
  3156. keyAlgo = readString(payload, payload._pos, 'ascii', self, callback);
  3157. if (keyAlgo === false)
  3158. return false;
  3159. key = readString(payload, payload._pos, self, callback);
  3160. if (key === false)
  3161. return false;
  3162. if (pkSigned || method === 'hostbased') {
  3163. if (method === 'hostbased') {
  3164. hostname = readString(payload, payload._pos, 'ascii', self, callback);
  3165. if (hostname === false)
  3166. return false;
  3167. userlocal = readString(payload, payload._pos, 'utf8', self, callback);
  3168. if (userlocal === false)
  3169. return false;
  3170. }
  3171. var blobEnd = payload._pos;
  3172. signature = readString(payload, blobEnd, self, callback);
  3173. if (signature === false)
  3174. return false;
  3175. if (signature.length > (4 + keyAlgo.length + 4)
  3176. && signature.toString('ascii', 4, 4 + keyAlgo.length) === keyAlgo) {
  3177. // Skip algoLen + algo + sigLen
  3178. signature = signature.slice(4 + keyAlgo.length + 4);
  3179. }
  3180. signature = sigSSHToASN1(signature, keyAlgo, self, callback);
  3181. if (signature === false)
  3182. return false;
  3183. blob = Buffer.allocUnsafe(4 + outstate.sessionId.length + blobEnd);
  3184. writeUInt32BE(blob, outstate.sessionId.length, 0);
  3185. outstate.sessionId.copy(blob, 4);
  3186. payload.copy(blob, 4 + outstate.sessionId.length, 0, blobEnd);
  3187. } else {
  3188. methodDesc = 'publickey -- check';
  3189. }
  3190. methodData = {
  3191. keyAlgo: keyAlgo,
  3192. key: key,
  3193. signature: signature,
  3194. blob: blob,
  3195. localHostname: hostname,
  3196. localUsername: userlocal
  3197. };
  3198. } else if (method === 'keyboard-interactive') {
  3199. // Skip language, it's deprecated
  3200. var skipLen = readInt(payload, payload._pos, self, callback);
  3201. if (skipLen === false)
  3202. return false;
  3203. methodData = readString(payload,
  3204. payload._pos + 4 + skipLen,
  3205. 'utf8',
  3206. self,
  3207. callback);
  3208. if (methodData === false)
  3209. return false;
  3210. } else if (method !== 'none')
  3211. methodData = payload.slice(payload._pos);
  3212. if (methodDesc === undefined)
  3213. methodDesc = method;
  3214. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: USERAUTH_REQUEST ('
  3215. + methodDesc
  3216. + ')');
  3217. self._state.authsQueue.push(method);
  3218. self.emit('USERAUTH_REQUEST', username, svcName, method, methodData);
  3219. } else if (type === MESSAGE.USERAUTH_SUCCESS) {
  3220. /*
  3221. byte SSH_MSG_USERAUTH_SUCCESS
  3222. */
  3223. if (outstate.compress.type === 'zlib@openssh.com') {
  3224. outstate.compress.instance = zlib.createDeflate(ZLIB_OPTS);
  3225. outstate.compress.queue = [];
  3226. }
  3227. if (instate.decompress.type === 'zlib@openssh.com')
  3228. instate.decompress.instance = zlib.createInflate(ZLIB_OPTS);
  3229. self._state.authsQueue.shift();
  3230. self.emit('USERAUTH_SUCCESS');
  3231. } else if (type === MESSAGE.USERAUTH_FAILURE) {
  3232. /*
  3233. byte SSH_MSG_USERAUTH_FAILURE
  3234. name-list authentications that can continue
  3235. boolean partial success
  3236. */
  3237. var auths = readString(payload, 1, 'ascii', self, callback);
  3238. if (auths === false)
  3239. return false;
  3240. var partSuccess = payload[payload._pos];
  3241. if (partSuccess === undefined)
  3242. return false;
  3243. partSuccess = (partSuccess !== 0);
  3244. auths = auths.split(',');
  3245. self._state.authsQueue.shift();
  3246. self.emit('USERAUTH_FAILURE', auths, partSuccess);
  3247. } else if (type === MESSAGE.USERAUTH_BANNER) {
  3248. /*
  3249. byte SSH_MSG_USERAUTH_BANNER
  3250. string message in ISO-10646 UTF-8 encoding
  3251. string language tag
  3252. */
  3253. message = readString(payload, 1, 'utf8', self, callback);
  3254. if (message === false)
  3255. return false;
  3256. lang = readString(payload, payload._pos, 'utf8', self, callback);
  3257. if (lang === false)
  3258. return false;
  3259. self.emit('USERAUTH_BANNER', message, lang);
  3260. } else if (type === MESSAGE.GLOBAL_REQUEST) {
  3261. /*
  3262. byte SSH_MSG_GLOBAL_REQUEST
  3263. string request name in US-ASCII only
  3264. boolean want reply
  3265. .... request-specific data follows
  3266. */
  3267. var request = readString(payload, 1, 'ascii', self, callback);
  3268. if (request === false) {
  3269. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: GLOBAL_REQUEST');
  3270. return false;
  3271. }
  3272. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: GLOBAL_REQUEST ('
  3273. + request
  3274. + ')');
  3275. var wantReply = payload[payload._pos++];
  3276. if (wantReply === undefined)
  3277. return false;
  3278. wantReply = (wantReply !== 0);
  3279. var reqData;
  3280. if (request === 'tcpip-forward' || request === 'cancel-tcpip-forward') {
  3281. var bindAddr = readString(payload, payload._pos, 'ascii', self, callback);
  3282. if (bindAddr === false)
  3283. return false;
  3284. var bindPort = readInt(payload, payload._pos, self, callback);
  3285. if (bindPort === false)
  3286. return false;
  3287. reqData = {
  3288. bindAddr: bindAddr,
  3289. bindPort: bindPort
  3290. };
  3291. } else if (request === 'streamlocal-forward@openssh.com'
  3292. || request === 'cancel-streamlocal-forward@openssh.com') {
  3293. socketPath = readString(payload, payload._pos, 'utf8', self, callback);
  3294. if (socketPath === false)
  3295. return false;
  3296. reqData = {
  3297. socketPath: socketPath
  3298. };
  3299. } else if (request === 'no-more-sessions@openssh.com') {
  3300. // No data
  3301. } else {
  3302. reqData = payload.slice(payload._pos);
  3303. }
  3304. self.emit('GLOBAL_REQUEST', request, wantReply, reqData);
  3305. } else if (type === MESSAGE.REQUEST_SUCCESS) {
  3306. /*
  3307. byte SSH_MSG_REQUEST_SUCCESS
  3308. .... response specific data
  3309. */
  3310. if (payload.length > 1)
  3311. self.emit('REQUEST_SUCCESS', payload.slice(1));
  3312. else
  3313. self.emit('REQUEST_SUCCESS');
  3314. } else if (type === MESSAGE.REQUEST_FAILURE) {
  3315. /*
  3316. byte SSH_MSG_REQUEST_FAILURE
  3317. */
  3318. self.emit('REQUEST_FAILURE');
  3319. } else if (type === MESSAGE.UNIMPLEMENTED) {
  3320. /*
  3321. byte SSH_MSG_UNIMPLEMENTED
  3322. uint32 packet sequence number of rejected message
  3323. */
  3324. // TODO
  3325. } else if (type === MESSAGE.KEXINIT)
  3326. return parse_KEXINIT(self, callback);
  3327. else if (type === MESSAGE.CHANNEL_REQUEST)
  3328. return parse_CHANNEL_REQUEST(self, callback);
  3329. else if (type >= 30 && type <= 49) // Key exchange method-specific messages
  3330. return parse_KEX(self, type, callback);
  3331. else if (type >= 60 && type <= 70) // User auth context-specific messages
  3332. return parse_USERAUTH(self, type, callback);
  3333. else {
  3334. // Unknown packet type
  3335. var unimpl = Buffer.allocUnsafe(1 + 4);
  3336. unimpl[0] = MESSAGE.UNIMPLEMENTED;
  3337. writeUInt32BE(unimpl, seqno, 1);
  3338. send(self, unimpl);
  3339. }
  3340. }
  3341. function parse_KEXINIT(self, callback) {
  3342. var instate = self._state.incoming;
  3343. var payload = instate.payload;
  3344. /*
  3345. byte SSH_MSG_KEXINIT
  3346. byte[16] cookie (random bytes)
  3347. name-list kex_algorithms
  3348. name-list server_host_key_algorithms
  3349. name-list encryption_algorithms_client_to_server
  3350. name-list encryption_algorithms_server_to_client
  3351. name-list mac_algorithms_client_to_server
  3352. name-list mac_algorithms_server_to_client
  3353. name-list compression_algorithms_client_to_server
  3354. name-list compression_algorithms_server_to_client
  3355. name-list languages_client_to_server
  3356. name-list languages_server_to_client
  3357. boolean first_kex_packet_follows
  3358. uint32 0 (reserved for future extension)
  3359. */
  3360. var init = {
  3361. algorithms: {
  3362. kex: undefined,
  3363. srvHostKey: undefined,
  3364. cs: {
  3365. encrypt: undefined,
  3366. mac: undefined,
  3367. compress: undefined
  3368. },
  3369. sc: {
  3370. encrypt: undefined,
  3371. mac: undefined,
  3372. compress: undefined
  3373. }
  3374. },
  3375. languages: {
  3376. cs: undefined,
  3377. sc: undefined
  3378. }
  3379. };
  3380. var val;
  3381. val = readList(payload, 17, self, callback);
  3382. if (val === false)
  3383. return false;
  3384. init.algorithms.kex = val;
  3385. val = readList(payload, payload._pos, self, callback);
  3386. if (val === false)
  3387. return false;
  3388. init.algorithms.srvHostKey = val;
  3389. val = readList(payload, payload._pos, self, callback);
  3390. if (val === false)
  3391. return false;
  3392. init.algorithms.cs.encrypt = val;
  3393. val = readList(payload, payload._pos, self, callback);
  3394. if (val === false)
  3395. return false;
  3396. init.algorithms.sc.encrypt = val;
  3397. val = readList(payload, payload._pos, self, callback);
  3398. if (val === false)
  3399. return false;
  3400. init.algorithms.cs.mac = val;
  3401. val = readList(payload, payload._pos, self, callback);
  3402. if (val === false)
  3403. return false;
  3404. init.algorithms.sc.mac = val;
  3405. val = readList(payload, payload._pos, self, callback);
  3406. if (val === false)
  3407. return false;
  3408. init.algorithms.cs.compress = val;
  3409. val = readList(payload, payload._pos, self, callback);
  3410. if (val === false)
  3411. return false;
  3412. init.algorithms.sc.compress = val;
  3413. val = readList(payload, payload._pos, self, callback);
  3414. if (val === false)
  3415. return false;
  3416. init.languages.cs = val;
  3417. val = readList(payload, payload._pos, self, callback);
  3418. if (val === false)
  3419. return false;
  3420. init.languages.sc = val;
  3421. var firstFollows = (payload._pos < payload.length
  3422. && payload[payload._pos] === 1);
  3423. instate.kexinit = payload;
  3424. self.emit('KEXINIT', init, firstFollows);
  3425. }
  3426. function parse_KEX(self, type, callback) {
  3427. var state = self._state;
  3428. var instate = state.incoming;
  3429. var payload = instate.payload;
  3430. var pktType = (RE_GEX.test(state.kexdh)
  3431. ? DYNAMIC_KEXDH_MESSAGE[type]
  3432. : KEXDH_MESSAGE[type]);
  3433. if (state.outgoing.status === OUT_READY
  3434. || instate.expectedPacket !== pktType) {
  3435. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, expected: '
  3436. + instate.expectedPacket
  3437. + ' but got: '
  3438. + pktType);
  3439. self.disconnect(DISCONNECT_REASON.PROTOCOL_ERROR);
  3440. var err = new Error('Received unexpected packet');
  3441. err.level = 'protocol';
  3442. self.emit('error', err);
  3443. return false;
  3444. }
  3445. if (RE_GEX.test(state.kexdh)) {
  3446. // Dynamic group exchange-related
  3447. if (self.server) {
  3448. // TODO: Support group exchange server-side
  3449. self.disconnect(DISCONNECT_REASON.PROTOCOL_ERROR);
  3450. var err = new Error('DH group exchange not supported by server');
  3451. err.level = 'handshake';
  3452. self.emit('error', err);
  3453. return false;
  3454. } else {
  3455. if (type === MESSAGE.KEXDH_GEX_GROUP) {
  3456. /*
  3457. byte SSH_MSG_KEX_DH_GEX_GROUP
  3458. mpint p, safe prime
  3459. mpint g, generator for subgroup in GF(p)
  3460. */
  3461. var prime = readString(payload, 1, self, callback);
  3462. if (prime === false)
  3463. return false;
  3464. var gen = readString(payload, payload._pos, self, callback);
  3465. if (gen === false)
  3466. return false;
  3467. self.emit('KEXDH_GEX_GROUP', prime, gen);
  3468. } else if (type === MESSAGE.KEXDH_GEX_REPLY)
  3469. return parse_KEXDH_REPLY(self, callback);
  3470. }
  3471. } else {
  3472. // Static group or ECDH-related
  3473. if (type === MESSAGE.KEXDH_INIT) {
  3474. /*
  3475. byte SSH_MSG_KEXDH_INIT
  3476. mpint e
  3477. */
  3478. var e = readString(payload, 1, self, callback);
  3479. if (e === false)
  3480. return false;
  3481. self.emit('KEXDH_INIT', e);
  3482. } else if (type === MESSAGE.KEXDH_REPLY)
  3483. return parse_KEXDH_REPLY(self, callback);
  3484. }
  3485. }
  3486. function parse_KEXDH_REPLY(self, callback) {
  3487. var payload = self._state.incoming.payload;
  3488. /*
  3489. byte SSH_MSG_KEXDH_REPLY
  3490. / SSH_MSG_KEX_DH_GEX_REPLY
  3491. / SSH_MSG_KEX_ECDH_REPLY
  3492. string server public host key and certificates (K_S)
  3493. mpint f
  3494. string signature of H
  3495. */
  3496. var hostkey = readString(payload, 1, self, callback);
  3497. if (hostkey === false)
  3498. return false;
  3499. var pubkey = readString(payload, payload._pos, self, callback);
  3500. if (pubkey === false)
  3501. return false;
  3502. var sig = readString(payload, payload._pos, self, callback);
  3503. if (sig === false)
  3504. return false;
  3505. var info = {
  3506. hostkey: hostkey,
  3507. hostkey_format: undefined,
  3508. pubkey: pubkey,
  3509. sig: sig,
  3510. sig_format: undefined
  3511. };
  3512. var hostkey_format = readString(hostkey, 0, 'ascii', self, callback);
  3513. if (hostkey_format === false)
  3514. return false;
  3515. info.hostkey_format = hostkey_format;
  3516. var sig_format = readString(sig, 0, 'ascii', self, callback);
  3517. if (sig_format === false)
  3518. return false;
  3519. info.sig_format = sig_format;
  3520. self.emit('KEXDH_REPLY', info);
  3521. }
  3522. function parse_USERAUTH(self, type, callback) {
  3523. var state = self._state;
  3524. var authMethod = state.authsQueue[0];
  3525. var payload = state.incoming.payload;
  3526. var message;
  3527. var lang;
  3528. var text;
  3529. if (authMethod === 'password') {
  3530. if (type === MESSAGE.USERAUTH_PASSWD_CHANGEREQ) {
  3531. /*
  3532. byte SSH_MSG_USERAUTH_PASSWD_CHANGEREQ
  3533. string prompt in ISO-10646 UTF-8 encoding
  3534. string language tag
  3535. */
  3536. message = readString(payload, 1, 'utf8', self, callback);
  3537. if (message === false)
  3538. return false;
  3539. lang = readString(payload, payload._pos, 'utf8', self, callback);
  3540. if (lang === false)
  3541. return false;
  3542. self.emit('USERAUTH_PASSWD_CHANGEREQ', message, lang);
  3543. }
  3544. } else if (authMethod === 'keyboard-interactive') {
  3545. if (type === MESSAGE.USERAUTH_INFO_REQUEST) {
  3546. /*
  3547. byte SSH_MSG_USERAUTH_INFO_REQUEST
  3548. string name (ISO-10646 UTF-8)
  3549. string instruction (ISO-10646 UTF-8)
  3550. string language tag -- MAY be empty
  3551. int num-prompts
  3552. string prompt[1] (ISO-10646 UTF-8)
  3553. boolean echo[1]
  3554. ...
  3555. string prompt[num-prompts] (ISO-10646 UTF-8)
  3556. boolean echo[num-prompts]
  3557. */
  3558. var name;
  3559. var instr;
  3560. var nprompts;
  3561. name = readString(payload, 1, 'utf8', self, callback);
  3562. if (name === false)
  3563. return false;
  3564. instr = readString(payload, payload._pos, 'utf8', self, callback);
  3565. if (instr === false)
  3566. return false;
  3567. lang = readString(payload, payload._pos, 'utf8', self, callback);
  3568. if (lang === false)
  3569. return false;
  3570. nprompts = readInt(payload, payload._pos, self, callback);
  3571. if (nprompts === false)
  3572. return false;
  3573. payload._pos += 4;
  3574. var prompts = [];
  3575. for (var prompt = 0; prompt < nprompts; ++prompt) {
  3576. text = readString(payload, payload._pos, 'utf8', self, callback);
  3577. if (text === false)
  3578. return false;
  3579. var echo = payload[payload._pos++];
  3580. if (echo === undefined)
  3581. return false;
  3582. echo = (echo !== 0);
  3583. prompts.push({
  3584. prompt: text,
  3585. echo: echo
  3586. });
  3587. }
  3588. self.emit('USERAUTH_INFO_REQUEST', name, instr, lang, prompts);
  3589. } else if (type === MESSAGE.USERAUTH_INFO_RESPONSE) {
  3590. /*
  3591. byte SSH_MSG_USERAUTH_INFO_RESPONSE
  3592. int num-responses
  3593. string response[1] (ISO-10646 UTF-8)
  3594. ...
  3595. string response[num-responses] (ISO-10646 UTF-8)
  3596. */
  3597. var nresponses = readInt(payload, 1, self, callback);
  3598. if (nresponses === false)
  3599. return false;
  3600. payload._pos = 5;
  3601. var responses = [];
  3602. for (var response = 0; response < nresponses; ++response) {
  3603. text = readString(payload, payload._pos, 'utf8', self, callback);
  3604. if (text === false)
  3605. return false;
  3606. responses.push(text);
  3607. }
  3608. self.emit('USERAUTH_INFO_RESPONSE', responses);
  3609. }
  3610. } else if (authMethod === 'publickey') {
  3611. if (type === MESSAGE.USERAUTH_PK_OK) {
  3612. /*
  3613. byte SSH_MSG_USERAUTH_PK_OK
  3614. string public key algorithm name from the request
  3615. string public key blob from the request
  3616. */
  3617. var authsQueue = self._state.authsQueue;
  3618. if (!authsQueue.length || authsQueue[0] !== 'publickey')
  3619. return;
  3620. authsQueue.shift();
  3621. self.emit('USERAUTH_PK_OK');
  3622. // XXX: Parse public key info? client currently can ignore it because
  3623. // there is only one outstanding auth request at any given time, so it
  3624. // knows which key was OK'd
  3625. }
  3626. } else if (authMethod !== undefined) {
  3627. // Invalid packet for this auth type
  3628. self.disconnect(DISCONNECT_REASON.PROTOCOL_ERROR);
  3629. var err = new Error('Invalid authentication method: ' + authMethod);
  3630. err.level = 'protocol';
  3631. self.emit('error', err);
  3632. }
  3633. }
  3634. function parse_CHANNEL_REQUEST(self, callback) {
  3635. var payload = self._state.incoming.payload;
  3636. var info;
  3637. var cols;
  3638. var rows;
  3639. var width;
  3640. var height;
  3641. var wantReply;
  3642. var signal;
  3643. var recipient = readInt(payload, 1, self, callback);
  3644. if (recipient === false)
  3645. return false;
  3646. var request = readString(payload, 5, 'ascii', self, callback);
  3647. if (request === false)
  3648. return false;
  3649. if (request === 'exit-status') { // Server->Client
  3650. /*
  3651. byte SSH_MSG_CHANNEL_REQUEST
  3652. uint32 recipient channel
  3653. string "exit-status"
  3654. boolean FALSE
  3655. uint32 exit_status
  3656. */
  3657. var code = readInt(payload, ++payload._pos, self, callback);
  3658. if (code === false)
  3659. return false;
  3660. info = {
  3661. recipient: recipient,
  3662. request: request,
  3663. wantReply: false,
  3664. code: code
  3665. };
  3666. } else if (request === 'exit-signal') { // Server->Client
  3667. /*
  3668. byte SSH_MSG_CHANNEL_REQUEST
  3669. uint32 recipient channel
  3670. string "exit-signal"
  3671. boolean FALSE
  3672. string signal name (without the "SIG" prefix)
  3673. boolean core dumped
  3674. string error message in ISO-10646 UTF-8 encoding
  3675. string language tag
  3676. */
  3677. var coredump;
  3678. if (!(self.remoteBugs & BUGS.OLD_EXIT)) {
  3679. signal = readString(payload, ++payload._pos, 'ascii', self, callback);
  3680. if (signal === false)
  3681. return false;
  3682. coredump = payload[payload._pos++];
  3683. if (coredump === undefined)
  3684. return false;
  3685. coredump = (coredump !== 0);
  3686. } else {
  3687. /*
  3688. Instead of `signal name` and `core dumped`, we have just:
  3689. uint32 signal number
  3690. */
  3691. signal = readInt(payload, ++payload._pos, self, callback);
  3692. if (signal === false)
  3693. return false;
  3694. switch (signal) {
  3695. case 1:
  3696. signal = 'HUP';
  3697. break;
  3698. case 2:
  3699. signal = 'INT';
  3700. break;
  3701. case 3:
  3702. signal = 'QUIT';
  3703. break;
  3704. case 6:
  3705. signal = 'ABRT';
  3706. break;
  3707. case 9:
  3708. signal = 'KILL';
  3709. break;
  3710. case 14:
  3711. signal = 'ALRM';
  3712. break;
  3713. case 15:
  3714. signal = 'TERM';
  3715. break;
  3716. default:
  3717. // Unknown or OS-specific
  3718. signal = 'UNKNOWN (' + signal + ')';
  3719. }
  3720. coredump = false;
  3721. }
  3722. var description = readString(payload, payload._pos, 'utf8', self,
  3723. callback);
  3724. if (description === false)
  3725. return false;
  3726. var lang = readString(payload, payload._pos, 'utf8', self, callback);
  3727. if (lang === false)
  3728. return false;
  3729. info = {
  3730. recipient: recipient,
  3731. request: request,
  3732. wantReply: false,
  3733. signal: signal,
  3734. coredump: coredump,
  3735. description: description,
  3736. lang: lang
  3737. };
  3738. } else if (request === 'pty-req') { // Client->Server
  3739. /*
  3740. byte SSH_MSG_CHANNEL_REQUEST
  3741. uint32 recipient channel
  3742. string "pty-req"
  3743. boolean want_reply
  3744. string TERM environment variable value (e.g., vt100)
  3745. uint32 terminal width, characters (e.g., 80)
  3746. uint32 terminal height, rows (e.g., 24)
  3747. uint32 terminal width, pixels (e.g., 640)
  3748. uint32 terminal height, pixels (e.g., 480)
  3749. string encoded terminal modes
  3750. */
  3751. wantReply = payload[payload._pos++];
  3752. if (wantReply === undefined)
  3753. return false;
  3754. wantReply = (wantReply !== 0);
  3755. var term = readString(payload, payload._pos, 'ascii', self, callback);
  3756. if (term === false)
  3757. return false;
  3758. cols = readInt(payload, payload._pos, self, callback);
  3759. if (cols === false)
  3760. return false;
  3761. rows = readInt(payload, payload._pos += 4, self, callback);
  3762. if (rows === false)
  3763. return false;
  3764. width = readInt(payload, payload._pos += 4, self, callback);
  3765. if (width === false)
  3766. return false;
  3767. height = readInt(payload, payload._pos += 4, self, callback);
  3768. if (height === false)
  3769. return false;
  3770. var modes = readString(payload, payload._pos += 4, self, callback);
  3771. if (modes === false)
  3772. return false;
  3773. modes = bytesToModes(modes);
  3774. info = {
  3775. recipient: recipient,
  3776. request: request,
  3777. wantReply: wantReply,
  3778. term: term,
  3779. cols: cols,
  3780. rows: rows,
  3781. width: width,
  3782. height: height,
  3783. modes: modes
  3784. };
  3785. } else if (request === 'window-change') { // Client->Server
  3786. /*
  3787. byte SSH_MSG_CHANNEL_REQUEST
  3788. uint32 recipient channel
  3789. string "window-change"
  3790. boolean FALSE
  3791. uint32 terminal width, columns
  3792. uint32 terminal height, rows
  3793. uint32 terminal width, pixels
  3794. uint32 terminal height, pixels
  3795. */
  3796. cols = readInt(payload, ++payload._pos, self, callback);
  3797. if (cols === false)
  3798. return false;
  3799. rows = readInt(payload, payload._pos += 4, self, callback);
  3800. if (rows === false)
  3801. return false;
  3802. width = readInt(payload, payload._pos += 4, self, callback);
  3803. if (width === false)
  3804. return false;
  3805. height = readInt(payload, payload._pos += 4, self, callback);
  3806. if (height === false)
  3807. return false;
  3808. info = {
  3809. recipient: recipient,
  3810. request: request,
  3811. wantReply: false,
  3812. cols: cols,
  3813. rows: rows,
  3814. width: width,
  3815. height: height
  3816. };
  3817. } else if (request === 'x11-req') { // Client->Server
  3818. /*
  3819. byte SSH_MSG_CHANNEL_REQUEST
  3820. uint32 recipient channel
  3821. string "x11-req"
  3822. boolean want reply
  3823. boolean single connection
  3824. string x11 authentication protocol
  3825. string x11 authentication cookie
  3826. uint32 x11 screen number
  3827. */
  3828. wantReply = payload[payload._pos++];
  3829. if (wantReply === undefined)
  3830. return false;
  3831. wantReply = (wantReply !== 0);
  3832. var single = payload[payload._pos++];
  3833. if (single === undefined)
  3834. return false;
  3835. single = (single !== 0);
  3836. var protocol = readString(payload, payload._pos, 'ascii', self, callback);
  3837. if (protocol === false)
  3838. return false;
  3839. var cookie = readString(payload, payload._pos, 'binary', self, callback);
  3840. if (cookie === false)
  3841. return false;
  3842. var screen = readInt(payload, payload._pos, self, callback);
  3843. if (screen === false)
  3844. return false;
  3845. info = {
  3846. recipient: recipient,
  3847. request: request,
  3848. wantReply: wantReply,
  3849. single: single,
  3850. protocol: protocol,
  3851. cookie: cookie,
  3852. screen: screen
  3853. };
  3854. } else if (request === 'env') { // Client->Server
  3855. /*
  3856. byte SSH_MSG_CHANNEL_REQUEST
  3857. uint32 recipient channel
  3858. string "env"
  3859. boolean want reply
  3860. string variable name
  3861. string variable value
  3862. */
  3863. wantReply = payload[payload._pos++];
  3864. if (wantReply === undefined)
  3865. return false;
  3866. wantReply = (wantReply !== 0);
  3867. var key = readString(payload, payload._pos, 'utf8', self, callback);
  3868. if (key === false)
  3869. return false;
  3870. var val = readString(payload, payload._pos, 'utf8', self, callback);
  3871. if (val === false)
  3872. return false;
  3873. info = {
  3874. recipient: recipient,
  3875. request: request,
  3876. wantReply: wantReply,
  3877. key: key,
  3878. val: val
  3879. };
  3880. } else if (request === 'shell') { // Client->Server
  3881. /*
  3882. byte SSH_MSG_CHANNEL_REQUEST
  3883. uint32 recipient channel
  3884. string "shell"
  3885. boolean want reply
  3886. */
  3887. wantReply = payload[payload._pos];
  3888. if (wantReply === undefined)
  3889. return false;
  3890. wantReply = (wantReply !== 0);
  3891. info = {
  3892. recipient: recipient,
  3893. request: request,
  3894. wantReply: wantReply
  3895. };
  3896. } else if (request === 'exec') { // Client->Server
  3897. /*
  3898. byte SSH_MSG_CHANNEL_REQUEST
  3899. uint32 recipient channel
  3900. string "exec"
  3901. boolean want reply
  3902. string command
  3903. */
  3904. wantReply = payload[payload._pos++];
  3905. if (wantReply === undefined)
  3906. return false;
  3907. wantReply = (wantReply !== 0);
  3908. var command = readString(payload, payload._pos, 'utf8', self, callback);
  3909. if (command === false)
  3910. return false;
  3911. info = {
  3912. recipient: recipient,
  3913. request: request,
  3914. wantReply: wantReply,
  3915. command: command
  3916. };
  3917. } else if (request === 'subsystem') { // Client->Server
  3918. /*
  3919. byte SSH_MSG_CHANNEL_REQUEST
  3920. uint32 recipient channel
  3921. string "subsystem"
  3922. boolean want reply
  3923. string subsystem name
  3924. */
  3925. wantReply = payload[payload._pos++];
  3926. if (wantReply === undefined)
  3927. return false;
  3928. wantReply = (wantReply !== 0);
  3929. var subsystem = readString(payload, payload._pos, 'utf8', self, callback);
  3930. if (subsystem === false)
  3931. return false;
  3932. info = {
  3933. recipient: recipient,
  3934. request: request,
  3935. wantReply: wantReply,
  3936. subsystem: subsystem
  3937. };
  3938. } else if (request === 'signal') { // Client->Server
  3939. /*
  3940. byte SSH_MSG_CHANNEL_REQUEST
  3941. uint32 recipient channel
  3942. string "signal"
  3943. boolean FALSE
  3944. string signal name (without the "SIG" prefix)
  3945. */
  3946. signal = readString(payload, ++payload._pos, 'ascii', self, callback);
  3947. if (signal === false)
  3948. return false;
  3949. info = {
  3950. recipient: recipient,
  3951. request: request,
  3952. wantReply: false,
  3953. signal: 'SIG' + signal
  3954. };
  3955. } else if (request === 'xon-xoff') { // Client->Server
  3956. /*
  3957. byte SSH_MSG_CHANNEL_REQUEST
  3958. uint32 recipient channel
  3959. string "xon-xoff"
  3960. boolean FALSE
  3961. boolean client can do
  3962. */
  3963. var clientControl = payload[++payload._pos];
  3964. if (clientControl === undefined)
  3965. return false;
  3966. clientControl = (clientControl !== 0);
  3967. info = {
  3968. recipient: recipient,
  3969. request: request,
  3970. wantReply: false,
  3971. clientControl: clientControl
  3972. };
  3973. } else if (request === 'auth-agent-req@openssh.com') { // Client->Server
  3974. /*
  3975. byte SSH_MSG_CHANNEL_REQUEST
  3976. uint32 recipient channel
  3977. string "auth-agent-req@openssh.com"
  3978. boolean want reply
  3979. */
  3980. wantReply = payload[payload._pos];
  3981. if (wantReply === undefined)
  3982. return false;
  3983. wantReply = (wantReply !== 0);
  3984. info = {
  3985. recipient: recipient,
  3986. request: request,
  3987. wantReply: wantReply
  3988. };
  3989. } else {
  3990. // Unknown request type
  3991. wantReply = payload[payload._pos];
  3992. if (wantReply === undefined)
  3993. return false;
  3994. wantReply = (wantReply !== 0);
  3995. info = {
  3996. recipient: recipient,
  3997. request: request,
  3998. wantReply: wantReply
  3999. };
  4000. }
  4001. self.debug('DEBUG: Parser: IN_PACKETDATAAFTER, packet: CHANNEL_REQUEST ('
  4002. + recipient
  4003. + ', '
  4004. + request
  4005. + ')');
  4006. self.emit('CHANNEL_REQUEST:' + recipient, info);
  4007. }
  4008. function hmacVerify(self, data) {
  4009. var instate = self._state.incoming;
  4010. var hmac = instate.hmac;
  4011. self.debug('DEBUG: Parser: Verifying MAC');
  4012. if (instate.decrypt.info.authLen > 0) {
  4013. var decrypt = instate.decrypt;
  4014. var instance = decrypt.instance;
  4015. instance.setAuthTag(data);
  4016. var payload = instance.update(instate.packet);
  4017. instate.payload = payload.slice(1, instate.packet.length + 4 - payload[0]);
  4018. iv_inc(decrypt.iv);
  4019. decrypt.instance = crypto.createDecipheriv(
  4020. SSH_TO_OPENSSL[decrypt.type],
  4021. decrypt.key,
  4022. decrypt.iv
  4023. );
  4024. decrypt.instance.setAutoPadding(false);
  4025. return true;
  4026. } else {
  4027. var calcHmac = crypto.createHmac(SSH_TO_OPENSSL[hmac.type], hmac.key);
  4028. writeUInt32BE(HMAC_COMPUTE, instate.seqno, 0);
  4029. writeUInt32BE(HMAC_COMPUTE, instate.pktLen, 4);
  4030. HMAC_COMPUTE[8] = instate.padLen;
  4031. calcHmac.update(HMAC_COMPUTE);
  4032. calcHmac.update(instate.packet);
  4033. var mac = calcHmac.digest();
  4034. if (mac.length > instate.hmac.info.actualLen)
  4035. mac = mac.slice(0, instate.hmac.info.actualLen);
  4036. return timingSafeEqual(mac, data);
  4037. }
  4038. }
  4039. function decryptData(self, data) {
  4040. var instance = self._state.incoming.decrypt.instance;
  4041. self.debug('DEBUG: Parser: Decrypting');
  4042. return instance.update(data);
  4043. }
  4044. function expectData(self, type, amount, buffer) {
  4045. var expect = self._state.incoming.expect;
  4046. expect.amount = amount;
  4047. expect.type = type;
  4048. expect.ptr = 0;
  4049. if (buffer)
  4050. expect.buf = buffer;
  4051. else if (amount)
  4052. expect.buf = Buffer.allocUnsafe(amount);
  4053. }
  4054. function readList(buffer, start, stream, callback) {
  4055. var list = readString(buffer, start, 'ascii', stream, callback);
  4056. return (list !== false ? (list.length ? list.split(',') : []) : false);
  4057. }
  4058. function bytesToModes(buffer) {
  4059. var modes = {};
  4060. for (var i = 0, len = buffer.length, opcode; i < len; i += 5) {
  4061. opcode = buffer[i];
  4062. if (opcode === TERMINAL_MODE.TTY_OP_END
  4063. || TERMINAL_MODE[opcode] === undefined
  4064. || i + 5 > len)
  4065. break;
  4066. modes[TERMINAL_MODE[opcode]] = readUInt32BE(buffer, i + 1);
  4067. }
  4068. return modes;
  4069. }
  4070. function modesToBytes(modes) {
  4071. var RE_IS_NUM = /^\d+$/;
  4072. var keys = Object.keys(modes);
  4073. var b = 0;
  4074. var bytes = [];
  4075. for (var i = 0, len = keys.length, key, opcode, val; i < len; ++i) {
  4076. key = keys[i];
  4077. opcode = TERMINAL_MODE[key];
  4078. if (opcode
  4079. && !RE_IS_NUM.test(key)
  4080. && typeof modes[key] === 'number'
  4081. && key !== 'TTY_OP_END') {
  4082. val = modes[key];
  4083. bytes[b++] = opcode;
  4084. bytes[b++] = (val >>> 24) & 0xFF;
  4085. bytes[b++] = (val >>> 16) & 0xFF;
  4086. bytes[b++] = (val >>> 8) & 0xFF;
  4087. bytes[b++] = val & 0xFF;
  4088. }
  4089. }
  4090. bytes[b] = TERMINAL_MODE.TTY_OP_END;
  4091. return bytes;
  4092. }
  4093. // Shared outgoing functions
  4094. function KEXINIT(self, cb) { // Client/Server
  4095. randBytes(16, function(myCookie) {
  4096. /*
  4097. byte SSH_MSG_KEXINIT
  4098. byte[16] cookie (random bytes)
  4099. name-list kex_algorithms
  4100. name-list server_host_key_algorithms
  4101. name-list encryption_algorithms_client_to_server
  4102. name-list encryption_algorithms_server_to_client
  4103. name-list mac_algorithms_client_to_server
  4104. name-list mac_algorithms_server_to_client
  4105. name-list compression_algorithms_client_to_server
  4106. name-list compression_algorithms_server_to_client
  4107. name-list languages_client_to_server
  4108. name-list languages_server_to_client
  4109. boolean first_kex_packet_follows
  4110. uint32 0 (reserved for future extension)
  4111. */
  4112. var algos = self.config.algorithms;
  4113. var kexBuf = algos.kexBuf;
  4114. if (self.remoteBugs & BUGS.BAD_DHGEX) {
  4115. var copied = false;
  4116. var kexList = algos.kex;
  4117. for (var j = kexList.length - 1; j >= 0; --j) {
  4118. if (kexList[j].indexOf('group-exchange') !== -1) {
  4119. if (!copied) {
  4120. kexList = kexList.slice();
  4121. copied = true;
  4122. }
  4123. kexList.splice(j, 1);
  4124. }
  4125. }
  4126. if (copied)
  4127. kexBuf = Buffer.from(kexList.join(','));
  4128. }
  4129. var hostKeyBuf = algos.serverHostKeyBuf;
  4130. var kexInitSize = 1 + 16
  4131. + 4 + kexBuf.length
  4132. + 4 + hostKeyBuf.length
  4133. + (2 * (4 + algos.cipherBuf.length))
  4134. + (2 * (4 + algos.hmacBuf.length))
  4135. + (2 * (4 + algos.compressBuf.length))
  4136. + (2 * (4 /* languages skipped */))
  4137. + 1 + 4;
  4138. var buf = Buffer.allocUnsafe(kexInitSize);
  4139. var p = 17;
  4140. buf[0] = MESSAGE.KEXINIT;
  4141. if (myCookie !== false)
  4142. myCookie.copy(buf, 1);
  4143. writeUInt32BE(buf, kexBuf.length, p);
  4144. p += 4;
  4145. kexBuf.copy(buf, p);
  4146. p += kexBuf.length;
  4147. writeUInt32BE(buf, hostKeyBuf.length, p);
  4148. p += 4;
  4149. hostKeyBuf.copy(buf, p);
  4150. p += hostKeyBuf.length;
  4151. writeUInt32BE(buf, algos.cipherBuf.length, p);
  4152. p += 4;
  4153. algos.cipherBuf.copy(buf, p);
  4154. p += algos.cipherBuf.length;
  4155. writeUInt32BE(buf, algos.cipherBuf.length, p);
  4156. p += 4;
  4157. algos.cipherBuf.copy(buf, p);
  4158. p += algos.cipherBuf.length;
  4159. writeUInt32BE(buf, algos.hmacBuf.length, p);
  4160. p += 4;
  4161. algos.hmacBuf.copy(buf, p);
  4162. p += algos.hmacBuf.length;
  4163. writeUInt32BE(buf, algos.hmacBuf.length, p);
  4164. p += 4;
  4165. algos.hmacBuf.copy(buf, p);
  4166. p += algos.hmacBuf.length;
  4167. writeUInt32BE(buf, algos.compressBuf.length, p);
  4168. p += 4;
  4169. algos.compressBuf.copy(buf, p);
  4170. p += algos.compressBuf.length;
  4171. writeUInt32BE(buf, algos.compressBuf.length, p);
  4172. p += 4;
  4173. algos.compressBuf.copy(buf, p);
  4174. p += algos.compressBuf.length;
  4175. // Skip language lists, first_kex_packet_follows, and reserved bytes
  4176. buf.fill(0, buf.length - 13);
  4177. self.debug('DEBUG: Outgoing: Writing KEXINIT');
  4178. self._state.incoming.expectedPacket = 'KEXINIT';
  4179. var outstate = self._state.outgoing;
  4180. outstate.kexinit = buf;
  4181. if (outstate.status === OUT_READY) {
  4182. // We are the one starting the rekeying process ...
  4183. outstate.status = OUT_REKEYING;
  4184. }
  4185. send(self, buf, cb, true);
  4186. });
  4187. return true;
  4188. }
  4189. function KEXDH_INIT(self) { // Client
  4190. var state = self._state;
  4191. var outstate = state.outgoing;
  4192. var buf = Buffer.allocUnsafe(1 + 4 + outstate.pubkey.length);
  4193. if (RE_GEX.test(state.kexdh)) {
  4194. state.incoming.expectedPacket = 'KEXDH_GEX_REPLY';
  4195. buf[0] = MESSAGE.KEXDH_GEX_INIT;
  4196. self.debug('DEBUG: Outgoing: Writing KEXDH_GEX_INIT');
  4197. } else {
  4198. state.incoming.expectedPacket = 'KEXDH_REPLY';
  4199. buf[0] = MESSAGE.KEXDH_INIT;
  4200. if (state.kexdh !== 'group')
  4201. self.debug('DEBUG: Outgoing: Writing KEXECDH_INIT');
  4202. else
  4203. self.debug('DEBUG: Outgoing: Writing KEXDH_INIT');
  4204. }
  4205. writeUInt32BE(buf, outstate.pubkey.length, 1);
  4206. outstate.pubkey.copy(buf, 5);
  4207. return send(self, buf, undefined, true);
  4208. }
  4209. function KEXDH_REPLY(self, e) { // Server
  4210. var state = self._state;
  4211. var outstate = state.outgoing;
  4212. var instate = state.incoming;
  4213. var curHostKey = self.config.hostKeys[state.hostkeyFormat];
  4214. if (Array.isArray(curHostKey))
  4215. curHostKey = curHostKey[0];
  4216. var hostkey = curHostKey.getPublicSSH();
  4217. var hostkeyAlgo = curHostKey.type;
  4218. // e === client DH public key
  4219. var slicepos = -1;
  4220. for (var i = 0, len = e.length; i < len; ++i) {
  4221. if (e[i] === 0)
  4222. ++slicepos;
  4223. else
  4224. break;
  4225. }
  4226. if (slicepos > -1)
  4227. e = e.slice(slicepos + 1);
  4228. var secret = tryComputeSecret(state.kex, e);
  4229. if (secret instanceof Error) {
  4230. secret.message = 'Error while computing DH secret ('
  4231. + state.kexdh + '): '
  4232. + secret.message;
  4233. secret.level = 'handshake';
  4234. self.emit('error', secret);
  4235. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  4236. return false;
  4237. }
  4238. var hashAlgo;
  4239. if (state.kexdh === 'group')
  4240. hashAlgo = 'sha1';
  4241. else
  4242. hashAlgo = RE_KEX_HASH.exec(state.kexdh)[1];
  4243. var hash = crypto.createHash(hashAlgo);
  4244. var len_ident = Buffer.byteLength(instate.identRaw);
  4245. var len_sident = Buffer.byteLength(self.config.ident);
  4246. var len_init = instate.kexinit.length;
  4247. var len_sinit = outstate.kexinit.length;
  4248. var len_hostkey = hostkey.length;
  4249. var len_pubkey = e.length;
  4250. var len_spubkey = outstate.pubkey.length;
  4251. var len_secret = secret.length;
  4252. var idx_spubkey = 0;
  4253. var idx_secret = 0;
  4254. while (outstate.pubkey[idx_spubkey] === 0x00) {
  4255. ++idx_spubkey;
  4256. --len_spubkey;
  4257. }
  4258. while (secret[idx_secret] === 0x00) {
  4259. ++idx_secret;
  4260. --len_secret;
  4261. }
  4262. if (e[0] & 0x80)
  4263. ++len_pubkey;
  4264. if (outstate.pubkey[idx_spubkey] & 0x80)
  4265. ++len_spubkey;
  4266. if (secret[idx_secret] & 0x80)
  4267. ++len_secret;
  4268. var exchangeBufLen = len_ident
  4269. + len_sident
  4270. + len_init
  4271. + len_sinit
  4272. + len_hostkey
  4273. + len_pubkey
  4274. + len_spubkey
  4275. + len_secret
  4276. + (4 * 8); // Length fields for above values
  4277. // Group exchange-related
  4278. var isGEX = RE_GEX.test(state.kexdh);
  4279. var len_gex_prime = 0;
  4280. var len_gex_gen = 0;
  4281. var idx_gex_prime = 0;
  4282. var idx_gex_gen = 0;
  4283. var gex_prime;
  4284. var gex_gen;
  4285. if (isGEX) {
  4286. gex_prime = state.kex.getPrime();
  4287. gex_gen = state.kex.getGenerator();
  4288. len_gex_prime = gex_prime.length;
  4289. len_gex_gen = gex_gen.length;
  4290. while (gex_prime[idx_gex_prime] === 0x00) {
  4291. ++idx_gex_prime;
  4292. --len_gex_prime;
  4293. }
  4294. while (gex_gen[idx_gex_gen] === 0x00) {
  4295. ++idx_gex_gen;
  4296. --len_gex_gen;
  4297. }
  4298. if (gex_prime[idx_gex_prime] & 0x80)
  4299. ++len_gex_prime;
  4300. if (gex_gen[idx_gex_gen] & 0x80)
  4301. ++len_gex_gen;
  4302. exchangeBufLen += (4 * 3); // min, n, max values
  4303. exchangeBufLen += (4 * 2); // prime, generator length fields
  4304. exchangeBufLen += len_gex_prime;
  4305. exchangeBufLen += len_gex_gen;
  4306. }
  4307. var bp = 0;
  4308. var exchangeBuf = Buffer.allocUnsafe(exchangeBufLen);
  4309. writeUInt32BE(exchangeBuf, len_ident, bp);
  4310. bp += 4;
  4311. exchangeBuf.write(instate.identRaw, bp, 'utf8'); // V_C
  4312. bp += len_ident;
  4313. writeUInt32BE(exchangeBuf, len_sident, bp);
  4314. bp += 4;
  4315. exchangeBuf.write(self.config.ident, bp, 'utf8'); // V_S
  4316. bp += len_sident;
  4317. writeUInt32BE(exchangeBuf, len_init, bp);
  4318. bp += 4;
  4319. instate.kexinit.copy(exchangeBuf, bp); // I_C
  4320. bp += len_init;
  4321. instate.kexinit = undefined;
  4322. writeUInt32BE(exchangeBuf, len_sinit, bp);
  4323. bp += 4;
  4324. outstate.kexinit.copy(exchangeBuf, bp); // I_S
  4325. bp += len_sinit;
  4326. outstate.kexinit = undefined;
  4327. writeUInt32BE(exchangeBuf, len_hostkey, bp);
  4328. bp += 4;
  4329. hostkey.copy(exchangeBuf, bp); // K_S
  4330. bp += len_hostkey;
  4331. if (isGEX) {
  4332. KEXDH_GEX_REQ_PACKET.slice(1).copy(exchangeBuf, bp); // min, n, max
  4333. bp += (4 * 3); // Skip over bytes just copied
  4334. writeUInt32BE(exchangeBuf, len_gex_prime, bp);
  4335. bp += 4;
  4336. if (gex_prime[idx_gex_prime] & 0x80)
  4337. exchangeBuf[bp++] = 0;
  4338. gex_prime.copy(exchangeBuf, bp, idx_gex_prime); // p
  4339. bp += len_gex_prime - (gex_prime[idx_gex_prime] & 0x80 ? 1 : 0);
  4340. writeUInt32BE(exchangeBuf, len_gex_gen, bp);
  4341. bp += 4;
  4342. if (gex_gen[idx_gex_gen] & 0x80)
  4343. exchangeBuf[bp++] = 0;
  4344. gex_gen.copy(exchangeBuf, bp, idx_gex_gen); // g
  4345. bp += len_gex_gen - (gex_gen[idx_gex_gen] & 0x80 ? 1 : 0);
  4346. }
  4347. writeUInt32BE(exchangeBuf, len_pubkey, bp);
  4348. bp += 4;
  4349. if (e[0] & 0x80)
  4350. exchangeBuf[bp++] = 0;
  4351. e.copy(exchangeBuf, bp); // e
  4352. bp += len_pubkey - (e[0] & 0x80 ? 1 : 0);
  4353. writeUInt32BE(exchangeBuf, len_spubkey, bp);
  4354. bp += 4;
  4355. if (outstate.pubkey[idx_spubkey] & 0x80)
  4356. exchangeBuf[bp++] = 0;
  4357. outstate.pubkey.copy(exchangeBuf, bp, idx_spubkey); // f
  4358. bp += len_spubkey - (outstate.pubkey[idx_spubkey] & 0x80 ? 1 : 0);
  4359. writeUInt32BE(exchangeBuf, len_secret, bp);
  4360. bp += 4;
  4361. if (secret[idx_secret] & 0x80)
  4362. exchangeBuf[bp++] = 0;
  4363. secret.copy(exchangeBuf, bp, idx_secret); // K
  4364. outstate.exchangeHash = hash.update(exchangeBuf).digest(); // H
  4365. if (outstate.sessionId === undefined)
  4366. outstate.sessionId = outstate.exchangeHash;
  4367. outstate.kexsecret = secret;
  4368. var signature = curHostKey.sign(outstate.exchangeHash);
  4369. if (signature instanceof Error) {
  4370. signature.message = 'Error while signing data with host key ('
  4371. + hostkeyAlgo + '): '
  4372. + signature.message;
  4373. signature.level = 'handshake';
  4374. self.emit('error', signature);
  4375. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  4376. return false;
  4377. }
  4378. signature = convertSignature(signature, hostkeyAlgo);
  4379. if (signature === false) {
  4380. signature.message = 'Error while converting handshake signature';
  4381. signature.level = 'handshake';
  4382. self.emit('error', signature);
  4383. self.disconnect(DISCONNECT_REASON.KEY_EXCHANGE_FAILED);
  4384. return false;
  4385. }
  4386. /*
  4387. byte SSH_MSG_KEXDH_REPLY
  4388. string server public host key and certificates (K_S)
  4389. mpint f
  4390. string signature of H
  4391. */
  4392. var siglen = 4 + hostkeyAlgo.length + 4 + signature.length;
  4393. var buf = Buffer.allocUnsafe(1
  4394. + 4 + len_hostkey
  4395. + 4 + len_spubkey
  4396. + 4 + siglen);
  4397. bp = 0;
  4398. buf[bp] = (!isGEX ? MESSAGE.KEXDH_REPLY : MESSAGE.KEXDH_GEX_REPLY);
  4399. ++bp;
  4400. writeUInt32BE(buf, len_hostkey, bp);
  4401. bp += 4;
  4402. hostkey.copy(buf, bp); // K_S
  4403. bp += len_hostkey;
  4404. writeUInt32BE(buf, len_spubkey, bp);
  4405. bp += 4;
  4406. if (outstate.pubkey[idx_spubkey] & 0x80)
  4407. buf[bp++] = 0;
  4408. outstate.pubkey.copy(buf, bp, idx_spubkey); // f
  4409. bp += len_spubkey - (outstate.pubkey[idx_spubkey] & 0x80 ? 1 : 0);
  4410. writeUInt32BE(buf, siglen, bp);
  4411. bp += 4;
  4412. writeUInt32BE(buf, hostkeyAlgo.length, bp);
  4413. bp += 4;
  4414. buf.write(hostkeyAlgo, bp, hostkeyAlgo.length, 'ascii');
  4415. bp += hostkeyAlgo.length;
  4416. writeUInt32BE(buf, signature.length, bp);
  4417. bp += 4;
  4418. signature.copy(buf, bp);
  4419. state.incoming.expectedPacket = 'NEWKEYS';
  4420. if (isGEX)
  4421. self.debug('DEBUG: Outgoing: Writing KEXDH_GEX_REPLY');
  4422. else if (state.kexdh !== 'group')
  4423. self.debug('DEBUG: Outgoing: Writing KEXECDH_REPLY');
  4424. else
  4425. self.debug('DEBUG: Outgoing: Writing KEXDH_REPLY');
  4426. send(self, buf, undefined, true);
  4427. outstate.sentNEWKEYS = true;
  4428. self.debug('DEBUG: Outgoing: Writing NEWKEYS');
  4429. return send(self, NEWKEYS_PACKET, undefined, true);
  4430. }
  4431. function KEXDH_GEX_REQ(self) { // Client
  4432. self._state.incoming.expectedPacket = 'KEXDH_GEX_GROUP';
  4433. self.debug('DEBUG: Outgoing: Writing KEXDH_GEX_REQUEST');
  4434. return send(self, KEXDH_GEX_REQ_PACKET, undefined, true);
  4435. }
  4436. function compressPayload(self, payload, cb) {
  4437. var compress = self._state.outgoing.compress.instance;
  4438. compress.write(payload);
  4439. compress.flush(Z_PARTIAL_FLUSH, compressFlushCb.bind(self, cb));
  4440. }
  4441. function compressFlushCb(cb) {
  4442. if (this._readableState.ended || this._writableState.ended)
  4443. return;
  4444. send_(this, this._state.outgoing.compress.instance.read(), cb);
  4445. var queue = this._state.outgoing.compress.queue;
  4446. queue.shift();
  4447. if (queue.length > 0)
  4448. compressPayload(this, queue[0][0], queue[0][1]);
  4449. }
  4450. function send(self, payload, cb, bypass) {
  4451. var state = self._state;
  4452. if (!state)
  4453. return false;
  4454. var outstate = state.outgoing;
  4455. if (outstate.status === OUT_REKEYING && !bypass) {
  4456. if (typeof cb === 'function')
  4457. outstate.rekeyQueue.push([payload, cb]);
  4458. else
  4459. outstate.rekeyQueue.push(payload);
  4460. return false;
  4461. } else if (self._readableState.ended || self._writableState.ended) {
  4462. return false;
  4463. }
  4464. if (outstate.compress.instance) {
  4465. // This queue nonsense only exists because of a change made in node v10.12.0
  4466. // that changed flushing behavior, which now coalesces multiple writes to a
  4467. // single flush, which does not work for us.
  4468. var queue = outstate.compress.queue;
  4469. queue.push([payload, cb]);
  4470. if (queue.length === 1)
  4471. compressPayload(self, queue[0][0], queue[0][1]);
  4472. return true;
  4473. } else {
  4474. return send_(self, payload, cb);
  4475. }
  4476. }
  4477. function send_(self, payload, cb) {
  4478. // TODO: Implement length checks
  4479. var state = self._state;
  4480. var outstate = state.outgoing;
  4481. var encrypt = outstate.encrypt;
  4482. var hmac = outstate.hmac;
  4483. var pktLen;
  4484. var padLen;
  4485. var buf;
  4486. var mac;
  4487. var ret;
  4488. pktLen = payload.length + 9;
  4489. if (encrypt.instance !== false) {
  4490. if (encrypt.info.authLen > 0) {
  4491. var ptlen = 1 + payload.length + 4/* Must have at least 4 bytes padding*/;
  4492. while ((ptlen % encrypt.info.blockLen) !== 0)
  4493. ++ptlen;
  4494. padLen = ptlen - 1 - payload.length;
  4495. pktLen = 4 + ptlen;
  4496. } else {
  4497. var blockLen = encrypt.info.blockLen;
  4498. pktLen += ((blockLen - 1) * pktLen) % blockLen;
  4499. padLen = pktLen - payload.length - 5;
  4500. }
  4501. } else {
  4502. pktLen += (7 * pktLen) % 8;
  4503. padLen = pktLen - payload.length - 5;
  4504. }
  4505. buf = Buffer.allocUnsafe(pktLen);
  4506. writeUInt32BE(buf, pktLen - 4, 0);
  4507. buf[4] = padLen;
  4508. payload.copy(buf, 5);
  4509. copyRandPadBytes(buf, 5 + payload.length, padLen);
  4510. if (hmac.type !== false && hmac.key) {
  4511. mac = crypto.createHmac(SSH_TO_OPENSSL[hmac.type], hmac.key);
  4512. writeUInt32BE(outstate.bufSeqno, outstate.seqno, 0);
  4513. mac.update(outstate.bufSeqno);
  4514. mac.update(buf);
  4515. mac = mac.digest();
  4516. if (mac.length > hmac.info.actualLen)
  4517. mac = mac.slice(0, hmac.info.actualLen);
  4518. }
  4519. var nb = 0;
  4520. var encData;
  4521. if (encrypt.instance !== false) {
  4522. if (encrypt.info.authLen > 0) {
  4523. var encrypter = crypto.createCipheriv(SSH_TO_OPENSSL[encrypt.type],
  4524. encrypt.key,
  4525. encrypt.iv);
  4526. encrypter.setAutoPadding(false);
  4527. var lenbuf = buf.slice(0, 4);
  4528. encrypter.setAAD(lenbuf);
  4529. self.push(lenbuf);
  4530. nb += lenbuf;
  4531. encData = encrypter.update(buf.slice(4));
  4532. self.push(encData);
  4533. nb += encData.length;
  4534. var final = encrypter.final();
  4535. if (final.length) {
  4536. self.push(final);
  4537. nb += final.length;
  4538. }
  4539. var authTag = encrypter.getAuthTag();
  4540. ret = self.push(authTag);
  4541. nb += authTag.length;
  4542. iv_inc(encrypt.iv);
  4543. } else {
  4544. encData = encrypt.instance.update(buf);
  4545. self.push(encData);
  4546. nb += encData.length;
  4547. ret = self.push(mac);
  4548. nb += mac.length;
  4549. }
  4550. } else {
  4551. ret = self.push(buf);
  4552. nb = buf.length;
  4553. }
  4554. self.bytesSent += nb;
  4555. if (++outstate.seqno > MAX_SEQNO)
  4556. outstate.seqno = 0;
  4557. cb && cb();
  4558. return ret;
  4559. }
  4560. var copyRandPadBytes = (function() {
  4561. if (typeof crypto.randomFillSync === 'function') {
  4562. return crypto.randomFillSync;
  4563. } else {
  4564. return function copyRandPadBytes(buf, offset, count) {
  4565. var padBytes = crypto.randomBytes(count);
  4566. padBytes.copy(buf, offset);
  4567. };
  4568. }
  4569. })();
  4570. function randBytes(n, cb) {
  4571. crypto.randomBytes(n, function retry(err, buf) {
  4572. if (err)
  4573. return crypto.randomBytes(n, retry);
  4574. cb && cb(buf);
  4575. });
  4576. }
  4577. function tryComputeSecret(dh, e) {
  4578. try {
  4579. return dh.computeSecret(e);
  4580. } catch (err) {
  4581. return err;
  4582. }
  4583. }
  4584. function convertSignature(signature, keyType) {
  4585. switch (keyType) {
  4586. case 'ssh-dss':
  4587. return DSASigBERToBare(signature);
  4588. case 'ecdsa-sha2-nistp256':
  4589. case 'ecdsa-sha2-nistp384':
  4590. case 'ecdsa-sha2-nistp521':
  4591. return ECDSASigASN1ToSSH(signature);
  4592. }
  4593. return signature;
  4594. }
  4595. var timingSafeEqual = (function() {
  4596. if (typeof crypto.timingSafeEqual === 'function') {
  4597. return function timingSafeEquals(a, b) {
  4598. if (a.length !== b.length) {
  4599. crypto.timingSafeEqual(a, a);
  4600. return false;
  4601. } else {
  4602. return crypto.timingSafeEqual(a, b);
  4603. }
  4604. };
  4605. } else {
  4606. return function timingSafeEquals(a, b) {
  4607. var val;
  4608. if (a.length === b.length) {
  4609. val = 0;
  4610. } else {
  4611. val = 1;
  4612. b = a;
  4613. }
  4614. for (var i = 0, len = a.length; i < len; ++i)
  4615. val |= (a[i] ^ b[i]);
  4616. return (val === 0);
  4617. }
  4618. }
  4619. })();
  4620. module.exports = SSH2Stream;
  4621. module.exports._send = send;