sshd.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "libwebsockets.h"
  25. #include "lws-ssh.h"
  26. #include <string.h>
  27. #include <stdlib.h>
  28. void *sshd_zalloc(size_t s)
  29. {
  30. void *p = malloc(s);
  31. if (p)
  32. memset(p, 0, s);
  33. return p;
  34. }
  35. uint32_t
  36. lws_g32(uint8_t **p)
  37. {
  38. uint32_t v = 0;
  39. v = (v << 8) | *((*p)++);
  40. v = (v << 8) | *((*p)++);
  41. v = (v << 8) | *((*p)++);
  42. v = (v << 8) | *((*p)++);
  43. return v;
  44. }
  45. uint32_t
  46. lws_p32(uint8_t *p, uint32_t v)
  47. {
  48. *p++ = v >> 24;
  49. *p++ = v >> 16;
  50. *p++ = v >> 8;
  51. *p++ = v;
  52. return v;
  53. }
  54. int
  55. lws_cstr(uint8_t **p, const char *s, uint32_t max)
  56. {
  57. uint32_t n = (uint32_t)strlen(s);
  58. if (n > max)
  59. return 1;
  60. lws_p32(*p, n);
  61. *p += 4;
  62. strcpy((char *)(*p), s);
  63. *p += n;
  64. return 0;
  65. }
  66. int
  67. lws_buf(uint8_t **p, void *s, uint32_t len)
  68. {
  69. lws_p32(*p, len);
  70. *p += 4;
  71. memcpy((char *)(*p), s, len);
  72. *p += len;
  73. return 0;
  74. }
  75. void
  76. write_task(struct per_session_data__sshd *pss, struct lws_ssh_channel *ch,
  77. int task)
  78. {
  79. pss->write_task[pss->wt_head] = task;
  80. pss->write_channel[pss->wt_head] = ch;
  81. pss->wt_head = (pss->wt_head + 1) & 7;
  82. lws_callback_on_writable(pss->wsi);
  83. }
  84. void
  85. write_task_insert(struct per_session_data__sshd *pss, struct lws_ssh_channel *ch,
  86. int task)
  87. {
  88. pss->wt_tail = (pss->wt_tail - 1) & 7;
  89. pss->write_task[pss->wt_tail] = task;
  90. pss->write_channel[pss->wt_tail] = ch;
  91. lws_callback_on_writable(pss->wsi);
  92. }
  93. void
  94. lws_pad_set_length(struct per_session_data__sshd *pss, void *start, uint8_t **p,
  95. struct lws_ssh_keys *keys)
  96. {
  97. uint32_t len = lws_ptr_diff(*p, start);
  98. uint8_t padc = 4, *bs = start;
  99. if (keys->full_length)
  100. len -= 4;
  101. if ((len + padc) & (keys->padding_alignment - 1))
  102. padc += keys->padding_alignment -
  103. ((len + padc) & (keys->padding_alignment - 1));
  104. bs[4] = padc;
  105. len += padc;
  106. if (!keys->valid) /* no crypto = pad with 00 */
  107. while (padc--)
  108. *((*p)++) = 0;
  109. else { /* crypto active = pad with random */
  110. lws_get_random(pss->vhd->context, *p, padc);
  111. (*p) += padc;
  112. }
  113. if (keys->full_length)
  114. len += 4;
  115. lws_p32(start, len - 4);
  116. }
  117. static uint32_t
  118. offer(struct per_session_data__sshd *pss, uint8_t *p, uint32_t len, int first,
  119. int *payload_len)
  120. {
  121. uint8_t *op = p, *lp, *end = p + len - 1;
  122. int n, padc = 4, keylen;
  123. char keyt[32];
  124. uint8_t keybuf[256];
  125. keylen = (int)get_gen_server_key_25519(pss, keybuf, (int)sizeof(keybuf));
  126. if (!keylen) {
  127. lwsl_notice("get_gen_server_key failed\n");
  128. return 1;
  129. }
  130. lwsl_info("keylen %d\n", keylen);
  131. n = ed25519_key_parse(keybuf, keylen,
  132. keyt, sizeof(keyt), NULL, NULL);
  133. if (n) {
  134. lwsl_notice("unable to parse server key: %d\n", n);
  135. return 1;
  136. }
  137. /*
  138. * byte SSH_MSG_KEXINIT
  139. * byte[16] cookie (random bytes)
  140. * name-list kex_algorithms
  141. * name-list server_host_key_algorithms
  142. * name-list encryption_algorithms_client_to_server
  143. * name-list encryption_algorithms_server_to_client
  144. * name-list mac_algorithms_client_to_server
  145. * name-list mac_algorithms_server_to_client
  146. * name-list compression_algorithms_client_to_server
  147. * name-list compression_algorithms_server_to_client
  148. * name-list langua->es_client_to_server
  149. * name-list langua->es_server_to_client
  150. * boolean first_kex_packet_follows
  151. * uint32 0 (reserved for future extension)
  152. */
  153. p += 5; /* msg len + padding */
  154. *p++ = SSH_MSG_KEXINIT;
  155. lws_get_random(pss->vhd->context, p, 16);
  156. p += 16;
  157. /* KEX algorithms */
  158. lp = p;
  159. p += 4;
  160. n = lws_snprintf((char *)p, end - p, "[email protected]");
  161. p += lws_p32(lp, n);
  162. /* Server Host Key Algorithms */
  163. lp = p;
  164. p += 4;
  165. n = lws_snprintf((char *)p, end - p, "%s", keyt);
  166. p += lws_p32(lp, n);
  167. /* Encryption Algorithms: C -> S */
  168. lp = p;
  169. p += 4;
  170. // n = lws_snprintf((char *)p, end - p, "[email protected]");
  171. n = lws_snprintf((char *)p, end - p, "[email protected]");
  172. p += lws_p32(lp, n);
  173. /* Encryption Algorithms: S -> C */
  174. lp = p;
  175. p += 4;
  176. // n = lws_snprintf((char *)p, end - p, "[email protected]");
  177. n = lws_snprintf((char *)p, end - p, "[email protected]");
  178. p += lws_p32(lp, n);
  179. /* MAC Algorithms: C -> S */
  180. lp = p;
  181. p += 4;
  182. /* bogus: chacha20 does not use MACs, but 'none' is not offered */
  183. n = lws_snprintf((char *)p, end - p, "hmac-sha2-256");
  184. p += lws_p32(lp, n);
  185. /* MAC Algorithms: S -> C */
  186. lp = p;
  187. p += 4;
  188. /* bogus: chacha20 does not use MACs, but 'none' is not offered */
  189. n = lws_snprintf((char *)p, end - p, "hmac-sha2-256");
  190. p += lws_p32(lp, n);
  191. /* Compression Algorithms: C -> S */
  192. lp = p;
  193. p += 4;
  194. n = lws_snprintf((char *)p, end - p, "none");
  195. p += lws_p32(lp, n);
  196. /* Compression Algorithms: S -> C */
  197. lp = p;
  198. p += 4;
  199. n = lws_snprintf((char *)p, end - p, "none");
  200. p += lws_p32(lp, n);
  201. if (p - op < 13 + padc + 8)
  202. return 0;
  203. /* Languages: C -> S */
  204. *p++ = 0;
  205. *p++ = 0;
  206. *p++ = 0;
  207. *p++ = 0;
  208. /* Languages: S -> C */
  209. *p++ = 0;
  210. *p++ = 0;
  211. *p++ = 0;
  212. *p++ = 0;
  213. /* First KEX packet coming */
  214. *p++ = !!first;
  215. /* Reserved */
  216. *p++ = 0;
  217. *p++ = 0;
  218. *p++ = 0;
  219. *p++ = 0;
  220. len = lws_ptr_diff(p, op);
  221. if (payload_len)
  222. /* starts at buf + 5 and excludes padding */
  223. *payload_len = len - 5;
  224. /* we must give at least 4 bytes of 00 padding */
  225. if ((len + padc) & 7)
  226. padc += 8 - ((len + padc) & 7);
  227. op[4] = padc;
  228. len += padc;
  229. while (padc--)
  230. *p++ = 0;
  231. /* recorded length does not include the uint32_t len itself */
  232. lws_p32(op, len - 4);
  233. return len;
  234. }
  235. static int
  236. handle_name(struct per_session_data__sshd *pss)
  237. {
  238. struct lws_kex *kex = pss->kex;
  239. char keyt[32];
  240. uint8_t keybuf[256];
  241. int n = 0, len;
  242. switch (pss->parser_state) {
  243. case SSH_KEX_NL_KEX_ALGS:
  244. if (!strcmp(pss->name, "[email protected]"))
  245. kex->match_bitfield |= 1;
  246. break;
  247. case SSH_KEX_NL_SHK_ALGS:
  248. len = (int)get_gen_server_key_25519(pss, keybuf, (int)sizeof(keybuf));
  249. if (!len)
  250. break;
  251. if (ed25519_key_parse(keybuf, len,
  252. keyt, sizeof(keyt),
  253. NULL, NULL)) {
  254. lwsl_err("Unable to parse host key %d\n", n);
  255. } else {
  256. if (!strcmp(pss->name, keyt)) {
  257. kex->match_bitfield |= 2;
  258. break;
  259. }
  260. }
  261. break;
  262. case SSH_KEX_NL_EACTS_ALGS:
  263. if (!strcmp(pss->name, "[email protected]"))
  264. kex->match_bitfield |= 4;
  265. break;
  266. case SSH_KEX_NL_EASTC_ALGS:
  267. if (!strcmp(pss->name, "[email protected]"))
  268. kex->match_bitfield |= 8;
  269. break;
  270. case SSH_KEX_NL_MACTS_ALGS:
  271. if (!strcmp(pss->name, "hmac-sha2-256"))
  272. kex->match_bitfield |= 16;
  273. break;
  274. case SSH_KEX_NL_MASTC_ALGS:
  275. if (!strcmp(pss->name, "hmac-sha2-256"))
  276. kex->match_bitfield |= 32;
  277. break;
  278. case SSH_KEX_NL_CACTS_ALGS:
  279. if (!strcmp(pss->name, "none"))
  280. kex->match_bitfield |= 64;
  281. break;
  282. case SSH_KEX_NL_CASTC_ALGS:
  283. if (!strcmp(pss->name, "none"))
  284. kex->match_bitfield |= 128;
  285. break;
  286. case SSH_KEX_NL_LCTS_ALGS:
  287. case SSH_KEX_NL_LSTC_ALGS:
  288. break;
  289. default:
  290. break;
  291. }
  292. return 0;
  293. }
  294. static int
  295. lws_kex_create(struct per_session_data__sshd *pss)
  296. {
  297. pss->kex = sshd_zalloc(sizeof(struct lws_kex));
  298. lwsl_info("%s\n", __func__);
  299. return !pss->kex;
  300. }
  301. static void
  302. lws_kex_destroy(struct per_session_data__sshd *pss)
  303. {
  304. if (!pss->kex)
  305. return;
  306. lwsl_info("Destroying KEX\n");
  307. if (pss->kex->I_C) {
  308. free(pss->kex->I_C);
  309. pss->kex->I_C = NULL;
  310. }
  311. if (pss->kex->I_S) {
  312. free(pss->kex->I_S);
  313. pss->kex->I_S = NULL;
  314. }
  315. lws_explicit_bzero(pss->kex, sizeof(*pss->kex));
  316. free(pss->kex);
  317. pss->kex = NULL;
  318. }
  319. static void
  320. ssh_free(void *p)
  321. {
  322. if (!p)
  323. return;
  324. lwsl_debug("%s: FREE %p\n", __func__, p);
  325. free(p);
  326. }
  327. #define ssh_free_set_NULL(x) if (x) { ssh_free(x); (x) = NULL; }
  328. static void
  329. lws_ua_destroy(struct per_session_data__sshd *pss)
  330. {
  331. if (!pss->ua)
  332. return;
  333. lwsl_info("%s\n", __func__);
  334. if (pss->ua->username)
  335. ssh_free(pss->ua->username);
  336. if (pss->ua->service)
  337. ssh_free(pss->ua->service);
  338. if (pss->ua->alg)
  339. ssh_free(pss->ua->alg);
  340. if (pss->ua->pubkey)
  341. ssh_free(pss->ua->pubkey);
  342. if (pss->ua->sig) {
  343. lws_explicit_bzero(pss->ua->sig, pss->ua->sig_len);
  344. ssh_free(pss->ua->sig);
  345. }
  346. lws_explicit_bzero(pss->ua, sizeof(*pss->ua));
  347. free(pss->ua);
  348. pss->ua = NULL;
  349. }
  350. static int
  351. rsa_hash_alg_from_ident(const char *ident)
  352. {
  353. if (strcmp(ident, "ssh-rsa") == 0 ||
  354. strcmp(ident, "[email protected]") == 0)
  355. return LWS_GENHASH_TYPE_SHA1;
  356. if (strcmp(ident, "rsa-sha2-256") == 0)
  357. return LWS_GENHASH_TYPE_SHA256;
  358. if (strcmp(ident, "rsa-sha2-512") == 0)
  359. return LWS_GENHASH_TYPE_SHA512;
  360. return -1;
  361. }
  362. static void
  363. state_get_string_alloc(struct per_session_data__sshd *pss, int next)
  364. {
  365. pss->parser_state = SSHS_GET_STRING_LEN_ALLOC;
  366. pss->state_after_string = next;
  367. }
  368. static void
  369. state_get_string(struct per_session_data__sshd *pss, int next)
  370. {
  371. pss->parser_state = SSHS_GET_STRING_LEN;
  372. pss->state_after_string = next;
  373. }
  374. static void
  375. state_get_u32(struct per_session_data__sshd *pss, int next)
  376. {
  377. pss->parser_state = SSHS_GET_U32;
  378. pss->state_after_string = next;
  379. }
  380. static struct lws_ssh_channel *
  381. ssh_get_server_ch(struct per_session_data__sshd *pss, uint32_t chi)
  382. {
  383. struct lws_ssh_channel *ch = pss->ch_list;
  384. while (ch) {
  385. if (ch->server_ch == chi)
  386. return ch;
  387. ch = ch->next;
  388. }
  389. return NULL;
  390. }
  391. #if 0
  392. static struct lws_ssh_channel *
  393. ssh_get_peer_ch(struct per_session_data__sshd *pss, uint32_t chi)
  394. {
  395. struct lws_ssh_channel *ch = pss->ch_list;
  396. while (ch) {
  397. if (ch->sender_ch == chi)
  398. return ch;
  399. ch = ch->next;
  400. }
  401. return NULL;
  402. }
  403. #endif
  404. static void
  405. ssh_destroy_channel(struct per_session_data__sshd *pss,
  406. struct lws_ssh_channel *ch)
  407. {
  408. lws_start_foreach_llp(struct lws_ssh_channel **, ppch, pss->ch_list) {
  409. if (*ppch == ch) {
  410. lwsl_info("Deleting ch %p\n", ch);
  411. if (pss->vhd && pss->vhd->ops &&
  412. pss->vhd->ops->channel_destroy)
  413. pss->vhd->ops->channel_destroy(ch->priv);
  414. *ppch = ch->next;
  415. if (ch->sub)
  416. free(ch->sub);
  417. free(ch);
  418. return;
  419. }
  420. } lws_end_foreach_llp(ppch, next);
  421. lwsl_notice("Failed to delete ch\n");
  422. }
  423. static void
  424. lws_ssh_exec_finish(void *finish_handle, int retcode)
  425. {
  426. struct lws_ssh_channel *ch = (struct lws_ssh_channel *)finish_handle;
  427. struct per_session_data__sshd *pss = ch->pss;
  428. ch->retcode = retcode;
  429. write_task(pss, ch, SSH_WT_EXIT_STATUS);
  430. ch->scheduled_close = 1;
  431. write_task(pss, ch, SSH_WT_CH_CLOSE);
  432. }
  433. static int
  434. lws_ssh_parse_plaintext(struct per_session_data__sshd *pss, uint8_t *p, size_t len)
  435. {
  436. struct lws_gencrypto_keyelem e[LWS_GENCRYPTO_RSA_KEYEL_COUNT];
  437. struct lws_genrsa_ctx ctx;
  438. struct lws_ssh_channel *ch;
  439. struct lws_subprotocol_scp *scp;
  440. uint8_t *pp, *ps, hash[64], *otmp;
  441. uint32_t m;
  442. int n;
  443. while (len --) {
  444. again:
  445. switch(pss->parser_state) {
  446. case SSH_INITIALIZE_TRANSIENT:
  447. pss->parser_state = SSHS_IDSTRING;
  448. pss->ctr = 0;
  449. pss->copy_to_I_C = 0;
  450. /* fallthru */
  451. case SSHS_IDSTRING:
  452. if (*p == 0x0d) {
  453. pss->V_C[pss->npos] = '\0';
  454. pss->npos = 0;
  455. lwsl_info("peer id: %s\n", pss->V_C);
  456. p++;
  457. pss->parser_state = SSHS_IDSTRING_CR;
  458. break;
  459. }
  460. if (pss->npos < sizeof(pss->V_C) - 1)
  461. pss->V_C[pss->npos++] = *p;
  462. p++;
  463. break;
  464. case SSHS_IDSTRING_CR:
  465. if (*p++ != 0x0a) {
  466. lwsl_notice("mangled id string\n");
  467. return 1;
  468. }
  469. pss->ssh_sequence_ctr_cts = 0;
  470. pss->parser_state = SSHS_MSG_LEN;
  471. break;
  472. case SSHS_MSG_LEN:
  473. pss->msg_len = (pss->msg_len << 8) | *p++;
  474. if (++pss->ctr != 4)
  475. break;
  476. if (pss->active_keys_cts.valid) {
  477. uint8_t b[4];
  478. POKE_U32(b, pss->msg_len);
  479. pss->msg_len = lws_chachapoly_get_length(
  480. &pss->active_keys_cts,
  481. pss->ssh_sequence_ctr_cts, b);
  482. } else
  483. pss->ssh_sequence_ctr_cts++;
  484. lwsl_info("msg len %d\n", pss->msg_len);
  485. pss->parser_state = SSHS_MSG_PADDING;
  486. pss->ctr = 0;
  487. pss->pos = 4;
  488. if (pss->msg_len < 2 + 4) {
  489. lwsl_notice("illegal msg size\n");
  490. goto bail;
  491. }
  492. break;
  493. case SSHS_MSG_PADDING:
  494. pss->msg_padding = *p++;
  495. pss->parser_state = SSHS_MSG_ID;
  496. break;
  497. case SSHS_MSG_ID:
  498. pss->msg_id = *p++;
  499. pss->ctr = 0;
  500. switch (pss->msg_id) {
  501. case SSH_MSG_DISCONNECT:
  502. /*
  503. * byte SSH_MSG_DISCONNECT
  504. * uint32 reason code
  505. * string description in ISO-10646
  506. * UTF-8 encoding [RFC3629]
  507. * string language tag [RFC3066]
  508. */
  509. lwsl_notice("SSH_MSG_DISCONNECT\n");
  510. state_get_u32(pss, SSHS_NVC_DISCONNECT_REASON);
  511. break;
  512. case SSH_MSG_IGNORE:
  513. lwsl_notice("SSH_MSG_IGNORE\n");
  514. break;
  515. case SSH_MSG_UNIMPLEMENTED:
  516. lwsl_notice("SSH_MSG_UNIMPLEMENTED\n");
  517. break;
  518. case SSH_MSG_DEBUG:
  519. lwsl_notice("SSH_MSG_DEBUG\n");
  520. break;
  521. case SSH_MSG_SERVICE_REQUEST:
  522. lwsl_info("SSH_MSG_SERVICE_REQUEST\n");
  523. /* payload is a string */
  524. state_get_string(pss, SSHS_DO_SERVICE_REQUEST);
  525. break;
  526. case SSH_MSG_SERVICE_ACCEPT:
  527. lwsl_notice("SSH_MSG_ACCEPT\n");
  528. break;
  529. case SSH_MSG_KEXINIT:
  530. if (pss->kex_state !=
  531. KEX_STATE_EXPECTING_CLIENT_OFFER) {
  532. pss->parser_state = SSH_KEX_STATE_SKIP;
  533. break;
  534. }
  535. if (!pss->kex) {
  536. lwsl_notice("%s: SSH_MSG_KEXINIT: NULL pss->kex\n", __func__);
  537. goto bail;
  538. }
  539. pss->parser_state = SSH_KEX_STATE_COOKIE;
  540. pss->kex->I_C_payload_len = 0;
  541. pss->kex->I_C_alloc_len = pss->msg_len;
  542. pss->kex->I_C = sshd_zalloc(pss->kex->I_C_alloc_len);
  543. if (!pss->kex->I_C) {
  544. lwsl_notice("OOM 3\n");
  545. goto bail;
  546. }
  547. pss->kex->I_C[pss->kex->I_C_payload_len++] =
  548. pss->msg_id;
  549. pss->copy_to_I_C = 1;
  550. break;
  551. case SSH_MSG_KEX_ECDH_INIT:
  552. pss->parser_state = SSH_KEX_STATE_ECDH_KEYLEN;
  553. break;
  554. case SSH_MSG_NEWKEYS:
  555. if (pss->kex_state !=
  556. KEX_STATE_REPLIED_TO_OFFER &&
  557. pss->kex_state !=
  558. KEX_STATE_CRYPTO_INITIALIZED) {
  559. lwsl_notice("unexpected newkeys\n");
  560. goto bail;
  561. }
  562. /*
  563. * it means we should now use the keys we
  564. * agreed on
  565. */
  566. lwsl_info("Activating CTS keys\n");
  567. pss->active_keys_cts = pss->kex->keys_next_cts;
  568. if (lws_chacha_activate(&pss->active_keys_cts))
  569. goto bail;
  570. pss->kex->newkeys |= 2;
  571. if (pss->kex->newkeys == 3)
  572. lws_kex_destroy(pss);
  573. if (pss->msg_padding) {
  574. pss->copy_to_I_C = 0;
  575. pss->parser_state =
  576. SSHS_MSG_EAT_PADDING;
  577. break;
  578. } else {
  579. pss->parser_state = SSHS_MSG_LEN;
  580. }
  581. break;
  582. case SSH_MSG_USERAUTH_REQUEST:
  583. /*
  584. * byte SSH_MSG_USERAUTH_REQUEST
  585. * string user name in UTF-8
  586. * encoding [RFC3629]
  587. * string service name in US-ASCII
  588. * string "publickey"
  589. * boolean FALSE
  590. * string public key alg
  591. * string public key blob
  592. */
  593. lwsl_info("SSH_MSG_USERAUTH_REQUEST\n");
  594. if (pss->ua) {
  595. lwsl_notice("pss->ua overwrite\n");
  596. goto bail;
  597. }
  598. pss->ua = sshd_zalloc(sizeof(*pss->ua));
  599. if (!pss->ua)
  600. goto bail;
  601. state_get_string_alloc(pss, SSHS_DO_UAR_SVC);
  602. /* username is destroyed with userauth struct */
  603. if (!pss->sent_banner) {
  604. if (pss->vhd->ops->banner)
  605. write_task(pss, NULL,
  606. SSH_WT_UA_BANNER);
  607. pss->sent_banner = 1;
  608. }
  609. break;
  610. case SSH_MSG_USERAUTH_FAILURE:
  611. goto bail;
  612. case SSH_MSG_USERAUTH_SUCCESS:
  613. goto bail;
  614. case SSH_MSG_USERAUTH_BANNER:
  615. goto bail;
  616. case SSH_MSG_CHANNEL_OPEN:
  617. state_get_string(pss, SSHS_NVC_CHOPEN_TYPE);
  618. break;
  619. case SSH_MSG_CHANNEL_REQUEST:
  620. /* RFC4254
  621. *
  622. * byte SSH_MSG_CHANNEL_REQUEST
  623. * uint32 recipient channel
  624. * string "pty-req"
  625. * boolean want_reply
  626. * string TERM environment variable value
  627. * (e.g., vt100)
  628. * uint32 terminal width, characters
  629. * (e.g., 80)
  630. * uint32 terminal height, rows (e.g., 24)
  631. * uint32 terminal width, px (e.g., 640)
  632. * uint32 terminal height, px (e.g., 480)
  633. * string encoded terminal modes
  634. */
  635. state_get_u32(pss, SSHS_NVC_CHRQ_RECIP);
  636. break;
  637. case SSH_MSG_CHANNEL_EOF:
  638. /* RFC4254
  639. * When a party will no longer send more data
  640. * to a channel, it SHOULD send
  641. * SSH_MSG_CHANNEL_EOF.
  642. *
  643. * byte SSH_MSG_CHANNEL_EOF
  644. * uint32 recipient channel
  645. */
  646. state_get_u32(pss, SSHS_NVC_CH_EOF);
  647. break;
  648. case SSH_MSG_CHANNEL_CLOSE:
  649. /* RFC4254
  650. *
  651. * byte SSH_MSG_CHANNEL_CLOSE
  652. * uint32 recipient channel
  653. *
  654. * This message does not consume window space
  655. * and can be sent even if no window space is
  656. * available.
  657. *
  658. * It is RECOMMENDED that all data sent before
  659. * this message be delivered to the actual
  660. * destination, if possible.
  661. */
  662. state_get_u32(pss, SSHS_NVC_CH_CLOSE);
  663. break;
  664. case SSH_MSG_CHANNEL_DATA:
  665. /* RFC4254
  666. *
  667. * byte SSH_MSG_CHANNEL_DATA
  668. * uint32 recipient channel
  669. * string data
  670. */
  671. state_get_u32(pss, SSHS_NVC_CD_RECIP);
  672. break;
  673. case SSH_MSG_CHANNEL_WINDOW_ADJUST:
  674. /* RFC452
  675. *
  676. * byte SSH_MSG_CHANNEL_WINDOW_ADJUST
  677. * uint32 recipient channel
  678. * uint32 bytes to add
  679. */
  680. if (!pss->ch_list)
  681. goto bail;
  682. state_get_u32(pss, SSHS_NVC_WA_RECIP);
  683. break;
  684. default:
  685. lwsl_notice("unk msg_id %d\n", pss->msg_id);
  686. goto bail;
  687. }
  688. break;
  689. case SSH_KEX_STATE_COOKIE:
  690. if (pss->msg_len < 16 + 1 + 1 + (10 * 4) + 5) {
  691. lwsl_notice("sanity: kex length failed\n");
  692. goto bail;
  693. }
  694. pss->kex->kex_cookie[pss->ctr++] = *p++;
  695. if (pss->ctr != sizeof(pss->kex->kex_cookie))
  696. break;
  697. pss->parser_state = SSH_KEX_NL_KEX_ALGS_LEN;
  698. pss->ctr = 0;
  699. break;
  700. case SSH_KEX_NL_KEX_ALGS_LEN:
  701. case SSH_KEX_NL_SHK_ALGS_LEN:
  702. case SSH_KEX_NL_EACTS_ALGS_LEN:
  703. case SSH_KEX_NL_EASTC_ALGS_LEN:
  704. case SSH_KEX_NL_MACTS_ALGS_LEN:
  705. case SSH_KEX_NL_MASTC_ALGS_LEN:
  706. case SSH_KEX_NL_CACTS_ALGS_LEN:
  707. case SSH_KEX_NL_CASTC_ALGS_LEN:
  708. case SSH_KEX_NL_LCTS_ALGS_LEN:
  709. case SSH_KEX_NL_LSTC_ALGS_LEN:
  710. case SSH_KEX_STATE_ECDH_KEYLEN:
  711. pss->len = (pss->len << 8) | *p++;
  712. if (++pss->ctr != 4)
  713. break;
  714. switch (pss->parser_state) {
  715. case SSH_KEX_STATE_ECDH_KEYLEN:
  716. pss->parser_state = SSH_KEX_STATE_ECDH_Q_C;
  717. break;
  718. default:
  719. pss->parser_state++;
  720. if (pss->len == 0)
  721. pss->parser_state++;
  722. break;
  723. }
  724. pss->ctr = 0;
  725. pss->npos = 0;
  726. if (pss->msg_len - pss->pos < pss->len) {
  727. lwsl_notice("sanity: length %d - %d < %d\n",
  728. pss->msg_len, pss->pos, pss->len);
  729. goto bail;
  730. }
  731. break;
  732. case SSH_KEX_NL_KEX_ALGS:
  733. case SSH_KEX_NL_SHK_ALGS:
  734. case SSH_KEX_NL_EACTS_ALGS:
  735. case SSH_KEX_NL_EASTC_ALGS:
  736. case SSH_KEX_NL_MACTS_ALGS:
  737. case SSH_KEX_NL_MASTC_ALGS:
  738. case SSH_KEX_NL_CACTS_ALGS:
  739. case SSH_KEX_NL_CASTC_ALGS:
  740. case SSH_KEX_NL_LCTS_ALGS:
  741. case SSH_KEX_NL_LSTC_ALGS:
  742. if (*p != ',') {
  743. if (pss->npos < sizeof(pss->name) - 1)
  744. pss->name[pss->npos++] = *p;
  745. } else {
  746. pss->name[pss->npos] = '\0';
  747. pss->npos = 0;
  748. handle_name(pss);
  749. }
  750. p++;
  751. if (!--pss->len) {
  752. pss->name[pss->npos] = '\0';
  753. if (pss->npos)
  754. handle_name(pss);
  755. pss->parser_state++;
  756. break;
  757. }
  758. break;
  759. case SSH_KEX_FIRST_PKT:
  760. pss->first_coming = !!*p++;
  761. pss->parser_state = SSH_KEX_RESERVED;
  762. break;
  763. case SSH_KEX_RESERVED:
  764. pss->len = (pss->len << 8) | *p++;
  765. if (++pss->ctr != 4)
  766. break;
  767. pss->ctr = 0;
  768. if (pss->msg_padding) {
  769. pss->copy_to_I_C = 0;
  770. pss->parser_state = SSHS_MSG_EAT_PADDING;
  771. break;
  772. }
  773. pss->parser_state = SSHS_MSG_LEN;
  774. break;
  775. case SSH_KEX_STATE_ECDH_Q_C:
  776. if (pss->len != 32) {
  777. lwsl_notice("wrong key len\n");
  778. goto bail;
  779. }
  780. pss->kex->Q_C[pss->ctr++] = *p++;
  781. if (pss->ctr != 32)
  782. break;
  783. lwsl_info("Q_C parsed\n");
  784. pss->parser_state = SSHS_MSG_EAT_PADDING;
  785. break;
  786. case SSH_KEX_STATE_SKIP:
  787. if (pss->pos - 4 < pss->msg_len) {
  788. p++;
  789. break;
  790. }
  791. lwsl_debug("skip done pos %d, msg_len %d len=%ld, \n",
  792. pss->pos, pss->msg_len, (long)len);
  793. pss->parser_state = SSHS_MSG_LEN;
  794. pss->ctr = 0;
  795. break;
  796. case SSHS_MSG_EAT_PADDING:
  797. p++;
  798. if (--pss->msg_padding)
  799. break;
  800. if (pss->msg_len + 4 != pss->pos) {
  801. lwsl_notice("sanity: kex end mismatch %d %d\n",
  802. pss->pos, pss->msg_len);
  803. goto bail;
  804. }
  805. switch (pss->msg_id) {
  806. case SSH_MSG_KEX_ECDH_INIT:
  807. if (pss->kex->match_bitfield != 0xff) {
  808. lwsl_notice("unable to negotiate\n");
  809. goto bail;
  810. }
  811. if (kex_ecdh(pss, pss->kex->kex_r,
  812. &pss->kex->kex_r_len)) {
  813. lwsl_notice("hex_ecdh failed\n");
  814. goto bail;
  815. }
  816. write_task(pss, NULL, SSH_WT_OFFER_REPLY);
  817. break;
  818. }
  819. pss->parser_state = SSHS_MSG_LEN;
  820. pss->ctr = 0;
  821. break;
  822. case SSHS_GET_STRING_LEN:
  823. pss->npos = 0;
  824. pss->len = (pss->len << 8) | *p++;
  825. if (++pss->ctr != 4)
  826. break;
  827. pss->ctr = 0;
  828. pss->parser_state = SSHS_GET_STRING;
  829. break;
  830. case SSHS_GET_STRING:
  831. if (pss->npos >= sizeof(pss->name) - 1) {
  832. lwsl_notice("non-alloc string too big\n");
  833. goto bail;
  834. }
  835. pss->name[pss->npos++] = *p++;
  836. if (pss->npos != pss->len)
  837. break;
  838. pss->name[pss->npos] = '\0';
  839. pss->parser_state = pss->state_after_string;
  840. goto again;
  841. case SSHS_GET_STRING_LEN_ALLOC:
  842. pss->npos = 0;
  843. pss->len = (pss->len << 8) | *p++;
  844. if (++pss->ctr != 4)
  845. break;
  846. pss->ctr = 0;
  847. pss->last_alloc = sshd_zalloc(pss->len + 1);
  848. lwsl_debug("SSHS_GET_STRING_LEN_ALLOC: %p, state %d\n",
  849. pss->last_alloc, pss->state_after_string);
  850. if (!pss->last_alloc) {
  851. lwsl_notice("alloc string too big\n");
  852. goto bail;
  853. }
  854. pss->parser_state = SSHS_GET_STRING_ALLOC;
  855. break;
  856. case SSHS_GET_STRING_ALLOC:
  857. if (pss->npos >= pss->len)
  858. goto bail;
  859. pss->last_alloc[pss->npos++] = *p++;
  860. if (pss->npos != pss->len)
  861. break;
  862. pss->last_alloc[pss->npos] = '\0';
  863. pss->parser_state = pss->state_after_string;
  864. goto again;
  865. /*
  866. * User Authentication
  867. */
  868. case SSHS_DO_SERVICE_REQUEST:
  869. pss->okayed_userauth = 1;
  870. pss->parser_state = SSHS_MSG_EAT_PADDING;
  871. /*
  872. * this only 'accepts' that we can negotiate auth for
  873. * this service, not accepts the auth
  874. */
  875. write_task(pss, NULL, SSH_WT_UA_ACCEPT);
  876. break;
  877. case SSHS_DO_UAR_SVC:
  878. pss->ua->username = (char *)pss->last_alloc;
  879. pss->last_alloc = NULL; /* it was adopted */
  880. state_get_string_alloc(pss, SSHS_DO_UAR_PUBLICKEY);
  881. /* destroyed with UA struct */
  882. break;
  883. case SSHS_DO_UAR_PUBLICKEY:
  884. pss->ua->service = (char *)pss->last_alloc;
  885. pss->last_alloc = NULL; /* it was adopted */
  886. /* Sect 5, RFC4252
  887. *
  888. * The 'user name' and 'service name' are repeated in
  889. * every new authentication attempt, and MAY change.
  890. *
  891. * The server implementation MUST carefully check them
  892. * in every message, and MUST flush any accumulated
  893. * authentication states if they change. If it is
  894. * unable to flush an authentication state, it MUST
  895. * disconnect if the 'user name' or 'service name'
  896. * changes.
  897. */
  898. if (pss->seen_auth_req_before && (
  899. strcmp(pss->ua->username,
  900. pss->last_auth_req_username) ||
  901. strcmp(pss->ua->service,
  902. pss->last_auth_req_service))) {
  903. lwsl_notice("username / svc changed\n");
  904. goto bail;
  905. }
  906. pss->seen_auth_req_before = 1;
  907. lws_strncpy(pss->last_auth_req_username,
  908. pss->ua->username,
  909. sizeof(pss->last_auth_req_username));
  910. lws_strncpy(pss->last_auth_req_service,
  911. pss->ua->service,
  912. sizeof(pss->last_auth_req_service));
  913. if (strcmp(pss->ua->service, "ssh-connection"))
  914. goto ua_fail;
  915. state_get_string(pss, SSHS_NVC_DO_UAR_CHECK_PUBLICKEY);
  916. break;
  917. case SSHS_NVC_DO_UAR_CHECK_PUBLICKEY:
  918. if (!strcmp(pss->name, "none")) {
  919. /* we must fail it */
  920. lwsl_info("got 'none' req, refusing\n");
  921. goto ua_fail;
  922. }
  923. if (strcmp(pss->name, "publickey")) {
  924. lwsl_notice("expected 'publickey' got '%s'\n",
  925. pss->name);
  926. goto ua_fail;
  927. }
  928. pss->parser_state = SSHS_DO_UAR_SIG_PRESENT;
  929. break;
  930. case SSHS_DO_UAR_SIG_PRESENT:
  931. lwsl_info("SSHS_DO_UAR_SIG_PRESENT\n");
  932. pss->ua->sig_present = *p++;
  933. state_get_string_alloc(pss, SSHS_NVC_DO_UAR_ALG);
  934. /* destroyed with UA struct */
  935. break;
  936. case SSHS_NVC_DO_UAR_ALG:
  937. pss->ua->alg = (char *)pss->last_alloc;
  938. pss->last_alloc = NULL; /* it was adopted */
  939. if (rsa_hash_alg_from_ident(pss->ua->alg) < 0) {
  940. lwsl_notice("unknown alg\n");
  941. goto ua_fail;
  942. }
  943. state_get_string_alloc(pss, SSHS_NVC_DO_UAR_PUBKEY_BLOB);
  944. /* destroyed with UA struct */
  945. break;
  946. case SSHS_NVC_DO_UAR_PUBKEY_BLOB:
  947. pss->ua->pubkey = pss->last_alloc;
  948. pss->last_alloc = NULL; /* it was adopted */
  949. pss->ua->pubkey_len = pss->npos;
  950. /*
  951. * RFC4253
  952. *
  953. * ssh-rsa
  954. *
  955. * The structure inside the blob is
  956. *
  957. * mpint e
  958. * mpint n
  959. *
  960. * Let's see if this key is authorized
  961. */
  962. n = 1;
  963. if (pss->vhd->ops && pss->vhd->ops->is_pubkey_authorized)
  964. n = pss->vhd->ops->is_pubkey_authorized(
  965. pss->ua->username, pss->ua->alg,
  966. pss->ua->pubkey, pss->ua->pubkey_len);
  967. if (n) {
  968. lwsl_info("rejecting peer pubkey\n");
  969. goto ua_fail;
  970. }
  971. if (pss->ua->sig_present) {
  972. state_get_string_alloc(pss, SSHS_NVC_DO_UAR_SIG);
  973. /* destroyed with UA struct */
  974. break;
  975. }
  976. /*
  977. * This key is at least one we would be prepared
  978. * to accept if he really has it... since no sig
  979. * client should resend everything with a sig
  980. * appended. OK it and delete this initial UA
  981. */
  982. write_task(pss, NULL, SSH_WT_UA_PK_OK);
  983. pss->parser_state = SSHS_MSG_EAT_PADDING;
  984. break;
  985. case SSHS_NVC_DO_UAR_SIG:
  986. /*
  987. * Now the pubkey is coming with a sig
  988. */
  989. /* Sect 5.1 RFC4252
  990. *
  991. * SSH_MSG_USERAUTH_SUCCESS MUST be sent only once.
  992. * When SSH_MSG_USERAUTH_SUCCESS has been sent, any
  993. * further authentication requests received after that
  994. * SHOULD be silently ignored.
  995. */
  996. if (pss->ssh_auth_state == SSH_AUTH_STATE_GAVE_AUTH_IGNORE_REQS) {
  997. lwsl_info("Silently ignoring auth req after accepted\n");
  998. goto ua_fail_silently;
  999. }
  1000. lwsl_info("SSHS_DO_UAR_SIG\n");
  1001. pss->ua->sig = pss->last_alloc;
  1002. pss->last_alloc = NULL; /* it was adopted */
  1003. pss->ua->sig_len = pss->npos;
  1004. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1005. /*
  1006. * RFC 4252 p9
  1007. *
  1008. * The value of 'signature' is a signature with
  1009. * the private host key of the following data, in
  1010. * this order:
  1011. *
  1012. * string session identifier
  1013. * byte SSH_MSG_USERAUTH_REQUEST
  1014. * string user name
  1015. * string service name
  1016. * string "publickey"
  1017. * boolean TRUE
  1018. * string public key algorithm name
  1019. * string public key to be used for auth
  1020. *
  1021. * We reproduce the signature plaintext and the
  1022. * hash, and then decrypt the incoming signed block.
  1023. * What comes out is some ASN1, in there is the
  1024. * hash decrypted. We find it and confirm it
  1025. * matches the hash we computed ourselves.
  1026. *
  1027. * First step is generate the sig plaintext
  1028. */
  1029. n = 4 + 32 +
  1030. 1 +
  1031. 4 + (int)strlen(pss->ua->username) +
  1032. 4 + (int)strlen(pss->ua->service) +
  1033. 4 + 9 +
  1034. 1 +
  1035. 4 + (int)strlen(pss->ua->alg) +
  1036. 4 + (int)pss->ua->pubkey_len;
  1037. ps = sshd_zalloc(n);
  1038. if (!ps) {
  1039. lwsl_notice("OOM 4\n");
  1040. goto ua_fail;
  1041. }
  1042. pp = ps;
  1043. lws_buf(&pp, pss->session_id, 32);
  1044. *pp++ = SSH_MSG_USERAUTH_REQUEST;
  1045. lws_cstr(&pp, pss->ua->username, 64);
  1046. lws_cstr(&pp, pss->ua->service, 64);
  1047. lws_cstr(&pp, "publickey", 64);
  1048. *pp++ = 1;
  1049. lws_cstr(&pp, pss->ua->alg, 64);
  1050. lws_buf(&pp, pss->ua->pubkey, pss->ua->pubkey_len);
  1051. /* Next hash the plaintext */
  1052. if (lws_genhash_init(&pss->ua->hash_ctx,
  1053. rsa_hash_alg_from_ident(pss->ua->alg))) {
  1054. lwsl_notice("genhash init failed\n");
  1055. free(ps);
  1056. goto ua_fail;
  1057. }
  1058. if (lws_genhash_update(&pss->ua->hash_ctx, ps, pp - ps)) {
  1059. lwsl_notice("genhash update failed\n");
  1060. free(ps);
  1061. goto ua_fail;
  1062. }
  1063. lws_genhash_destroy(&pss->ua->hash_ctx, hash);
  1064. free(ps);
  1065. /*
  1066. * Prepare the RSA decryption context: load in
  1067. * the E and N factors
  1068. */
  1069. memset(e, 0, sizeof(e));
  1070. pp = pss->ua->pubkey;
  1071. m = lws_g32(&pp);
  1072. pp += m;
  1073. m = lws_g32(&pp);
  1074. e[LWS_GENCRYPTO_RSA_KEYEL_E].buf = pp;
  1075. e[LWS_GENCRYPTO_RSA_KEYEL_E].len = m;
  1076. pp += m;
  1077. m = lws_g32(&pp);
  1078. e[LWS_GENCRYPTO_RSA_KEYEL_N].buf = pp;
  1079. e[LWS_GENCRYPTO_RSA_KEYEL_N].len = m;
  1080. if (lws_genrsa_create(&ctx, e, pss->vhd->context,
  1081. LGRSAM_PKCS1_1_5,
  1082. LWS_GENHASH_TYPE_UNKNOWN))
  1083. goto ua_fail;
  1084. /*
  1085. * point to the encrypted signature payload we
  1086. * were sent
  1087. */
  1088. pp = pss->ua->sig;
  1089. m = lws_g32(&pp);
  1090. pp += m;
  1091. m = lws_g32(&pp);
  1092. /*
  1093. * decrypt it, resulting in an error, or some ASN1
  1094. * including the decrypted signature
  1095. */
  1096. otmp = sshd_zalloc(m);
  1097. if (!otmp)
  1098. /* ua_fail1 frees bn_e, bn_n and rsa */
  1099. goto ua_fail1;
  1100. n = lws_genrsa_public_decrypt(&ctx, pp, m, otmp, m);
  1101. if (n > 0) {
  1102. /* the decrypted sig is in ASN1 format */
  1103. m = 0;
  1104. while ((int)m < n) {
  1105. /* sig payload */
  1106. if (otmp[m] == 0x04 &&
  1107. otmp[m + 1] == lws_genhash_size(
  1108. pss->ua->hash_ctx.type)) {
  1109. m = memcmp(&otmp[m + 2], hash,
  1110. lws_genhash_size(pss->ua->hash_ctx.type));
  1111. break;
  1112. }
  1113. /* go into these */
  1114. if (otmp[m] == 0x30) {
  1115. m += 2;
  1116. continue;
  1117. }
  1118. /* otherwise skip payloads */
  1119. m += otmp[m + 1] + 2;
  1120. }
  1121. }
  1122. free(otmp);
  1123. lws_genrsa_destroy(&ctx);
  1124. /*
  1125. * if no good, m is nonzero and inform peer
  1126. */
  1127. if (n <= 0) {
  1128. lwsl_notice("hash sig verify fail: %d\n", m);
  1129. goto ua_fail;
  1130. }
  1131. /* if it checks out, inform peer */
  1132. lwsl_info("sig check OK\n");
  1133. /* Sect 5.1 RFC4252
  1134. *
  1135. * SSH_MSG_USERAUTH_SUCCESS MUST be sent only once.
  1136. * When SSH_MSG_USERAUTH_SUCCESS has been sent, any
  1137. * further authentication requests received after that
  1138. * SHOULD be silently ignored.
  1139. */
  1140. pss->ssh_auth_state = SSH_AUTH_STATE_GAVE_AUTH_IGNORE_REQS;
  1141. write_task(pss, NULL, SSH_WT_UA_SUCCESS);
  1142. lws_ua_destroy(pss);
  1143. break;
  1144. /*
  1145. * Channels
  1146. */
  1147. case SSHS_GET_U32:
  1148. pss->len = (pss->len << 8) | *p++;
  1149. if (++pss->ctr != 4)
  1150. break;
  1151. pss->ctr = 0;
  1152. pss->parser_state = pss->state_after_string;
  1153. goto again;
  1154. /*
  1155. * Channel: Disconnect
  1156. */
  1157. case SSHS_NVC_DISCONNECT_REASON:
  1158. pss->disconnect_reason = pss->len;
  1159. state_get_string_alloc(pss, SSHS_NVC_DISCONNECT_DESC);
  1160. break;
  1161. case SSHS_NVC_DISCONNECT_DESC:
  1162. pss->disconnect_desc = (char *)pss->last_alloc;
  1163. pss->last_alloc = NULL; /* it was adopted */
  1164. state_get_string(pss, SSHS_NVC_DISCONNECT_LANG);
  1165. break;
  1166. case SSHS_NVC_DISCONNECT_LANG:
  1167. lwsl_notice("SSHS_NVC_DISCONNECT_LANG\n");
  1168. if (pss->vhd->ops && pss->vhd->ops->disconnect_reason)
  1169. pss->vhd->ops->disconnect_reason(
  1170. pss->disconnect_reason,
  1171. pss->disconnect_desc, pss->name);
  1172. ssh_free_set_NULL(pss->last_alloc);
  1173. break;
  1174. /*
  1175. * Channel: Open
  1176. */
  1177. case SSHS_NVC_CHOPEN_TYPE:
  1178. /* channel open */
  1179. if (strcmp(pss->name, "session")) {
  1180. lwsl_notice("Failing on not session\n");
  1181. pss->reason = 3;
  1182. goto ch_fail;
  1183. }
  1184. lwsl_info("SSHS_NVC_CHOPEN_TYPE: creating session\n");
  1185. pss->ch_temp = sshd_zalloc(sizeof(*pss->ch_temp));
  1186. if (!pss->ch_temp)
  1187. return -1;
  1188. pss->ch_temp->type = SSH_CH_TYPE_SESSION;
  1189. pss->ch_temp->pss = pss;
  1190. state_get_u32(pss, SSHS_NVC_CHOPEN_SENDER_CH);
  1191. break;
  1192. case SSHS_NVC_CHOPEN_SENDER_CH:
  1193. pss->ch_temp->sender_ch = pss->len;
  1194. state_get_u32(pss, SSHS_NVC_CHOPEN_WINSIZE);
  1195. break;
  1196. case SSHS_NVC_CHOPEN_WINSIZE:
  1197. lwsl_info("Initial window set to %d\n", pss->len);
  1198. pss->ch_temp->window = pss->len;
  1199. state_get_u32(pss, SSHS_NVC_CHOPEN_PKTSIZE);
  1200. break;
  1201. case SSHS_NVC_CHOPEN_PKTSIZE:
  1202. pss->ch_temp->max_pkt = pss->len;
  1203. pss->ch_temp->peer_window_est = LWS_SSH_INITIAL_WINDOW;
  1204. pss->ch_temp->server_ch = pss->next_ch_num++;
  1205. /*
  1206. * add us to channel list... leave as ch_temp
  1207. * as write task needs it and will NULL down
  1208. */
  1209. lwsl_info("creating new session ch\n");
  1210. pss->ch_temp->next = pss->ch_list;
  1211. pss->ch_list = pss->ch_temp;
  1212. if (pss->vhd->ops && pss->vhd->ops->channel_create)
  1213. pss->vhd->ops->channel_create(pss->wsi,
  1214. &pss->ch_temp->priv);
  1215. write_task(pss, pss->ch_temp, SSH_WT_CH_OPEN_CONF);
  1216. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1217. break;
  1218. /*
  1219. * SSH_MSG_CHANNEL_REQUEST
  1220. */
  1221. case SSHS_NVC_CHRQ_RECIP:
  1222. pss->ch_recip = pss->len;
  1223. state_get_string(pss, SSHS_NVC_CHRQ_TYPE);
  1224. break;
  1225. case SSHS_NVC_CHRQ_TYPE:
  1226. pss->parser_state = SSHS_CHRQ_WANT_REPLY;
  1227. break;
  1228. case SSHS_CHRQ_WANT_REPLY:
  1229. pss->rq_want_reply = *p++;
  1230. lwsl_info("SSHS_CHRQ_WANT_REPLY: %s, wantrep: %d\n",
  1231. pss->name, pss->rq_want_reply);
  1232. pss->ch_temp = ssh_get_server_ch(pss, pss->ch_recip);
  1233. /* after this they differ by the request */
  1234. /*
  1235. * a PTY for a shell
  1236. */
  1237. if (!strcmp(pss->name, "pty-req")) {
  1238. state_get_string(pss, SSHS_NVC_CHRQ_TERM);
  1239. break;
  1240. }
  1241. /*
  1242. * a shell
  1243. */
  1244. if (!strcmp(pss->name, "shell")) {
  1245. pss->channel_doing_spawn = pss->ch_temp->server_ch;
  1246. if (pss->vhd->ops && pss->vhd->ops->shell &&
  1247. !pss->vhd->ops->shell(pss->ch_temp->priv,
  1248. pss->wsi,
  1249. lws_ssh_exec_finish, pss->ch_temp)) {
  1250. if (pss->rq_want_reply)
  1251. write_task_insert(pss, pss->ch_temp,
  1252. SSH_WT_CHRQ_SUCC);
  1253. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1254. break;
  1255. }
  1256. goto chrq_fail;
  1257. }
  1258. /*
  1259. * env vars to be set in the shell
  1260. */
  1261. if (!strcmp(pss->name, "env")) {
  1262. state_get_string(pss, SSHS_NVC_CHRQ_ENV_NAME);
  1263. break;
  1264. }
  1265. /*
  1266. * exec something
  1267. */
  1268. if (!strcmp(pss->name, "exec")) {
  1269. state_get_string_alloc(pss, SSHS_NVC_CHRQ_EXEC_CMD);
  1270. break;
  1271. }
  1272. /*
  1273. * spawn a subsystem
  1274. */
  1275. if (!strcmp(pss->name, "subsystem")) {
  1276. lwsl_notice("subsystem\n");
  1277. state_get_string_alloc(pss,
  1278. SSHS_NVC_CHRQ_SUBSYSTEM);
  1279. break;
  1280. }
  1281. if (pss->rq_want_reply)
  1282. goto chrq_fail;
  1283. pss->parser_state = SSH_KEX_STATE_SKIP;
  1284. break;
  1285. /* CHRQ pty-req */
  1286. case SSHS_NVC_CHRQ_TERM:
  1287. memcpy(pss->args.pty.term, pss->name,
  1288. sizeof(pss->args.pty.term) - 1);
  1289. state_get_u32(pss, SSHS_NVC_CHRQ_TW);
  1290. break;
  1291. case SSHS_NVC_CHRQ_TW:
  1292. pss->args.pty.width_ch = pss->len;
  1293. state_get_u32(pss, SSHS_NVC_CHRQ_TH);
  1294. break;
  1295. case SSHS_NVC_CHRQ_TH:
  1296. pss->args.pty.height_ch = pss->len;
  1297. state_get_u32(pss, SSHS_NVC_CHRQ_TWP);
  1298. break;
  1299. case SSHS_NVC_CHRQ_TWP:
  1300. pss->args.pty.width_px = pss->len;
  1301. state_get_u32(pss, SSHS_NVC_CHRQ_THP);
  1302. break;
  1303. case SSHS_NVC_CHRQ_THP:
  1304. pss->args.pty.height_px = pss->len;
  1305. state_get_string_alloc(pss, SSHS_NVC_CHRQ_MODES);
  1306. break;
  1307. case SSHS_NVC_CHRQ_MODES:
  1308. /* modes is a stream of byte-pairs, not a string */
  1309. pss->args.pty.modes = (char *)pss->last_alloc;
  1310. pss->last_alloc = NULL; /* it was adopted */
  1311. pss->args.pty.modes_len = pss->npos;
  1312. n = 0;
  1313. if (pss->vhd->ops && pss->vhd->ops->pty_req)
  1314. n = pss->vhd->ops->pty_req(pss->ch_temp->priv,
  1315. &pss->args.pty);
  1316. ssh_free_set_NULL(pss->args.pty.modes);
  1317. if (n)
  1318. goto chrq_fail;
  1319. if (pss->rq_want_reply)
  1320. write_task(pss, pss->ch_temp, SSH_WT_CHRQ_SUCC);
  1321. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1322. break;
  1323. /* CHRQ env */
  1324. case SSHS_NVC_CHRQ_ENV_NAME:
  1325. strcpy(pss->args.aux, pss->name);
  1326. state_get_string(pss, SSHS_NVC_CHRQ_ENV_VALUE);
  1327. break;
  1328. case SSHS_NVC_CHRQ_ENV_VALUE:
  1329. if (pss->vhd->ops && pss->vhd->ops->set_env)
  1330. if (pss->vhd->ops->set_env(pss->ch_temp->priv,
  1331. pss->args.aux, pss->name))
  1332. goto chrq_fail;
  1333. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1334. break;
  1335. /* CHRQ exec */
  1336. case SSHS_NVC_CHRQ_EXEC_CMD:
  1337. /*
  1338. * byte SSH_MSG_CHANNEL_REQUEST
  1339. * uint32 recipient channel
  1340. * string "exec"
  1341. * boolean want reply
  1342. * string command
  1343. *
  1344. * This message will request that the server start the
  1345. * execution of the given command. The 'command' string
  1346. * may contain a path. Normal precautions MUST be taken
  1347. * to prevent the execution of unauthorized commands.
  1348. *
  1349. * scp sends "scp -t /path/..."
  1350. */
  1351. lwsl_info("exec cmd: %s %02X\n", pss->last_alloc, *p);
  1352. pss->channel_doing_spawn = pss->ch_temp->server_ch;
  1353. if (pss->vhd->ops && pss->vhd->ops->exec &&
  1354. !pss->vhd->ops->exec(pss->ch_temp->priv, pss->wsi,
  1355. (const char *)pss->last_alloc,
  1356. lws_ssh_exec_finish, pss->ch_temp)) {
  1357. ssh_free_set_NULL(pss->last_alloc);
  1358. if (pss->rq_want_reply)
  1359. write_task_insert(pss, pss->ch_temp,
  1360. SSH_WT_CHRQ_SUCC);
  1361. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1362. break;
  1363. }
  1364. /*
  1365. * even if he doesn't want to exec it, we know how to
  1366. * fake scp
  1367. */
  1368. /* we only alloc "exec" of scp for scp destination */
  1369. n = 1;
  1370. if (pss->last_alloc[0] != 's' ||
  1371. pss->last_alloc[1] != 'c' ||
  1372. pss->last_alloc[2] != 'p' ||
  1373. pss->last_alloc[3] != ' ')
  1374. /* disallow it */
  1375. n = 0;
  1376. ssh_free_set_NULL(pss->last_alloc);
  1377. if (!n)
  1378. goto chrq_fail;
  1379. /* our channel speaks SCP protocol now */
  1380. scp = sshd_zalloc(sizeof(*scp));
  1381. if (!scp)
  1382. return -1;
  1383. pss->ch_temp->type = SSH_CH_TYPE_SCP;
  1384. pss->ch_temp->sub = (lws_subprotocol *)scp;
  1385. scp->ips = SSHS_SCP_COLLECTSTR;
  1386. if (pss->rq_want_reply)
  1387. write_task(pss, pss->ch_temp, SSH_WT_CHRQ_SUCC);
  1388. /* we start the scp protocol first by sending an ACK */
  1389. write_task(pss, pss->ch_temp, SSH_WT_SCP_ACK_OKAY);
  1390. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1391. break;
  1392. case SSHS_NVC_CHRQ_SUBSYSTEM:
  1393. lwsl_notice("subsystem: %s", pss->last_alloc);
  1394. n = 0;
  1395. #if 0
  1396. if (!strcmp(pss->name, "sftp")) {
  1397. lwsl_notice("SFTP session\n");
  1398. pss->ch_temp->type = SSH_CH_TYPE_SFTP;
  1399. n = 1;
  1400. }
  1401. #endif
  1402. ssh_free_set_NULL(pss->last_alloc);
  1403. // if (!n)
  1404. goto ch_fail;
  1405. #if 0
  1406. if (pss->rq_want_reply)
  1407. write_task(pss, ssh_get_server_ch(pss,
  1408. pss->ch_recip), SSH_WT_CHRQ_SUCC);
  1409. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1410. break;
  1411. #endif
  1412. /* SSH_MSG_CHANNEL_DATA */
  1413. case SSHS_NVC_CD_RECIP:
  1414. pss->ch_recip = pss->len;
  1415. ch = ssh_get_server_ch(pss, pss->ch_recip);
  1416. ch->peer_window_est -= pss->msg_len;
  1417. if (pss->msg_len < sizeof(pss->name))
  1418. state_get_string(pss, SSHS_NVC_CD_DATA);
  1419. else
  1420. state_get_string_alloc(pss,
  1421. SSHS_NVC_CD_DATA_ALLOC);
  1422. break;
  1423. case SSHS_NVC_CD_DATA_ALLOC:
  1424. case SSHS_NVC_CD_DATA:
  1425. /*
  1426. * Actual protocol incoming payload
  1427. */
  1428. if (pss->parser_state == SSHS_NVC_CD_DATA_ALLOC)
  1429. pp = pss->last_alloc;
  1430. else
  1431. pp = (uint8_t *)pss->name;
  1432. lwsl_info("SSHS_NVC_CD_DATA\n");
  1433. ch = ssh_get_server_ch(pss, pss->ch_recip);
  1434. switch (ch->type) {
  1435. case SSH_CH_TYPE_SCP:
  1436. scp = &ch->sub->scp;
  1437. switch (scp->ips) {
  1438. case SSHS_SCP_COLLECTSTR:
  1439. /* gather the ascii-coded headers */
  1440. for (n = 0; n < (int)pss->npos; n++)
  1441. lwsl_notice("0x%02X %c\n",
  1442. pp[n], pp[n]);
  1443. /* Header triggers the transfer? */
  1444. if (pp[0] == 'C' && pp[pss->npos - 1] == '\x0a') {
  1445. while (*pp != ' ' && *pp != '\x0a')
  1446. pp++;
  1447. if (*pp++ != ' ') {
  1448. write_task(pss, ch,
  1449. SSH_WT_SCP_ACK_ERROR);
  1450. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1451. break;
  1452. }
  1453. scp->len = atoll((const char *)pp);
  1454. lwsl_notice("scp payload %llu expected\n",
  1455. (unsigned long long)scp->len);
  1456. scp->ips = SSHS_SCP_PAYLOADIN;
  1457. }
  1458. /* ack it */
  1459. write_task(pss, pss->ch_temp,
  1460. SSH_WT_SCP_ACK_OKAY);
  1461. break;
  1462. case SSHS_SCP_PAYLOADIN:
  1463. /* the scp file payload */
  1464. if (pss->vhd->ops)
  1465. pss->vhd->ops->rx(ch->priv,
  1466. pss->wsi, pp, pss->npos);
  1467. if (scp->len >= pss->npos)
  1468. scp->len -= pss->npos;
  1469. else
  1470. scp->len = 0;
  1471. if (!scp->len) {
  1472. lwsl_notice("scp txfer completed\n");
  1473. scp->ips = SSHS_SCP_COLLECTSTR;
  1474. break;
  1475. }
  1476. break;
  1477. }
  1478. break;
  1479. default: /* scp payload */
  1480. if (pss->vhd->ops)
  1481. pss->vhd->ops->rx(ch->priv, pss->wsi,
  1482. pp, pss->npos);
  1483. break;
  1484. }
  1485. if (pss->parser_state == SSHS_NVC_CD_DATA_ALLOC)
  1486. ssh_free_set_NULL(pss->last_alloc);
  1487. if (ch->peer_window_est < 32768) {
  1488. write_task(pss, ch, SSH_WT_WINDOW_ADJUST);
  1489. ch->peer_window_est += 32768;
  1490. lwsl_info("extra peer WINDOW_ADJUST (~ %d)\n",
  1491. ch->peer_window_est);
  1492. }
  1493. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1494. break;
  1495. case SSHS_NVC_WA_RECIP:
  1496. pss->ch_recip = pss->len;
  1497. state_get_u32(pss, SSHS_NVC_WA_ADD);
  1498. break;
  1499. case SSHS_NVC_WA_ADD:
  1500. ch = ssh_get_server_ch(pss, pss->ch_recip);
  1501. if (ch) {
  1502. ch->window += pss->len;
  1503. lwsl_notice("got additional window %d (now %d)\n",
  1504. pss->len, ch->window);
  1505. }
  1506. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1507. break;
  1508. /*
  1509. * channel close
  1510. */
  1511. case SSHS_NVC_CH_EOF:
  1512. /*
  1513. * No explicit response is sent to this
  1514. * message. However, the application may send
  1515. * EOF to whatever is at the other end of the
  1516. * channel. Note that the channel remains open
  1517. * after this message, and more data may still
  1518. * be sent in the other direction. This message
  1519. * does not consume window space and can be sent
  1520. * even if no window space is available.
  1521. */
  1522. lwsl_notice("SSH_MSG_CHANNEL_EOF: %d\n", pss->ch_recip);
  1523. ch = ssh_get_server_ch(pss, pss->ch_recip);
  1524. if (!ch) {
  1525. lwsl_notice("unknown ch %d\n", pss->ch_recip);
  1526. return -1;
  1527. }
  1528. if (!ch->scheduled_close) {
  1529. lwsl_notice("scheduling CLOSE\n");
  1530. ch->scheduled_close = 1;
  1531. write_task(pss, ch, SSH_WT_CH_CLOSE);
  1532. }
  1533. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1534. break;
  1535. case SSHS_NVC_CH_CLOSE:
  1536. /*
  1537. * When either party wishes to terminate the
  1538. * channel, it sends SSH_MSG_CHANNEL_CLOSE.
  1539. * Upon receiving this message, a party MUST
  1540. * send back an SSH_MSG_CHANNEL_CLOSE unless it
  1541. * has already sent this message for the
  1542. * channel. The channel is considered closed
  1543. * for a party when it has both sent and
  1544. * received SSH_MSG_CHANNEL_CLOSE, and the
  1545. * party may then reuse the channel number.
  1546. * A party MAY send SSH_MSG_CHANNEL_CLOSE
  1547. * without having sent or received
  1548. * SSH_MSG_CHANNEL_EOF.
  1549. */
  1550. lwsl_notice("SSH_MSG_CHANNEL_CLOSE ch %d\n",
  1551. pss->ch_recip);
  1552. ch = ssh_get_server_ch(pss, pss->ch_recip);
  1553. if (!ch)
  1554. goto bail;
  1555. pss->parser_state = SSHS_MSG_EAT_PADDING;
  1556. if (ch->sent_close) {
  1557. /*
  1558. * This is acking our sent close...
  1559. * we can destroy the channel with no
  1560. * further communication.
  1561. */
  1562. ssh_destroy_channel(pss, ch);
  1563. break;
  1564. }
  1565. ch->received_close = 1;
  1566. ch->scheduled_close = 1;
  1567. write_task(pss, ch, SSH_WT_CH_CLOSE);
  1568. break;
  1569. default:
  1570. break;
  1571. chrq_fail:
  1572. lwsl_notice("chrq_fail\n");
  1573. write_task(pss, pss->ch_temp, SSH_WT_CHRQ_FAILURE);
  1574. pss->parser_state = SSH_KEX_STATE_SKIP;
  1575. break;
  1576. ch_fail:
  1577. if (pss->ch_temp) {
  1578. free(pss->ch_temp);
  1579. pss->ch_temp = NULL;
  1580. }
  1581. write_task(pss, pss->ch_temp, SSH_WT_CH_FAILURE);
  1582. pss->parser_state = SSH_KEX_STATE_SKIP;
  1583. break;
  1584. ua_fail1:
  1585. lws_genrsa_destroy(&ctx);
  1586. ua_fail:
  1587. write_task(pss, NULL, SSH_WT_UA_FAILURE);
  1588. ua_fail_silently:
  1589. lws_ua_destroy(pss);
  1590. /* Sect 4, RFC4252
  1591. *
  1592. * Additionally, the implementation SHOULD limit the
  1593. * number of failed authentication attempts a client
  1594. * may perform in a single session (the RECOMMENDED
  1595. * limit is 20 attempts). If the threshold is
  1596. * exceeded, the server SHOULD disconnect.
  1597. */
  1598. if (pss->count_auth_attempts++ > 20)
  1599. goto bail;
  1600. pss->parser_state = SSH_KEX_STATE_SKIP;
  1601. break;
  1602. }
  1603. pss->pos++;
  1604. }
  1605. return 0;
  1606. bail:
  1607. lws_kex_destroy(pss);
  1608. lws_ua_destroy(pss);
  1609. return SSH_DISCONNECT_KEY_EXCHANGE_FAILED;
  1610. }
  1611. static int
  1612. parse(struct per_session_data__sshd *pss, uint8_t *p, size_t len)
  1613. {
  1614. while (len--) {
  1615. if (pss->copy_to_I_C && pss->kex->I_C_payload_len <
  1616. pss->kex->I_C_alloc_len &&
  1617. pss->parser_state != SSHS_MSG_EAT_PADDING)
  1618. pss->kex->I_C[pss->kex->I_C_payload_len++] = *p;
  1619. if (pss->active_keys_cts.valid &&
  1620. pss->parser_state == SSHS_MSG_LEN)
  1621. /* take a copy for full decrypt */
  1622. pss->packet_assembly[pss->pa_pos++] = *p;
  1623. if (pss->active_keys_cts.valid &&
  1624. pss->parser_state == SSHS_MSG_PADDING &&
  1625. pss->msg_len) {
  1626. /* we are going to have to decrypt it */
  1627. uint32_t cp, l = pss->msg_len + 4 +
  1628. pss->active_keys_cts.MAC_length;
  1629. uint8_t pt[2048];
  1630. len++;
  1631. cp = (uint32_t)len;
  1632. if (cp > l - pss->pa_pos)
  1633. cp = l - pss->pa_pos;
  1634. if (cp > sizeof(pss->packet_assembly) -
  1635. pss->pa_pos) {
  1636. lwsl_err("Packet is too big to decrypt\n");
  1637. goto bail;
  1638. }
  1639. if (pss->msg_len < 2 + 4) {
  1640. lwsl_err("packet too small\n");
  1641. goto bail;
  1642. }
  1643. memcpy(&pss->packet_assembly[pss->pa_pos], p, cp);
  1644. pss->pa_pos += cp;
  1645. len -= cp;
  1646. p += cp;
  1647. if (pss->pa_pos != l)
  1648. return 0;
  1649. /* decrypt it */
  1650. cp = lws_chacha_decrypt(&pss->active_keys_cts,
  1651. pss->ssh_sequence_ctr_cts++,
  1652. pss->packet_assembly,
  1653. pss->pa_pos, pt);
  1654. if (cp) {
  1655. lwsl_notice("Decryption failed: %d\n", cp);
  1656. goto bail;
  1657. }
  1658. if (lws_ssh_parse_plaintext(pss, pt + 4, pss->msg_len))
  1659. goto bail;
  1660. pss->pa_pos = 0;
  1661. pss->ctr = 0;
  1662. continue;
  1663. }
  1664. if (lws_ssh_parse_plaintext(pss, p, 1))
  1665. goto bail;
  1666. p++;
  1667. }
  1668. return 0;
  1669. bail:
  1670. lws_kex_destroy(pss);
  1671. lws_ua_destroy(pss);
  1672. return SSH_DISCONNECT_KEY_EXCHANGE_FAILED;
  1673. }
  1674. static uint32_t
  1675. pad_and_encrypt(uint8_t *dest, void *ps, uint8_t *pp,
  1676. struct per_session_data__sshd *pss, int skip_pad)
  1677. {
  1678. uint32_t n;
  1679. if (!skip_pad)
  1680. lws_pad_set_length(pss, ps, &pp, &pss->active_keys_stc);
  1681. n = lws_ptr_diff(pp, ps);
  1682. if (!pss->active_keys_stc.valid) {
  1683. memcpy(dest, ps, n);
  1684. return n;
  1685. }
  1686. lws_chacha_encrypt(&pss->active_keys_stc, pss->ssh_sequence_ctr_stc,
  1687. ps, n, dest);
  1688. n += pss->active_keys_stc.MAC_length;
  1689. return n;
  1690. }
  1691. static int
  1692. lws_callback_raw_sshd(struct lws *wsi, enum lws_callback_reasons reason,
  1693. void *user, void *in, size_t len)
  1694. {
  1695. struct per_session_data__sshd *pss =
  1696. (struct per_session_data__sshd *)user, **p;
  1697. struct per_vhost_data__sshd *vhd = NULL;
  1698. uint8_t buf[LWS_PRE + 1024], *pp, *ps = &buf[LWS_PRE + 512], *ps1 = NULL;
  1699. const struct lws_protocol_vhost_options *pvo;
  1700. const struct lws_protocols *prot;
  1701. struct lws_ssh_channel *ch;
  1702. char lang[10];
  1703. int n, m, o;
  1704. /*
  1705. * Because we are an abstract protocol plugin, we will get called by
  1706. * wsi that actually bind to a plugin "on top of us" that calls thru
  1707. * to our callback.
  1708. *
  1709. * Under those circumstances, we can't simply get a pointer to our own
  1710. * protocol from the wsi. If there's a pss already, we can get it from
  1711. * there, but the first time for each connection we have to look it up.
  1712. */
  1713. if (pss && pss->vhd)
  1714. vhd = (struct per_vhost_data__sshd *)
  1715. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  1716. pss->vhd->protocol);
  1717. else
  1718. if (lws_get_vhost(wsi))
  1719. vhd = (struct per_vhost_data__sshd *)
  1720. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  1721. lws_vhost_name_to_protocol(
  1722. lws_get_vhost(wsi), "lws-ssh-base"));
  1723. switch ((int)reason) {
  1724. case LWS_CALLBACK_PROTOCOL_INIT:
  1725. vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  1726. lws_get_protocol(wsi),
  1727. sizeof(struct per_vhost_data__sshd));
  1728. vhd->context = lws_get_context(wsi);
  1729. vhd->protocol = lws_get_protocol(wsi);
  1730. vhd->vhost = lws_get_vhost(wsi);
  1731. pvo = (const struct lws_protocol_vhost_options *)in;
  1732. while (pvo) {
  1733. /*
  1734. * the user code passes the ops struct address to us
  1735. * using a pvo (per-vhost option)
  1736. */
  1737. if (!strcmp(pvo->name, "ops"))
  1738. vhd->ops = (const struct lws_ssh_ops *)pvo->value;
  1739. /*
  1740. * the user code is telling us to get the ops struct
  1741. * from another protocol's protocol.user pointer
  1742. */
  1743. if (!strcmp(pvo->name, "ops-from")) {
  1744. prot = lws_vhost_name_to_protocol(vhd->vhost,
  1745. pvo->value);
  1746. if (prot)
  1747. vhd->ops = (const struct lws_ssh_ops *)prot->user;
  1748. else
  1749. lwsl_err("%s: can't find protocol %s\n",
  1750. __func__, pvo->value);
  1751. }
  1752. pvo = pvo->next;
  1753. }
  1754. if (!vhd->ops) {
  1755. lwsl_err("ssh pvo \"ops\" is mandatory\n");
  1756. return 1;
  1757. }
  1758. /*
  1759. * The user code ops api_version has to be current
  1760. */
  1761. if (vhd->ops->api_version != LWS_SSH_OPS_VERSION) {
  1762. lwsl_err("FATAL ops is api_version v%d but code is v%d\n",
  1763. vhd->ops->api_version, LWS_SSH_OPS_VERSION);
  1764. return 1;
  1765. }
  1766. break;
  1767. case LWS_CALLBACK_RAW_ADOPT:
  1768. lwsl_info("LWS_CALLBACK_RAW_ADOPT\n");
  1769. if (!vhd)
  1770. return -1;
  1771. pss->next = vhd->live_pss_list;
  1772. vhd->live_pss_list = pss;
  1773. pss->parser_state = SSH_INITIALIZE_TRANSIENT;
  1774. pss->wsi = wsi;
  1775. pss->vhd = vhd;
  1776. pss->kex_state = KEX_STATE_EXPECTING_CLIENT_OFFER;
  1777. pss->active_keys_cts.padding_alignment = 8;
  1778. pss->active_keys_stc.padding_alignment = 8;
  1779. if (lws_kex_create(pss))
  1780. return -1;
  1781. write_task(pss, NULL, SSH_WT_VERSION);
  1782. /* sect 4 RFC4252
  1783. *
  1784. * The server SHOULD have a timeout for authentication and
  1785. * disconnect if the authentication has not been accepted
  1786. * within the timeout period.
  1787. *
  1788. * The RECOMMENDED timeout period is 10 minutes.
  1789. */
  1790. lws_set_timeout(wsi,
  1791. SSH_PENDING_TIMEOUT_CONNECT_TO_SUCCESSFUL_AUTH, 10 * 60);
  1792. break;
  1793. case LWS_CALLBACK_RAW_CLOSE:
  1794. if (!pss)
  1795. return -1;
  1796. lwsl_info("LWS_CALLBACK_RAW_CLOSE\n");
  1797. lws_kex_destroy(pss);
  1798. lws_ua_destroy(pss);
  1799. ssh_free_set_NULL(pss->last_alloc);
  1800. while (pss->ch_list)
  1801. ssh_destroy_channel(pss, pss->ch_list);
  1802. lws_chacha_destroy(&pss->active_keys_cts);
  1803. lws_chacha_destroy(&pss->active_keys_stc);
  1804. p = &vhd->live_pss_list;
  1805. while (*p) {
  1806. if ((*p) == pss) {
  1807. *p = pss->next;
  1808. continue;
  1809. }
  1810. p = &((*p)->next);
  1811. }
  1812. break;
  1813. case LWS_CALLBACK_RAW_RX:
  1814. if (!pss)
  1815. return -1;
  1816. if (parse(pss, in, len))
  1817. return -1;
  1818. break;
  1819. case LWS_CALLBACK_RAW_WRITEABLE:
  1820. if (!pss)
  1821. break;
  1822. n = 0;
  1823. o = pss->write_task[pss->wt_tail];
  1824. ch = pss->write_channel[pss->wt_tail];
  1825. if (pss->wt_head == pss->wt_tail)
  1826. o = SSH_WT_NONE;
  1827. switch (o) {
  1828. case SSH_WT_VERSION:
  1829. if (!pss->vhd)
  1830. break;
  1831. n = lws_snprintf((char *)buf + LWS_PRE,
  1832. sizeof(buf) - LWS_PRE - 1, "%s\r\n",
  1833. pss->vhd->ops->server_string);
  1834. write_task(pss, NULL, SSH_WT_OFFER);
  1835. break;
  1836. case SSH_WT_OFFER:
  1837. if (!pss->vhd)
  1838. break;
  1839. m = 0;
  1840. n = offer(pss, buf + LWS_PRE,
  1841. sizeof(buf) - LWS_PRE, 0, &m);
  1842. if (n == 0) {
  1843. lwsl_notice("Too small\n");
  1844. return -1;
  1845. }
  1846. if (!pss->kex) {
  1847. lwsl_notice("%s: SSH_WT_OFFER: pss->kex is NULL\n",
  1848. __func__);
  1849. return -1;
  1850. }
  1851. /* we need a copy of it to generate the hash later */
  1852. if (pss->kex->I_S)
  1853. free(pss->kex->I_S);
  1854. pss->kex->I_S = sshd_zalloc(m);
  1855. if (!pss->kex->I_S) {
  1856. lwsl_notice("OOM 5: %d\n", m);
  1857. return -1;
  1858. }
  1859. /* without length + padcount part */
  1860. memcpy(pss->kex->I_S, buf + LWS_PRE + 5, m);
  1861. pss->kex->I_S_payload_len = m; /* without padding */
  1862. break;
  1863. case SSH_WT_OFFER_REPLY:
  1864. memcpy(ps, pss->kex->kex_r, pss->kex->kex_r_len);
  1865. n = pad_and_encrypt(&buf[LWS_PRE], ps,
  1866. ps + pss->kex->kex_r_len, pss, 1);
  1867. pss->kex_state = KEX_STATE_REPLIED_TO_OFFER;
  1868. /* afterwards, must do newkeys */
  1869. write_task(pss, NULL, SSH_WT_SEND_NEWKEYS);
  1870. break;
  1871. case SSH_WT_SEND_NEWKEYS:
  1872. pp = ps + 5;
  1873. *pp++ = SSH_MSG_NEWKEYS;
  1874. goto pac;
  1875. case SSH_WT_UA_ACCEPT:
  1876. /*
  1877. * If the server supports the service (and permits
  1878. * the client to use it), it MUST respond with the
  1879. * following:
  1880. *
  1881. * byte SSH_MSG_SERVICE_ACCEPT
  1882. * string service name
  1883. */
  1884. pp = ps + 5;
  1885. *pp++ = SSH_MSG_SERVICE_ACCEPT;
  1886. lws_p32(pp, pss->npos);
  1887. pp += 4;
  1888. strcpy((char *)pp, pss->name);
  1889. pp += pss->npos;
  1890. goto pac;
  1891. case SSH_WT_UA_FAILURE:
  1892. pp = ps + 5;
  1893. *pp++ = SSH_MSG_USERAUTH_FAILURE;
  1894. lws_p32(pp, 9);
  1895. pp += 4;
  1896. strcpy((char *)pp, "publickey");
  1897. pp += 9;
  1898. *pp++ = 0;
  1899. goto pac;
  1900. case SSH_WT_UA_BANNER:
  1901. pp = ps + 5;
  1902. *pp++ = SSH_MSG_USERAUTH_BANNER;
  1903. if (pss->vhd && pss->vhd->ops->banner)
  1904. n = (int)pss->vhd->ops->banner((char *)&buf[650],
  1905. 150 - 1,
  1906. lang, (int)sizeof(lang));
  1907. lws_p32(pp, n);
  1908. pp += 4;
  1909. strcpy((char *)pp, (char *)&buf[650]);
  1910. pp += n;
  1911. if (lws_cstr(&pp, lang, sizeof(lang)))
  1912. goto bail;
  1913. goto pac;
  1914. case SSH_WT_UA_PK_OK:
  1915. /*
  1916. * The server MUST respond to this message with
  1917. * either SSH_MSG_USERAUTH_FAILURE or with the
  1918. * following:
  1919. *
  1920. * byte SSH_MSG_USERAUTH_PK_OK
  1921. * string public key alg name from the request
  1922. * string public key blob from the request
  1923. */
  1924. n = 74 + pss->ua->pubkey_len;
  1925. if (n > (int)sizeof(buf) - LWS_PRE) {
  1926. lwsl_notice("pubkey too large\n");
  1927. goto bail;
  1928. }
  1929. ps1 = sshd_zalloc(n);
  1930. if (!ps1)
  1931. goto bail;
  1932. ps = ps1;
  1933. pp = ps1 + 5;
  1934. *pp++ = SSH_MSG_USERAUTH_PK_OK;
  1935. if (lws_cstr(&pp, pss->ua->alg, 64)) {
  1936. free(ps1);
  1937. goto bail;
  1938. }
  1939. lws_p32(pp, pss->ua->pubkey_len);
  1940. pp += 4;
  1941. memcpy(pp, pss->ua->pubkey, pss->ua->pubkey_len);
  1942. pp += pss->ua->pubkey_len;
  1943. /* we no longer need the UA now we judged it */
  1944. lws_ua_destroy(pss);
  1945. goto pac;
  1946. case SSH_WT_UA_SUCCESS:
  1947. pp = ps + 5;
  1948. *pp++ = SSH_MSG_USERAUTH_SUCCESS;
  1949. /* end SSH_PENDING_TIMEOUT_CONNECT_TO_SUCCESSFUL_AUTH */
  1950. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  1951. goto pac;
  1952. case SSH_WT_CH_OPEN_CONF:
  1953. pp = ps + 5;
  1954. *pp++ = SSH_MSG_CHANNEL_OPEN_CONFIRMATION;
  1955. lws_p32(pp, pss->ch_temp->server_ch);
  1956. pp += 4;
  1957. lws_p32(pp, pss->ch_temp->sender_ch);
  1958. pp += 4;
  1959. /* tx initial window size towards us */
  1960. lws_p32(pp, LWS_SSH_INITIAL_WINDOW);
  1961. pp += 4;
  1962. /* maximum packet size towards us */
  1963. lws_p32(pp, 800);
  1964. pp += 4;
  1965. lwsl_info("SSH_WT_CH_OPEN_CONF\n");
  1966. /* it's on the linked-list */
  1967. pss->ch_temp = NULL;
  1968. goto pac;
  1969. case SSH_WT_CH_FAILURE:
  1970. pp = ps + 5;
  1971. *pp++ = SSH_MSG_CHANNEL_OPEN_FAILURE;
  1972. lws_p32(pp, ch->server_ch);
  1973. pp += 4;
  1974. lws_p32(pp, ch->sender_ch);
  1975. pp += 4;
  1976. lws_cstr(&pp, "reason", 64);
  1977. lws_cstr(&pp, "en/US", 64);
  1978. lwsl_info("SSH_WT_CH_FAILURE\n");
  1979. goto pac;
  1980. case SSH_WT_CHRQ_SUCC:
  1981. pp = ps + 5;
  1982. *pp++ = SSH_MSG_CHANNEL_SUCCESS;
  1983. lws_p32(pp, ch->server_ch);
  1984. lwsl_info("SSH_WT_CHRQ_SUCC\n");
  1985. pp += 4;
  1986. goto pac;
  1987. case SSH_WT_CHRQ_FAILURE:
  1988. pp = ps + 5;
  1989. *pp++ = SSH_MSG_CHANNEL_FAILURE;
  1990. lws_p32(pp, ch->server_ch);
  1991. pp += 4;
  1992. lwsl_info("SSH_WT_CHRQ_FAILURE\n");
  1993. goto pac;
  1994. case SSH_WT_CH_CLOSE:
  1995. pp = ps + 5;
  1996. *pp++ = SSH_MSG_CHANNEL_CLOSE;
  1997. lws_p32(pp, ch->server_ch);
  1998. lwsl_info("SSH_WT_CH_CLOSE\n");
  1999. pp += 4;
  2000. goto pac;
  2001. case SSH_WT_CH_EOF:
  2002. pp = ps + 5;
  2003. *pp++ = SSH_MSG_CHANNEL_EOF;
  2004. lws_p32(pp, ch->server_ch);
  2005. lwsl_info("SSH_WT_CH_EOF\n");
  2006. pp += 4;
  2007. goto pac;
  2008. case SSH_WT_SCP_ACK_ERROR:
  2009. case SSH_WT_SCP_ACK_OKAY:
  2010. pp = ps + 5;
  2011. *pp++ = SSH_MSG_CHANNEL_DATA;
  2012. /* ps + 6 */
  2013. lws_p32(pp, ch->sender_ch);
  2014. pp += 4;
  2015. lws_p32(pp, 1);
  2016. pp += 4;
  2017. if (o == SSH_WT_SCP_ACK_ERROR)
  2018. *pp++ = 2;
  2019. else
  2020. *pp++ = 0;
  2021. lwsl_info("SSH_WT_SCP_ACK_OKAY\n");
  2022. goto pac;
  2023. case SSH_WT_WINDOW_ADJUST:
  2024. pp = ps + 5;
  2025. *pp++ = SSH_MSG_CHANNEL_WINDOW_ADJUST;
  2026. /* ps + 6 */
  2027. lws_p32(pp, ch->sender_ch);
  2028. pp += 4;
  2029. lws_p32(pp, 32768);
  2030. pp += 4;
  2031. lwsl_info("send SSH_MSG_CHANNEL_WINDOW_ADJUST\n");
  2032. goto pac;
  2033. case SSH_WT_EXIT_STATUS:
  2034. pp = ps + 5;
  2035. *pp++ = SSH_MSG_CHANNEL_REQUEST;
  2036. lws_p32(pp, ch->sender_ch);
  2037. pp += 4;
  2038. lws_p32(pp, 11);
  2039. pp += 4;
  2040. strcpy((char *)pp, "exit-status");
  2041. pp += 11;
  2042. *pp++ = 0;
  2043. lws_p32(pp, ch->retcode);
  2044. pp += 4;
  2045. lwsl_info("send SSH_MSG_CHANNEL_EXIT_STATUS\n");
  2046. goto pac;
  2047. case SSH_WT_NONE:
  2048. default:
  2049. /* sending payload */
  2050. ch = ssh_get_server_ch(pss, 0);
  2051. /* have a channel up to send on? */
  2052. if (!ch)
  2053. break;
  2054. if (!pss->vhd || !pss->vhd->ops)
  2055. break;
  2056. n = pss->vhd->ops->tx_waiting(ch->priv);
  2057. if (n < 0)
  2058. return -1;
  2059. if (!n)
  2060. /* nothing to send */
  2061. break;
  2062. if (n == (LWS_STDOUT | LWS_STDERR)) {
  2063. /* pick one using round-robin */
  2064. if (pss->serviced_stderr_last)
  2065. n = LWS_STDOUT;
  2066. else
  2067. n = LWS_STDERR;
  2068. }
  2069. pss->serviced_stderr_last = !!(n & LWS_STDERR);
  2070. /* stdout or stderr */
  2071. pp = ps + 5;
  2072. if (n == LWS_STDOUT)
  2073. *pp++ = SSH_MSG_CHANNEL_DATA;
  2074. else
  2075. *pp++ = SSH_MSG_CHANNEL_EXTENDED_DATA;
  2076. /* ps + 6 */
  2077. lws_p32(pp, pss->ch_list->server_ch);
  2078. m = 14;
  2079. if (n == LWS_STDERR) {
  2080. pp += 4;
  2081. /* data type code... 1 for stderr payload */
  2082. lws_p32(pp, SSH_EXTENDED_DATA_STDERR);
  2083. m = 18;
  2084. }
  2085. /* also skip another strlen u32 at + 10 / +14 */
  2086. pp += 8;
  2087. /* ps + 14 / + 18 */
  2088. pp += pss->vhd->ops->tx(ch->priv, n, pp,
  2089. &buf[sizeof(buf) - 1] - pp);
  2090. lws_p32(ps + m - 4, lws_ptr_diff(pp, (ps + m)));
  2091. if (pss->vhd->ops->tx_waiting(ch->priv) > 0)
  2092. lws_callback_on_writable(wsi);
  2093. ch->window -= lws_ptr_diff(pp, ps) - m;
  2094. //lwsl_debug("our send window: %d\n", ch->window);
  2095. /* fallthru */
  2096. pac:
  2097. if (!pss->vhd)
  2098. break;
  2099. n = pad_and_encrypt(&buf[LWS_PRE], ps, pp, pss, 0);
  2100. break;
  2101. bail:
  2102. lws_ua_destroy(pss);
  2103. lws_kex_destroy(pss);
  2104. return 1;
  2105. }
  2106. if (n > 0) {
  2107. m = lws_write(wsi, (unsigned char *)buf + LWS_PRE, n,
  2108. LWS_WRITE_HTTP);
  2109. switch(o) {
  2110. case SSH_WT_SEND_NEWKEYS:
  2111. lwsl_info("Activating STC keys\n");
  2112. pss->active_keys_stc = pss->kex->keys_next_stc;
  2113. lws_chacha_activate(&pss->active_keys_stc);
  2114. pss->kex_state = KEX_STATE_CRYPTO_INITIALIZED;
  2115. pss->kex->newkeys |= 1;
  2116. if (pss->kex->newkeys == 3)
  2117. lws_kex_destroy(pss);
  2118. break;
  2119. case SSH_WT_UA_PK_OK:
  2120. free(ps1);
  2121. break;
  2122. case SSH_WT_CH_CLOSE:
  2123. if (ch->received_close) {
  2124. /*
  2125. * We are sending this at the behest of
  2126. * the remote peer...
  2127. * we can destroy the channel with no
  2128. * further communication.
  2129. */
  2130. ssh_destroy_channel(pss, ch);
  2131. break;
  2132. }
  2133. ch->sent_close = 1;
  2134. break;
  2135. }
  2136. if (m < 0) {
  2137. lwsl_err("ERR %d from write\n", m);
  2138. goto bail;
  2139. }
  2140. if (o != SSH_WT_VERSION)
  2141. pss->ssh_sequence_ctr_stc++;
  2142. if (o != SSH_WT_NONE)
  2143. pss->wt_tail =
  2144. (pss->wt_tail + 1) & 7;
  2145. } else
  2146. if (o == SSH_WT_UA_PK_OK) /* free it either way */
  2147. free(ps1);
  2148. ch = ssh_get_server_ch(pss, 0);
  2149. if (pss->wt_head != pss->wt_tail ||
  2150. (ch && ch->priv && pss->vhd &&
  2151. pss->vhd->ops->tx_waiting(ch->priv)))
  2152. lws_callback_on_writable(wsi);
  2153. break;
  2154. case LWS_CALLBACK_SSH_UART_SET_RXFLOW:
  2155. /*
  2156. * this is sent to set rxflow state on any connections that
  2157. * sink on a particular sink. The sink index affected is in len
  2158. *
  2159. * More than one protocol may sink to the same uart, and the
  2160. * protocol may select the sink itself, eg, in the URL used
  2161. * to set up the connection.
  2162. */
  2163. lwsl_notice("sshd LWS_CALLBACK_SSH_UART_SET_RXFLOW: wsi %p, %d\n",
  2164. wsi, (int)len & 1);
  2165. lws_rx_flow_control(wsi, len & 1);
  2166. break;
  2167. case LWS_CALLBACK_CGI:
  2168. if (!pss)
  2169. break;
  2170. if (pss->vhd && pss->vhd->ops &&
  2171. pss->vhd->ops->child_process_io &&
  2172. pss->vhd->ops->child_process_io(pss->ch_temp->priv,
  2173. pss->wsi, (struct lws_cgi_args *)in))
  2174. return -1;
  2175. break;
  2176. case LWS_CALLBACK_CGI_PROCESS_ATTACH:
  2177. if (!pss)
  2178. break;
  2179. ch = ssh_get_server_ch(pss, pss->channel_doing_spawn);
  2180. if (ch) {
  2181. ch->spawn_pid = (int)len; /* child process PID */
  2182. lwsl_notice("associated PID %d to ch %d\n", (int)len,
  2183. pss->channel_doing_spawn);
  2184. }
  2185. break;
  2186. case LWS_CALLBACK_CGI_TERMINATED:
  2187. if (!pss)
  2188. break;
  2189. if (pss->vhd && pss->vhd->ops &&
  2190. pss->vhd->ops->child_process_terminated)
  2191. pss->vhd->ops->child_process_terminated(pss->ch_temp->priv,
  2192. pss->wsi);
  2193. /*
  2194. * we have the child PID in len... we need to match it to a
  2195. * channel that is on the wsi
  2196. */
  2197. ch = pss->ch_list;
  2198. while (ch) {
  2199. if (ch->spawn_pid == len) {
  2200. lwsl_notice("starting close of ch with PID %d\n",
  2201. (int)len);
  2202. ch->scheduled_close = 1;
  2203. write_task(pss, ch, SSH_WT_CH_CLOSE);
  2204. break;
  2205. }
  2206. ch = ch->next;
  2207. }
  2208. break;
  2209. default:
  2210. break;
  2211. }
  2212. return 0;
  2213. }
  2214. #define LWS_PLUGIN_PROTOCOL_LWS_RAW_SSHD { \
  2215. "lws-ssh-base", \
  2216. lws_callback_raw_sshd, \
  2217. sizeof(struct per_session_data__sshd), \
  2218. 1024, 0, NULL, 900 \
  2219. }
  2220. const struct lws_protocols protocols_sshd[] = {
  2221. LWS_PLUGIN_PROTOCOL_LWS_RAW_SSHD,
  2222. { NULL, NULL, 0, 0, 0, NULL, 0 } /* terminator */
  2223. };
  2224. #if !defined (LWS_PLUGIN_STATIC)
  2225. LWS_VISIBLE const lws_plugin_protocol_t lws_ssh_base = {
  2226. .hdr = {
  2227. "ssh base",
  2228. "lws_protocol_plugin",
  2229. LWS_PLUGIN_API_MAGIC
  2230. },
  2231. .protocols = protocols_sshd,
  2232. .count_protocols = LWS_ARRAY_SIZE(protocols_sshd),
  2233. .extensions = NULL,
  2234. .count_extensions = 0,
  2235. };
  2236. #endif