transport2.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  1. /*
  2. * Packet protocol layer for the SSH-2 transport protocol (RFC 4253).
  3. */
  4. #include <assert.h>
  5. #include "putty.h"
  6. #include "ssh.h"
  7. #include "bpp.h"
  8. #include "ppl.h"
  9. #include "sshcr.h"
  10. #ifndef WINSCP
  11. #include "server.h"
  12. #endif
  13. #include "storage.h"
  14. #include "transport2.h"
  15. #include "mpint.h"
  16. const struct ssh_signkey_with_user_pref_id ssh2_hostkey_algs[] = {
  17. #define ARRAYENT_HOSTKEY_ALGORITHM(type, alg) { &alg, type },
  18. HOSTKEY_ALGORITHMS(ARRAYENT_HOSTKEY_ALGORITHM)
  19. };
  20. const static ssh2_macalg *const macs[] = {
  21. &ssh_hmac_sha256, &ssh_hmac_sha1, &ssh_hmac_sha1_96, &ssh_hmac_md5
  22. };
  23. const static ssh2_macalg *const buggymacs[] = {
  24. &ssh_hmac_sha1_buggy, &ssh_hmac_sha1_96_buggy, &ssh_hmac_md5
  25. };
  26. static ssh_compressor *ssh_comp_none_init(void)
  27. {
  28. return NULL;
  29. }
  30. static void ssh_comp_none_cleanup(ssh_compressor *handle)
  31. {
  32. }
  33. static ssh_decompressor *ssh_decomp_none_init(void)
  34. {
  35. return NULL;
  36. }
  37. static void ssh_decomp_none_cleanup(ssh_decompressor *handle)
  38. {
  39. }
  40. static void ssh_comp_none_block(ssh_compressor *handle,
  41. const unsigned char *block, int len,
  42. unsigned char **outblock, int *outlen,
  43. int minlen)
  44. {
  45. }
  46. static bool ssh_decomp_none_block(ssh_decompressor *handle,
  47. const unsigned char *block, int len,
  48. unsigned char **outblock, int *outlen)
  49. {
  50. return false;
  51. }
  52. static const ssh_compression_alg ssh_comp_none = {
  53. // WINSCP
  54. /*.name =*/ "none",
  55. /*.delayed_name =*/ NULL,
  56. /*.compress_new =*/ ssh_comp_none_init,
  57. /*.compress_free =*/ ssh_comp_none_cleanup,
  58. /*.compress =*/ ssh_comp_none_block,
  59. /*.decompress_new =*/ ssh_decomp_none_init,
  60. /*.decompress_free =*/ ssh_decomp_none_cleanup,
  61. /*.decompress =*/ ssh_decomp_none_block,
  62. /*.text_name =*/ NULL,
  63. };
  64. const static ssh_compression_alg *const compressions[] = {
  65. &ssh_zlib, &ssh_comp_none
  66. };
  67. static void ssh2_transport_free(PacketProtocolLayer *);
  68. static void ssh2_transport_process_queue(PacketProtocolLayer *);
  69. static bool ssh2_transport_get_specials(
  70. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx);
  71. static void ssh2_transport_special_cmd(PacketProtocolLayer *ppl,
  72. SessionSpecialCode code, int arg);
  73. static void ssh2_transport_reconfigure(PacketProtocolLayer *ppl, Conf *conf);
  74. static size_t ssh2_transport_queued_data_size(PacketProtocolLayer *ppl);
  75. static void ssh2_transport_set_max_data_size(struct ssh2_transport_state *s);
  76. static unsigned long sanitise_rekey_time(int rekey_time, unsigned long def);
  77. static void ssh2_transport_higher_layer_packet_callback(void *context);
  78. static unsigned int ssh2_transport_winscp_query(PacketProtocolLayer *ppl, int query);
  79. static const PacketProtocolLayerVtable ssh2_transport_vtable = {
  80. // WINSCP
  81. /*.free =*/ ssh2_transport_free,
  82. /*.process_queue =*/ ssh2_transport_process_queue,
  83. /*.get_specials =*/ ssh2_transport_get_specials,
  84. /*.special_cmd =*/ ssh2_transport_special_cmd,
  85. /*.reconfigure =*/ ssh2_transport_reconfigure,
  86. /*.queued_data_size =*/ ssh2_transport_queued_data_size,
  87. /*.name =*/ NULL, /* no protocol name for this layer */
  88. ssh2_transport_winscp_query,
  89. };
  90. #ifndef NO_GSSAPI
  91. static void ssh2_transport_gss_update(struct ssh2_transport_state *s,
  92. bool definitely_rekeying);
  93. #endif
  94. static bool ssh2_transport_timer_update(struct ssh2_transport_state *s,
  95. unsigned long rekey_time);
  96. static SeatPromptResult ssh2_transport_confirm_weak_crypto_primitive(
  97. struct ssh2_transport_state *s, const char *type, const char *name,
  98. const void *alg);
  99. static const char *const kexlist_descr[NKEXLIST] = {
  100. "key exchange algorithm",
  101. "host key algorithm",
  102. "client-to-server cipher",
  103. "server-to-client cipher",
  104. "client-to-server MAC",
  105. "server-to-client MAC",
  106. "client-to-server compression method",
  107. "server-to-client compression method"
  108. };
  109. static int weak_algorithm_compare(void *av, void *bv);
  110. PacketProtocolLayer *ssh2_transport_new(
  111. Conf *conf, const char *host, int port, const char *fullhostname,
  112. const char *client_greeting, const char *server_greeting,
  113. struct ssh_connection_shared_gss_state *shgss,
  114. struct DataTransferStats *stats, PacketProtocolLayer *higher_layer,
  115. const SshServerConfig *ssc)
  116. {
  117. struct ssh2_transport_state *s = snew(struct ssh2_transport_state);
  118. memset(s, 0, sizeof(*s));
  119. s->ppl.vt = &ssh2_transport_vtable;
  120. s->conf = conf_copy(conf);
  121. s->savedhost = dupstr(host);
  122. s->savedport = port;
  123. s->fullhostname = dupstr(fullhostname);
  124. s->shgss = shgss;
  125. s->client_greeting = dupstr(client_greeting);
  126. s->server_greeting = dupstr(server_greeting);
  127. s->stats = stats;
  128. s->hostkeyblob = strbuf_new();
  129. pq_in_init(&s->pq_in_higher, higher_layer->seat); // WINSCP
  130. pq_out_init(&s->pq_out_higher, higher_layer->seat); // WINSCP
  131. s->pq_out_higher.pqb.ic = &s->ic_pq_out_higher;
  132. s->ic_pq_out_higher.fn = ssh2_transport_higher_layer_packet_callback;
  133. s->ic_pq_out_higher.ctx = &s->ppl;
  134. s->ic_pq_out_higher.set = get_seat_callback_set(higher_layer->seat);
  135. s->higher_layer = higher_layer;
  136. s->higher_layer->selfptr = &s->higher_layer;
  137. ssh_ppl_setup_queues(s->higher_layer, &s->pq_in_higher, &s->pq_out_higher);
  138. #ifndef NO_GSSAPI
  139. s->gss_cred_expiry = GSS_NO_EXPIRATION;
  140. s->shgss->srv_name = GSS_C_NO_NAME;
  141. s->shgss->ctx = NULL;
  142. #endif
  143. s->thc = ssh_transient_hostkey_cache_new();
  144. s->gss_kex_used = false;
  145. s->outgoing_kexinit = strbuf_new();
  146. s->incoming_kexinit = strbuf_new();
  147. if (ssc) {
  148. s->ssc = ssc;
  149. s->client_kexinit = s->incoming_kexinit;
  150. s->server_kexinit = s->outgoing_kexinit;
  151. s->cstrans = &s->in;
  152. s->sctrans = &s->out;
  153. s->out.mkkey_adjust = 1;
  154. } else {
  155. s->client_kexinit = s->outgoing_kexinit;
  156. s->server_kexinit = s->incoming_kexinit;
  157. s->cstrans = &s->out;
  158. s->sctrans = &s->in;
  159. s->in.mkkey_adjust = 1;
  160. }
  161. s->weak_algorithms_consented_to = newtree234(weak_algorithm_compare);
  162. ssh2_transport_set_max_data_size(s);
  163. return &s->ppl;
  164. }
  165. static void ssh2_transport_free(PacketProtocolLayer *ppl)
  166. {
  167. struct ssh2_transport_state *s =
  168. container_of(ppl, struct ssh2_transport_state, ppl);
  169. /*
  170. * As our last act before being freed, move any outgoing packets
  171. * off our higher layer's output queue on to our own output queue.
  172. * We might be being freed while the SSH connection is still alive
  173. * (because we're initiating shutdown from our end), in which case
  174. * we don't want those last few packets to get lost.
  175. *
  176. * (If our owner were to have already destroyed our output pq
  177. * before wanting to free us, then it would have to reset our
  178. * publicly visible out_pq field to NULL to inhibit this attempt.
  179. * But that's not how I expect the shutdown sequence to go in
  180. * practice.)
  181. */
  182. if (s->ppl.out_pq)
  183. pq_concatenate(s->ppl.out_pq, s->ppl.out_pq, &s->pq_out_higher);
  184. conf_free(s->conf);
  185. ssh_ppl_free(s->higher_layer);
  186. pq_in_clear(&s->pq_in_higher);
  187. pq_out_clear(&s->pq_out_higher);
  188. sfree(s->savedhost);
  189. sfree(s->fullhostname);
  190. sfree(s->client_greeting);
  191. sfree(s->server_greeting);
  192. sfree(s->keystr);
  193. sfree(s->hostkey_str);
  194. strbuf_free(s->hostkeyblob);
  195. if (s->hkey && !s->hostkeys) {
  196. ssh_key_free(s->hkey);
  197. s->hkey = NULL;
  198. }
  199. if (s->f) mp_free(s->f);
  200. if (s->p) mp_free(s->p);
  201. if (s->g) mp_free(s->g);
  202. if (s->K) mp_free(s->K);
  203. if (s->dh_ctx)
  204. dh_cleanup(s->dh_ctx);
  205. if (s->rsa_kex_key_needs_freeing) {
  206. ssh_rsakex_freekey(s->rsa_kex_key);
  207. sfree(s->rsa_kex_key);
  208. }
  209. if (s->ecdh_key)
  210. ssh_ecdhkex_freekey(s->ecdh_key);
  211. if (s->exhash)
  212. ssh_hash_free(s->exhash);
  213. strbuf_free(s->outgoing_kexinit);
  214. strbuf_free(s->incoming_kexinit);
  215. ssh_transient_hostkey_cache_free(s->thc);
  216. freetree234(s->weak_algorithms_consented_to);
  217. expire_timer_context(s);
  218. sfree(s);
  219. }
  220. /*
  221. * SSH-2 key derivation (RFC 4253 section 7.2).
  222. */
  223. static void ssh2_mkkey(
  224. struct ssh2_transport_state *s, strbuf *out,
  225. mp_int *K, unsigned char *H, char chr, int keylen)
  226. {
  227. int hlen = s->kex_alg->hash->hlen;
  228. int keylen_padded;
  229. unsigned char *key;
  230. ssh_hash *h;
  231. if (keylen == 0)
  232. return;
  233. /*
  234. * Round the requested amount of key material up to a multiple of
  235. * the length of the hash we're using to make it. This makes life
  236. * simpler because then we can just write each hash output block
  237. * straight into the output buffer without fiddling about
  238. * truncating the last one. Since it's going into a strbuf, and
  239. * strbufs are always smemclr()ed on free, there's no need to
  240. * worry about leaving extra potentially-sensitive data in memory
  241. * that the caller didn't ask for.
  242. */
  243. keylen_padded = ((keylen + hlen - 1) / hlen) * hlen;
  244. strbuf_clear(out);
  245. key = strbuf_append(out, keylen_padded);
  246. /* First hlen bytes. */
  247. h = ssh_hash_new(s->kex_alg->hash);
  248. if (!(s->ppl.remote_bugs & BUG_SSH2_DERIVEKEY))
  249. put_mp_ssh2(h, K);
  250. put_data(h, H, hlen);
  251. put_byte(h, chr);
  252. put_data(h, s->session_id, s->session_id_len);
  253. ssh_hash_digest(h, key);
  254. /* Subsequent blocks of hlen bytes. */
  255. if (keylen_padded > hlen) {
  256. int offset;
  257. ssh_hash_reset(h);
  258. if (!(s->ppl.remote_bugs & BUG_SSH2_DERIVEKEY))
  259. put_mp_ssh2(h, K);
  260. put_data(h, H, hlen);
  261. for (offset = hlen; offset < keylen_padded; offset += hlen) {
  262. put_data(h, key + offset - hlen, hlen);
  263. ssh_hash_digest_nondestructive(h, key + offset);
  264. }
  265. }
  266. ssh_hash_free(h);
  267. }
  268. /*
  269. * Find a slot in a KEXINIT algorithm list to use for a new algorithm.
  270. * If the algorithm is already in the list, return a pointer to its
  271. * entry, otherwise return an entry from the end of the list.
  272. * This assumes that every time a particular name is passed in, it
  273. * comes from the same string constant. If this isn't true, this
  274. * function may need to be rewritten to use strcmp() instead.
  275. */
  276. static struct kexinit_algorithm *ssh2_kexinit_addalg(struct kexinit_algorithm
  277. *list, const char *name)
  278. {
  279. int i;
  280. for (i = 0; i < MAXKEXLIST; i++)
  281. if (list[i].name == NULL || list[i].name == name) {
  282. list[i].name = name;
  283. return &list[i];
  284. }
  285. unreachable("Should never run out of space in KEXINIT list");
  286. }
  287. bool ssh2_common_filter_queue(PacketProtocolLayer *ppl)
  288. {
  289. static const char *const ssh2_disconnect_reasons[] = {
  290. NULL,
  291. "host not allowed to connect",
  292. "protocol error",
  293. "key exchange failed",
  294. "host authentication failed",
  295. "MAC error",
  296. "compression error",
  297. "service not available",
  298. "protocol version not supported",
  299. "host key not verifiable",
  300. "connection lost",
  301. "by application",
  302. "too many connections",
  303. "auth cancelled by user",
  304. "no more auth methods available",
  305. "illegal user name",
  306. };
  307. PktIn *pktin;
  308. ptrlen msg;
  309. int reason;
  310. while ((pktin = pq_peek(ppl->in_pq)) != NULL) {
  311. switch (pktin->type) {
  312. case SSH2_MSG_DISCONNECT:
  313. reason = get_uint32(pktin);
  314. msg = get_string(pktin);
  315. ssh_remote_error(
  316. ppl->ssh, "Remote side sent disconnect message\n"
  317. "type %d (%s):\n\"%.*s\"", reason,
  318. ((reason > 0 && reason < lenof(ssh2_disconnect_reasons)) ?
  319. ssh2_disconnect_reasons[reason] : "unknown"),
  320. PTRLEN_PRINTF(msg));
  321. /* don't try to pop the queue, because we've been freed! */
  322. return true; /* indicate that we've been freed */
  323. case SSH2_MSG_DEBUG:
  324. /* XXX maybe we should actually take notice of the return value */
  325. get_bool(pktin);
  326. msg = get_string(pktin);
  327. ppl_logevent("Remote debug message: %.*s", PTRLEN_PRINTF(msg));
  328. pq_pop(ppl->in_pq);
  329. break;
  330. case SSH2_MSG_IGNORE:
  331. /* Do nothing, because we're ignoring it! Duhh. */
  332. pq_pop(ppl->in_pq);
  333. break;
  334. case SSH2_MSG_EXT_INFO: {
  335. /*
  336. * The BPP enforces that these turn up only at legal
  337. * points in the protocol. In particular, it will not pass
  338. * an EXT_INFO on to us if it arrives before encryption is
  339. * enabled (which is when a MITM could inject one
  340. * maliciously).
  341. *
  342. * However, one of the criteria for legality is that a
  343. * server is permitted to send this message immediately
  344. * _before_ USERAUTH_SUCCESS. So we may receive this
  345. * message not yet knowing whether it's legal to have sent
  346. * it - we won't know until the BPP processes the next
  347. * packet.
  348. *
  349. * But that should be OK, because firstly, an
  350. * out-of-sequence EXT_INFO that's still within the
  351. * encrypted session is only a _protocol_ violation, not
  352. * an attack; secondly, any data we set in response to
  353. * such an illegal EXT_INFO won't have a chance to affect
  354. * the session before the BPP aborts it anyway.
  355. */
  356. uint32_t nexts = get_uint32(pktin);
  357. uint32_t i; // WINSCP
  358. for (i = 0; i < nexts && !get_err(pktin); i++) {
  359. ptrlen extname = get_string(pktin);
  360. ptrlen extvalue = get_string(pktin);
  361. if (ptrlen_eq_string(extname, "server-sig-algs")) {
  362. /*
  363. * Server has sent a list of signature algorithms
  364. * it will potentially accept for user
  365. * authentication keys. Check in particular
  366. * whether the RFC 8332 improved versions of
  367. * ssh-rsa are in the list, and set flags in the
  368. * BPP if so.
  369. *
  370. * TODO: another thing we _could_ do here is to
  371. * record a full list of the algorithm identifiers
  372. * we've seen, whether we understand them
  373. * ourselves or not. Then we could use that as a
  374. * pre-filter during userauth, to skip keys in the
  375. * SSH agent if we already know the server can't
  376. * possibly accept them. (Even if the key
  377. * algorithm is one that the agent and the server
  378. * both understand but we do not.)
  379. */
  380. ptrlen algname;
  381. while (get_commasep_word(&extvalue, &algname)) {
  382. if (ptrlen_eq_string(algname, "rsa-sha2-256"))
  383. ppl->bpp->ext_info_rsa_sha256_ok = true;
  384. if (ptrlen_eq_string(algname, "rsa-sha2-512"))
  385. ppl->bpp->ext_info_rsa_sha512_ok = true;
  386. }
  387. }
  388. }
  389. pq_pop(ppl->in_pq);
  390. break;
  391. }
  392. default:
  393. return false;
  394. }
  395. }
  396. return false;
  397. }
  398. static bool ssh2_transport_filter_queue(struct ssh2_transport_state *s)
  399. {
  400. PktIn *pktin;
  401. while (1) {
  402. if (ssh2_common_filter_queue(&s->ppl))
  403. return true;
  404. if ((pktin = pq_peek(s->ppl.in_pq)) == NULL)
  405. return false;
  406. /* Pass on packets to the next layer if they're outside
  407. * the range reserved for the transport protocol. */
  408. if (pktin->type >= 50) {
  409. /* ... except that we shouldn't tolerate higher-layer
  410. * packets coming from the server before we've seen
  411. * the first NEWKEYS. */
  412. if (!s->higher_layer_ok) {
  413. ssh_proto_error(s->ppl.ssh, "Received premature higher-"
  414. "layer packet, type %d (%s)", pktin->type,
  415. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  416. s->ppl.bpp->pls->actx,
  417. pktin->type));
  418. return true;
  419. }
  420. pq_pop(s->ppl.in_pq);
  421. pq_push(&s->pq_in_higher, pktin);
  422. } else {
  423. /* Anything else is a transport-layer packet that the main
  424. * process_queue coroutine should handle. */
  425. return false;
  426. }
  427. }
  428. }
  429. PktIn *ssh2_transport_pop(struct ssh2_transport_state *s)
  430. {
  431. if (ssh2_transport_filter_queue(s))
  432. return NULL; /* we've been freed */
  433. return pq_pop(s->ppl.in_pq);
  434. }
  435. static void ssh2_write_kexinit_lists(
  436. /*WINSCP*/ Seat * seat, BinarySink *pktout,
  437. struct kexinit_algorithm kexlists[NKEXLIST][MAXKEXLIST],
  438. Conf *conf, const SshServerConfig *ssc, int remote_bugs,
  439. const char *hk_host, int hk_port, const ssh_keyalg *hk_prev,
  440. ssh_transient_hostkey_cache *thc,
  441. ssh_key *const *our_hostkeys, int our_nhostkeys,
  442. bool first_time, bool can_gssapi_keyex, bool transient_hostkey_mode)
  443. {
  444. int i, j, k;
  445. bool warn;
  446. int n_preferred_kex;
  447. const ssh_kexes *preferred_kex[KEX_MAX + 1]; /* +1 for GSSAPI */
  448. int n_preferred_hk;
  449. int preferred_hk[HK_MAX];
  450. int n_preferred_ciphers;
  451. const ssh2_ciphers *preferred_ciphers[CIPHER_MAX];
  452. const ssh_compression_alg *preferred_comp;
  453. const ssh2_macalg *const *maclist;
  454. int nmacs;
  455. struct kexinit_algorithm *alg;
  456. /*
  457. * Set up the preferred key exchange. (NULL => warn below here)
  458. */
  459. n_preferred_kex = 0;
  460. if (can_gssapi_keyex)
  461. preferred_kex[n_preferred_kex++] = &ssh_gssk5_sha1_kex;
  462. for (i = 0; i < KEX_MAX; i++) {
  463. switch (conf_get_int_int(conf, CONF_ssh_kexlist, i)) {
  464. case KEX_DHGEX:
  465. preferred_kex[n_preferred_kex++] =
  466. &ssh_diffiehellman_gex;
  467. break;
  468. case KEX_DHGROUP14:
  469. preferred_kex[n_preferred_kex++] =
  470. &ssh_diffiehellman_group14;
  471. break;
  472. case KEX_DHGROUP1:
  473. preferred_kex[n_preferred_kex++] =
  474. &ssh_diffiehellman_group1;
  475. break;
  476. case KEX_RSA:
  477. preferred_kex[n_preferred_kex++] =
  478. &ssh_rsa_kex;
  479. break;
  480. case KEX_ECDH:
  481. preferred_kex[n_preferred_kex++] =
  482. &ssh_ecdh_kex;
  483. break;
  484. case KEX_WARN:
  485. /* Flag for later. Don't bother if it's the last in
  486. * the list. */
  487. if (i < KEX_MAX - 1) {
  488. preferred_kex[n_preferred_kex++] = NULL;
  489. }
  490. break;
  491. }
  492. }
  493. /*
  494. * Set up the preferred host key types. These are just the ids
  495. * in the enum in putty.h, so 'warn below here' is indicated
  496. * by HK_WARN.
  497. */
  498. n_preferred_hk = 0;
  499. for (i = 0; i < HK_MAX; i++) {
  500. int id = conf_get_int_int(conf, CONF_ssh_hklist, i);
  501. /* As above, don't bother with HK_WARN if it's last in the
  502. * list */
  503. if (id != HK_WARN || i < HK_MAX - 1)
  504. preferred_hk[n_preferred_hk++] = id;
  505. }
  506. /*
  507. * Set up the preferred ciphers. (NULL => warn below here)
  508. */
  509. n_preferred_ciphers = 0;
  510. for (i = 0; i < CIPHER_MAX; i++) {
  511. switch (conf_get_int_int(conf, CONF_ssh_cipherlist, i)) {
  512. case CIPHER_BLOWFISH:
  513. preferred_ciphers[n_preferred_ciphers++] = &ssh2_blowfish;
  514. break;
  515. case CIPHER_DES:
  516. if (conf_get_bool(conf, CONF_ssh2_des_cbc))
  517. preferred_ciphers[n_preferred_ciphers++] = &ssh2_des;
  518. break;
  519. case CIPHER_3DES:
  520. preferred_ciphers[n_preferred_ciphers++] = &ssh2_3des;
  521. break;
  522. case CIPHER_AES:
  523. preferred_ciphers[n_preferred_ciphers++] = &ssh2_aes;
  524. break;
  525. case CIPHER_ARCFOUR:
  526. preferred_ciphers[n_preferred_ciphers++] = &ssh2_arcfour;
  527. break;
  528. case CIPHER_CHACHA20:
  529. preferred_ciphers[n_preferred_ciphers++] = &ssh2_ccp;
  530. break;
  531. case CIPHER_WARN:
  532. /* Flag for later. Don't bother if it's the last in
  533. * the list. */
  534. if (i < CIPHER_MAX - 1) {
  535. preferred_ciphers[n_preferred_ciphers++] = NULL;
  536. }
  537. break;
  538. }
  539. }
  540. /*
  541. * Set up preferred compression.
  542. */
  543. if (conf_get_bool(conf, CONF_compression))
  544. preferred_comp = &ssh_zlib;
  545. else
  546. preferred_comp = &ssh_comp_none;
  547. for (i = 0; i < NKEXLIST; i++)
  548. for (j = 0; j < MAXKEXLIST; j++)
  549. kexlists[i][j].name = NULL;
  550. /* List key exchange algorithms. */
  551. warn = false;
  552. for (i = 0; i < n_preferred_kex; i++) {
  553. const ssh_kexes *k = preferred_kex[i];
  554. if (!k) warn = true;
  555. else for (j = 0; j < k->nkexes; j++) {
  556. alg = ssh2_kexinit_addalg(kexlists[KEXLIST_KEX],
  557. k->list[j]->name);
  558. alg->u.kex.kex = k->list[j];
  559. alg->u.kex.warn = warn;
  560. }
  561. }
  562. /* List server host key algorithms. */
  563. if (our_hostkeys) {
  564. /*
  565. * In server mode, we just list the algorithms that match the
  566. * host keys we actually have.
  567. */
  568. for (i = 0; i < our_nhostkeys; i++) {
  569. const ssh_keyalg *keyalg = ssh_key_alg(our_hostkeys[i]);
  570. alg = ssh2_kexinit_addalg(kexlists[KEXLIST_HOSTKEY],
  571. keyalg->ssh_id);
  572. alg->u.hk.hostkey = keyalg;
  573. alg->u.hk.hkflags = 0;
  574. alg->u.hk.warn = false;
  575. if (keyalg == &ssh_rsa) {
  576. alg = ssh2_kexinit_addalg(kexlists[KEXLIST_HOSTKEY],
  577. "rsa-sha2-256");
  578. alg->u.hk.hostkey = keyalg;
  579. alg->u.hk.hkflags = SSH_AGENT_RSA_SHA2_256;
  580. alg->u.hk.warn = false;
  581. alg = ssh2_kexinit_addalg(kexlists[KEXLIST_HOSTKEY],
  582. "rsa-sha2-512");
  583. alg->u.hk.hostkey = keyalg;
  584. alg->u.hk.hkflags = SSH_AGENT_RSA_SHA2_512;
  585. alg->u.hk.warn = false;
  586. }
  587. }
  588. } else if (first_time) {
  589. /*
  590. * In the first key exchange, we list all the algorithms we're
  591. * prepared to cope with, but (if configured to) we prefer
  592. * those algorithms for which we have a host key for this
  593. * host.
  594. *
  595. * If the host key algorithm is below the warning
  596. * threshold, we warn even if we did already have a key
  597. * for it, on the basis that if the user has just
  598. * reconfigured that host key type to be warned about,
  599. * they surely _do_ want to be alerted that a server
  600. * they're actually connecting to is using it.
  601. */
  602. warn = false;
  603. for (i = 0; i < n_preferred_hk; i++) {
  604. if (preferred_hk[i] == HK_WARN)
  605. warn = true;
  606. for (j = 0; j < lenof(ssh2_hostkey_algs); j++) {
  607. if (ssh2_hostkey_algs[j].id != preferred_hk[i])
  608. continue;
  609. if (conf_get_bool(conf, CONF_ssh_prefer_known_hostkeys) &&
  610. have_ssh_host_key(seat, hk_host, hk_port, // WINSCP
  611. ssh2_hostkey_algs[j].alg->cache_id)) {
  612. alg = ssh2_kexinit_addalg(kexlists[KEXLIST_HOSTKEY],
  613. ssh2_hostkey_algs[j].alg->ssh_id);
  614. alg->u.hk.hostkey = ssh2_hostkey_algs[j].alg;
  615. alg->u.hk.warn = warn;
  616. }
  617. }
  618. }
  619. warn = false;
  620. for (i = 0; i < n_preferred_hk; i++) {
  621. if (preferred_hk[i] == HK_WARN)
  622. warn = true;
  623. for (j = 0; j < lenof(ssh2_hostkey_algs); j++) {
  624. if (ssh2_hostkey_algs[j].id != preferred_hk[i])
  625. continue;
  626. alg = ssh2_kexinit_addalg(kexlists[KEXLIST_HOSTKEY],
  627. ssh2_hostkey_algs[j].alg->ssh_id);
  628. alg->u.hk.hostkey = ssh2_hostkey_algs[j].alg;
  629. alg->u.hk.warn = warn;
  630. }
  631. }
  632. #ifndef NO_GSSAPI
  633. } else if (transient_hostkey_mode) {
  634. /*
  635. * If we've previously done a GSSAPI KEX, then we list
  636. * precisely the algorithms for which a previous GSS key
  637. * exchange has delivered us a host key, because we expect
  638. * one of exactly those keys to be used in any subsequent
  639. * non-GSS-based rekey.
  640. *
  641. * An exception is if this is the key exchange we
  642. * triggered for the purposes of populating that cache -
  643. * in which case the cache will currently be empty, which
  644. * isn't helpful!
  645. */
  646. warn = false;
  647. for (i = 0; i < n_preferred_hk; i++) {
  648. if (preferred_hk[i] == HK_WARN)
  649. warn = true;
  650. for (j = 0; j < lenof(ssh2_hostkey_algs); j++) {
  651. if (ssh2_hostkey_algs[j].id != preferred_hk[i])
  652. continue;
  653. if (ssh_transient_hostkey_cache_has(
  654. thc, ssh2_hostkey_algs[j].alg)) {
  655. alg = ssh2_kexinit_addalg(kexlists[KEXLIST_HOSTKEY],
  656. ssh2_hostkey_algs[j].alg->ssh_id);
  657. alg->u.hk.hostkey = ssh2_hostkey_algs[j].alg;
  658. alg->u.hk.warn = warn;
  659. }
  660. }
  661. }
  662. #endif
  663. } else {
  664. /*
  665. * In subsequent key exchanges, we list only the host key
  666. * algorithm that was selected in the first key exchange,
  667. * so that we keep getting the same host key and hence
  668. * don't have to interrupt the user's session to ask for
  669. * reverification.
  670. */
  671. assert(hk_prev);
  672. alg = ssh2_kexinit_addalg(kexlists[KEXLIST_HOSTKEY], hk_prev->ssh_id);
  673. alg->u.hk.hostkey = hk_prev;
  674. alg->u.hk.warn = false;
  675. }
  676. if (can_gssapi_keyex) {
  677. alg = ssh2_kexinit_addalg(kexlists[KEXLIST_HOSTKEY], "null");
  678. alg->u.hk.hostkey = NULL;
  679. }
  680. /* List encryption algorithms (client->server then server->client). */
  681. for (k = KEXLIST_CSCIPHER; k <= KEXLIST_SCCIPHER; k++) {
  682. warn = false;
  683. #ifdef FUZZING
  684. alg = ssh2_kexinit_addalg(kexlists[k], "none");
  685. alg->u.cipher.cipher = NULL;
  686. alg->u.cipher.warn = warn;
  687. #endif /* FUZZING */
  688. for (i = 0; i < n_preferred_ciphers; i++) {
  689. const ssh2_ciphers *c = preferred_ciphers[i];
  690. if (!c) warn = true;
  691. else for (j = 0; j < c->nciphers; j++) {
  692. alg = ssh2_kexinit_addalg(kexlists[k],
  693. c->list[j]->ssh2_id);
  694. alg->u.cipher.cipher = c->list[j];
  695. alg->u.cipher.warn = warn;
  696. }
  697. }
  698. }
  699. /*
  700. * Be prepared to work around the buggy MAC problem.
  701. */
  702. if (remote_bugs & BUG_SSH2_HMAC) {
  703. maclist = buggymacs;
  704. nmacs = lenof(buggymacs);
  705. } else {
  706. maclist = macs;
  707. nmacs = lenof(macs);
  708. }
  709. /* List MAC algorithms (client->server then server->client). */
  710. for (j = KEXLIST_CSMAC; j <= KEXLIST_SCMAC; j++) {
  711. #ifdef FUZZING
  712. alg = ssh2_kexinit_addalg(kexlists[j], "none");
  713. alg->u.mac.mac = NULL;
  714. alg->u.mac.etm = false;
  715. #endif /* FUZZING */
  716. for (i = 0; i < nmacs; i++) {
  717. alg = ssh2_kexinit_addalg(kexlists[j], maclist[i]->name);
  718. alg->u.mac.mac = maclist[i];
  719. alg->u.mac.etm = false;
  720. }
  721. for (i = 0; i < nmacs; i++) {
  722. /* For each MAC, there may also be an ETM version,
  723. * which we list second. */
  724. if (maclist[i]->etm_name) {
  725. alg = ssh2_kexinit_addalg(kexlists[j], maclist[i]->etm_name);
  726. alg->u.mac.mac = maclist[i];
  727. alg->u.mac.etm = true;
  728. }
  729. }
  730. }
  731. /* List client->server compression algorithms,
  732. * then server->client compression algorithms. (We use the
  733. * same set twice.) */
  734. for (j = KEXLIST_CSCOMP; j <= KEXLIST_SCCOMP; j++) {
  735. assert(lenof(compressions) > 1);
  736. /* Prefer non-delayed versions */
  737. alg = ssh2_kexinit_addalg(kexlists[j], preferred_comp->name);
  738. alg->u.comp.comp = preferred_comp;
  739. alg->u.comp.delayed = false;
  740. if (preferred_comp->delayed_name) {
  741. alg = ssh2_kexinit_addalg(kexlists[j],
  742. preferred_comp->delayed_name);
  743. alg->u.comp.comp = preferred_comp;
  744. alg->u.comp.delayed = true;
  745. }
  746. for (i = 0; i < lenof(compressions); i++) {
  747. const ssh_compression_alg *c = compressions[i];
  748. alg = ssh2_kexinit_addalg(kexlists[j], c->name);
  749. alg->u.comp.comp = c;
  750. alg->u.comp.delayed = false;
  751. if (c->delayed_name) {
  752. alg = ssh2_kexinit_addalg(kexlists[j], c->delayed_name);
  753. alg->u.comp.comp = c;
  754. alg->u.comp.delayed = true;
  755. }
  756. }
  757. }
  758. /*
  759. * Finally, format the lists into text and write them into the
  760. * outgoing KEXINIT packet.
  761. */
  762. for (i = 0; i < NKEXLIST; i++) {
  763. strbuf *list = strbuf_new();
  764. #ifndef WINSCP
  765. if (ssc && ssc->kex_override[i].ptr) {
  766. put_datapl(list, ssc->kex_override[i]);
  767. } else {
  768. #endif
  769. for (j = 0; j < MAXKEXLIST; j++) {
  770. if (kexlists[i][j].name == NULL) break;
  771. add_to_commasep(list, kexlists[i][j].name);
  772. }
  773. #ifndef WINSCP
  774. }
  775. #endif
  776. if (i == KEXLIST_KEX && first_time) {
  777. if (our_hostkeys) /* we're the server */
  778. add_to_commasep(list, "ext-info-s");
  779. else /* we're the client */
  780. add_to_commasep(list, "ext-info-c");
  781. }
  782. put_stringsb(pktout, list);
  783. }
  784. /* List client->server languages. Empty list. */
  785. put_stringz(pktout, "");
  786. /* List server->client languages. Empty list. */
  787. put_stringz(pktout, "");
  788. }
  789. static bool ssh2_scan_kexinits(
  790. ptrlen client_kexinit, ptrlen server_kexinit,
  791. struct kexinit_algorithm kexlists[NKEXLIST][MAXKEXLIST],
  792. const ssh_kex **kex_alg, const ssh_keyalg **hostkey_alg,
  793. transport_direction *cs, transport_direction *sc,
  794. bool *warn_kex, bool *warn_hk, bool *warn_cscipher, bool *warn_sccipher,
  795. Ssh *ssh, bool *ignore_guess_cs_packet, bool *ignore_guess_sc_packet,
  796. int *n_server_hostkeys, int server_hostkeys[MAXKEXLIST], unsigned *hkflags,
  797. bool *can_send_ext_info)
  798. {
  799. BinarySource client[1], server[1];
  800. int i;
  801. bool guess_correct;
  802. ptrlen clists[NKEXLIST], slists[NKEXLIST];
  803. const struct kexinit_algorithm *selected[NKEXLIST];
  804. BinarySource_BARE_INIT_PL(client, client_kexinit);
  805. BinarySource_BARE_INIT_PL(server, server_kexinit);
  806. /* Skip packet type bytes and random cookies. */
  807. get_data(client, 1 + 16);
  808. get_data(server, 1 + 16);
  809. guess_correct = true;
  810. /* Find the matching string in each list, and map it to its
  811. * kexinit_algorithm structure. */
  812. for (i = 0; i < NKEXLIST; i++) {
  813. ptrlen clist, slist, cword, sword, found;
  814. bool cfirst, sfirst;
  815. int j;
  816. clists[i] = get_string(client);
  817. slists[i] = get_string(server);
  818. if (get_err(client) || get_err(server)) {
  819. /* Report a better error than the spurious "Couldn't
  820. * agree" that we'd generate if we pressed on regardless
  821. * and treated the empty get_string() result as genuine */
  822. ssh_proto_error(ssh, "KEXINIT packet was incomplete");
  823. return false;
  824. }
  825. for (cfirst = true, clist = clists[i];
  826. get_commasep_word(&clist, &cword); cfirst = false)
  827. for (sfirst = true, slist = slists[i];
  828. get_commasep_word(&slist, &sword); sfirst = false)
  829. if (ptrlen_eq_ptrlen(cword, sword)) {
  830. found = cword;
  831. goto found_match;
  832. }
  833. /* No matching string found in the two lists. Delay reporting
  834. * a fatal error until below, because sometimes it turns out
  835. * not to be fatal. */
  836. selected[i] = NULL;
  837. /*
  838. * However, even if a failure to agree on any algorithm at all
  839. * is not completely fatal (e.g. because it's the MAC
  840. * negotiation for a cipher that comes with a built-in MAC),
  841. * it still invalidates the guessed key exchange packet. (RFC
  842. * 4253 section 7, not contradicted by OpenSSH's
  843. * PROTOCOL.chacha20poly1305 or as far as I can see by their
  844. * code.)
  845. */
  846. guess_correct = false;
  847. continue;
  848. found_match:
  849. selected[i] = NULL;
  850. for (j = 0; j < MAXKEXLIST; j++) {
  851. if (kexlists[i][j].name &&
  852. ptrlen_eq_string(found, kexlists[i][j].name)) {
  853. selected[i] = &kexlists[i][j];
  854. break;
  855. }
  856. }
  857. if (!selected[i]) {
  858. /*
  859. * In the client, this should never happen! But in the
  860. * server, where we allow manual override on the command
  861. * line of the exact KEXINIT strings, it can happen
  862. * because the command line contained a typo. So we
  863. * produce a reasonably useful message instead of an
  864. * assertion failure.
  865. */
  866. ssh_sw_abort(ssh, "Selected %s \"%.*s\" does not correspond to "
  867. "any supported algorithm",
  868. kexlist_descr[i], PTRLEN_PRINTF(found));
  869. return false;
  870. }
  871. /*
  872. * If the kex or host key algorithm is not the first one in
  873. * both sides' lists, that means the guessed key exchange
  874. * packet (if any) is officially wrong.
  875. */
  876. if ((i == KEXLIST_KEX || i == KEXLIST_HOSTKEY) && !(cfirst || sfirst))
  877. guess_correct = false;
  878. }
  879. /*
  880. * Skip language strings in both KEXINITs, and read the flags
  881. * saying whether a guessed KEX packet follows.
  882. */
  883. get_string(client);
  884. get_string(client);
  885. get_string(server);
  886. get_string(server);
  887. if (ignore_guess_cs_packet)
  888. *ignore_guess_cs_packet = get_bool(client) && !guess_correct;
  889. if (ignore_guess_sc_packet)
  890. *ignore_guess_sc_packet = get_bool(server) && !guess_correct;
  891. /*
  892. * Now transcribe the selected algorithm set into the output data.
  893. */
  894. for (i = 0; i < NKEXLIST; i++) {
  895. const struct kexinit_algorithm *alg;
  896. /*
  897. * If we've already selected a cipher which requires a
  898. * particular MAC, then just select that. This is the case in
  899. * which it's not a fatal error if the actual MAC string lists
  900. * didn't include any matching error.
  901. */
  902. if (i == KEXLIST_CSMAC && cs->cipher &&
  903. cs->cipher->required_mac) {
  904. cs->mac = cs->cipher->required_mac;
  905. cs->etm_mode = !!(cs->mac->etm_name);
  906. continue;
  907. }
  908. if (i == KEXLIST_SCMAC && sc->cipher &&
  909. sc->cipher->required_mac) {
  910. sc->mac = sc->cipher->required_mac;
  911. sc->etm_mode = !!(sc->mac->etm_name);
  912. continue;
  913. }
  914. alg = selected[i];
  915. if (!alg) {
  916. /*
  917. * Otherwise, any match failure _is_ a fatal error.
  918. */
  919. ssh_sw_abort(ssh, "Couldn't agree a %s (available: %.*s)",
  920. kexlist_descr[i], PTRLEN_PRINTF(slists[i]));
  921. return false;
  922. }
  923. switch (i) {
  924. case KEXLIST_KEX:
  925. *kex_alg = alg->u.kex.kex;
  926. *warn_kex = alg->u.kex.warn;
  927. break;
  928. case KEXLIST_HOSTKEY:
  929. /*
  930. * Ignore an unexpected/inappropriate offer of "null",
  931. * we offer "null" when we're willing to use GSS KEX,
  932. * but it is only acceptable when GSSKEX is actually
  933. * selected.
  934. */
  935. if (alg->u.hk.hostkey == NULL &&
  936. (*kex_alg)->main_type != KEXTYPE_GSS)
  937. continue;
  938. *hostkey_alg = alg->u.hk.hostkey;
  939. *hkflags = alg->u.hk.hkflags;
  940. *warn_hk = alg->u.hk.warn;
  941. break;
  942. case KEXLIST_CSCIPHER:
  943. cs->cipher = alg->u.cipher.cipher;
  944. *warn_cscipher = alg->u.cipher.warn;
  945. break;
  946. case KEXLIST_SCCIPHER:
  947. sc->cipher = alg->u.cipher.cipher;
  948. *warn_sccipher = alg->u.cipher.warn;
  949. break;
  950. case KEXLIST_CSMAC:
  951. cs->mac = alg->u.mac.mac;
  952. cs->etm_mode = alg->u.mac.etm;
  953. break;
  954. case KEXLIST_SCMAC:
  955. sc->mac = alg->u.mac.mac;
  956. sc->etm_mode = alg->u.mac.etm;
  957. break;
  958. case KEXLIST_CSCOMP:
  959. cs->comp = alg->u.comp.comp;
  960. cs->comp_delayed = alg->u.comp.delayed;
  961. break;
  962. case KEXLIST_SCCOMP:
  963. sc->comp = alg->u.comp.comp;
  964. sc->comp_delayed = alg->u.comp.delayed;
  965. break;
  966. default:
  967. unreachable("Bad list index in scan_kexinits");
  968. }
  969. }
  970. /*
  971. * Check whether the other side advertised support for EXT_INFO.
  972. */
  973. {
  974. ptrlen extinfo_advert =
  975. (server_hostkeys ? PTRLEN_LITERAL("ext-info-c") :
  976. PTRLEN_LITERAL("ext-info-s"));
  977. ptrlen list = (server_hostkeys ? clists[KEXLIST_KEX] :
  978. slists[KEXLIST_KEX]);
  979. ptrlen word; // WINSCP
  980. for (; get_commasep_word(&list, &word) ;)
  981. if (ptrlen_eq_ptrlen(word, extinfo_advert))
  982. *can_send_ext_info = true;
  983. }
  984. if (server_hostkeys) {
  985. /*
  986. * Finally, make an auxiliary pass over the server's host key
  987. * list to find all the host key algorithms offered by the
  988. * server which we know about at all, whether we selected each
  989. * one or not. We return these as a list of indices into the
  990. * constant ssh2_hostkey_algs[] array.
  991. */
  992. *n_server_hostkeys = 0;
  993. { // WINSCP
  994. ptrlen list = slists[KEXLIST_HOSTKEY];
  995. ptrlen word; // WINSCP
  996. for (; get_commasep_word(&list, &word) ;) {
  997. for (i = 0; i < lenof(ssh2_hostkey_algs); i++)
  998. if (ptrlen_eq_string(word, ssh2_hostkey_algs[i].alg->ssh_id)) {
  999. server_hostkeys[(*n_server_hostkeys)++] = i;
  1000. break;
  1001. }
  1002. }
  1003. } // WINSCP
  1004. }
  1005. return true;
  1006. }
  1007. void ssh2transport_finalise_exhash(struct ssh2_transport_state *s)
  1008. {
  1009. put_mp_ssh2(s->exhash, s->K);
  1010. assert(ssh_hash_alg(s->exhash)->hlen <= sizeof(s->exchange_hash));
  1011. ssh_hash_final(s->exhash, s->exchange_hash);
  1012. s->exhash = NULL;
  1013. #if 0
  1014. debug("Exchange hash is:\n");
  1015. dmemdump(s->exchange_hash, s->kex_alg->hash->hlen);
  1016. #endif
  1017. }
  1018. static void ssh2_transport_process_queue(PacketProtocolLayer *ppl)
  1019. {
  1020. struct ssh2_transport_state *s =
  1021. container_of(ppl, struct ssh2_transport_state, ppl);
  1022. PktIn *pktin;
  1023. PktOut *pktout;
  1024. /* Filter centrally handled messages off the front of the queue on
  1025. * every entry to this coroutine, no matter where we're resuming
  1026. * from, even if we're _not_ looping on pq_pop. That way we can
  1027. * still proactively handle those messages even if we're waiting
  1028. * for a user response. */
  1029. if (ssh2_transport_filter_queue(s))
  1030. return; /* we've been freed */
  1031. crBegin(s->crState);
  1032. s->in.cipher = s->out.cipher = NULL;
  1033. s->in.mac = s->out.mac = NULL;
  1034. s->in.comp = s->out.comp = NULL;
  1035. s->got_session_id = false;
  1036. s->need_gss_transient_hostkey = false;
  1037. s->warned_about_no_gss_transient_hostkey = false;
  1038. begin_key_exchange:
  1039. #ifndef NO_GSSAPI
  1040. if (s->need_gss_transient_hostkey) {
  1041. /*
  1042. * This flag indicates a special case in which we must not do
  1043. * GSS key exchange even if we could. (See comments below,
  1044. * where the flag was set on the previous key exchange.)
  1045. */
  1046. s->can_gssapi_keyex = false;
  1047. } else if (conf_get_bool(s->conf, CONF_try_gssapi_kex)) {
  1048. /*
  1049. * We always check if we have GSS creds before we come up with
  1050. * the kex algorithm list, otherwise future rekeys will fail
  1051. * when creds expire. To make this so, this code section must
  1052. * follow the begin_key_exchange label above, otherwise this
  1053. * section would execute just once per-connection.
  1054. *
  1055. * Update GSS state unless the reason we're here is that a
  1056. * timer just checked the GSS state and decided that we should
  1057. * rekey to update delegated credentials. In that case, the
  1058. * state is "fresh".
  1059. */
  1060. if (s->rekey_class != RK_GSS_UPDATE)
  1061. ssh2_transport_gss_update(s, true);
  1062. /* Do GSSAPI KEX when capable */
  1063. s->can_gssapi_keyex = s->gss_status & GSS_KEX_CAPABLE;
  1064. /*
  1065. * But not when failure is likely. [ GSS implementations may
  1066. * attempt (and fail) to use a ticket that is almost expired
  1067. * when retrieved from the ccache that actually expires by the
  1068. * time the server receives it. ]
  1069. *
  1070. * Note: The first time always try KEXGSS if we can, failures
  1071. * will be very rare, and disabling the initial GSS KEX is
  1072. * worse. Some day GSS libraries will ignore cached tickets
  1073. * whose lifetime is critically short, and will instead use
  1074. * fresh ones.
  1075. */
  1076. if (!s->got_session_id && (s->gss_status & GSS_CTXT_MAYFAIL) != 0)
  1077. s->can_gssapi_keyex = false;
  1078. s->gss_delegate = conf_get_bool(s->conf, CONF_gssapifwd);
  1079. } else {
  1080. s->can_gssapi_keyex = false;
  1081. }
  1082. #endif
  1083. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_NOKEX;
  1084. /*
  1085. * Construct our KEXINIT packet, in a strbuf so we can refer to it
  1086. * later.
  1087. */
  1088. strbuf_clear(s->client_kexinit);
  1089. put_byte(s->outgoing_kexinit, SSH2_MSG_KEXINIT);
  1090. random_read(strbuf_append(s->outgoing_kexinit, 16), 16);
  1091. ssh2_write_kexinit_lists(
  1092. /*WINSCP*/ s->ppl.seat, BinarySink_UPCAST(s->outgoing_kexinit), s->kexlists,
  1093. s->conf, s->ssc, s->ppl.remote_bugs,
  1094. s->savedhost, s->savedport, s->hostkey_alg, s->thc,
  1095. s->hostkeys, s->nhostkeys,
  1096. !s->got_session_id, s->can_gssapi_keyex,
  1097. s->gss_kex_used && !s->need_gss_transient_hostkey);
  1098. /* First KEX packet does _not_ follow, because we're not that brave. */
  1099. put_bool(s->outgoing_kexinit, false);
  1100. put_uint32(s->outgoing_kexinit, 0); /* reserved */
  1101. /*
  1102. * Send our KEXINIT.
  1103. */
  1104. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEXINIT);
  1105. put_data(pktout, s->outgoing_kexinit->u + 1,
  1106. s->outgoing_kexinit->len - 1); /* omit initial packet type byte */
  1107. pq_push(s->ppl.out_pq, pktout);
  1108. /*
  1109. * Flag that KEX is in progress.
  1110. */
  1111. s->kex_in_progress = true;
  1112. /*
  1113. * Wait for the other side's KEXINIT, and save it.
  1114. */
  1115. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  1116. if (pktin->type != SSH2_MSG_KEXINIT) {
  1117. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  1118. "expecting KEXINIT, type %d (%s)", pktin->type,
  1119. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  1120. s->ppl.bpp->pls->actx, pktin->type));
  1121. return;
  1122. }
  1123. strbuf_clear(s->incoming_kexinit);
  1124. put_byte(s->incoming_kexinit, SSH2_MSG_KEXINIT);
  1125. put_data(s->incoming_kexinit, get_ptr(pktin), get_avail(pktin));
  1126. /*
  1127. * Work through the two KEXINIT packets in parallel to find the
  1128. * selected algorithm identifiers.
  1129. */
  1130. {
  1131. int nhk, hks[MAXKEXLIST], i, j;
  1132. if (!ssh2_scan_kexinits(
  1133. ptrlen_from_strbuf(s->client_kexinit),
  1134. ptrlen_from_strbuf(s->server_kexinit),
  1135. s->kexlists, &s->kex_alg, &s->hostkey_alg, s->cstrans,
  1136. s->sctrans, &s->warn_kex, &s->warn_hk, &s->warn_cscipher,
  1137. &s->warn_sccipher, s->ppl.ssh, NULL, &s->ignorepkt, &nhk, hks,
  1138. &s->hkflags, &s->can_send_ext_info))
  1139. return; /* false means a fatal error function was called */
  1140. /*
  1141. * In addition to deciding which host key we're actually going
  1142. * to use, we should make a list of the host keys offered by
  1143. * the server which we _don't_ have cached. These will be
  1144. * offered as cross-certification options by ssh_get_specials.
  1145. *
  1146. * We also count the key we're currently using for KEX as one
  1147. * we've already got, because by the time this menu becomes
  1148. * visible, it will be.
  1149. */
  1150. s->n_uncert_hostkeys = 0;
  1151. for (i = 0; i < nhk; i++) {
  1152. j = hks[i];
  1153. if (ssh2_hostkey_algs[j].alg != s->hostkey_alg &&
  1154. !have_ssh_host_key(s->ppl.seat, s->savedhost, s->savedport, // WINSCP
  1155. ssh2_hostkey_algs[j].alg->cache_id)) {
  1156. s->uncert_hostkeys[s->n_uncert_hostkeys++] = j;
  1157. }
  1158. }
  1159. }
  1160. if (s->warn_kex) {
  1161. s->spr = ssh2_transport_confirm_weak_crypto_primitive(
  1162. s, "key-exchange algorithm", s->kex_alg->name, s->kex_alg);
  1163. crMaybeWaitUntilV(s->spr.kind != SPRK_INCOMPLETE);
  1164. if (spr_is_abort(s->spr)) {
  1165. ssh_spr_close(s->ppl.ssh, s->spr, "kex warning");
  1166. return;
  1167. }
  1168. }
  1169. if (s->warn_hk) {
  1170. int j, k;
  1171. char *betteralgs;
  1172. /*
  1173. * Change warning box wording depending on why we chose a
  1174. * warning-level host key algorithm. If it's because
  1175. * that's all we have *cached*, list the host keys we
  1176. * could usefully cross-certify. Otherwise, use the same
  1177. * standard wording as any other weak crypto primitive.
  1178. */
  1179. betteralgs = NULL;
  1180. for (j = 0; j < s->n_uncert_hostkeys; j++) {
  1181. const struct ssh_signkey_with_user_pref_id *hktype =
  1182. &ssh2_hostkey_algs[s->uncert_hostkeys[j]];
  1183. bool better = false;
  1184. for (k = 0; k < HK_MAX; k++) {
  1185. int id = conf_get_int_int(s->conf, CONF_ssh_hklist, k);
  1186. if (id == HK_WARN) {
  1187. break;
  1188. } else if (id == hktype->id) {
  1189. better = true;
  1190. break;
  1191. }
  1192. }
  1193. if (better) {
  1194. if (betteralgs) {
  1195. char *old_ba = betteralgs;
  1196. betteralgs = dupcat(betteralgs, ",", hktype->alg->ssh_id);
  1197. sfree(old_ba);
  1198. } else {
  1199. betteralgs = dupstr(hktype->alg->ssh_id);
  1200. }
  1201. }
  1202. }
  1203. if (betteralgs) {
  1204. /* Use the special warning prompt that lets us provide
  1205. * a list of better algorithms */
  1206. s->spr = seat_confirm_weak_cached_hostkey(
  1207. ppl_get_iseat(&s->ppl), s->hostkey_alg->ssh_id, betteralgs,
  1208. ssh2_transport_dialog_callback, s);
  1209. sfree(betteralgs);
  1210. } else {
  1211. /* If none exist, use the more general 'weak crypto'
  1212. * warning prompt */
  1213. s->spr = ssh2_transport_confirm_weak_crypto_primitive(
  1214. s, "host key type", s->hostkey_alg->ssh_id,
  1215. s->hostkey_alg);
  1216. }
  1217. crMaybeWaitUntilV(s->spr.kind != SPRK_INCOMPLETE);
  1218. if (spr_is_abort(s->spr)) {
  1219. ssh_spr_close(s->ppl.ssh, s->spr, "host key warning");
  1220. return;
  1221. }
  1222. }
  1223. if (s->warn_cscipher) {
  1224. s->spr = ssh2_transport_confirm_weak_crypto_primitive(
  1225. s, "client-to-server cipher", s->out.cipher->ssh2_id,
  1226. s->out.cipher);
  1227. crMaybeWaitUntilV(s->spr.kind != SPRK_INCOMPLETE);
  1228. if (spr_is_abort(s->spr)) {
  1229. ssh_spr_close(s->ppl.ssh, s->spr, "cipher warning");
  1230. return;
  1231. }
  1232. }
  1233. if (s->warn_sccipher) {
  1234. s->spr = ssh2_transport_confirm_weak_crypto_primitive(
  1235. s, "server-to-client cipher", s->in.cipher->ssh2_id,
  1236. s->in.cipher);
  1237. crMaybeWaitUntilV(s->spr.kind != SPRK_INCOMPLETE);
  1238. if (spr_is_abort(s->spr)) {
  1239. ssh_spr_close(s->ppl.ssh, s->spr, "cipher warning");
  1240. return;
  1241. }
  1242. }
  1243. /*
  1244. * If the other side has sent an initial key exchange packet that
  1245. * we must treat as a wrong guess, wait for it, and discard it.
  1246. */
  1247. if (s->ignorepkt)
  1248. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  1249. /*
  1250. * Actually perform the key exchange.
  1251. */
  1252. s->exhash = ssh_hash_new(s->kex_alg->hash);
  1253. put_stringz(s->exhash, s->client_greeting);
  1254. put_stringz(s->exhash, s->server_greeting);
  1255. put_string(s->exhash, s->client_kexinit->u, s->client_kexinit->len);
  1256. put_string(s->exhash, s->server_kexinit->u, s->server_kexinit->len);
  1257. s->crStateKex = 0;
  1258. while (1) {
  1259. bool aborted = false;
  1260. ssh2kex_coroutine(s, &aborted);
  1261. if (aborted)
  1262. return; /* disaster: our entire state has been freed */
  1263. if (!s->crStateKex)
  1264. break; /* kex phase has terminated normally */
  1265. crReturnV;
  1266. }
  1267. /*
  1268. * The exchange hash from the very first key exchange is also
  1269. * the session id, used in session key construction and
  1270. * authentication.
  1271. */
  1272. if (!s->got_session_id) {
  1273. assert(sizeof(s->exchange_hash) <= sizeof(s->session_id));
  1274. memcpy(s->session_id, s->exchange_hash, sizeof(s->exchange_hash));
  1275. s->session_id_len = s->kex_alg->hash->hlen;
  1276. assert(s->session_id_len <= sizeof(s->session_id));
  1277. s->got_session_id = true;
  1278. }
  1279. /*
  1280. * Send SSH2_MSG_NEWKEYS.
  1281. */
  1282. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_NEWKEYS);
  1283. pq_push(s->ppl.out_pq, pktout);
  1284. /* Start counting down the outgoing-data limit for these cipher keys. */
  1285. dts_reset(&s->stats->out, s->max_data_size);
  1286. /*
  1287. * Force the BPP to synchronously marshal all packets up to and
  1288. * including that NEWKEYS into wire format, before we switch over
  1289. * to new crypto.
  1290. */
  1291. ssh_bpp_handle_output(s->ppl.bpp);
  1292. /*
  1293. * We've sent outgoing NEWKEYS, so create and initialise outgoing
  1294. * session keys.
  1295. */
  1296. {
  1297. strbuf *cipher_key = strbuf_new_nm();
  1298. strbuf *cipher_iv = strbuf_new_nm();
  1299. strbuf *mac_key = strbuf_new_nm();
  1300. if (s->out.cipher) {
  1301. ssh2_mkkey(s, cipher_iv, s->K, s->exchange_hash,
  1302. 'A' + s->out.mkkey_adjust, s->out.cipher->blksize);
  1303. ssh2_mkkey(s, cipher_key, s->K, s->exchange_hash,
  1304. 'C' + s->out.mkkey_adjust,
  1305. s->out.cipher->padded_keybytes);
  1306. }
  1307. if (s->out.mac) {
  1308. ssh2_mkkey(s, mac_key, s->K, s->exchange_hash,
  1309. 'E' + s->out.mkkey_adjust, s->out.mac->keylen);
  1310. }
  1311. ssh2_bpp_new_outgoing_crypto(
  1312. s->ppl.bpp,
  1313. s->out.cipher, cipher_key->u, cipher_iv->u,
  1314. s->out.mac, s->out.etm_mode, mac_key->u,
  1315. s->out.comp, s->out.comp_delayed);
  1316. strbuf_free(cipher_key);
  1317. strbuf_free(cipher_iv);
  1318. strbuf_free(mac_key);
  1319. }
  1320. /*
  1321. * If that was our first key exchange, this is the moment to send
  1322. * our EXT_INFO, if we're sending one.
  1323. */
  1324. if (!s->post_newkeys_ext_info) {
  1325. s->post_newkeys_ext_info = true; /* never do this again */
  1326. if (s->can_send_ext_info) {
  1327. strbuf *extinfo = strbuf_new();
  1328. uint32_t n_exts = 0;
  1329. if (s->ssc) {
  1330. /* Server->client EXT_INFO lists our supported user
  1331. * key algorithms. */
  1332. n_exts++;
  1333. put_stringz(extinfo, "server-sig-algs");
  1334. { // WINSCP
  1335. strbuf *list = strbuf_new();
  1336. size_t i; // WINSCP
  1337. for (i = 0; i < n_keyalgs; i++)
  1338. add_to_commasep(list, all_keyalgs[i]->ssh_id);
  1339. put_stringsb(extinfo, list);
  1340. } // WINSCP
  1341. } else {
  1342. /* Client->server EXT_INFO is currently not sent, but here's
  1343. * where we should put things in it if we ever want to. */
  1344. }
  1345. /* Only send EXT_INFO if it's non-empty */
  1346. if (n_exts) {
  1347. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_EXT_INFO);
  1348. put_uint32(pktout, n_exts);
  1349. put_datapl(pktout, ptrlen_from_strbuf(extinfo));
  1350. pq_push(s->ppl.out_pq, pktout);
  1351. }
  1352. strbuf_free(extinfo);
  1353. }
  1354. }
  1355. /*
  1356. * Now our end of the key exchange is complete, we can send all
  1357. * our queued higher-layer packets. Transfer the whole of the next
  1358. * layer's outgoing queue on to our own.
  1359. */
  1360. pq_concatenate(s->ppl.out_pq, s->ppl.out_pq, &s->pq_out_higher);
  1361. ssh_sendbuffer_changed(s->ppl.ssh);
  1362. /*
  1363. * Expect SSH2_MSG_NEWKEYS from server.
  1364. */
  1365. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  1366. if (pktin->type != SSH2_MSG_NEWKEYS) {
  1367. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  1368. "expecting SSH_MSG_NEWKEYS, type %d (%s)",
  1369. pktin->type,
  1370. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  1371. s->ppl.bpp->pls->actx,
  1372. pktin->type));
  1373. return;
  1374. }
  1375. /* Start counting down the incoming-data limit for these cipher keys. */
  1376. dts_reset(&s->stats->in, s->max_data_size);
  1377. /*
  1378. * We've seen incoming NEWKEYS, so create and initialise
  1379. * incoming session keys.
  1380. */
  1381. {
  1382. strbuf *cipher_key = strbuf_new_nm();
  1383. strbuf *cipher_iv = strbuf_new_nm();
  1384. strbuf *mac_key = strbuf_new_nm();
  1385. if (s->in.cipher) {
  1386. ssh2_mkkey(s, cipher_iv, s->K, s->exchange_hash,
  1387. 'A' + s->in.mkkey_adjust, s->in.cipher->blksize);
  1388. ssh2_mkkey(s, cipher_key, s->K, s->exchange_hash,
  1389. 'C' + s->in.mkkey_adjust,
  1390. s->in.cipher->padded_keybytes);
  1391. }
  1392. if (s->in.mac) {
  1393. ssh2_mkkey(s, mac_key, s->K, s->exchange_hash,
  1394. 'E' + s->in.mkkey_adjust, s->in.mac->keylen);
  1395. }
  1396. ssh2_bpp_new_incoming_crypto(
  1397. s->ppl.bpp,
  1398. s->in.cipher, cipher_key->u, cipher_iv->u,
  1399. s->in.mac, s->in.etm_mode, mac_key->u,
  1400. s->in.comp, s->in.comp_delayed);
  1401. strbuf_free(cipher_key);
  1402. strbuf_free(cipher_iv);
  1403. strbuf_free(mac_key);
  1404. }
  1405. /*
  1406. * Free shared secret.
  1407. */
  1408. mp_free(s->K); s->K = NULL;
  1409. /*
  1410. * Update the specials menu to list the remaining uncertified host
  1411. * keys.
  1412. */
  1413. seat_update_specials_menu(s->ppl.seat);
  1414. /*
  1415. * Key exchange is over. Loop straight back round if we have a
  1416. * deferred rekey reason.
  1417. */
  1418. if (s->deferred_rekey_reason) {
  1419. ppl_logevent("%s", s->deferred_rekey_reason);
  1420. pktin = NULL;
  1421. s->deferred_rekey_reason = NULL;
  1422. goto begin_key_exchange;
  1423. }
  1424. /*
  1425. * Otherwise, schedule a timer for our next rekey.
  1426. */
  1427. s->kex_in_progress = false;
  1428. s->last_rekey = GETTICKCOUNT();
  1429. (void) ssh2_transport_timer_update(s, 0);
  1430. /*
  1431. * Now we're encrypting. Get the next-layer protocol started if it
  1432. * hasn't already, and then sit here waiting for reasons to go
  1433. * back to the start and do a repeat key exchange. One of those
  1434. * reasons is that we receive KEXINIT from the other end; the
  1435. * other is if we find rekey_reason is non-NULL, i.e. we've
  1436. * decided to initiate a rekey ourselves for some reason.
  1437. */
  1438. if (!s->higher_layer_ok) {
  1439. if (!s->hostkeys) {
  1440. /* We're the client, so send SERVICE_REQUEST. */
  1441. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_SERVICE_REQUEST);
  1442. put_stringz(pktout, s->higher_layer->vt->name);
  1443. pq_push(s->ppl.out_pq, pktout);
  1444. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  1445. if (pktin->type != SSH2_MSG_SERVICE_ACCEPT) {
  1446. ssh_sw_abort(s->ppl.ssh, "Server refused request to start "
  1447. "'%s' protocol", s->higher_layer->vt->name);
  1448. return;
  1449. }
  1450. } else {
  1451. ptrlen service_name;
  1452. /* We're the server, so expect SERVICE_REQUEST. */
  1453. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  1454. if (pktin->type != SSH2_MSG_SERVICE_REQUEST) {
  1455. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  1456. "expecting SERVICE_REQUEST, type %d (%s)",
  1457. pktin->type,
  1458. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  1459. s->ppl.bpp->pls->actx,
  1460. pktin->type));
  1461. return;
  1462. }
  1463. service_name = get_string(pktin);
  1464. if (!ptrlen_eq_string(service_name, s->higher_layer->vt->name)) {
  1465. ssh_proto_error(s->ppl.ssh, "Client requested service "
  1466. "'%.*s' when we only support '%s'",
  1467. PTRLEN_PRINTF(service_name),
  1468. s->higher_layer->vt->name);
  1469. return;
  1470. }
  1471. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_SERVICE_ACCEPT);
  1472. put_stringz(pktout, s->higher_layer->vt->name);
  1473. pq_push(s->ppl.out_pq, pktout);
  1474. }
  1475. s->higher_layer_ok = true;
  1476. queue_idempotent_callback(&s->higher_layer->ic_process_queue);
  1477. }
  1478. s->rekey_class = RK_NONE;
  1479. do {
  1480. crReturnV;
  1481. /* Pass through outgoing packets from the higher layer. */
  1482. pq_concatenate(s->ppl.out_pq, s->ppl.out_pq, &s->pq_out_higher);
  1483. ssh_sendbuffer_changed(s->ppl.ssh);
  1484. /* Wait for either a KEXINIT, or something setting
  1485. * s->rekey_class. This call to ssh2_transport_pop also has
  1486. * the side effect of transferring incoming packets _to_ the
  1487. * higher layer (via filter_queue). */
  1488. if ((pktin = ssh2_transport_pop(s)) != NULL) {
  1489. if (pktin->type != SSH2_MSG_KEXINIT) {
  1490. ssh_proto_error(s->ppl.ssh, "Received unexpected transport-"
  1491. "layer packet outside a key exchange, "
  1492. "type %d (%s)", pktin->type,
  1493. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  1494. s->ppl.bpp->pls->actx,
  1495. pktin->type));
  1496. return;
  1497. }
  1498. pq_push_front(s->ppl.in_pq, pktin);
  1499. ppl_logevent("Remote side initiated key re-exchange");
  1500. s->rekey_class = RK_SERVER;
  1501. }
  1502. if (s->rekey_class == RK_POST_USERAUTH) {
  1503. /*
  1504. * userauth has seen a USERAUTH_SUCCESS. This may be the
  1505. * moment to do an immediate rekey with different
  1506. * parameters. But it may not; so here we turn that rekey
  1507. * class into either RK_NONE or RK_NORMAL.
  1508. *
  1509. * Currently the only reason for this is if we've done a
  1510. * GSS key exchange and don't have anything in our
  1511. * transient hostkey cache, in which case we should make
  1512. * an attempt to populate the cache now.
  1513. */
  1514. if (s->need_gss_transient_hostkey) {
  1515. s->rekey_reason = "populating transient host key cache";
  1516. s->rekey_class = RK_NORMAL;
  1517. } else {
  1518. /* No need to rekey at this time. */
  1519. s->rekey_class = RK_NONE;
  1520. }
  1521. }
  1522. if (!s->rekey_class) {
  1523. /* If we don't yet have any other reason to rekey, check
  1524. * if we've hit our data limit in either direction. */
  1525. if (s->stats->in.expired) {
  1526. s->rekey_reason = "too much data received";
  1527. s->rekey_class = RK_NORMAL;
  1528. } else if (s->stats->out.expired) {
  1529. s->rekey_reason = "too much data sent";
  1530. s->rekey_class = RK_NORMAL;
  1531. }
  1532. }
  1533. if (s->rekey_class != RK_NONE && s->rekey_class != RK_SERVER) {
  1534. /*
  1535. * Special case: if the server bug is set that doesn't
  1536. * allow rekeying, we give a different log message and
  1537. * continue waiting. (If such a server _initiates_ a
  1538. * rekey, we process it anyway!)
  1539. */
  1540. if ((s->ppl.remote_bugs & BUG_SSH2_REKEY)) {
  1541. ppl_logevent("Remote bug prevents key re-exchange (%s)",
  1542. s->rekey_reason);
  1543. /* Reset the counters, so that at least this message doesn't
  1544. * hit the event log _too_ often. */
  1545. dts_reset(&s->stats->in, s->max_data_size);
  1546. dts_reset(&s->stats->out, s->max_data_size);
  1547. (void) ssh2_transport_timer_update(s, 0);
  1548. s->rekey_class = RK_NONE;
  1549. } else {
  1550. ppl_logevent("Initiating key re-exchange (%s)",
  1551. s->rekey_reason);
  1552. }
  1553. }
  1554. } while (s->rekey_class == RK_NONE);
  1555. /* Once we exit the above loop, we really are rekeying. */
  1556. goto begin_key_exchange;
  1557. crFinishV;
  1558. }
  1559. static void ssh2_transport_higher_layer_packet_callback(void *context)
  1560. {
  1561. PacketProtocolLayer *ppl = (PacketProtocolLayer *)context;
  1562. ssh_ppl_process_queue(ppl);
  1563. }
  1564. static void ssh2_transport_timer(void *ctx, unsigned long now)
  1565. {
  1566. struct ssh2_transport_state *s = (struct ssh2_transport_state *)ctx;
  1567. unsigned long mins;
  1568. unsigned long ticks;
  1569. // WINSCP: our WINSCP_QUERY_TIMER implementation of schedule_timer
  1570. // does not guarantee the `now` to be exactly as scheduled
  1571. if (s->kex_in_progress || now < s->next_rekey)
  1572. return;
  1573. mins = sanitise_rekey_time(conf_get_int(s->conf, CONF_ssh_rekey_time), 60);
  1574. if (mins == 0)
  1575. return;
  1576. /* Rekey if enough time has elapsed */
  1577. ticks = mins * 60 * TICKSPERSEC;
  1578. if (now - s->last_rekey > ticks - 30*TICKSPERSEC) {
  1579. s->rekey_reason = "timeout";
  1580. s->rekey_class = RK_NORMAL;
  1581. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1582. return;
  1583. }
  1584. #ifndef NO_GSSAPI
  1585. /*
  1586. * Rekey now if we have a new cred or context expires this cycle,
  1587. * but not if this is unsafe.
  1588. */
  1589. if (conf_get_int(s->conf, CONF_gssapirekey)) {
  1590. ssh2_transport_gss_update(s, false);
  1591. if ((s->gss_status & GSS_KEX_CAPABLE) != 0 &&
  1592. (s->gss_status & GSS_CTXT_MAYFAIL) == 0 &&
  1593. (s->gss_status & (GSS_CRED_UPDATED|GSS_CTXT_EXPIRES)) != 0) {
  1594. s->rekey_reason = "GSS credentials updated";
  1595. s->rekey_class = RK_GSS_UPDATE;
  1596. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1597. return;
  1598. }
  1599. }
  1600. #endif
  1601. /* Try again later. */
  1602. (void) ssh2_transport_timer_update(s, 0);
  1603. }
  1604. /*
  1605. * The rekey_time is zero except when re-configuring.
  1606. *
  1607. * We either schedule the next timer and return false, or return true
  1608. * to run the callback now, which will call us again to re-schedule on
  1609. * completion.
  1610. */
  1611. static bool ssh2_transport_timer_update(struct ssh2_transport_state *s,
  1612. unsigned long rekey_time)
  1613. {
  1614. unsigned long mins;
  1615. unsigned long ticks;
  1616. mins = sanitise_rekey_time(conf_get_int(s->conf, CONF_ssh_rekey_time), 60);
  1617. ticks = mins * 60 * TICKSPERSEC;
  1618. /* Handle change from previous setting */
  1619. if (rekey_time != 0 && rekey_time != mins) {
  1620. unsigned long next;
  1621. unsigned long now = GETTICKCOUNT();
  1622. mins = rekey_time;
  1623. ticks = mins * 60 * TICKSPERSEC;
  1624. next = s->last_rekey + ticks;
  1625. /* If overdue, caller will rekey synchronously now */
  1626. if (now - s->last_rekey > ticks)
  1627. return true;
  1628. ticks = next - now;
  1629. }
  1630. #ifndef NO_GSSAPI
  1631. if (s->gss_kex_used) {
  1632. /*
  1633. * If we've used GSSAPI key exchange, then we should
  1634. * periodically check whether we need to do another one to
  1635. * pass new credentials to the server.
  1636. */
  1637. unsigned long gssmins;
  1638. /* Check cascade conditions more frequently if configured */
  1639. gssmins = sanitise_rekey_time(
  1640. conf_get_int(s->conf, CONF_gssapirekey), GSS_DEF_REKEY_MINS);
  1641. if (gssmins > 0) {
  1642. if (gssmins < mins)
  1643. ticks = (mins = gssmins) * 60 * TICKSPERSEC;
  1644. if ((s->gss_status & GSS_KEX_CAPABLE) != 0) {
  1645. /*
  1646. * Run next timer even sooner if it would otherwise be
  1647. * too close to the context expiration time
  1648. */
  1649. if ((s->gss_status & GSS_CTXT_EXPIRES) == 0 &&
  1650. s->gss_ctxt_lifetime - mins * 60 < 2 * MIN_CTXT_LIFETIME)
  1651. ticks -= 2 * MIN_CTXT_LIFETIME * TICKSPERSEC;
  1652. }
  1653. }
  1654. }
  1655. #endif
  1656. /* Schedule the next timer */
  1657. s->next_rekey = schedule_timer(ticks, ssh2_transport_timer, s);
  1658. return false;
  1659. }
  1660. void ssh2_transport_dialog_callback(void *vctx, SeatPromptResult spr)
  1661. {
  1662. struct ssh2_transport_state *s = (struct ssh2_transport_state *)vctx;
  1663. s->spr = spr;
  1664. ssh_ppl_process_queue(&s->ppl);
  1665. }
  1666. #ifndef NO_GSSAPI
  1667. /*
  1668. * This is called at the beginning of each SSH rekey to determine
  1669. * whether we are GSS capable, and if we did GSS key exchange, and are
  1670. * delegating credentials, it is also called periodically to determine
  1671. * whether we should rekey in order to delegate (more) fresh
  1672. * credentials. This is called "credential cascading".
  1673. *
  1674. * On Windows, with SSPI, we may not get the credential expiration, as
  1675. * Windows automatically renews from cached passwords, so the
  1676. * credential effectively never expires. Since we still want to
  1677. * cascade when the local TGT is updated, we use the expiration of a
  1678. * newly obtained context as a proxy for the expiration of the TGT.
  1679. */
  1680. static void ssh2_transport_gss_update(struct ssh2_transport_state *s,
  1681. bool definitely_rekeying)
  1682. {
  1683. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  1684. int gss_stat;
  1685. time_t gss_cred_expiry;
  1686. unsigned long mins;
  1687. Ssh_gss_buf gss_sndtok;
  1688. Ssh_gss_buf gss_rcvtok;
  1689. Ssh_gss_ctx gss_ctx;
  1690. s->gss_status = 0;
  1691. /*
  1692. * Nothing to do if no GSSAPI libraries are configured or GSSAPI
  1693. * auth is not enabled.
  1694. */
  1695. if (s->shgss->libs->nlibraries == 0)
  1696. return;
  1697. if (!conf_get_bool(s->conf, CONF_try_gssapi_auth) &&
  1698. !conf_get_bool(s->conf, CONF_try_gssapi_kex))
  1699. return;
  1700. /* Import server name and cache it */
  1701. if (s->shgss->srv_name == GSS_C_NO_NAME) {
  1702. gss_stat = s->shgss->lib->import_name(
  1703. s->shgss->lib, s->fullhostname, &s->shgss->srv_name);
  1704. if (gss_stat != SSH_GSS_OK) {
  1705. if (gss_stat == SSH_GSS_BAD_HOST_NAME)
  1706. ppl_logevent("GSSAPI import name failed - Bad service name;"
  1707. " won't use GSS key exchange");
  1708. else
  1709. ppl_logevent("GSSAPI import name failed;"
  1710. " won't use GSS key exchange");
  1711. return;
  1712. }
  1713. }
  1714. /*
  1715. * Do we (still) have credentials? Capture the credential
  1716. * expiration when available
  1717. */
  1718. gss_stat = s->shgss->lib->acquire_cred(
  1719. s->shgss->lib, &gss_ctx, &gss_cred_expiry);
  1720. if (gss_stat != SSH_GSS_OK)
  1721. return;
  1722. SSH_GSS_CLEAR_BUF(&gss_sndtok);
  1723. SSH_GSS_CLEAR_BUF(&gss_rcvtok);
  1724. /*
  1725. * When acquire_cred yields no useful expiration, get a proxy for
  1726. * the cred expiration from the context expiration.
  1727. */
  1728. gss_stat = s->shgss->lib->init_sec_context(
  1729. s->shgss->lib, &gss_ctx, s->shgss->srv_name,
  1730. 0 /* don't delegate */, &gss_rcvtok, &gss_sndtok,
  1731. (gss_cred_expiry == GSS_NO_EXPIRATION ? &gss_cred_expiry : NULL),
  1732. &s->gss_ctxt_lifetime);
  1733. /* This context was for testing only. */
  1734. if (gss_ctx)
  1735. s->shgss->lib->release_cred(s->shgss->lib, &gss_ctx);
  1736. if (gss_stat != SSH_GSS_OK &&
  1737. gss_stat != SSH_GSS_S_CONTINUE_NEEDED) {
  1738. /*
  1739. * No point in verbosely interrupting the user to tell them we
  1740. * couldn't get GSS credentials, if this was only a check
  1741. * between key exchanges to see if fresh ones were available.
  1742. * When we do do a rekey, this message (if displayed) will
  1743. * appear among the standard rekey blurb, but when we're not,
  1744. * it shouldn't pop up all the time regardless.
  1745. */
  1746. if (definitely_rekeying)
  1747. ppl_logevent("No GSSAPI security context available");
  1748. return;
  1749. }
  1750. if (gss_sndtok.length)
  1751. s->shgss->lib->free_tok(s->shgss->lib, &gss_sndtok);
  1752. s->gss_status |= GSS_KEX_CAPABLE;
  1753. /*
  1754. * When rekeying to cascade, avoid doing this too close to the
  1755. * context expiration time, since the key exchange might fail.
  1756. */
  1757. if (s->gss_ctxt_lifetime < MIN_CTXT_LIFETIME)
  1758. s->gss_status |= GSS_CTXT_MAYFAIL;
  1759. /*
  1760. * If we're not delegating credentials, rekeying is not used to
  1761. * refresh them. We must avoid setting GSS_CRED_UPDATED or
  1762. * GSS_CTXT_EXPIRES when credential delegation is disabled.
  1763. */
  1764. if (!conf_get_bool(s->conf, CONF_gssapifwd))
  1765. return;
  1766. if (s->gss_cred_expiry != GSS_NO_EXPIRATION &&
  1767. difftime(gss_cred_expiry, s->gss_cred_expiry) > 0)
  1768. s->gss_status |= GSS_CRED_UPDATED;
  1769. mins = sanitise_rekey_time(
  1770. conf_get_int(s->conf, CONF_gssapirekey), GSS_DEF_REKEY_MINS);
  1771. if (mins > 0 && s->gss_ctxt_lifetime <= mins * 60)
  1772. s->gss_status |= GSS_CTXT_EXPIRES;
  1773. }
  1774. #endif /* NO_GSSAPI */
  1775. ptrlen ssh2_transport_get_session_id(PacketProtocolLayer *ppl)
  1776. {
  1777. struct ssh2_transport_state *s;
  1778. assert(ppl->vt == &ssh2_transport_vtable);
  1779. s = container_of(ppl, struct ssh2_transport_state, ppl);
  1780. assert(s->got_session_id);
  1781. return make_ptrlen(s->session_id, s->session_id_len);
  1782. }
  1783. void ssh2_transport_notify_auth_done(PacketProtocolLayer *ppl)
  1784. {
  1785. struct ssh2_transport_state *s;
  1786. assert(ppl->vt == &ssh2_transport_vtable);
  1787. s = container_of(ppl, struct ssh2_transport_state, ppl);
  1788. s->rekey_reason = NULL; /* will be filled in later */
  1789. s->rekey_class = RK_POST_USERAUTH;
  1790. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1791. }
  1792. static bool ssh2_transport_get_specials(
  1793. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  1794. {
  1795. struct ssh2_transport_state *s =
  1796. container_of(ppl, struct ssh2_transport_state, ppl);
  1797. bool need_separator = false;
  1798. bool toret = false;
  1799. if (ssh_ppl_get_specials(s->higher_layer, add_special, ctx)) {
  1800. need_separator = true;
  1801. toret = true;
  1802. }
  1803. /*
  1804. * Don't bother offering rekey-based specials if we've decided the
  1805. * remote won't cope with it, since we wouldn't bother sending it
  1806. * if asked anyway.
  1807. */
  1808. if (!(s->ppl.remote_bugs & BUG_SSH2_REKEY)) {
  1809. if (need_separator) {
  1810. add_special(ctx, NULL, SS_SEP, 0);
  1811. need_separator = false;
  1812. }
  1813. add_special(ctx, "Repeat key exchange", SS_REKEY, 0);
  1814. toret = true;
  1815. if (s->n_uncert_hostkeys) {
  1816. int i;
  1817. add_special(ctx, NULL, SS_SEP, 0);
  1818. add_special(ctx, "Cache new host key type", SS_SUBMENU, 0);
  1819. for (i = 0; i < s->n_uncert_hostkeys; i++) {
  1820. const ssh_keyalg *alg =
  1821. ssh2_hostkey_algs[s->uncert_hostkeys[i]].alg;
  1822. add_special(ctx, alg->ssh_id, SS_XCERT, s->uncert_hostkeys[i]);
  1823. }
  1824. add_special(ctx, NULL, SS_EXITMENU, 0);
  1825. }
  1826. }
  1827. return toret;
  1828. }
  1829. static void ssh2_transport_special_cmd(PacketProtocolLayer *ppl,
  1830. SessionSpecialCode code, int arg)
  1831. {
  1832. struct ssh2_transport_state *s =
  1833. container_of(ppl, struct ssh2_transport_state, ppl);
  1834. if (code == SS_REKEY) {
  1835. if (!s->kex_in_progress) {
  1836. s->rekey_reason = "at user request";
  1837. s->rekey_class = RK_NORMAL;
  1838. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1839. }
  1840. } else if (code == SS_XCERT) {
  1841. if (!s->kex_in_progress) {
  1842. s->cross_certifying = s->hostkey_alg = ssh2_hostkey_algs[arg].alg;
  1843. s->rekey_reason = "cross-certifying new host key";
  1844. s->rekey_class = RK_NORMAL;
  1845. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1846. }
  1847. } else {
  1848. /* Send everything else to the next layer up. This includes
  1849. * SS_PING/SS_NOP, which we _could_ handle here - but it's
  1850. * better to put them in the connection layer, so they'll
  1851. * still work in bare connection mode. */
  1852. ssh_ppl_special_cmd(s->higher_layer, code, arg);
  1853. }
  1854. }
  1855. /* Safely convert rekey_time to unsigned long minutes */
  1856. static unsigned long sanitise_rekey_time(int rekey_time, unsigned long def)
  1857. {
  1858. if (rekey_time < 0 || rekey_time > MAX_TICK_MINS)
  1859. rekey_time = def;
  1860. return (unsigned long)rekey_time;
  1861. }
  1862. static void ssh2_transport_set_max_data_size(struct ssh2_transport_state *s)
  1863. {
  1864. s->max_data_size = parse_blocksize(
  1865. conf_get_str(s->conf, CONF_ssh_rekey_data));
  1866. }
  1867. static void ssh2_transport_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  1868. {
  1869. struct ssh2_transport_state *s;
  1870. const char *rekey_reason = NULL;
  1871. bool rekey_mandatory = false;
  1872. unsigned long old_max_data_size, rekey_time;
  1873. int i;
  1874. assert(ppl->vt == &ssh2_transport_vtable);
  1875. s = container_of(ppl, struct ssh2_transport_state, ppl);
  1876. rekey_time = sanitise_rekey_time(
  1877. conf_get_int(conf, CONF_ssh_rekey_time), 60);
  1878. if (ssh2_transport_timer_update(s, rekey_time))
  1879. rekey_reason = "timeout shortened";
  1880. old_max_data_size = s->max_data_size;
  1881. ssh2_transport_set_max_data_size(s);
  1882. if (old_max_data_size != s->max_data_size &&
  1883. s->max_data_size != 0) {
  1884. if (s->max_data_size < old_max_data_size) {
  1885. unsigned long diff = old_max_data_size - s->max_data_size;
  1886. dts_consume(&s->stats->out, diff);
  1887. dts_consume(&s->stats->in, diff);
  1888. if (s->stats->out.expired || s->stats->in.expired)
  1889. rekey_reason = "data limit lowered";
  1890. } else {
  1891. unsigned long diff = s->max_data_size - old_max_data_size;
  1892. if (s->stats->out.running)
  1893. s->stats->out.remaining += diff;
  1894. if (s->stats->in.running)
  1895. s->stats->in.remaining += diff;
  1896. }
  1897. }
  1898. if (conf_get_bool(s->conf, CONF_compression) !=
  1899. conf_get_bool(conf, CONF_compression)) {
  1900. rekey_reason = "compression setting changed";
  1901. rekey_mandatory = true;
  1902. }
  1903. for (i = 0; i < CIPHER_MAX; i++)
  1904. if (conf_get_int_int(s->conf, CONF_ssh_cipherlist, i) !=
  1905. conf_get_int_int(conf, CONF_ssh_cipherlist, i)) {
  1906. rekey_reason = "cipher settings changed";
  1907. rekey_mandatory = true;
  1908. }
  1909. if (conf_get_bool(s->conf, CONF_ssh2_des_cbc) !=
  1910. conf_get_bool(conf, CONF_ssh2_des_cbc)) {
  1911. rekey_reason = "cipher settings changed";
  1912. rekey_mandatory = true;
  1913. }
  1914. conf_free(s->conf);
  1915. s->conf = conf_copy(conf);
  1916. if (rekey_reason) {
  1917. if (!s->kex_in_progress && !ssh2_bpp_rekey_inadvisable(s->ppl.bpp)) {
  1918. s->rekey_reason = rekey_reason;
  1919. s->rekey_class = RK_NORMAL;
  1920. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1921. } else if (rekey_mandatory) {
  1922. s->deferred_rekey_reason = rekey_reason;
  1923. }
  1924. }
  1925. /* Also pass the configuration along to our higher layer */
  1926. ssh_ppl_reconfigure(s->higher_layer, conf);
  1927. }
  1928. static int weak_algorithm_compare(void *av, void *bv)
  1929. {
  1930. uintptr_t a = (uintptr_t)av, b = (uintptr_t)bv;
  1931. return a < b ? -1 : a > b ? +1 : 0;
  1932. }
  1933. /*
  1934. * Wrapper on seat_confirm_weak_crypto_primitive(), which uses the
  1935. * tree234 s->weak_algorithms_consented_to to ensure we ask at most
  1936. * once about any given crypto primitive.
  1937. */
  1938. static SeatPromptResult ssh2_transport_confirm_weak_crypto_primitive(
  1939. struct ssh2_transport_state *s, const char *type, const char *name,
  1940. const void *alg)
  1941. {
  1942. if (find234(s->weak_algorithms_consented_to, (void *)alg, NULL))
  1943. return SPR_OK;
  1944. add234(s->weak_algorithms_consented_to, (void *)alg);
  1945. return seat_confirm_weak_crypto_primitive(
  1946. ppl_get_iseat(&s->ppl), type, name, ssh2_transport_dialog_callback, s);
  1947. }
  1948. static size_t ssh2_transport_queued_data_size(PacketProtocolLayer *ppl)
  1949. {
  1950. struct ssh2_transport_state *s =
  1951. container_of(ppl, struct ssh2_transport_state, ppl);
  1952. return (ssh_ppl_default_queued_data_size(ppl) +
  1953. ssh_ppl_queued_data_size(s->higher_layer));
  1954. }
  1955. #include "puttyexp.h"
  1956. static unsigned int ssh2_transport_winscp_query(PacketProtocolLayer *ppl, int query)
  1957. {
  1958. struct ssh2_transport_state *s =
  1959. container_of(ppl, struct ssh2_transport_state, ppl);
  1960. if (query == WINSCP_QUERY_TIMER)
  1961. {
  1962. ssh2_transport_timer(s, GETTICKCOUNT());
  1963. return 1;
  1964. }
  1965. else if (s->higher_layer->vt->winscp_query != NULL)
  1966. {
  1967. return ssh_ppl_winscp_query(s->higher_layer, query);
  1968. }
  1969. else
  1970. {
  1971. return 0;
  1972. }
  1973. }
  1974. void call_ssh_timer(Backend * be)
  1975. {
  1976. // TODO
  1977. }
  1978. // WINSCP
  1979. void get_hostkey_algs(int * count, cp_ssh_keyalg * SignKeys)
  1980. {
  1981. int i;
  1982. assert(lenof(ssh2_hostkey_algs) <= *count);
  1983. *count = lenof(ssh2_hostkey_algs);
  1984. for (i = 0; i < *count; i++)
  1985. {
  1986. *(SignKeys + i) = ssh2_hostkey_algs[i].alg;
  1987. }
  1988. }
  1989. // WINSCP
  1990. void get_macs(int * count, const struct ssh2_macalg *** amacs)
  1991. {
  1992. *amacs = macs;
  1993. *count = lenof(macs);
  1994. }
  1995. int have_any_ssh2_hostkey(Seat * seat, const char * host, int port)
  1996. {
  1997. int j;
  1998. for (j = 0; j < lenof(ssh2_hostkey_algs); j++)
  1999. {
  2000. if (have_ssh_host_key(seat, host, port, ssh2_hostkey_algs[j].alg->cache_id))
  2001. {
  2002. return 1;
  2003. }
  2004. }
  2005. return 0;
  2006. }