common_p.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. /*
  2. * Supporting routines used in common by all the various components of
  3. * the SSH system.
  4. */
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. #include "putty.h"
  8. #include "mpint.h"
  9. #include "ssh.h"
  10. #include "storage.h"
  11. #include "bpp.h"
  12. #include "ppl.h"
  13. #include "channel.h"
  14. /* ----------------------------------------------------------------------
  15. * Implementation of PacketQueue.
  16. */
  17. static void pq_ensure_unlinked(PacketQueueNode *node)
  18. {
  19. if (node->on_free_queue) {
  20. node->next->prev = node->prev;
  21. node->prev->next = node->next;
  22. } else {
  23. assert(!node->next);
  24. assert(!node->prev);
  25. }
  26. }
  27. void pq_base_push(PacketQueueBase *pqb, PacketQueueNode *node)
  28. {
  29. pq_ensure_unlinked(node);
  30. node->next = &pqb->end;
  31. node->prev = pqb->end.prev;
  32. node->next->prev = node;
  33. node->prev->next = node;
  34. pqb->total_size += node->formal_size;
  35. if (pqb->ic)
  36. queue_idempotent_callback(pqb->ic);
  37. }
  38. void pq_base_push_front(PacketQueueBase *pqb, PacketQueueNode *node)
  39. {
  40. pq_ensure_unlinked(node);
  41. node->prev = &pqb->end;
  42. node->next = pqb->end.next;
  43. node->next->prev = node;
  44. node->prev->next = node;
  45. pqb->total_size += node->formal_size;
  46. if (pqb->ic)
  47. queue_idempotent_callback(pqb->ic);
  48. }
  49. #ifndef WINSCP
  50. static PacketQueueNode pktin_freeq_head = {
  51. &pktin_freeq_head, &pktin_freeq_head, true
  52. };
  53. #endif
  54. /*WINSCP static*/ void pktin_free_queue_callback(void *vctx)
  55. {
  56. struct callback_set * set = (struct callback_set *)vctx;
  57. while (set->pktin_freeq_head->next != set->pktin_freeq_head) {
  58. PacketQueueNode *node = set->pktin_freeq_head->next;
  59. PktIn *pktin = container_of(node, PktIn, qnode);
  60. set->pktin_freeq_head->next = node->next;
  61. sfree(pktin);
  62. }
  63. set->pktin_freeq_head->prev = set->pktin_freeq_head;
  64. }
  65. #ifndef WINSCP
  66. static IdempotentCallback ic_pktin_free = {
  67. pktin_free_queue_callback, NULL, false
  68. };
  69. #endif
  70. static inline void pq_unlink_common(PacketQueueBase *pqb,
  71. PacketQueueNode *node)
  72. {
  73. node->next->prev = node->prev;
  74. node->prev->next = node->next;
  75. /* Check total_size doesn't drift out of sync downwards, by
  76. * ensuring it doesn't underflow when we do this subtraction */
  77. assert(pqb->total_size >= node->formal_size);
  78. pqb->total_size -= node->formal_size;
  79. /* Check total_size doesn't drift out of sync upwards, by checking
  80. * that it's returned to exactly zero whenever a queue is
  81. * emptied */
  82. assert(pqb->end.next != &pqb->end || pqb->total_size == 0);
  83. }
  84. static PktIn *pq_in_after(PacketQueueBase *pqb,
  85. PacketQueueNode *prev, bool pop)
  86. {
  87. PacketQueueNode *node = prev->next;
  88. if (node == &pqb->end)
  89. return NULL;
  90. if (pop) {
  91. #ifdef WINSCP
  92. struct callback_set * set = get_seat_callback_set(pqb->seat);
  93. assert(set != NULL);
  94. if (set->ic_pktin_free == NULL)
  95. {
  96. set->pktin_freeq_head = snew(PacketQueueNode);
  97. set->pktin_freeq_head->next = set->pktin_freeq_head;
  98. set->pktin_freeq_head->prev = set->pktin_freeq_head;
  99. set->pktin_freeq_head->on_free_queue = TRUE;
  100. set->ic_pktin_free = snew(IdempotentCallback);
  101. set->ic_pktin_free->fn = pktin_free_queue_callback;
  102. set->ic_pktin_free->ctx = set;
  103. set->ic_pktin_free->queued = FALSE;
  104. set->ic_pktin_free->set = set;
  105. }
  106. #endif
  107. pq_unlink_common(pqb, node);
  108. node->prev = set->pktin_freeq_head->prev; // WINSCP
  109. node->next = set->pktin_freeq_head; // WINSCP
  110. node->next->prev = node;
  111. node->prev->next = node;
  112. node->on_free_queue = true;
  113. queue_idempotent_callback(set->ic_pktin_free); // WINSCP
  114. }
  115. return container_of(node, PktIn, qnode);
  116. }
  117. static PktOut *pq_out_after(PacketQueueBase *pqb,
  118. PacketQueueNode *prev, bool pop)
  119. {
  120. PacketQueueNode *node = prev->next;
  121. if (node == &pqb->end)
  122. return NULL;
  123. if (pop) {
  124. pq_unlink_common(pqb, node);
  125. node->prev = node->next = NULL;
  126. }
  127. return container_of(node, PktOut, qnode);
  128. }
  129. void pq_in_init(PktInQueue *pq, Seat * seat) // WINSCP
  130. {
  131. pq->pqb.ic = NULL;
  132. pq->pqb.seat = seat;
  133. pq->pqb.end.next = pq->pqb.end.prev = &pq->pqb.end;
  134. pq->after = pq_in_after;
  135. pq->pqb.total_size = 0;
  136. }
  137. void pq_out_init(PktOutQueue *pq, Seat * seat) // WINSCP
  138. {
  139. pq->pqb.ic = NULL;
  140. pq->pqb.seat = seat;
  141. pq->pqb.end.next = pq->pqb.end.prev = &pq->pqb.end;
  142. pq->after = pq_out_after;
  143. pq->pqb.total_size = 0;
  144. }
  145. void pq_in_clear(PktInQueue *pq)
  146. {
  147. PktIn *pkt;
  148. pq->pqb.ic = NULL;
  149. while ((pkt = pq_pop(pq)) != NULL) {
  150. /* No need to actually free these packets: pq_pop on a
  151. * PktInQueue will automatically move them to the free
  152. * queue. */
  153. }
  154. }
  155. void pq_out_clear(PktOutQueue *pq)
  156. {
  157. PktOut *pkt;
  158. pq->pqb.ic = NULL;
  159. while ((pkt = pq_pop(pq)) != NULL)
  160. ssh_free_pktout(pkt);
  161. }
  162. /*
  163. * Concatenate the contents of the two queues q1 and q2, and leave the
  164. * result in qdest. qdest must be either empty, or one of the input
  165. * queues.
  166. */
  167. void pq_base_concatenate(PacketQueueBase *qdest,
  168. PacketQueueBase *q1, PacketQueueBase *q2)
  169. {
  170. struct PacketQueueNode *head1, *tail1, *head2, *tail2;
  171. size_t total_size = q1->total_size + q2->total_size;
  172. /*
  173. * Extract the contents from both input queues, and empty them.
  174. */
  175. head1 = (q1->end.next == &q1->end ? NULL : q1->end.next);
  176. tail1 = (q1->end.prev == &q1->end ? NULL : q1->end.prev);
  177. head2 = (q2->end.next == &q2->end ? NULL : q2->end.next);
  178. tail2 = (q2->end.prev == &q2->end ? NULL : q2->end.prev);
  179. q1->end.next = q1->end.prev = &q1->end;
  180. q2->end.next = q2->end.prev = &q2->end;
  181. q1->total_size = q2->total_size = 0;
  182. /*
  183. * Link the two lists together, handling the case where one or
  184. * both is empty.
  185. */
  186. if (tail1)
  187. tail1->next = head2;
  188. else
  189. head1 = head2;
  190. if (head2)
  191. head2->prev = tail1;
  192. else
  193. tail2 = tail1;
  194. /*
  195. * Check the destination queue is currently empty. (If it was one
  196. * of the input queues, then it will be, because we emptied both
  197. * of those just a moment ago.)
  198. */
  199. assert(qdest->end.next == &qdest->end);
  200. assert(qdest->end.prev == &qdest->end);
  201. /*
  202. * If our concatenated list has anything in it, then put it in
  203. * dest.
  204. */
  205. if (!head1) {
  206. assert(!tail2);
  207. } else {
  208. assert(tail2);
  209. qdest->end.next = head1;
  210. qdest->end.prev = tail2;
  211. head1->prev = &qdest->end;
  212. tail2->next = &qdest->end;
  213. if (qdest->ic)
  214. queue_idempotent_callback(qdest->ic);
  215. }
  216. qdest->total_size = total_size;
  217. }
  218. /* ----------------------------------------------------------------------
  219. * Low-level functions for the packet structures themselves.
  220. */
  221. static void ssh_pkt_BinarySink_write(BinarySink *bs,
  222. const void *data, size_t len);
  223. PktOut *ssh_new_packet(void)
  224. {
  225. PktOut *pkt = snew(PktOut);
  226. BinarySink_INIT(pkt, ssh_pkt_BinarySink_write);
  227. pkt->data = NULL;
  228. pkt->length = 0;
  229. pkt->maxlen = 0;
  230. pkt->downstream_id = 0;
  231. pkt->additional_log_text = NULL;
  232. pkt->qnode.next = pkt->qnode.prev = NULL;
  233. pkt->qnode.on_free_queue = false;
  234. return pkt;
  235. }
  236. static void ssh_pkt_adddata(PktOut *pkt, const void *data, int len)
  237. {
  238. sgrowarrayn_nm(pkt->data, pkt->maxlen, pkt->length, len);
  239. memcpy(pkt->data + pkt->length, data, len);
  240. pkt->length += len;
  241. pkt->qnode.formal_size = pkt->length;
  242. }
  243. static void ssh_pkt_BinarySink_write(BinarySink *bs,
  244. const void *data, size_t len)
  245. {
  246. PktOut *pkt = BinarySink_DOWNCAST(bs, PktOut);
  247. ssh_pkt_adddata(pkt, data, len);
  248. }
  249. void ssh_free_pktout(PktOut *pkt)
  250. {
  251. sfree(pkt->data);
  252. sfree(pkt);
  253. }
  254. /* ----------------------------------------------------------------------
  255. * Implement zombiechan_new() and its trivial vtable.
  256. */
  257. static void zombiechan_free(Channel *chan);
  258. static size_t zombiechan_send(
  259. Channel *chan, bool is_stderr, const void *, size_t);
  260. static void zombiechan_set_input_wanted(Channel *chan, bool wanted);
  261. static void zombiechan_do_nothing(Channel *chan);
  262. static void zombiechan_open_failure(Channel *chan, const char *);
  263. static bool zombiechan_want_close(Channel *chan, bool sent_eof, bool rcvd_eof);
  264. static char *zombiechan_log_close_msg(Channel *chan) { return NULL; }
  265. static const ChannelVtable zombiechan_channelvt = {
  266. /*.free =*/ zombiechan_free,
  267. /*.open_confirmation =*/ zombiechan_do_nothing,
  268. /*.open_failed =*/ zombiechan_open_failure,
  269. /*.send =*/ zombiechan_send,
  270. /*.send_eof =*/ zombiechan_do_nothing,
  271. /*.set_input_wanted =*/ zombiechan_set_input_wanted,
  272. /*.log_close_msg =*/ zombiechan_log_close_msg,
  273. /*.want_close =*/ zombiechan_want_close,
  274. /*.rcvd_exit_status =*/ chan_no_exit_status,
  275. /*.rcvd_exit_signal =*/ chan_no_exit_signal,
  276. /*.rcvd_exit_signal_numeric =*/ chan_no_exit_signal_numeric,
  277. /*.run_shell =*/ chan_no_run_shell,
  278. /*.run_command =*/ chan_no_run_command,
  279. /*.run_subsystem =*/ chan_no_run_subsystem,
  280. /*.enable_x11_forwarding =*/ chan_no_enable_x11_forwarding,
  281. /*.enable_agent_forwarding =*/ chan_no_enable_agent_forwarding,
  282. /*.allocate_pty =*/ chan_no_allocate_pty,
  283. /*.set_env =*/ chan_no_set_env,
  284. /*.send_break =*/ chan_no_send_break,
  285. /*.send_signal =*/ chan_no_send_signal,
  286. /*.change_window_size =*/ chan_no_change_window_size,
  287. /*.request_response =*/ chan_no_request_response,
  288. };
  289. Channel *zombiechan_new(void)
  290. {
  291. Channel *chan = snew(Channel);
  292. chan->vt = &zombiechan_channelvt;
  293. chan->initial_fixed_window_size = 0;
  294. return chan;
  295. }
  296. static void zombiechan_free(Channel *chan)
  297. {
  298. assert(chan->vt == &zombiechan_channelvt);
  299. sfree(chan);
  300. }
  301. static void zombiechan_do_nothing(Channel *chan)
  302. {
  303. assert(chan->vt == &zombiechan_channelvt);
  304. }
  305. static void zombiechan_open_failure(Channel *chan, const char *errtext)
  306. {
  307. assert(chan->vt == &zombiechan_channelvt);
  308. }
  309. static size_t zombiechan_send(Channel *chan, bool is_stderr,
  310. const void *data, size_t length)
  311. {
  312. assert(chan->vt == &zombiechan_channelvt);
  313. return 0;
  314. }
  315. static void zombiechan_set_input_wanted(Channel *chan, bool enable)
  316. {
  317. assert(chan->vt == &zombiechan_channelvt);
  318. }
  319. static bool zombiechan_want_close(Channel *chan, bool sent_eof, bool rcvd_eof)
  320. {
  321. return true;
  322. }
  323. /* ----------------------------------------------------------------------
  324. * Common routines for handling SSH tty modes.
  325. */
  326. static unsigned real_ttymode_opcode(unsigned our_opcode, int ssh_version)
  327. {
  328. switch (our_opcode) {
  329. case TTYMODE_ISPEED:
  330. return ssh_version == 1 ? TTYMODE_ISPEED_SSH1 : TTYMODE_ISPEED_SSH2;
  331. case TTYMODE_OSPEED:
  332. return ssh_version == 1 ? TTYMODE_OSPEED_SSH1 : TTYMODE_OSPEED_SSH2;
  333. default:
  334. return our_opcode;
  335. }
  336. }
  337. static unsigned our_ttymode_opcode(unsigned real_opcode, int ssh_version)
  338. {
  339. if (ssh_version == 1) {
  340. switch (real_opcode) {
  341. case TTYMODE_ISPEED_SSH1:
  342. return TTYMODE_ISPEED;
  343. case TTYMODE_OSPEED_SSH1:
  344. return TTYMODE_OSPEED;
  345. default:
  346. return real_opcode;
  347. }
  348. } else {
  349. switch (real_opcode) {
  350. case TTYMODE_ISPEED_SSH2:
  351. return TTYMODE_ISPEED;
  352. case TTYMODE_OSPEED_SSH2:
  353. return TTYMODE_OSPEED;
  354. default:
  355. return real_opcode;
  356. }
  357. }
  358. }
  359. struct ssh_ttymodes get_ttymodes_from_conf(Seat *seat, Conf *conf)
  360. {
  361. struct ssh_ttymodes modes;
  362. size_t i;
  363. static const struct mode_name_type {
  364. const char *mode;
  365. int opcode;
  366. enum { TYPE_CHAR, TYPE_BOOL } type;
  367. } modes_names_types[] = {
  368. #define TTYMODE_CHAR(name, val, index) { #name, val, TYPE_CHAR },
  369. #define TTYMODE_FLAG(name, val, field, mask) { #name, val, TYPE_BOOL },
  370. #include "ttymode-list.h"
  371. #undef TTYMODE_CHAR
  372. #undef TTYMODE_FLAG
  373. };
  374. memset(&modes, 0, sizeof(modes));
  375. for (i = 0; i < lenof(modes_names_types); i++) {
  376. const struct mode_name_type *mode = &modes_names_types[i];
  377. const char *sval = conf_get_str_str(conf, CONF_ttymodes, mode->mode);
  378. char *to_free = NULL;
  379. if (!sval)
  380. sval = "N"; /* just in case */
  381. /*
  382. * sval[0] can be
  383. * - 'V', indicating that an explicit value follows it;
  384. * - 'A', indicating that we should pass the value through from
  385. * the local environment via get_ttymode; or
  386. * - 'N', indicating that we should explicitly not send this
  387. * mode.
  388. */
  389. if (sval[0] == 'A') {
  390. sval = to_free = seat_get_ttymode(seat, mode->mode);
  391. } else if (sval[0] == 'V') {
  392. sval++; /* skip the 'V' */
  393. } else {
  394. /* else 'N', or something from the future we don't understand */
  395. continue;
  396. }
  397. if (sval) {
  398. /*
  399. * Parse the string representation of the tty mode
  400. * into the integer value it will take on the wire.
  401. */
  402. unsigned ival = 0;
  403. switch (mode->type) {
  404. case TYPE_CHAR:
  405. if (*sval) {
  406. char *next = NULL;
  407. /* We know ctrlparse won't write to the string, so
  408. * casting away const is ugly but allowable. */
  409. ival = ctrlparse((char *)sval, &next);
  410. if (!next)
  411. ival = sval[0];
  412. } else {
  413. ival = 255; /* special value meaning "don't set" */
  414. }
  415. break;
  416. case TYPE_BOOL:
  417. if (stricmp(sval, "yes") == 0 ||
  418. stricmp(sval, "on") == 0 ||
  419. stricmp(sval, "true") == 0 ||
  420. stricmp(sval, "+") == 0)
  421. ival = 1; /* true */
  422. else if (stricmp(sval, "no") == 0 ||
  423. stricmp(sval, "off") == 0 ||
  424. stricmp(sval, "false") == 0 ||
  425. stricmp(sval, "-") == 0)
  426. ival = 0; /* false */
  427. else
  428. ival = (atoi(sval) != 0);
  429. break;
  430. default:
  431. unreachable("Bad mode->type");
  432. }
  433. modes.have_mode[mode->opcode] = true;
  434. modes.mode_val[mode->opcode] = ival;
  435. }
  436. sfree(to_free);
  437. }
  438. {
  439. unsigned ospeed, ispeed;
  440. /* Unpick the terminal-speed config string. */
  441. ospeed = ispeed = 38400; /* last-resort defaults */
  442. sscanf(conf_get_str(conf, CONF_termspeed), "%u,%u", &ospeed, &ispeed);
  443. /* Currently we unconditionally set these */
  444. modes.have_mode[TTYMODE_ISPEED] = true;
  445. modes.mode_val[TTYMODE_ISPEED] = ispeed;
  446. modes.have_mode[TTYMODE_OSPEED] = true;
  447. modes.mode_val[TTYMODE_OSPEED] = ospeed;
  448. }
  449. return modes;
  450. }
  451. struct ssh_ttymodes read_ttymodes_from_packet(
  452. BinarySource *bs, int ssh_version)
  453. {
  454. struct ssh_ttymodes modes;
  455. memset(&modes, 0, sizeof(modes));
  456. while (1) {
  457. unsigned real_opcode, our_opcode;
  458. real_opcode = get_byte(bs);
  459. if (real_opcode == TTYMODE_END_OF_LIST)
  460. break;
  461. if (real_opcode >= 160) {
  462. /*
  463. * RFC 4254 (and the SSH 1.5 spec): "Opcodes 160 to 255
  464. * are not yet defined, and cause parsing to stop (they
  465. * should only be used after any other data)."
  466. *
  467. * My interpretation of this is that if one of these
  468. * opcodes appears, it's not a parse _error_, but it is
  469. * something that we don't know how to parse even well
  470. * enough to step over it to find the next opcode, so we
  471. * stop parsing now and assume that the rest of the string
  472. * is composed entirely of things we don't understand and
  473. * (as usual for unsupported terminal modes) silently
  474. * ignore.
  475. */
  476. return modes;
  477. }
  478. our_opcode = our_ttymode_opcode(real_opcode, ssh_version);
  479. assert(our_opcode < TTYMODE_LIMIT);
  480. modes.have_mode[our_opcode] = true;
  481. if (ssh_version == 1 && real_opcode >= 1 && real_opcode <= 127)
  482. modes.mode_val[our_opcode] = get_byte(bs);
  483. else
  484. modes.mode_val[our_opcode] = get_uint32(bs);
  485. }
  486. return modes;
  487. }
  488. void write_ttymodes_to_packet(BinarySink *bs, int ssh_version,
  489. struct ssh_ttymodes modes)
  490. {
  491. unsigned i;
  492. for (i = 0; i < TTYMODE_LIMIT; i++) {
  493. if (modes.have_mode[i]) {
  494. unsigned val = modes.mode_val[i];
  495. unsigned opcode = real_ttymode_opcode(i, ssh_version);
  496. put_byte(bs, opcode);
  497. if (ssh_version == 1 && opcode >= 1 && opcode <= 127)
  498. put_byte(bs, val);
  499. else
  500. put_uint32(bs, val);
  501. }
  502. }
  503. put_byte(bs, TTYMODE_END_OF_LIST);
  504. }
  505. /* ----------------------------------------------------------------------
  506. * Routine for allocating a new channel ID, given a means of finding
  507. * the index field in a given channel structure.
  508. */
  509. unsigned alloc_channel_id_general(tree234 *channels, size_t localid_offset)
  510. {
  511. const unsigned CHANNEL_NUMBER_OFFSET = 256;
  512. search234_state ss;
  513. /*
  514. * First-fit allocation of channel numbers: we always pick the
  515. * lowest unused one.
  516. *
  517. * Every channel before that, and no channel after it, has an ID
  518. * exactly equal to its tree index plus CHANNEL_NUMBER_OFFSET. So
  519. * we can use the search234 system to identify the length of that
  520. * initial sequence, in a single log-time pass down the channels
  521. * tree.
  522. */
  523. search234_start(&ss, channels);
  524. while (ss.element) {
  525. unsigned localid = *(unsigned *)((char *)ss.element + localid_offset);
  526. if (localid == ss.index + CHANNEL_NUMBER_OFFSET)
  527. search234_step(&ss, +1);
  528. else
  529. search234_step(&ss, -1);
  530. }
  531. /*
  532. * Now ss.index gives exactly the number of channels in that
  533. * initial sequence. So adding CHANNEL_NUMBER_OFFSET to it must
  534. * give precisely the lowest unused channel number.
  535. */
  536. return ss.index + CHANNEL_NUMBER_OFFSET;
  537. }
  538. /* ----------------------------------------------------------------------
  539. * Functions for handling the comma-separated strings used to store
  540. * lists of protocol identifiers in SSH-2.
  541. */
  542. void add_to_commasep_pl(strbuf *buf, ptrlen data)
  543. {
  544. if (buf->len > 0)
  545. put_byte(buf, ',');
  546. put_datapl(buf, data);
  547. }
  548. void add_to_commasep(strbuf *buf, const char *data)
  549. {
  550. add_to_commasep_pl(buf, ptrlen_from_asciz(data));
  551. }
  552. bool get_commasep_word(ptrlen *list, ptrlen *word)
  553. {
  554. const char *comma;
  555. /*
  556. * Discard empty list elements, should there be any, because we
  557. * never want to return one as if it was a real string. (This
  558. * introduces a mild tolerance of badly formatted data in lists we
  559. * receive, but I think that's acceptable.)
  560. */
  561. while (list->len > 0 && *(const char *)list->ptr == ',') {
  562. list->ptr = (const char *)list->ptr + 1;
  563. list->len--;
  564. }
  565. if (!list->len)
  566. return false;
  567. comma = memchr(list->ptr, ',', list->len);
  568. if (!comma) {
  569. *word = *list;
  570. list->len = 0;
  571. } else {
  572. size_t wordlen = comma - (const char *)list->ptr;
  573. word->ptr = list->ptr;
  574. word->len = wordlen;
  575. list->ptr = (const char *)list->ptr + wordlen + 1;
  576. list->len -= wordlen + 1;
  577. }
  578. return true;
  579. }
  580. /* ----------------------------------------------------------------------
  581. * Functions for translating SSH packet type codes into their symbolic
  582. * string names.
  583. */
  584. #define TRANSLATE_UNIVERSAL(y, name, value) \
  585. if (type == value) return #name;
  586. #define TRANSLATE_KEX(y, name, value, ctx) \
  587. if (type == value && pkt_kctx == ctx) return #name;
  588. #define TRANSLATE_AUTH(y, name, value, ctx) \
  589. if (type == value && pkt_actx == ctx) return #name;
  590. const char *ssh1_pkt_type(int type)
  591. {
  592. SSH1_MESSAGE_TYPES(TRANSLATE_UNIVERSAL, y);
  593. return "unknown";
  594. }
  595. const char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type)
  596. {
  597. SSH2_MESSAGE_TYPES(TRANSLATE_UNIVERSAL, TRANSLATE_KEX, TRANSLATE_AUTH, y);
  598. return "unknown";
  599. }
  600. #undef TRANSLATE_UNIVERSAL
  601. #undef TRANSLATE_KEX
  602. #undef TRANSLATE_AUTH
  603. /* ----------------------------------------------------------------------
  604. * Common helper function for clients and implementations of
  605. * PacketProtocolLayer.
  606. */
  607. void ssh_ppl_replace(PacketProtocolLayer *old, PacketProtocolLayer *new)
  608. {
  609. new->bpp = old->bpp;
  610. ssh_ppl_setup_queues(new, old->in_pq, old->out_pq);
  611. new->selfptr = old->selfptr;
  612. new->seat = old->seat;
  613. new->ssh = old->ssh;
  614. *new->selfptr = new;
  615. ssh_ppl_free(old);
  616. /* The new layer might need to be the first one that sends a
  617. * packet, so trigger a call to its main coroutine immediately. If
  618. * it doesn't need to go first, the worst that will do is return
  619. * straight away. */
  620. queue_idempotent_callback(&new->ic_process_queue);
  621. }
  622. void ssh_ppl_free(PacketProtocolLayer *ppl)
  623. {
  624. delete_callbacks_for_context(get_seat_callback_set(ppl->seat), ppl); // WINSCP
  625. ppl->vt->free(ppl);
  626. }
  627. static void ssh_ppl_ic_process_queue_callback(void *context)
  628. {
  629. PacketProtocolLayer *ppl = (PacketProtocolLayer *)context;
  630. ssh_ppl_process_queue(ppl);
  631. }
  632. void ssh_ppl_setup_queues(PacketProtocolLayer *ppl,
  633. PktInQueue *inq, PktOutQueue *outq)
  634. {
  635. ppl->in_pq = inq;
  636. ppl->out_pq = outq;
  637. ppl->in_pq->pqb.ic = &ppl->ic_process_queue;
  638. ppl->ic_process_queue.fn = ssh_ppl_ic_process_queue_callback;
  639. ppl->ic_process_queue.ctx = ppl;
  640. ppl->ic_process_queue.set = get_seat_callback_set(ppl->seat);
  641. /* If there's already something on the input queue, it will want
  642. * handling immediately. */
  643. if (pq_peek(ppl->in_pq))
  644. queue_idempotent_callback(&ppl->ic_process_queue);
  645. }
  646. void ssh_ppl_user_output_string_and_free(PacketProtocolLayer *ppl, char *text)
  647. {
  648. /* Messages sent via this function are from the SSH layer, not
  649. * from the server-side process, so they always have the stderr
  650. * flag set. */
  651. SeatOutputType stderrflag = (SeatOutputType)-1; // WINSCP
  652. seat_output(ppl->seat, stderrflag, text, strlen(text)); // WINSCP
  653. sfree(text);
  654. }
  655. size_t ssh_ppl_default_queued_data_size(PacketProtocolLayer *ppl)
  656. {
  657. return ppl->out_pq->pqb.total_size;
  658. }
  659. void ssh_ppl_default_final_output(PacketProtocolLayer *ppl)
  660. {
  661. }
  662. static void ssh_ppl_prompts_callback(void *ctx)
  663. {
  664. ssh_ppl_process_queue((PacketProtocolLayer *)ctx);
  665. }
  666. prompts_t *ssh_ppl_new_prompts(PacketProtocolLayer *ppl)
  667. {
  668. prompts_t *p = new_prompts();
  669. p->callback = ssh_ppl_prompts_callback;
  670. p->callback_ctx = ppl;
  671. return p;
  672. }
  673. /* ----------------------------------------------------------------------
  674. * Common helper functions for clients and implementations of
  675. * BinaryPacketProtocol.
  676. */
  677. static void ssh_bpp_input_raw_data_callback(void *context)
  678. {
  679. BinaryPacketProtocol *bpp = (BinaryPacketProtocol *)context;
  680. Ssh *ssh = bpp->ssh; /* in case bpp is about to get freed */
  681. ssh_bpp_handle_input(bpp);
  682. /* If we've now cleared enough backlog on the input connection, we
  683. * may need to unfreeze it. */
  684. ssh_conn_processed_data(ssh);
  685. }
  686. static void ssh_bpp_output_packet_callback(void *context)
  687. {
  688. BinaryPacketProtocol *bpp = (BinaryPacketProtocol *)context;
  689. ssh_bpp_handle_output(bpp);
  690. }
  691. void ssh_bpp_common_setup(BinaryPacketProtocol *bpp)
  692. {
  693. pq_in_init(&bpp->in_pq, get_log_seat(bpp->logctx)); // WINSCP
  694. pq_out_init(&bpp->out_pq, get_log_seat(bpp->logctx)); // WINSCP
  695. bpp->input_eof = false;
  696. bpp->ic_in_raw.fn = ssh_bpp_input_raw_data_callback;
  697. bpp->ic_in_raw.set = get_log_callback_set(bpp->logctx);
  698. bpp->ic_in_raw.ctx = bpp;
  699. bpp->ic_out_pq.fn = ssh_bpp_output_packet_callback;
  700. bpp->ic_out_pq.set = get_log_callback_set(bpp->logctx);
  701. bpp->ic_out_pq.ctx = bpp;
  702. bpp->out_pq.pqb.ic = &bpp->ic_out_pq;
  703. }
  704. void ssh_bpp_free(BinaryPacketProtocol *bpp)
  705. {
  706. // WINSCP
  707. delete_callbacks_for_context(get_log_callback_set(bpp->logctx), bpp);
  708. bpp->vt->free(bpp);
  709. }
  710. void ssh2_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  711. const char *msg, int category)
  712. {
  713. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH2_MSG_DISCONNECT);
  714. put_uint32(pkt, category);
  715. put_stringz(pkt, msg);
  716. put_stringz(pkt, "en"); /* language tag */
  717. pq_push(&bpp->out_pq, pkt);
  718. }
  719. #define BITMAP_UNIVERSAL(y, name, value) \
  720. | (value >= y && value < y+32 \
  721. ? 1UL << (value >= y && value < y+32 ? (value-y) : 0) \
  722. : 0)
  723. #define BITMAP_CONDITIONAL(y, name, value, ctx) \
  724. BITMAP_UNIVERSAL(y, name, value)
  725. #define SSH2_BITMAP_WORD(y) \
  726. (0 SSH2_MESSAGE_TYPES(BITMAP_UNIVERSAL, BITMAP_CONDITIONAL, \
  727. BITMAP_CONDITIONAL, (32*y)))
  728. bool ssh2_bpp_check_unimplemented(BinaryPacketProtocol *bpp, PktIn *pktin)
  729. {
  730. #pragma warn -osh
  731. static const unsigned valid_bitmap[] = {
  732. SSH2_BITMAP_WORD(0),
  733. SSH2_BITMAP_WORD(1),
  734. SSH2_BITMAP_WORD(2),
  735. SSH2_BITMAP_WORD(3),
  736. SSH2_BITMAP_WORD(4),
  737. SSH2_BITMAP_WORD(5),
  738. SSH2_BITMAP_WORD(6),
  739. SSH2_BITMAP_WORD(7),
  740. };
  741. #pragma warn +osh
  742. if (pktin->type < 0x100 &&
  743. !((valid_bitmap[pktin->type >> 5] >> (pktin->type & 0x1F)) & 1)) {
  744. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH2_MSG_UNIMPLEMENTED);
  745. put_uint32(pkt, pktin->sequence);
  746. pq_push(&bpp->out_pq, pkt);
  747. return true;
  748. }
  749. return false;
  750. }
  751. #undef BITMAP_UNIVERSAL
  752. #undef BITMAP_CONDITIONAL
  753. #undef SSH2_BITMAP_WORD
  754. /* ----------------------------------------------------------------------
  755. * Centralised component of SSH host key verification.
  756. *
  757. * verify_ssh_host_key is called from both the SSH-1 and SSH-2
  758. * transport layers, and does the initial work of checking whether the
  759. * host key is already known. If so, it returns success on its own
  760. * account; otherwise, it calls out to the Seat to give an interactive
  761. * prompt (the nature of which varies depending on the Seat itself).
  762. */
  763. SeatPromptResult verify_ssh_host_key(
  764. InteractionReadySeat iseat, Conf *conf, const char *host, int port,
  765. ssh_key *key, const char *keytype, char *keystr, const char *keydisp,
  766. char **fingerprints, int ca_count,
  767. void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
  768. {
  769. /*
  770. * First, check if the Conf includes a manual specification of the
  771. * expected host key. If so, that completely supersedes everything
  772. * else, including the normal host key cache _and_ including
  773. * manual overrides: we return success or failure immediately,
  774. * entirely based on whether the key matches the Conf.
  775. */
  776. if (conf_get_str_nthstrkey(conf, CONF_ssh_manual_hostkeys, 0)) {
  777. if (fingerprints) {
  778. size_t i; // WINSCP
  779. for (i = 0; i < SSH_N_FPTYPES; i++) {
  780. /*
  781. * Each fingerprint string we've been given will have
  782. * things like 'ssh-rsa 2048' at the front of it. Strip
  783. * those off and narrow down to just the hash at the end
  784. * of the string.
  785. */
  786. const char *fingerprint = fingerprints[i];
  787. if (!fingerprint)
  788. continue;
  789. { // WINSCP
  790. const char *p = strrchr(fingerprint, ' ');
  791. fingerprint = p ? p+1 : fingerprint;
  792. if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys,
  793. fingerprint))
  794. return SPR_OK;
  795. } // WINSCP
  796. }
  797. }
  798. if (key) {
  799. /*
  800. * Construct the base64-encoded public key blob and see if
  801. * that's listed.
  802. */
  803. strbuf *binblob;
  804. char *base64blob;
  805. int atoms, i;
  806. binblob = strbuf_new();
  807. ssh_key_public_blob(key, BinarySink_UPCAST(binblob));
  808. atoms = (binblob->len + 2) / 3;
  809. base64blob = snewn(atoms * 4 + 1, char);
  810. for (i = 0; i < atoms; i++)
  811. base64_encode_atom(binblob->u + 3*i,
  812. binblob->len - 3*i, base64blob + 4*i);
  813. base64blob[atoms * 4] = '\0';
  814. strbuf_free(binblob);
  815. if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys,
  816. base64blob)) {
  817. sfree(base64blob);
  818. return SPR_OK;
  819. }
  820. sfree(base64blob);
  821. }
  822. return SPR_SW_ABORT("Host key not in manually configured list");
  823. }
  824. /*
  825. * Next, check the host key cache.
  826. */
  827. { // WINSCP
  828. int storage_status = 1; // WINSCP check_stored_host_key(host, port, keytype, keystr);
  829. if (storage_status == 0) /* matching key was found in the cache */
  830. return SPR_OK;
  831. /*
  832. * The key is either missing from the cache, or does not match.
  833. * Either way, fall back to an interactive prompt from the Seat.
  834. */
  835. { // WINSCP
  836. SeatDialogText *text = seat_dialog_text_new();
  837. const SeatDialogPromptDescriptions *pds =
  838. seat_prompt_descriptions(iseat.seat);
  839. FingerprintType fptype_default =
  840. ssh2_pick_default_fingerprint(fingerprints);
  841. seat_dialog_text_append(
  842. text, SDT_TITLE, "%s Security Alert", appname);
  843. { // WINSCP
  844. HelpCtx helpctx;
  845. if (key && ssh_key_alg(key)->is_certificate) {
  846. seat_dialog_text_append(
  847. text, SDT_SCARY_HEADING, "WARNING - POTENTIAL SECURITY BREACH!");
  848. seat_dialog_text_append(
  849. text, SDT_PARA, "This server presented a certified host key:");
  850. seat_dialog_text_append(
  851. text, SDT_DISPLAY, "%s (port %d)", host, port);
  852. if (ca_count) {
  853. seat_dialog_text_append(
  854. text, SDT_PARA, "which was signed by a different "
  855. "certification authority from the %s %s is configured to "
  856. "trust for this server.", ca_count > 1 ? "ones" : "one",
  857. appname);
  858. if (storage_status == 2) {
  859. seat_dialog_text_append(
  860. text, SDT_PARA, "ALSO, that key does not match the key "
  861. "%s had previously cached for this server.", appname);
  862. seat_dialog_text_append(
  863. text, SDT_PARA, "This means that either another "
  864. "certification authority is operating in this realm AND "
  865. "the server administrator has changed the host key, or "
  866. "you have actually connected to another computer "
  867. "pretending to be the server.");
  868. } else {
  869. seat_dialog_text_append(
  870. text, SDT_PARA, "This means that either another "
  871. "certification authority is operating in this realm, or "
  872. "you have actually connected to another computer "
  873. "pretending to be the server.");
  874. }
  875. } else {
  876. // assert(storage_status == 2); WINSCP
  877. seat_dialog_text_append(
  878. text, SDT_PARA, "which does not match the certified key %s "
  879. "had previously cached for this server.", appname);
  880. seat_dialog_text_append(
  881. text, SDT_PARA, "This means that either the server "
  882. "administrator has changed the host key, or you have actually "
  883. "connected to another computer pretending to be the server.");
  884. }
  885. seat_dialog_text_append(
  886. text, SDT_PARA, "The new %s key fingerprint is:", keytype);
  887. seat_dialog_text_append(
  888. text, SDT_DISPLAY, "%s", fingerprints[fptype_default]);
  889. helpctx = HELPCTX(errors_cert_mismatch);
  890. } else if (storage_status == 1) {
  891. seat_dialog_text_append(
  892. text, SDT_PARA, "The host key is not cached for this server:");
  893. seat_dialog_text_append(
  894. text, SDT_DISPLAY, "%s (port %d)", host, port);
  895. seat_dialog_text_append(
  896. text, SDT_PARA, "You have no guarantee that the server is the "
  897. "computer you think it is.");
  898. seat_dialog_text_append(
  899. text, SDT_PARA, "The server's %s key fingerprint is:", keytype);
  900. seat_dialog_text_append(
  901. text, SDT_DISPLAY, "%s", fingerprints[fptype_default]);
  902. helpctx = HELPCTX(errors_hostkey_absent);
  903. } else {
  904. seat_dialog_text_append(
  905. text, SDT_SCARY_HEADING, "WARNING - POTENTIAL SECURITY BREACH!");
  906. seat_dialog_text_append(
  907. text, SDT_PARA, "The host key does not match the one %s has "
  908. "cached for this server:", appname);
  909. seat_dialog_text_append(
  910. text, SDT_DISPLAY, "%s (port %d)", host, port);
  911. seat_dialog_text_append(
  912. text, SDT_PARA, "This means that either the server administrator "
  913. "has changed the host key, or you have actually connected to "
  914. "another computer pretending to be the server.");
  915. seat_dialog_text_append(
  916. text, SDT_PARA, "The new %s key fingerprint is:", keytype);
  917. seat_dialog_text_append(
  918. text, SDT_DISPLAY, "%s", fingerprints[fptype_default]);
  919. helpctx = HELPCTX(errors_hostkey_changed);
  920. }
  921. /* The above text is printed even in batch mode. Here's where we stop if
  922. * we can't present interactive prompts. */
  923. seat_dialog_text_append(
  924. text, SDT_BATCH_ABORT, "Connection abandoned.");
  925. if (storage_status == 1) {
  926. seat_dialog_text_append(
  927. text, SDT_PARA, "If you trust this host, %s to add the key to "
  928. "%s's cache and carry on connecting.",
  929. pds->hk_accept_action, appname);
  930. if (key && ssh_key_alg(key)->is_certificate) {
  931. seat_dialog_text_append(
  932. text, SDT_PARA, "(Storing this certified key in the cache "
  933. "will NOT cause its certification authority to be trusted "
  934. "for any other key or host.)");
  935. }
  936. seat_dialog_text_append(
  937. text, SDT_PARA, "If you want to carry on connecting just once, "
  938. "without adding the key to the cache, %s.",
  939. pds->hk_connect_once_action);
  940. seat_dialog_text_append(
  941. text, SDT_PARA, "If you do not trust this host, %s to abandon the "
  942. "connection.", pds->hk_cancel_action);
  943. seat_dialog_text_append(
  944. text, SDT_PROMPT, "Store key in cache?");
  945. } else {
  946. seat_dialog_text_append(
  947. text, SDT_PARA, "If you were expecting this change and trust the "
  948. "new key, %s to update %s's cache and carry on connecting.",
  949. pds->hk_accept_action, appname);
  950. if (key && ssh_key_alg(key)->is_certificate) {
  951. seat_dialog_text_append(
  952. text, SDT_PARA, "(Storing this certified key in the cache "
  953. "will NOT cause its certification authority to be trusted "
  954. "for any other key or host.)");
  955. }
  956. seat_dialog_text_append(
  957. text, SDT_PARA, "If you want to carry on connecting but without "
  958. "updating the cache, %s.", pds->hk_connect_once_action);
  959. seat_dialog_text_append(
  960. text, SDT_PARA, "If you want to abandon the connection "
  961. "completely, %s to cancel. %s is the ONLY guaranteed safe choice.",
  962. pds->hk_cancel_action, pds->hk_cancel_action_Participle);
  963. seat_dialog_text_append(
  964. text, SDT_PROMPT, "Update cached key?");
  965. }
  966. seat_dialog_text_append(text, SDT_MORE_INFO_KEY,
  967. "Full text of host's public key");
  968. seat_dialog_text_append(text, SDT_MORE_INFO_VALUE_BLOB, "%s", keydisp);
  969. if (fingerprints[SSH_FPTYPE_SHA256]) {
  970. seat_dialog_text_append(text, SDT_MORE_INFO_KEY, "SHA256 fingerprint");
  971. seat_dialog_text_append(text, SDT_MORE_INFO_VALUE_SHORT, "%s",
  972. fingerprints[SSH_FPTYPE_SHA256]);
  973. }
  974. if (fingerprints[SSH_FPTYPE_MD5]) {
  975. seat_dialog_text_append(text, SDT_MORE_INFO_KEY, "MD5 fingerprint");
  976. seat_dialog_text_append(text, SDT_MORE_INFO_VALUE_SHORT, "%s",
  977. fingerprints[SSH_FPTYPE_MD5]);
  978. }
  979. { // WINSCP
  980. SeatPromptResult toret = seat_confirm_ssh_host_key(
  981. iseat, host, port, keytype, keystr, text, helpctx, callback, ctx,
  982. fingerprints, key && ssh_key_alg(key)->is_certificate, ca_count, false); // WINSCP
  983. seat_dialog_text_free(text);
  984. return toret;
  985. } // WINSCP
  986. } // WINSCP
  987. } // WINSCP
  988. } // WINSCP
  989. }
  990. SeatPromptResult confirm_weak_crypto_primitive(
  991. InteractionReadySeat iseat, const char *algtype, const char *algname,
  992. void (*callback)(void *ctx, SeatPromptResult result), void *ctx,
  993. WeakCryptoReason wcr)
  994. {
  995. SeatDialogText *text = seat_dialog_text_new();
  996. const SeatDialogPromptDescriptions *pds =
  997. seat_prompt_descriptions(iseat.seat);
  998. seat_dialog_text_append(text, SDT_TITLE, "%s Security Alert", appname);
  999. switch (wcr) {
  1000. case WCR_BELOW_THRESHOLD:
  1001. seat_dialog_text_append(
  1002. text, SDT_PARA,
  1003. "The first %s supported by the server is %s, "
  1004. "which is below the configured warning threshold.",
  1005. algtype, algname);
  1006. break;
  1007. case WCR_TERRAPIN:
  1008. case WCR_TERRAPIN_AVOIDABLE:
  1009. seat_dialog_text_append(
  1010. text, SDT_PARA,
  1011. "The %s selected for this session is %s, "
  1012. "which, with this server, is vulnerable to the 'Terrapin' attack "
  1013. "CVE-2023-48795, potentially allowing an attacker to modify "
  1014. "the encrypted session.",
  1015. algtype, algname);
  1016. seat_dialog_text_append(
  1017. text, SDT_PARA,
  1018. "Upgrading, patching, or reconfiguring this SSH server is the "
  1019. "best way to avoid this vulnerability, if possible.");
  1020. if (wcr == WCR_TERRAPIN_AVOIDABLE) {
  1021. seat_dialog_text_append(
  1022. text, SDT_PARA,
  1023. "You can also avoid this vulnerability by abandoning "
  1024. "this connection, moving ChaCha20 to below the "
  1025. "'warn below here' line in PuTTY's SSH cipher "
  1026. "configuration (so that an algorithm without the "
  1027. "vulnerability will be selected), and starting a new "
  1028. "connection.");
  1029. }
  1030. break;
  1031. default:
  1032. unreachable("bad WeakCryptoReason");
  1033. }
  1034. /* In batch mode, we print the above information and then this
  1035. * abort message, and stop. */
  1036. seat_dialog_text_append(text, SDT_BATCH_ABORT, "Connection abandoned.");
  1037. seat_dialog_text_append(
  1038. text, SDT_PARA, "To accept the risk and continue, %s. "
  1039. "To abandon the connection, %s.",
  1040. pds->weak_accept_action, pds->weak_cancel_action);
  1041. seat_dialog_text_append(text, SDT_PROMPT, "Continue with connection?");
  1042. { // WINSCP
  1043. SeatPromptResult toret = seat_confirm_weak_crypto_primitive(
  1044. iseat, text, callback, ctx,
  1045. algtype, algname, wcr); // WINSCP
  1046. seat_dialog_text_free(text);
  1047. return toret;
  1048. } // WINSCP
  1049. }
  1050. SeatPromptResult confirm_weak_cached_hostkey(
  1051. InteractionReadySeat iseat, const char *algname, const char **betteralgs,
  1052. void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
  1053. {
  1054. SeatDialogText *text = seat_dialog_text_new();
  1055. const SeatDialogPromptDescriptions *pds =
  1056. seat_prompt_descriptions(iseat.seat);
  1057. seat_dialog_text_append(text, SDT_TITLE, "%s Security Alert", appname);
  1058. seat_dialog_text_append(
  1059. text, SDT_PARA,
  1060. "The first host key type we have stored for this server "
  1061. "is %s, which is below the configured warning threshold.", algname);
  1062. seat_dialog_text_append(
  1063. text, SDT_PARA,
  1064. "The server also provides the following types of host key "
  1065. "above the threshold, which we do not have stored:");
  1066. { // WINSCP
  1067. const char **p; // WINSCP
  1068. for (p = betteralgs; *p; p++)
  1069. seat_dialog_text_append(text, SDT_DISPLAY, "%s", *p);
  1070. } // WINSCP
  1071. /* In batch mode, we print the above information and then this
  1072. * abort message, and stop. */
  1073. seat_dialog_text_append(text, SDT_BATCH_ABORT, "Connection abandoned.");
  1074. seat_dialog_text_append(
  1075. text, SDT_PARA, "To accept the risk and continue, %s. "
  1076. "To abandon the connection, %s.",
  1077. pds->weak_accept_action, pds->weak_cancel_action);
  1078. seat_dialog_text_append(text, SDT_PROMPT, "Continue with connection?");
  1079. { // WINSCP
  1080. SeatPromptResult toret = seat_confirm_weak_cached_hostkey(
  1081. iseat, text, callback, ctx);
  1082. seat_dialog_text_free(text);
  1083. return toret;
  1084. } // WINSCP
  1085. }
  1086. /* ----------------------------------------------------------------------
  1087. * Common functions shared between SSH-1 layers.
  1088. */
  1089. bool ssh1_common_get_specials(
  1090. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  1091. {
  1092. /*
  1093. * Don't bother offering IGNORE if we've decided the remote
  1094. * won't cope with it, since we wouldn't bother sending it if
  1095. * asked anyway.
  1096. */
  1097. if (!(ppl->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) {
  1098. add_special(ctx, "IGNORE message", SS_NOP, 0);
  1099. return true;
  1100. }
  1101. return false;
  1102. }
  1103. bool ssh1_common_filter_queue(PacketProtocolLayer *ppl)
  1104. {
  1105. PktIn *pktin;
  1106. ptrlen msg;
  1107. while ((pktin = pq_peek(ppl->in_pq)) != NULL) {
  1108. switch (pktin->type) {
  1109. case SSH1_MSG_DISCONNECT:
  1110. msg = get_string(pktin);
  1111. ssh_remote_error(ppl->ssh,
  1112. "Remote side sent disconnect message:\n\"%.*s\"",
  1113. PTRLEN_PRINTF(msg));
  1114. /* don't try to pop the queue, because we've been freed! */
  1115. return true; /* indicate that we've been freed */
  1116. case SSH1_MSG_DEBUG:
  1117. msg = get_string(pktin);
  1118. ppl_logevent("Remote debug message: %.*s", PTRLEN_PRINTF(msg));
  1119. pq_pop(ppl->in_pq);
  1120. break;
  1121. case SSH1_MSG_IGNORE:
  1122. /* Do nothing, because we're ignoring it! Duhh. */
  1123. pq_pop(ppl->in_pq);
  1124. break;
  1125. default:
  1126. return false;
  1127. }
  1128. }
  1129. return false;
  1130. }
  1131. void ssh1_compute_session_id(
  1132. unsigned char *session_id, const unsigned char *cookie,
  1133. RSAKey *hostkey, RSAKey *servkey)
  1134. {
  1135. ssh_hash *hash = ssh_hash_new(&ssh_md5);
  1136. size_t i; // WINSCP
  1137. for (i = (mp_get_nbits(hostkey->modulus) + 7) / 8; i-- ;)
  1138. put_byte(hash, mp_get_byte(hostkey->modulus, i));
  1139. for (i = (mp_get_nbits(servkey->modulus) + 7) / 8; i-- ;)
  1140. put_byte(hash, mp_get_byte(servkey->modulus, i));
  1141. put_data(hash, cookie, 8);
  1142. ssh_hash_final(hash, session_id);
  1143. }
  1144. /* ----------------------------------------------------------------------
  1145. * Wrapper function to handle the abort-connection modes of a
  1146. * SeatPromptResult without a lot of verbiage at every call site.
  1147. *
  1148. * Can become ssh_sw_abort or ssh_user_close, depending on the kind of
  1149. * negative SeatPromptResult.
  1150. */
  1151. void ssh_spr_close(Ssh *ssh, SeatPromptResult spr, const char *context)
  1152. {
  1153. if (spr.kind == SPRK_USER_ABORT) {
  1154. ssh_user_close(ssh, "User aborted at %s", context);
  1155. } else {
  1156. assert(spr.kind == SPRK_SW_ABORT);
  1157. { // WINSCP
  1158. char *err = spr_get_error_message(spr);
  1159. ssh_sw_abort(ssh, "%s", err);
  1160. sfree(err);
  1161. } // WINSCP
  1162. }
  1163. }