ssh.js 157 KB

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