userauth2-client.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  1. /*
  2. * Packet protocol layer for the client side of the SSH-2 userauth
  3. * protocol (RFC 4252).
  4. */
  5. #include <assert.h>
  6. #include "putty.h"
  7. #include "ssh.h"
  8. #include "bpp.h"
  9. #include "ppl.h"
  10. #include "sshcr.h"
  11. #ifndef NO_GSSAPI
  12. #include "gssc.h"
  13. #include "gss.h"
  14. #endif
  15. #define BANNER_LIMIT 131072
  16. typedef struct agent_key {
  17. strbuf *blob, *comment;
  18. ptrlen algorithm;
  19. } agent_key;
  20. struct ssh2_userauth_state {
  21. int crState;
  22. PacketProtocolLayer *transport_layer, *successor_layer;
  23. Filename *keyfile;
  24. bool show_banner, tryagent, notrivialauth, change_username;
  25. char *hostname, *fullhostname;
  26. char *default_username;
  27. bool try_ki_auth, try_gssapi_auth, try_gssapi_kex_auth, gssapi_fwd;
  28. char *loghost; // WINSCP
  29. bool change_password; // WINSCP
  30. ptrlen session_id;
  31. enum {
  32. AUTH_TYPE_NONE,
  33. AUTH_TYPE_PUBLICKEY,
  34. AUTH_TYPE_PUBLICKEY_OFFER_LOUD,
  35. AUTH_TYPE_PUBLICKEY_OFFER_QUIET,
  36. AUTH_TYPE_PASSWORD,
  37. AUTH_TYPE_GSSAPI, /* always QUIET */
  38. AUTH_TYPE_KEYBOARD_INTERACTIVE,
  39. AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET
  40. } type;
  41. bool need_pw, can_pubkey, can_passwd, can_keyb_inter;
  42. SeatPromptResult spr;
  43. bool tried_pubkey_config, done_agent;
  44. struct ssh_connection_shared_gss_state *shgss;
  45. #ifndef NO_GSSAPI
  46. bool can_gssapi;
  47. bool can_gssapi_keyex_auth;
  48. bool tried_gssapi;
  49. bool tried_gssapi_keyex_auth;
  50. time_t gss_cred_expiry;
  51. Ssh_gss_buf gss_buf;
  52. Ssh_gss_buf gss_rcvtok, gss_sndtok;
  53. Ssh_gss_stat gss_stat;
  54. #endif
  55. bool suppress_wait_for_response_packet;
  56. strbuf *last_methods_string;
  57. bool kbd_inter_refused;
  58. prompts_t *cur_prompt;
  59. uint32_t num_prompts;
  60. const char *username;
  61. char *locally_allocated_username;
  62. char *password;
  63. bool got_username;
  64. strbuf *publickey_blob;
  65. bool privatekey_available, privatekey_encrypted;
  66. char *publickey_algorithm;
  67. char *publickey_comment;
  68. void *agent_response_to_free;
  69. ptrlen agent_response;
  70. BinarySource asrc[1]; /* for reading SSH agent response */
  71. size_t agent_keys_len;
  72. agent_key *agent_keys;
  73. size_t agent_key_index, agent_key_limit;
  74. ptrlen agent_keyalg;
  75. unsigned signflags;
  76. int len;
  77. PktOut *pktout;
  78. bool is_trivial_auth;
  79. agent_pending_query *auth_agent_query;
  80. bufchain banner;
  81. bufchain_sink banner_bs;
  82. StripCtrlChars *banner_scc;
  83. bool banner_scc_initialised;
  84. StripCtrlChars *ki_scc;
  85. bool ki_scc_initialised;
  86. bool ki_printed_header;
  87. Seat *seat; // WINSCP
  88. PacketProtocolLayer ppl;
  89. };
  90. static void ssh2_userauth_free(PacketProtocolLayer *);
  91. static void ssh2_userauth_process_queue(PacketProtocolLayer *);
  92. static bool ssh2_userauth_get_specials(
  93. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx);
  94. static void ssh2_userauth_special_cmd(PacketProtocolLayer *ppl,
  95. SessionSpecialCode code, int arg);
  96. static void ssh2_userauth_reconfigure(PacketProtocolLayer *ppl, Conf *conf);
  97. static void ssh2_userauth_agent_query(struct ssh2_userauth_state *, strbuf *);
  98. static void ssh2_userauth_agent_callback(void *, void *, int);
  99. static void ssh2_userauth_add_sigblob(
  100. struct ssh2_userauth_state *s, PktOut *pkt, ptrlen pkblob, ptrlen sigblob);
  101. static void ssh2_userauth_add_session_id(
  102. struct ssh2_userauth_state *s, strbuf *sigdata);
  103. #ifndef NO_GSSAPI
  104. static PktOut *ssh2_userauth_gss_packet(
  105. struct ssh2_userauth_state *s, const char *authtype);
  106. #endif
  107. static const PacketProtocolLayerVtable ssh2_userauth_vtable = {
  108. // WINSCP
  109. /*.free =*/ ssh2_userauth_free,
  110. /*.process_queue =*/ ssh2_userauth_process_queue,
  111. /*.get_specials =*/ ssh2_userauth_get_specials,
  112. /*.special_cmd =*/ ssh2_userauth_special_cmd,
  113. /*.reconfigure =*/ ssh2_userauth_reconfigure,
  114. /*.queued_data_size =*/ ssh_ppl_default_queued_data_size,
  115. /*.name =*/ "ssh-userauth",
  116. NULL, // WINSCP
  117. };
  118. PacketProtocolLayer *ssh2_userauth_new(
  119. PacketProtocolLayer *successor_layer,
  120. const char *hostname, const char *fullhostname,
  121. Filename *keyfile, bool show_banner, bool tryagent, bool notrivialauth,
  122. const char *default_username, bool change_username,
  123. bool try_ki_auth, bool try_gssapi_auth, bool try_gssapi_kex_auth,
  124. bool gssapi_fwd, struct ssh_connection_shared_gss_state *shgss,
  125. const char * loghost, bool change_password, Seat *seat) // WINSCP
  126. {
  127. struct ssh2_userauth_state *s = snew(struct ssh2_userauth_state);
  128. memset(s, 0, sizeof(*s));
  129. s->seat = seat;
  130. s->ppl.vt = &ssh2_userauth_vtable;
  131. s->successor_layer = successor_layer;
  132. s->hostname = dupstr(hostname);
  133. s->fullhostname = dupstr(fullhostname);
  134. s->keyfile = filename_copy(keyfile);
  135. s->show_banner = show_banner;
  136. s->tryagent = tryagent;
  137. s->notrivialauth = notrivialauth;
  138. s->default_username = dupstr(default_username);
  139. s->change_username = change_username;
  140. s->try_ki_auth = try_ki_auth;
  141. s->try_gssapi_auth = try_gssapi_auth;
  142. s->try_gssapi_kex_auth = try_gssapi_kex_auth;
  143. s->gssapi_fwd = gssapi_fwd;
  144. s->shgss = shgss;
  145. s->last_methods_string = strbuf_new();
  146. s->is_trivial_auth = true;
  147. s->loghost = dupstr(loghost); // WINSCP
  148. s->change_password = change_password;
  149. bufchain_init(&s->banner);
  150. bufchain_sink_init(&s->banner_bs, &s->banner);
  151. return &s->ppl;
  152. }
  153. void ssh2_userauth_set_transport_layer(PacketProtocolLayer *userauth,
  154. PacketProtocolLayer *transport)
  155. {
  156. struct ssh2_userauth_state *s =
  157. container_of(userauth, struct ssh2_userauth_state, ppl);
  158. s->transport_layer = transport;
  159. }
  160. static void ssh2_userauth_free(PacketProtocolLayer *ppl)
  161. {
  162. struct ssh2_userauth_state *s =
  163. container_of(ppl, struct ssh2_userauth_state, ppl);
  164. bufchain_clear(&s->banner);
  165. if (s->successor_layer)
  166. ssh_ppl_free(s->successor_layer);
  167. if (s->agent_keys) {
  168. size_t i; // WINSCP
  169. for (i = 0; i < s->agent_keys_len; i++) {
  170. strbuf_free(s->agent_keys[i].blob);
  171. strbuf_free(s->agent_keys[i].comment);
  172. }
  173. sfree(s->agent_keys);
  174. }
  175. sfree(s->agent_response_to_free);
  176. if (s->auth_agent_query)
  177. agent_cancel_query(s->auth_agent_query);
  178. filename_free(s->keyfile);
  179. sfree(s->default_username);
  180. sfree(s->locally_allocated_username);
  181. sfree(s->hostname);
  182. sfree(s->fullhostname);
  183. if (s->cur_prompt)
  184. free_prompts(s->cur_prompt);
  185. sfree(s->publickey_comment);
  186. sfree(s->publickey_algorithm);
  187. if (s->publickey_blob)
  188. strbuf_free(s->publickey_blob);
  189. strbuf_free(s->last_methods_string);
  190. sfree(s->loghost);
  191. if (s->banner_scc)
  192. stripctrl_free(s->banner_scc);
  193. if (s->ki_scc)
  194. stripctrl_free(s->ki_scc);
  195. sfree(s);
  196. }
  197. static void ssh2_userauth_filter_queue(struct ssh2_userauth_state *s)
  198. {
  199. PktIn *pktin;
  200. ptrlen string;
  201. while ((pktin = pq_peek(s->ppl.in_pq)) != NULL) {
  202. switch (pktin->type) {
  203. case SSH2_MSG_USERAUTH_BANNER:
  204. if (!s->show_banner) {
  205. pq_pop(s->ppl.in_pq);
  206. break;
  207. }
  208. string = get_string(pktin);
  209. if (string.len > BANNER_LIMIT - bufchain_size(&s->banner))
  210. string.len = BANNER_LIMIT - bufchain_size(&s->banner);
  211. if (!s->banner_scc_initialised) {
  212. s->banner_scc = seat_stripctrl_new(
  213. s->ppl.seat, BinarySink_UPCAST(&s->banner_bs), SIC_BANNER);
  214. if (s->banner_scc)
  215. stripctrl_enable_line_limiting(s->banner_scc);
  216. s->banner_scc_initialised = true;
  217. }
  218. if (s->banner_scc)
  219. put_datapl(s->banner_scc, string);
  220. else
  221. put_datapl(&s->banner_bs, string);
  222. pq_pop(s->ppl.in_pq);
  223. break;
  224. default:
  225. return;
  226. }
  227. }
  228. }
  229. static PktIn *ssh2_userauth_pop(struct ssh2_userauth_state *s)
  230. {
  231. ssh2_userauth_filter_queue(s);
  232. return pq_pop(s->ppl.in_pq);
  233. }
  234. static void ssh2_userauth_process_queue(PacketProtocolLayer *ppl)
  235. {
  236. struct ssh2_userauth_state *s =
  237. container_of(ppl, struct ssh2_userauth_state, ppl);
  238. PktIn *pktin;
  239. ssh2_userauth_filter_queue(s); /* no matter why we were called */
  240. crBegin(s->crState);
  241. #ifndef NO_GSSAPI
  242. s->tried_gssapi = false;
  243. s->tried_gssapi_keyex_auth = false;
  244. #endif
  245. /*
  246. * Misc one-time setup for authentication.
  247. */
  248. s->publickey_blob = NULL;
  249. s->session_id = ssh2_transport_get_session_id(s->transport_layer);
  250. /*
  251. * Load the public half of any configured public key file for
  252. * later use.
  253. */
  254. if (!filename_is_null(s->keyfile)) {
  255. int keytype;
  256. ppl_logevent(WINSCP_BOM "Reading key file \"%s\"",
  257. filename_to_str(s->keyfile));
  258. keytype = key_type(s->keyfile);
  259. if (keytype == SSH_KEYTYPE_SSH2 ||
  260. keytype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
  261. keytype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH) {
  262. const char *error;
  263. s->publickey_blob = strbuf_new();
  264. if (ppk_loadpub_f(s->keyfile, &s->publickey_algorithm,
  265. BinarySink_UPCAST(s->publickey_blob),
  266. &s->publickey_comment, &error)) {
  267. s->privatekey_available = (keytype == SSH_KEYTYPE_SSH2);
  268. if (!s->privatekey_available)
  269. ppl_logevent("Key file contains public key only");
  270. s->privatekey_encrypted = ppk_encrypted_f(s->keyfile, NULL);
  271. } else {
  272. ppl_logevent("Unable to load key (%s)", error);
  273. ppl_printf(WINSCP_BOM "Unable to load key file \"%s\" (%s)\r\n",
  274. filename_to_str(s->keyfile), error);
  275. strbuf_free(s->publickey_blob);
  276. s->publickey_blob = NULL;
  277. }
  278. } else {
  279. ppl_logevent("Unable to use this key file (%s)",
  280. key_type_to_str(keytype));
  281. ppl_printf(WINSCP_BOM "Unable to use key file \"%s\" (%s)\r\n",
  282. filename_to_str(s->keyfile),
  283. key_type_to_str(keytype));
  284. s->publickey_blob = NULL;
  285. }
  286. }
  287. /*
  288. * Find out about any keys Pageant has (but if there's a public
  289. * key configured, filter out all others).
  290. */
  291. if (s->tryagent && agent_exists()) {
  292. ppl_logevent("Pageant is running. Requesting keys.");
  293. /* Request the keys held by the agent. */
  294. {
  295. strbuf *request = strbuf_new_for_agent_query();
  296. put_byte(request, SSH2_AGENTC_REQUEST_IDENTITIES);
  297. ssh2_userauth_agent_query(s, request);
  298. strbuf_free(request);
  299. crWaitUntilV(!s->auth_agent_query);
  300. }
  301. BinarySource_BARE_INIT_PL(s->asrc, s->agent_response);
  302. get_uint32(s->asrc); /* skip length field */
  303. if (get_byte(s->asrc) == SSH2_AGENT_IDENTITIES_ANSWER) {
  304. size_t nkeys = get_uint32(s->asrc);
  305. size_t origpos = s->asrc->pos;
  306. /*
  307. * Check that the agent response is well formed.
  308. */
  309. { // WINSCP
  310. size_t i; // WINSCP
  311. for (i = 0; i < nkeys; i++) {
  312. get_string(s->asrc); /* blob */
  313. get_string(s->asrc); /* comment */
  314. if (get_err(s->asrc)) {
  315. ppl_logevent("Pageant's response was truncated");
  316. goto done_agent_query;
  317. }
  318. }
  319. } // WINSCP
  320. /*
  321. * Copy the list of public-key blobs out of the Pageant
  322. * response.
  323. */
  324. BinarySource_REWIND_TO(s->asrc, origpos);
  325. s->agent_keys_len = nkeys;
  326. s->agent_keys = snewn(s->agent_keys_len, agent_key);
  327. { // WINSCP
  328. size_t i; // WINSCP
  329. for (i = 0; i < nkeys; i++) {
  330. s->agent_keys[i].blob = strbuf_new();
  331. put_datapl(s->agent_keys[i].blob, get_string(s->asrc));
  332. s->agent_keys[i].comment = strbuf_new();
  333. put_datapl(s->agent_keys[i].comment, get_string(s->asrc));
  334. { // WINSCP
  335. /* Also, extract the algorithm string from the start
  336. * of the public-key blob. */
  337. BinarySource src[1];
  338. BinarySource_BARE_INIT_PL(src, ptrlen_from_strbuf(
  339. s->agent_keys[i].blob));
  340. s->agent_keys[i].algorithm = get_string(src);
  341. } // WINSCP
  342. }
  343. } // WINSCP
  344. ppl_logevent("Pageant has %"SIZEu" SSH-2 keys", nkeys);
  345. if (s->publickey_blob) {
  346. /*
  347. * If we've been given a specific public key blob,
  348. * filter the list of keys to try from the agent down
  349. * to only that one, or none if it's not there.
  350. */
  351. ptrlen our_blob = ptrlen_from_strbuf(s->publickey_blob);
  352. size_t i;
  353. for (i = 0; i < nkeys; i++) {
  354. if (ptrlen_eq_ptrlen(our_blob, ptrlen_from_strbuf(
  355. s->agent_keys[i].blob)))
  356. break;
  357. }
  358. if (i < nkeys) {
  359. ppl_logevent("Pageant key #%"SIZEu" matches "
  360. "configured key file", i);
  361. s->agent_key_index = i;
  362. s->agent_key_limit = i+1;
  363. } else {
  364. ppl_logevent("Configured key file not in Pageant");
  365. s->agent_key_index = 0;
  366. s->agent_key_limit = 0;
  367. }
  368. } else {
  369. /*
  370. * Otherwise, try them all.
  371. */
  372. s->agent_key_index = 0;
  373. s->agent_key_limit = nkeys;
  374. }
  375. } else {
  376. ppl_logevent("Failed to get reply from Pageant");
  377. }
  378. done_agent_query:;
  379. }
  380. /*
  381. * We repeat this whole loop, including the username prompt,
  382. * until we manage a successful authentication. If the user
  383. * types the wrong _password_, they can be sent back to the
  384. * beginning to try another username, if this is configured on.
  385. * (If they specify a username in the config, they are never
  386. * asked, even if they do give a wrong password.)
  387. *
  388. * I think this best serves the needs of
  389. *
  390. * - the people who have no configuration, no keys, and just
  391. * want to try repeated (username,password) pairs until they
  392. * type both correctly
  393. *
  394. * - people who have keys and configuration but occasionally
  395. * need to fall back to passwords
  396. *
  397. * - people with a key held in Pageant, who might not have
  398. * logged in to a particular machine before; so they want to
  399. * type a username, and then _either_ their key will be
  400. * accepted, _or_ they will type a password. If they mistype
  401. * the username they will want to be able to get back and
  402. * retype it!
  403. */
  404. s->got_username = false;
  405. while (1) {
  406. /*
  407. * Get a username.
  408. */
  409. if (s->got_username && !s->change_username) {
  410. /*
  411. * We got a username last time round this loop, and
  412. * with change_username turned off we don't try to get
  413. * it again.
  414. */
  415. } else if ((s->username = s->default_username) == NULL) {
  416. s->cur_prompt = ssh_ppl_new_prompts(&s->ppl);
  417. s->cur_prompt->to_server = true;
  418. s->cur_prompt->from_server = false;
  419. s->cur_prompt->name = dupstr("SSH login name");
  420. add_prompt(s->cur_prompt, dupstr("login as: "), true);
  421. s->spr = seat_get_userpass_input(
  422. ppl_get_iseat(&s->ppl), s->cur_prompt);
  423. while (s->spr.kind == SPRK_INCOMPLETE) {
  424. crReturnV;
  425. s->spr = seat_get_userpass_input(
  426. ppl_get_iseat(&s->ppl), s->cur_prompt);
  427. }
  428. if (spr_is_abort(s->spr)) {
  429. /*
  430. * seat_get_userpass_input() failed to get a username.
  431. * Terminate.
  432. */
  433. free_prompts(s->cur_prompt);
  434. s->cur_prompt = NULL;
  435. ssh_spr_close(s->ppl.ssh, s->spr, "username prompt");
  436. return;
  437. }
  438. sfree(s->locally_allocated_username); /* for change_username */
  439. s->username = s->locally_allocated_username =
  440. prompt_get_result(s->cur_prompt->prompts[0]);
  441. free_prompts(s->cur_prompt);
  442. s->cur_prompt = NULL;
  443. } else {
  444. if (seat_verbose(s->ppl.seat) || seat_interactive(s->ppl.seat))
  445. ppl_printf(WINSCP_BOM "Using username \"%s\".\r\n", s->username);
  446. }
  447. s->got_username = true;
  448. /*
  449. * Send an authentication request using method "none": (a)
  450. * just in case it succeeds, and (b) so that we know what
  451. * authentication methods we can usefully try next.
  452. */
  453. s->ppl.bpp->pls->actx = SSH2_PKTCTX_NOAUTH;
  454. s->pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  455. put_stringz(s->pktout, s->username);
  456. put_stringz(s->pktout, s->successor_layer->vt->name);
  457. put_stringz(s->pktout, "none"); /* method */
  458. pq_push(s->ppl.out_pq, s->pktout);
  459. s->type = AUTH_TYPE_NONE;
  460. s->tried_pubkey_config = false;
  461. s->kbd_inter_refused = false;
  462. s->done_agent = false;
  463. while (1) {
  464. /*
  465. * Wait for the result of the last authentication request,
  466. * unless the request terminated for some reason on our
  467. * own side.
  468. */
  469. if (s->suppress_wait_for_response_packet) {
  470. pktin = NULL;
  471. s->suppress_wait_for_response_packet = false;
  472. } else {
  473. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  474. }
  475. /*
  476. * Now is a convenient point to spew any banner material
  477. * that we've accumulated. (This should ensure that when
  478. * we exit the auth loop, we haven't any left to deal
  479. * with.)
  480. *
  481. * Don't show the banner if we're operating in non-verbose
  482. * non-interactive mode. (It's probably a script, which
  483. * means nobody will read the banner _anyway_, and
  484. * moreover the printing of the banner will screw up
  485. * processing on the output of (say) plink.)
  486. *
  487. * The banner data has been sanitised already by this
  488. * point, but we still need to precede and follow it with
  489. * anti-spoofing header lines.
  490. */
  491. if (bufchain_size(&s->banner) &&
  492. (seat_verbose(s->ppl.seat) || seat_interactive(s->ppl.seat))) {
  493. if (s->banner_scc) {
  494. seat_antispoof_msg(
  495. ppl_get_iseat(&s->ppl),
  496. "Pre-authentication banner message from server:");
  497. seat_set_trust_status(s->ppl.seat, false);
  498. }
  499. { // WINSCP
  500. bool mid_line = false;
  501. while (bufchain_size(&s->banner) > 0) {
  502. ptrlen data = bufchain_prefix(&s->banner);
  503. seat_banner_pl(ppl_get_iseat(&s->ppl), data);
  504. mid_line =
  505. (((const char *)data.ptr)[data.len-1] != '\n');
  506. bufchain_consume(&s->banner, data.len);
  507. }
  508. bufchain_clear(&s->banner);
  509. if (mid_line)
  510. seat_banner_pl(ppl_get_iseat(&s->ppl),
  511. PTRLEN_LITERAL("\r\n"));
  512. if (s->banner_scc) {
  513. seat_set_trust_status(s->ppl.seat, true);
  514. seat_antispoof_msg(ppl_get_iseat(&s->ppl),
  515. "End of banner message from server");
  516. }
  517. } // WINSCP
  518. }
  519. if (pktin && pktin->type == SSH2_MSG_USERAUTH_SUCCESS) {
  520. ppl_logevent("Access granted");
  521. goto userauth_success;
  522. }
  523. if (pktin && pktin->type != SSH2_MSG_USERAUTH_FAILURE &&
  524. s->type != AUTH_TYPE_GSSAPI) {
  525. ssh_proto_error(s->ppl.ssh, "Received unexpected packet "
  526. "in response to authentication request, "
  527. "type %d (%s)", pktin->type,
  528. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  529. s->ppl.bpp->pls->actx,
  530. pktin->type));
  531. return;
  532. }
  533. /*
  534. * OK, we're now sitting on a USERAUTH_FAILURE message, so
  535. * we can look at the string in it and know what we can
  536. * helpfully try next.
  537. */
  538. if (pktin && pktin->type == SSH2_MSG_USERAUTH_FAILURE) {
  539. ptrlen methods = get_string(pktin);
  540. bool partial_success = get_bool(pktin);
  541. if (!partial_success) {
  542. /*
  543. * We have received an unequivocal Access
  544. * Denied. This can translate to a variety of
  545. * messages, or no message at all.
  546. *
  547. * For forms of authentication which are attempted
  548. * implicitly, by which I mean without printing
  549. * anything in the window indicating that we're
  550. * trying them, we should never print 'Access
  551. * denied'.
  552. *
  553. * If we do print a message saying that we're
  554. * attempting some kind of authentication, it's OK
  555. * to print a followup message saying it failed -
  556. * but the message may sometimes be more specific
  557. * than simply 'Access denied'.
  558. *
  559. * Additionally, if we'd just tried password
  560. * authentication, we should break out of this
  561. * whole loop so as to go back to the username
  562. * prompt (iff we're configured to allow
  563. * username change attempts).
  564. */
  565. if (s->type == AUTH_TYPE_NONE) {
  566. /* do nothing */
  567. } else if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD ||
  568. s->type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) {
  569. if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD)
  570. ppl_printf("Server refused our key\r\n");
  571. ppl_logevent("Server refused our key");
  572. } else if (s->type == AUTH_TYPE_PUBLICKEY) {
  573. /* This _shouldn't_ happen except by a
  574. * protocol bug causing client and server to
  575. * disagree on what is a correct signature. */
  576. ppl_printf("Server refused public-key signature"
  577. " despite accepting key!\r\n");
  578. ppl_logevent("Server refused public-key signature"
  579. " despite accepting key!");
  580. } else if (s->type==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET) {
  581. /* quiet, so no ppl_printf */
  582. ppl_logevent("Server refused keyboard-interactive "
  583. "authentication");
  584. } else if (s->type==AUTH_TYPE_GSSAPI) {
  585. /* always quiet, so no ppl_printf */
  586. /* also, the code down in the GSSAPI block has
  587. * already logged this in the Event Log */
  588. } else if (s->type == AUTH_TYPE_KEYBOARD_INTERACTIVE) {
  589. ppl_logevent("Keyboard-interactive authentication "
  590. "failed");
  591. ppl_printf("Access denied\r\n");
  592. } else {
  593. assert(s->type == AUTH_TYPE_PASSWORD);
  594. ppl_logevent("Password authentication failed");
  595. ppl_printf("Access denied\r\n");
  596. if (s->change_username) {
  597. /* XXX perhaps we should allow
  598. * keyboard-interactive to do this too? */
  599. goto try_new_username;
  600. }
  601. }
  602. } else {
  603. ppl_printf("Further authentication required\r\n");
  604. ppl_logevent("Further authentication required");
  605. }
  606. /*
  607. * Save the methods string for use in error messages.
  608. */
  609. strbuf_clear(s->last_methods_string);
  610. put_datapl(s->last_methods_string, methods);
  611. #ifdef WINSCP
  612. ppl_logevent("Server offered these authentication methods: %s", s->last_methods_string->s);
  613. #endif
  614. /*
  615. * Scan it for method identifiers we know about.
  616. */
  617. { // WINSCP
  618. bool srv_pubkey = false, srv_passwd = false;
  619. bool srv_keyb_inter = false;
  620. #ifndef NO_GSSAPI
  621. bool srv_gssapi = false, srv_gssapi_keyex_auth = false;
  622. #endif
  623. ptrlen method; // WINSCP
  624. for (; get_commasep_word(&methods, &method) ;) {
  625. if (ptrlen_eq_string(method, "publickey"))
  626. srv_pubkey = true;
  627. else if (ptrlen_eq_string(method, "password"))
  628. srv_passwd = true;
  629. else if (ptrlen_eq_string(method, "keyboard-interactive"))
  630. srv_keyb_inter = true;
  631. #ifndef NO_GSSAPI
  632. else if (ptrlen_eq_string(method, "gssapi-with-mic"))
  633. srv_gssapi = true;
  634. else if (ptrlen_eq_string(method, "gssapi-keyex"))
  635. srv_gssapi_keyex_auth = true;
  636. #endif
  637. }
  638. /*
  639. * And combine those flags with our own configuration
  640. * and context to set the main can_foo variables.
  641. */
  642. s->can_pubkey = srv_pubkey;
  643. s->can_passwd = srv_passwd;
  644. s->can_keyb_inter = s->try_ki_auth && srv_keyb_inter;
  645. #ifndef NO_GSSAPI
  646. s->can_gssapi = s->try_gssapi_auth && srv_gssapi &&
  647. s->shgss->libs->nlibraries > 0;
  648. s->can_gssapi_keyex_auth = s->try_gssapi_kex_auth &&
  649. srv_gssapi_keyex_auth &&
  650. s->shgss->libs->nlibraries > 0 && s->shgss->ctx;
  651. #endif
  652. } // WINSCP
  653. }
  654. s->ppl.bpp->pls->actx = SSH2_PKTCTX_NOAUTH;
  655. #ifndef NO_GSSAPI
  656. if (s->can_gssapi_keyex_auth && !s->tried_gssapi_keyex_auth) {
  657. /* gssapi-keyex authentication */
  658. s->type = AUTH_TYPE_GSSAPI;
  659. s->tried_gssapi_keyex_auth = true;
  660. s->ppl.bpp->pls->actx = SSH2_PKTCTX_GSSAPI;
  661. if (s->shgss->lib->gsslogmsg)
  662. ppl_logevent("%s", s->shgss->lib->gsslogmsg);
  663. ppl_logevent("Trying gssapi-keyex...");
  664. s->pktout = ssh2_userauth_gss_packet(s, "gssapi-keyex");
  665. pq_push(s->ppl.out_pq, s->pktout);
  666. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  667. s->shgss->ctx = NULL;
  668. continue;
  669. } else
  670. #endif /* NO_GSSAPI */
  671. if (s->can_pubkey && !s->done_agent &&
  672. s->agent_key_index < s->agent_key_limit) {
  673. /*
  674. * Attempt public-key authentication using a key from Pageant.
  675. */
  676. s->agent_keyalg = s->agent_keys[s->agent_key_index].algorithm;
  677. s->signflags = 0;
  678. if (ptrlen_eq_string(s->agent_keyalg, "ssh-rsa")) {
  679. /* Try to upgrade ssh-rsa to one of the rsa-sha2-* family,
  680. * if the server has announced support for them. */
  681. if (s->ppl.bpp->ext_info_rsa_sha512_ok) {
  682. s->agent_keyalg = PTRLEN_LITERAL("rsa-sha2-512");
  683. s->signflags = SSH_AGENT_RSA_SHA2_512;
  684. } else if (s->ppl.bpp->ext_info_rsa_sha256_ok) {
  685. s->agent_keyalg = PTRLEN_LITERAL("rsa-sha2-256");
  686. s->signflags = SSH_AGENT_RSA_SHA2_256;
  687. }
  688. }
  689. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PUBLICKEY;
  690. ppl_logevent("Trying Pageant key #%"SIZEu, s->agent_key_index);
  691. /* See if server will accept it */
  692. s->pktout = ssh_bpp_new_pktout(
  693. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  694. put_stringz(s->pktout, s->username);
  695. put_stringz(s->pktout, s->successor_layer->vt->name);
  696. put_stringz(s->pktout, "publickey");
  697. /* method */
  698. put_bool(s->pktout, false); /* no signature included */
  699. put_stringpl(s->pktout, s->agent_keyalg);
  700. put_stringpl(s->pktout, ptrlen_from_strbuf(
  701. s->agent_keys[s->agent_key_index].blob));
  702. pq_push(s->ppl.out_pq, s->pktout);
  703. s->type = AUTH_TYPE_PUBLICKEY_OFFER_QUIET;
  704. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  705. if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {
  706. /* Offer of key refused, presumably via
  707. * USERAUTH_FAILURE. Requeue for the next iteration. */
  708. pq_push_front(s->ppl.in_pq, pktin);
  709. } else {
  710. strbuf *agentreq, *sigdata;
  711. ptrlen comment = ptrlen_from_strbuf(
  712. s->agent_keys[s->agent_key_index].comment);
  713. if (seat_verbose(s->ppl.seat))
  714. ppl_printf("Authenticating with public key "
  715. "\"%.*s\" from agent\r\n",
  716. PTRLEN_PRINTF(comment));
  717. /*
  718. * Server is willing to accept the key.
  719. * Construct a SIGN_REQUEST.
  720. */
  721. s->pktout = ssh_bpp_new_pktout(
  722. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  723. put_stringz(s->pktout, s->username);
  724. put_stringz(s->pktout, s->successor_layer->vt->name);
  725. put_stringz(s->pktout, "publickey");
  726. /* method */
  727. put_bool(s->pktout, true); /* signature included */
  728. put_stringpl(s->pktout, s->agent_keyalg);
  729. put_stringpl(s->pktout, ptrlen_from_strbuf(
  730. s->agent_keys[s->agent_key_index].blob));
  731. /* Ask agent for signature. */
  732. agentreq = strbuf_new_for_agent_query();
  733. put_byte(agentreq, SSH2_AGENTC_SIGN_REQUEST);
  734. put_stringpl(agentreq, ptrlen_from_strbuf(
  735. s->agent_keys[s->agent_key_index].blob));
  736. /* Now the data to be signed... */
  737. sigdata = strbuf_new();
  738. ssh2_userauth_add_session_id(s, sigdata);
  739. put_data(sigdata, s->pktout->data + 5,
  740. s->pktout->length - 5);
  741. put_stringsb(agentreq, sigdata);
  742. /* And finally the flags word. */
  743. put_uint32(agentreq, s->signflags);
  744. ssh2_userauth_agent_query(s, agentreq);
  745. strbuf_free(agentreq);
  746. crWaitUntilV(!s->auth_agent_query);
  747. if (s->agent_response.ptr) {
  748. ptrlen sigblob;
  749. BinarySource src[1];
  750. BinarySource_BARE_INIT(src, s->agent_response.ptr,
  751. s->agent_response.len);
  752. get_uint32(src); /* skip length field */
  753. if (get_byte(src) == SSH2_AGENT_SIGN_RESPONSE &&
  754. (sigblob = get_string(src), !get_err(src))) {
  755. ppl_logevent("Sending Pageant's response");
  756. ssh2_userauth_add_sigblob(
  757. s, s->pktout,
  758. ptrlen_from_strbuf(
  759. s->agent_keys[s->agent_key_index].blob),
  760. sigblob);
  761. pq_push(s->ppl.out_pq, s->pktout);
  762. s->type = AUTH_TYPE_PUBLICKEY;
  763. s->is_trivial_auth = false;
  764. } else {
  765. ppl_logevent("Pageant refused signing request");
  766. ppl_printf("Pageant failed to "
  767. "provide a signature\r\n");
  768. s->suppress_wait_for_response_packet = true;
  769. ssh_free_pktout(s->pktout);
  770. }
  771. } else {
  772. ppl_logevent("Pageant failed to respond to "
  773. "signing request");
  774. ppl_printf("Pageant failed to "
  775. "respond to signing request\r\n");
  776. s->suppress_wait_for_response_packet = true;
  777. ssh_free_pktout(s->pktout);
  778. }
  779. }
  780. /* Do we have any keys left to try? */
  781. if (++s->agent_key_index >= s->agent_key_limit)
  782. s->done_agent = true;
  783. } else if (s->can_pubkey && s->publickey_blob &&
  784. s->privatekey_available && !s->tried_pubkey_config) {
  785. ssh2_userkey *key; /* not live over crReturn */
  786. char *passphrase; /* not live over crReturn */
  787. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PUBLICKEY;
  788. s->tried_pubkey_config = true;
  789. /*
  790. * Try the public key supplied in the configuration.
  791. *
  792. * First, try to upgrade its algorithm.
  793. */
  794. if (!strcmp(s->publickey_algorithm, "ssh-rsa")) {
  795. /* Try to upgrade ssh-rsa to one of the rsa-sha2-* family,
  796. * if the server has announced support for them. */
  797. if (s->ppl.bpp->ext_info_rsa_sha512_ok) {
  798. sfree(s->publickey_algorithm);
  799. s->publickey_algorithm = dupstr("rsa-sha2-512");
  800. s->signflags = SSH_AGENT_RSA_SHA2_512;
  801. } else if (s->ppl.bpp->ext_info_rsa_sha256_ok) {
  802. sfree(s->publickey_algorithm);
  803. s->publickey_algorithm = dupstr("rsa-sha2-256");
  804. s->signflags = SSH_AGENT_RSA_SHA2_256;
  805. }
  806. }
  807. /*
  808. * Offer the public blob to see if the server is willing to
  809. * accept it.
  810. */
  811. s->pktout = ssh_bpp_new_pktout(
  812. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  813. put_stringz(s->pktout, s->username);
  814. put_stringz(s->pktout, s->successor_layer->vt->name);
  815. put_stringz(s->pktout, "publickey"); /* method */
  816. put_bool(s->pktout, false);
  817. /* no signature included */
  818. put_stringz(s->pktout, s->publickey_algorithm);
  819. put_string(s->pktout, s->publickey_blob->s,
  820. s->publickey_blob->len);
  821. pq_push(s->ppl.out_pq, s->pktout);
  822. ppl_logevent("Offered public key");
  823. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  824. if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {
  825. /* Key refused. Give up. */
  826. pq_push_front(s->ppl.in_pq, pktin);
  827. s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD;
  828. continue; /* process this new message */
  829. }
  830. ppl_logevent("Offer of public key accepted");
  831. /*
  832. * Actually attempt a serious authentication using
  833. * the key.
  834. */
  835. if (seat_verbose(s->ppl.seat))
  836. ppl_printf("Authenticating with public key \"%s\"\r\n",
  837. s->publickey_comment);
  838. key = NULL;
  839. while (!key) {
  840. const char *error; /* not live over crReturn */
  841. if (s->privatekey_encrypted) {
  842. /*
  843. * Get a passphrase from the user.
  844. */
  845. s->cur_prompt = ssh_ppl_new_prompts(&s->ppl);
  846. s->cur_prompt->to_server = false;
  847. s->cur_prompt->from_server = false;
  848. s->cur_prompt->name = dupstr("SSH key passphrase");
  849. add_prompt(s->cur_prompt,
  850. dupprintf("Passphrase for key \"%s\": ",
  851. s->publickey_comment),
  852. false);
  853. s->spr = seat_get_userpass_input(
  854. ppl_get_iseat(&s->ppl), s->cur_prompt);
  855. while (s->spr.kind == SPRK_INCOMPLETE) {
  856. crReturnV;
  857. s->spr = seat_get_userpass_input(
  858. ppl_get_iseat(&s->ppl), s->cur_prompt);
  859. }
  860. if (spr_is_abort(s->spr)) {
  861. /* Failed to get a passphrase. Terminate. */
  862. free_prompts(s->cur_prompt);
  863. s->cur_prompt = NULL;
  864. ssh_bpp_queue_disconnect(
  865. s->ppl.bpp, "Unable to authenticate",
  866. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  867. ssh_spr_close(s->ppl.ssh, s->spr,
  868. "passphrase prompt");
  869. return;
  870. }
  871. passphrase =
  872. prompt_get_result(s->cur_prompt->prompts[0]);
  873. free_prompts(s->cur_prompt);
  874. s->cur_prompt = NULL;
  875. } else {
  876. passphrase = NULL; /* no passphrase needed */
  877. }
  878. /*
  879. * Try decrypting the key.
  880. */
  881. key = ppk_load_f(s->keyfile, passphrase, &error);
  882. if (passphrase) {
  883. /* burn the evidence */
  884. smemclr(passphrase, strlen(passphrase));
  885. sfree(passphrase);
  886. }
  887. if (key == SSH2_WRONG_PASSPHRASE || key == NULL) {
  888. if (passphrase &&
  889. (key == SSH2_WRONG_PASSPHRASE)) {
  890. ppl_printf("Wrong passphrase\r\n");
  891. key = NULL;
  892. /* and loop again */
  893. } else {
  894. ppl_printf("Unable to load private key (%s)\r\n",
  895. error);
  896. key = NULL;
  897. s->suppress_wait_for_response_packet = true;
  898. break; /* try something else */
  899. }
  900. } else {
  901. /* FIXME: if we ever support variable signature
  902. * flags, this is somewhere they'll need to be
  903. * put */
  904. char *invalid = ssh_key_invalid(key->key, 0);
  905. if (invalid) {
  906. ppl_printf("Cannot use this private key (%s)\r\n",
  907. invalid);
  908. ssh_key_free(key->key);
  909. sfree(key->comment);
  910. sfree(key);
  911. sfree(invalid);
  912. key = NULL;
  913. s->suppress_wait_for_response_packet = true;
  914. break; /* try something else */
  915. }
  916. }
  917. }
  918. if (key) {
  919. strbuf *pkblob, *sigdata, *sigblob;
  920. /*
  921. * We have loaded the private key and the server
  922. * has announced that it's willing to accept it.
  923. * Hallelujah. Generate a signature and send it.
  924. */
  925. s->pktout = ssh_bpp_new_pktout(
  926. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  927. put_stringz(s->pktout, s->username);
  928. put_stringz(s->pktout, s->successor_layer->vt->name);
  929. put_stringz(s->pktout, "publickey"); /* method */
  930. put_bool(s->pktout, true); /* signature follows */
  931. put_stringz(s->pktout, s->publickey_algorithm);
  932. pkblob = strbuf_new();
  933. ssh_key_public_blob(key->key, BinarySink_UPCAST(pkblob));
  934. put_string(s->pktout, pkblob->s, pkblob->len);
  935. /*
  936. * The data to be signed is:
  937. *
  938. * string session-id
  939. *
  940. * followed by everything so far placed in the
  941. * outgoing packet.
  942. */
  943. sigdata = strbuf_new();
  944. ssh2_userauth_add_session_id(s, sigdata);
  945. put_data(sigdata, s->pktout->data + 5,
  946. s->pktout->length - 5);
  947. sigblob = strbuf_new();
  948. ssh_key_sign(key->key, ptrlen_from_strbuf(sigdata),
  949. s->signflags, BinarySink_UPCAST(sigblob));
  950. strbuf_free(sigdata);
  951. ssh2_userauth_add_sigblob(
  952. s, s->pktout, ptrlen_from_strbuf(pkblob),
  953. ptrlen_from_strbuf(sigblob));
  954. strbuf_free(pkblob);
  955. strbuf_free(sigblob);
  956. pq_push(s->ppl.out_pq, s->pktout);
  957. ppl_logevent("Sent public key signature");
  958. s->type = AUTH_TYPE_PUBLICKEY;
  959. ssh_key_free(key->key);
  960. sfree(key->comment);
  961. sfree(key);
  962. s->is_trivial_auth = false;
  963. }
  964. #ifndef NO_GSSAPI
  965. } else if (s->can_gssapi && !s->tried_gssapi) {
  966. /* gssapi-with-mic authentication */
  967. ptrlen data;
  968. s->type = AUTH_TYPE_GSSAPI;
  969. s->tried_gssapi = true;
  970. s->ppl.bpp->pls->actx = SSH2_PKTCTX_GSSAPI;
  971. if (s->shgss->lib->gsslogmsg)
  972. ppl_logevent("%s", s->shgss->lib->gsslogmsg);
  973. /* Sending USERAUTH_REQUEST with "gssapi-with-mic" method */
  974. ppl_logevent("Trying gssapi-with-mic...");
  975. s->pktout = ssh_bpp_new_pktout(
  976. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  977. put_stringz(s->pktout, s->username);
  978. put_stringz(s->pktout, s->successor_layer->vt->name);
  979. put_stringz(s->pktout, "gssapi-with-mic");
  980. ppl_logevent("Attempting GSSAPI authentication");
  981. /* add mechanism info */
  982. s->shgss->lib->indicate_mech(s->shgss->lib, &s->gss_buf);
  983. /* number of GSSAPI mechanisms */
  984. put_uint32(s->pktout, 1);
  985. /* length of OID + 2 */
  986. put_uint32(s->pktout, s->gss_buf.length + 2);
  987. put_byte(s->pktout, SSH2_GSS_OIDTYPE);
  988. /* length of OID */
  989. put_byte(s->pktout, s->gss_buf.length);
  990. put_data(s->pktout, s->gss_buf.value, s->gss_buf.length);
  991. pq_push(s->ppl.out_pq, s->pktout);
  992. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  993. if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_RESPONSE) {
  994. ppl_logevent("GSSAPI authentication request refused");
  995. pq_push_front(s->ppl.in_pq, pktin);
  996. continue;
  997. }
  998. /* check returned packet ... */
  999. data = get_string(pktin);
  1000. s->gss_rcvtok.value = (char *)data.ptr;
  1001. s->gss_rcvtok.length = data.len;
  1002. if (s->gss_rcvtok.length != s->gss_buf.length + 2 ||
  1003. ((char *)s->gss_rcvtok.value)[0] != SSH2_GSS_OIDTYPE ||
  1004. ((char *)s->gss_rcvtok.value)[1] != s->gss_buf.length ||
  1005. memcmp((char *)s->gss_rcvtok.value + 2,
  1006. s->gss_buf.value,s->gss_buf.length) ) {
  1007. ppl_logevent("GSSAPI authentication - wrong response "
  1008. "from server");
  1009. continue;
  1010. }
  1011. /* Import server name if not cached from KEX */
  1012. if (s->shgss->srv_name == GSS_C_NO_NAME) {
  1013. // WINSCP
  1014. const char * fullhostname = s->fullhostname;
  1015. if (s->loghost[0] != '\0')
  1016. {
  1017. fullhostname = s->loghost;
  1018. }
  1019. // /WINSCP
  1020. s->gss_stat = s->shgss->lib->import_name(
  1021. s->shgss->lib, fullhostname, &s->shgss->srv_name); // WINSCP
  1022. if (s->gss_stat != SSH_GSS_OK) {
  1023. if (s->gss_stat == SSH_GSS_BAD_HOST_NAME)
  1024. ppl_logevent("GSSAPI import name failed -"
  1025. " Bad service name");
  1026. else
  1027. ppl_logevent("GSSAPI import name failed");
  1028. continue;
  1029. }
  1030. }
  1031. /* Allocate our gss_ctx */
  1032. s->gss_stat = s->shgss->lib->acquire_cred(
  1033. s->shgss->lib, &s->shgss->ctx, NULL);
  1034. if (s->gss_stat != SSH_GSS_OK) {
  1035. ppl_logevent("GSSAPI authentication failed to get "
  1036. "credentials");
  1037. /* The failure was on our side, so the server
  1038. * won't be sending a response packet indicating
  1039. * failure. Avoid waiting for it next time round
  1040. * the loop. */
  1041. s->suppress_wait_for_response_packet = true;
  1042. continue;
  1043. }
  1044. /* initial tokens are empty */
  1045. SSH_GSS_CLEAR_BUF(&s->gss_rcvtok);
  1046. SSH_GSS_CLEAR_BUF(&s->gss_sndtok);
  1047. /* now enter the loop */
  1048. do {
  1049. /*
  1050. * When acquire_cred yields no useful expiration, go with
  1051. * the service ticket expiration.
  1052. */
  1053. s->gss_stat = s->shgss->lib->init_sec_context
  1054. (s->shgss->lib,
  1055. &s->shgss->ctx,
  1056. s->shgss->srv_name,
  1057. s->gssapi_fwd,
  1058. &s->gss_rcvtok,
  1059. &s->gss_sndtok,
  1060. NULL,
  1061. NULL);
  1062. if (s->gss_stat!=SSH_GSS_S_COMPLETE &&
  1063. s->gss_stat!=SSH_GSS_S_CONTINUE_NEEDED) {
  1064. ppl_logevent("GSSAPI authentication initialisation "
  1065. "failed");
  1066. if (s->shgss->lib->display_status(s->shgss->lib,
  1067. s->shgss->ctx, &s->gss_buf) == SSH_GSS_OK) {
  1068. ppl_logevent("%s", (char *)s->gss_buf.value);
  1069. sfree(s->gss_buf.value);
  1070. }
  1071. pq_push_front(s->ppl.in_pq, pktin);
  1072. break;
  1073. }
  1074. ppl_logevent("GSSAPI authentication initialised");
  1075. /*
  1076. * Client and server now exchange tokens until GSSAPI
  1077. * no longer says CONTINUE_NEEDED
  1078. */
  1079. if (s->gss_sndtok.length != 0) {
  1080. s->is_trivial_auth = false;
  1081. s->pktout =
  1082. ssh_bpp_new_pktout(
  1083. s->ppl.bpp, SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
  1084. put_string(s->pktout,
  1085. s->gss_sndtok.value, s->gss_sndtok.length);
  1086. pq_push(s->ppl.out_pq, s->pktout);
  1087. s->shgss->lib->free_tok(s->shgss->lib, &s->gss_sndtok);
  1088. }
  1089. if (s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED) {
  1090. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1091. if (pktin->type == SSH2_MSG_USERAUTH_GSSAPI_ERRTOK) {
  1092. /*
  1093. * Per RFC 4462 section 3.9, this packet
  1094. * type MUST immediately precede an
  1095. * ordinary USERAUTH_FAILURE.
  1096. *
  1097. * We currently don't know how to do
  1098. * anything with the GSSAPI error token
  1099. * contained in this packet, so we ignore
  1100. * it and just wait for the following
  1101. * FAILURE.
  1102. */
  1103. crMaybeWaitUntilV(
  1104. (pktin = ssh2_userauth_pop(s)) != NULL);
  1105. if (pktin->type != SSH2_MSG_USERAUTH_FAILURE) {
  1106. ssh_proto_error(
  1107. s->ppl.ssh, "Received unexpected packet "
  1108. "after SSH_MSG_USERAUTH_GSSAPI_ERRTOK "
  1109. "(expected SSH_MSG_USERAUTH_FAILURE): "
  1110. "type %d (%s)", pktin->type,
  1111. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  1112. s->ppl.bpp->pls->actx,
  1113. pktin->type));
  1114. return;
  1115. }
  1116. }
  1117. if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) {
  1118. ppl_logevent("GSSAPI authentication failed");
  1119. s->gss_stat = SSH_GSS_FAILURE;
  1120. pq_push_front(s->ppl.in_pq, pktin);
  1121. break;
  1122. } else if (pktin->type !=
  1123. SSH2_MSG_USERAUTH_GSSAPI_TOKEN) {
  1124. ppl_logevent("GSSAPI authentication -"
  1125. " bad server response");
  1126. s->gss_stat = SSH_GSS_FAILURE;
  1127. break;
  1128. }
  1129. data = get_string(pktin);
  1130. s->gss_rcvtok.value = (char *)data.ptr;
  1131. s->gss_rcvtok.length = data.len;
  1132. }
  1133. } while (s-> gss_stat == SSH_GSS_S_CONTINUE_NEEDED);
  1134. if (s->gss_stat != SSH_GSS_OK) {
  1135. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  1136. continue;
  1137. }
  1138. ppl_logevent("GSSAPI authentication loop finished OK");
  1139. /* Now send the MIC */
  1140. s->pktout = ssh2_userauth_gss_packet(s, "gssapi-with-mic");
  1141. pq_push(s->ppl.out_pq, s->pktout);
  1142. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  1143. continue;
  1144. #endif
  1145. } else if (s->can_keyb_inter && !s->kbd_inter_refused) {
  1146. /*
  1147. * Keyboard-interactive authentication.
  1148. */
  1149. s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE;
  1150. s->ppl.bpp->pls->actx = SSH2_PKTCTX_KBDINTER;
  1151. s->pktout = ssh_bpp_new_pktout(
  1152. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1153. put_stringz(s->pktout, s->username);
  1154. put_stringz(s->pktout, s->successor_layer->vt->name);
  1155. put_stringz(s->pktout, "keyboard-interactive");
  1156. /* method */
  1157. put_stringz(s->pktout, ""); /* lang */
  1158. put_stringz(s->pktout, ""); /* submethods */
  1159. pq_push(s->ppl.out_pq, s->pktout);
  1160. ppl_logevent("Attempting keyboard-interactive authentication");
  1161. if (!s->ki_scc_initialised) {
  1162. s->ki_scc = seat_stripctrl_new(
  1163. s->ppl.seat, NULL, SIC_KI_PROMPTS);
  1164. if (s->ki_scc)
  1165. stripctrl_enable_line_limiting(s->ki_scc);
  1166. s->ki_scc_initialised = true;
  1167. }
  1168. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1169. if (pktin->type != SSH2_MSG_USERAUTH_INFO_REQUEST) {
  1170. /* Server is not willing to do keyboard-interactive
  1171. * at all (or, bizarrely but legally, accepts the
  1172. * user without actually issuing any prompts).
  1173. * Give up on it entirely. */
  1174. pq_push_front(s->ppl.in_pq, pktin);
  1175. s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET;
  1176. s->kbd_inter_refused = true; /* don't try it again */
  1177. continue;
  1178. }
  1179. s->ki_printed_header = false;
  1180. /*
  1181. * Loop while the server continues to send INFO_REQUESTs.
  1182. */
  1183. while (pktin->type == SSH2_MSG_USERAUTH_INFO_REQUEST) {
  1184. ptrlen name, inst;
  1185. strbuf *sb;
  1186. /*
  1187. * We've got a fresh USERAUTH_INFO_REQUEST.
  1188. * Get the preamble and start building a prompt.
  1189. */
  1190. name = get_string(pktin);
  1191. inst = get_string(pktin);
  1192. get_string(pktin); /* skip language tag */
  1193. s->cur_prompt = ssh_ppl_new_prompts(&s->ppl);
  1194. s->cur_prompt->to_server = true;
  1195. s->cur_prompt->from_server = true;
  1196. /*
  1197. * Get any prompt(s) from the packet.
  1198. */
  1199. s->num_prompts = get_uint32(pktin);
  1200. { // WINSCP
  1201. uint32_t i;
  1202. for (i = 0; i < s->num_prompts; i++) {
  1203. s->is_trivial_auth = false;
  1204. { // WINSCP
  1205. ptrlen prompt = get_string(pktin);
  1206. bool echo = get_bool(pktin);
  1207. if (get_err(pktin)) {
  1208. ssh_proto_error(
  1209. s->ppl.ssh, "Server sent truncated "
  1210. "SSH_MSG_USERAUTH_INFO_REQUEST packet");
  1211. return;
  1212. }
  1213. sb = strbuf_new();
  1214. if (!prompt.len) {
  1215. put_datapl(sb, PTRLEN_LITERAL(
  1216. "<server failed to send prompt>: "));
  1217. } else if (s->ki_scc) {
  1218. stripctrl_retarget(
  1219. s->ki_scc, BinarySink_UPCAST(sb));
  1220. put_datapl(s->ki_scc, prompt);
  1221. stripctrl_retarget(s->ki_scc, NULL);
  1222. } else {
  1223. put_datapl(sb, prompt);
  1224. }
  1225. add_prompt(s->cur_prompt, strbuf_to_str(sb), echo);
  1226. } // WINSCP
  1227. }
  1228. } // WINSCP
  1229. /*
  1230. * Make the header strings. This includes the
  1231. * 'name' (optional dialog-box title) and
  1232. * 'instruction' from the server.
  1233. *
  1234. * First, display our disambiguating header line
  1235. * if this is the first time round the loop -
  1236. * _unless_ the server has sent a completely empty
  1237. * k-i packet with no prompts _or_ text, which
  1238. * apparently some do. In that situation there's
  1239. * no need to alert the user that the following
  1240. * text is server- supplied, because, well, _what_
  1241. * text?
  1242. *
  1243. * We also only do this if we got a stripctrl,
  1244. * because if we didn't, that suggests this is all
  1245. * being done via dialog boxes anyway.
  1246. */
  1247. if (!s->ki_printed_header && s->ki_scc &&
  1248. (s->num_prompts || name.len || inst.len)) {
  1249. seat_antispoof_msg(
  1250. ppl_get_iseat(&s->ppl), "Keyboard-interactive "
  1251. "authentication prompts from server:");
  1252. s->ki_printed_header = true;
  1253. seat_set_trust_status(s->ppl.seat, false);
  1254. }
  1255. sb = strbuf_new();
  1256. if (name.len) {
  1257. put_datapl(sb, PTRLEN_LITERAL("SSH server: ")); // WINSCP
  1258. if (s->ki_scc) {
  1259. stripctrl_retarget(s->ki_scc,
  1260. BinarySink_UPCAST(sb));
  1261. put_datapl(s->ki_scc, name);
  1262. stripctrl_retarget(s->ki_scc, NULL);
  1263. } else {
  1264. put_datapl(sb, name);
  1265. }
  1266. s->cur_prompt->name_reqd = true;
  1267. } else {
  1268. put_datapl(sb, PTRLEN_LITERAL(
  1269. "SSH server authentication"));
  1270. s->cur_prompt->name_reqd = false;
  1271. }
  1272. s->cur_prompt->name = strbuf_to_str(sb);
  1273. sb = strbuf_new();
  1274. if (inst.len) {
  1275. if (s->ki_scc) {
  1276. stripctrl_retarget(s->ki_scc,
  1277. BinarySink_UPCAST(sb));
  1278. put_datapl(s->ki_scc, inst);
  1279. stripctrl_retarget(s->ki_scc, NULL);
  1280. } else {
  1281. put_datapl(sb, inst);
  1282. }
  1283. s->cur_prompt->instr_reqd = true;
  1284. } else {
  1285. s->cur_prompt->instr_reqd = false;
  1286. }
  1287. if (sb->len)
  1288. s->cur_prompt->instruction = strbuf_to_str(sb);
  1289. else
  1290. strbuf_free(sb);
  1291. /*
  1292. * Our prompts_t is fully constructed now. Get the
  1293. * user's response(s).
  1294. */
  1295. s->spr = seat_get_userpass_input(
  1296. ppl_get_iseat(&s->ppl), s->cur_prompt);
  1297. while (s->spr.kind == SPRK_INCOMPLETE) {
  1298. crReturnV;
  1299. s->spr = seat_get_userpass_input(
  1300. ppl_get_iseat(&s->ppl), s->cur_prompt);
  1301. }
  1302. if (spr_is_abort(s->spr)) {
  1303. /*
  1304. * Failed to get responses. Terminate.
  1305. */
  1306. free_prompts(s->cur_prompt);
  1307. s->cur_prompt = NULL;
  1308. ssh_bpp_queue_disconnect(
  1309. s->ppl.bpp, "Unable to authenticate",
  1310. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1311. ssh_spr_close(s->ppl.ssh, s->spr, "keyboard-"
  1312. "interactive authentication prompt");
  1313. return;
  1314. }
  1315. /*
  1316. * Send the response(s) to the server.
  1317. */
  1318. s->pktout = ssh_bpp_new_pktout(
  1319. s->ppl.bpp, SSH2_MSG_USERAUTH_INFO_RESPONSE);
  1320. put_uint32(s->pktout, s->num_prompts);
  1321. { // WINSCP
  1322. uint32_t i; // WINSCP
  1323. for (i = 0; i < s->num_prompts; i++) {
  1324. put_stringz(s->pktout, prompt_get_result_ref(
  1325. s->cur_prompt->prompts[i]));
  1326. }
  1327. } // WINSCP
  1328. s->pktout->minlen = 256;
  1329. pq_push(s->ppl.out_pq, s->pktout);
  1330. /*
  1331. * Free the prompts structure from this iteration.
  1332. * If there's another, a new one will be allocated
  1333. * when we return to the top of this while loop.
  1334. */
  1335. free_prompts(s->cur_prompt);
  1336. s->cur_prompt = NULL;
  1337. /*
  1338. * Get the next packet in case it's another
  1339. * INFO_REQUEST.
  1340. */
  1341. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1342. }
  1343. /*
  1344. * Print our trailer line, if we printed a header.
  1345. */
  1346. if (s->ki_printed_header) {
  1347. seat_set_trust_status(s->ppl.seat, true);
  1348. seat_antispoof_msg(
  1349. ppl_get_iseat(&s->ppl),
  1350. "End of keyboard-interactive prompts from server");
  1351. }
  1352. /*
  1353. * We should have SUCCESS or FAILURE now.
  1354. */
  1355. pq_push_front(s->ppl.in_pq, pktin);
  1356. } else if (s->can_passwd) {
  1357. s->is_trivial_auth = false;
  1358. { // WINSCP
  1359. /*
  1360. * Plain old password authentication.
  1361. */
  1362. bool changereq_first_time; /* not live over crReturn */
  1363. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PASSWORD;
  1364. // WINSCP
  1365. if (s->change_password)
  1366. {
  1367. s->password = dupstr("");
  1368. s->type = AUTH_TYPE_PASSWORD;
  1369. }
  1370. else
  1371. {
  1372. // no indentation to ease merges
  1373. // /WINSCP
  1374. s->cur_prompt = ssh_ppl_new_prompts(&s->ppl);
  1375. s->cur_prompt->to_server = true;
  1376. s->cur_prompt->from_server = false;
  1377. s->cur_prompt->name = dupstr("SSH password");
  1378. add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ",
  1379. s->username, s->hostname),
  1380. false);
  1381. s->spr = seat_get_userpass_input(
  1382. ppl_get_iseat(&s->ppl), s->cur_prompt);
  1383. while (s->spr.kind == SPRK_INCOMPLETE) {
  1384. crReturnV;
  1385. s->spr = seat_get_userpass_input(
  1386. ppl_get_iseat(&s->ppl), s->cur_prompt);
  1387. }
  1388. if (spr_is_abort(s->spr)) {
  1389. /*
  1390. * Failed to get responses. Terminate.
  1391. */
  1392. free_prompts(s->cur_prompt);
  1393. s->cur_prompt = NULL;
  1394. ssh_bpp_queue_disconnect(
  1395. s->ppl.bpp, "Unable to authenticate",
  1396. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1397. ssh_spr_close(s->ppl.ssh, s->spr, "password prompt");
  1398. return;
  1399. }
  1400. /*
  1401. * Squirrel away the password. (We may need it later if
  1402. * asked to change it.)
  1403. */
  1404. s->password = prompt_get_result(s->cur_prompt->prompts[0]);
  1405. free_prompts(s->cur_prompt);
  1406. s->cur_prompt = NULL;
  1407. /*
  1408. * Send the password packet.
  1409. *
  1410. * We pad out the password packet to 256 bytes to make
  1411. * it harder for an attacker to find the length of the
  1412. * user's password.
  1413. *
  1414. * Anyone using a password longer than 256 bytes
  1415. * probably doesn't have much to worry about from
  1416. * people who find out how long their password is!
  1417. */
  1418. s->pktout = ssh_bpp_new_pktout(
  1419. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1420. put_stringz(s->pktout, s->username);
  1421. put_stringz(s->pktout, s->successor_layer->vt->name);
  1422. put_stringz(s->pktout, "password");
  1423. put_bool(s->pktout, false);
  1424. put_stringz(s->pktout, s->password);
  1425. s->pktout->minlen = 256;
  1426. pq_push(s->ppl.out_pq, s->pktout);
  1427. ppl_logevent("Sent password");
  1428. s->type = AUTH_TYPE_PASSWORD;
  1429. /*
  1430. * Wait for next packet, in case it's a password change
  1431. * request.
  1432. */
  1433. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1434. } // WINSCP
  1435. changereq_first_time = true;
  1436. while ((pktin->type == SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ) ||
  1437. s->change_password) { // WINSCP
  1438. /*
  1439. * We're being asked for a new password
  1440. * (perhaps not for the first time).
  1441. * Loop until the server accepts it.
  1442. */
  1443. bool got_new = false; /* not live over crReturn */
  1444. ptrlen prompt; /* not live over crReturn */
  1445. if (!s->change_password) // WINSCP
  1446. {
  1447. const char *msg;
  1448. if (changereq_first_time)
  1449. msg = "Server requested password change";
  1450. else
  1451. msg = "Server rejected new password";
  1452. ppl_logevent("%s", msg);
  1453. ppl_printf("%s\r\n", msg);
  1454. }
  1455. s->change_password = false; // WINSCP
  1456. prompt = get_string(pktin);
  1457. s->cur_prompt = ssh_ppl_new_prompts(&s->ppl);
  1458. s->cur_prompt->to_server = true;
  1459. s->cur_prompt->from_server = false;
  1460. s->cur_prompt->name = dupstr("New SSH password");
  1461. s->cur_prompt->instruction = mkstr(prompt);
  1462. s->cur_prompt->instr_reqd = true;
  1463. /*
  1464. * There's no explicit requirement in the protocol
  1465. * for the "old" passwords in the original and
  1466. * password-change messages to be the same, and
  1467. * apparently some Cisco kit supports password change
  1468. * by the user entering a blank password originally
  1469. * and the real password subsequently, so,
  1470. * reluctantly, we prompt for the old password again.
  1471. *
  1472. * (On the other hand, some servers don't even bother
  1473. * to check this field.)
  1474. */
  1475. add_prompt(s->cur_prompt,
  1476. dupstr("Current password (blank for previously entered password): "),
  1477. false);
  1478. add_prompt(s->cur_prompt, dupstr("Enter new password: "),
  1479. false);
  1480. add_prompt(s->cur_prompt, dupstr("Confirm new password: "),
  1481. false);
  1482. /*
  1483. * Loop until the user manages to enter the same
  1484. * password twice.
  1485. */
  1486. while (!got_new) {
  1487. s->spr = seat_get_userpass_input(
  1488. ppl_get_iseat(&s->ppl), s->cur_prompt);
  1489. while (s->spr.kind == SPRK_INCOMPLETE) {
  1490. crReturnV;
  1491. s->spr = seat_get_userpass_input(
  1492. ppl_get_iseat(&s->ppl), s->cur_prompt);
  1493. }
  1494. if (spr_is_abort(s->spr)) {
  1495. /*
  1496. * Failed to get responses. Terminate.
  1497. */
  1498. /* burn the evidence */
  1499. free_prompts(s->cur_prompt);
  1500. s->cur_prompt = NULL;
  1501. smemclr(s->password, strlen(s->password));
  1502. sfree(s->password);
  1503. ssh_bpp_queue_disconnect(
  1504. s->ppl.bpp, "Unable to authenticate",
  1505. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1506. ssh_spr_close(s->ppl.ssh, s->spr,
  1507. "password-change prompt");
  1508. return;
  1509. }
  1510. /*
  1511. * If the user specified a new original password
  1512. * (IYSWIM), overwrite any previously specified
  1513. * one.
  1514. * (A side effect is that the user doesn't have to
  1515. * re-enter it if they louse up the new password.)
  1516. */
  1517. if (s->cur_prompt->prompts[0]->result->s[0]) {
  1518. smemclr(s->password, strlen(s->password));
  1519. /* burn the evidence */
  1520. sfree(s->password);
  1521. s->password = prompt_get_result(
  1522. s->cur_prompt->prompts[0]);
  1523. }
  1524. /*
  1525. * Check the two new passwords match.
  1526. */
  1527. got_new = !strcmp(
  1528. prompt_get_result_ref(s->cur_prompt->prompts[1]),
  1529. prompt_get_result_ref(s->cur_prompt->prompts[2]));
  1530. if (!got_new)
  1531. /* They don't. Silly user. */
  1532. ppl_printf("Passwords do not match\r\n");
  1533. }
  1534. /*
  1535. * Send the new password (along with the old one).
  1536. * (see above for padding rationale)
  1537. */
  1538. s->pktout = ssh_bpp_new_pktout(
  1539. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1540. put_stringz(s->pktout, s->username);
  1541. put_stringz(s->pktout, s->successor_layer->vt->name);
  1542. put_stringz(s->pktout, "password");
  1543. put_bool(s->pktout, true);
  1544. put_stringz(s->pktout, s->password);
  1545. put_stringz(s->pktout, prompt_get_result_ref(
  1546. s->cur_prompt->prompts[1]));
  1547. free_prompts(s->cur_prompt);
  1548. s->cur_prompt = NULL;
  1549. s->pktout->minlen = 256;
  1550. pq_push(s->ppl.out_pq, s->pktout);
  1551. ppl_logevent("Sent new password");
  1552. /*
  1553. * Now see what the server has to say about it.
  1554. * (If it's CHANGEREQ again, it's not happy with the
  1555. * new password.)
  1556. */
  1557. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1558. changereq_first_time = false;
  1559. }
  1560. /*
  1561. * We need to reexamine the current pktin at the top
  1562. * of the loop. Either:
  1563. * - we weren't asked to change password at all, in
  1564. * which case it's a SUCCESS or FAILURE with the
  1565. * usual meaning
  1566. * - we sent a new password, and the server was
  1567. * either OK with it (SUCCESS or FAILURE w/partial
  1568. * success) or unhappy with the _old_ password
  1569. * (FAILURE w/o partial success)
  1570. * In any of these cases, we go back to the top of
  1571. * the loop and start again.
  1572. */
  1573. pq_push_front(s->ppl.in_pq, pktin);
  1574. /*
  1575. * We don't need the old password any more, in any
  1576. * case. Burn the evidence.
  1577. */
  1578. smemclr(s->password, strlen(s->password));
  1579. sfree(s->password);
  1580. } // WINSCP
  1581. } else {
  1582. ssh_bpp_queue_disconnect(
  1583. s->ppl.bpp,
  1584. "No supported authentication methods available",
  1585. SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE);
  1586. ssh_sw_abort(s->ppl.ssh, "No supported authentication methods "
  1587. "available (server sent: %s)",
  1588. s->last_methods_string->s);
  1589. return;
  1590. }
  1591. }
  1592. try_new_username:;
  1593. }
  1594. userauth_success:
  1595. if (s->notrivialauth && s->is_trivial_auth) {
  1596. ssh_proto_error(s->ppl.ssh, "Authentication was trivial! "
  1597. "Abandoning session as specified in configuration.");
  1598. return;
  1599. }
  1600. /*
  1601. * We've just received USERAUTH_SUCCESS, and we haven't sent
  1602. * any packets since. Signal the transport layer to consider
  1603. * doing an immediate rekey, if it has any reason to want to.
  1604. */
  1605. ssh2_transport_notify_auth_done(s->transport_layer);
  1606. /*
  1607. * Finally, hand over to our successor layer, and return
  1608. * immediately without reaching the crFinishV: ssh_ppl_replace
  1609. * will have freed us, so crFinishV's zeroing-out of crState would
  1610. * be a use-after-free bug.
  1611. */
  1612. {
  1613. PacketProtocolLayer *successor = s->successor_layer;
  1614. s->successor_layer = NULL; /* avoid freeing it ourself */
  1615. ssh_ppl_replace(&s->ppl, successor);
  1616. return; /* we've just freed s, so avoid even touching s->crState */
  1617. }
  1618. crFinishV;
  1619. }
  1620. static void ssh2_userauth_add_session_id(
  1621. struct ssh2_userauth_state *s, strbuf *sigdata)
  1622. {
  1623. if (s->ppl.remote_bugs & BUG_SSH2_PK_SESSIONID) {
  1624. put_datapl(sigdata, s->session_id);
  1625. } else {
  1626. put_stringpl(sigdata, s->session_id);
  1627. }
  1628. }
  1629. static void ssh2_userauth_agent_query(
  1630. struct ssh2_userauth_state *s, strbuf *req)
  1631. {
  1632. void *response;
  1633. int response_len;
  1634. sfree(s->agent_response_to_free);
  1635. s->agent_response_to_free = NULL;
  1636. s->auth_agent_query = agent_query(req, &response, &response_len,
  1637. ssh2_userauth_agent_callback, s, get_seat_callback_set(s->seat)); // WINSCP
  1638. if (!s->auth_agent_query)
  1639. ssh2_userauth_agent_callback(s, response, response_len);
  1640. }
  1641. static void ssh2_userauth_agent_callback(void *uav, void *reply, int replylen)
  1642. {
  1643. struct ssh2_userauth_state *s = (struct ssh2_userauth_state *)uav;
  1644. s->auth_agent_query = NULL;
  1645. s->agent_response_to_free = reply;
  1646. s->agent_response = make_ptrlen(reply, replylen);
  1647. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1648. }
  1649. /*
  1650. * Helper function to add an SSH-2 signature blob to a packet. Expects
  1651. * to be shown the public key blob as well as the signature blob.
  1652. * Normally just appends the sig blob unmodified as a string, except
  1653. * that it optionally breaks it open and fiddle with it to work around
  1654. * BUG_SSH2_RSA_PADDING.
  1655. */
  1656. static void ssh2_userauth_add_sigblob(
  1657. struct ssh2_userauth_state *s, PktOut *pkt, ptrlen pkblob, ptrlen sigblob)
  1658. {
  1659. BinarySource pk[1], sig[1];
  1660. BinarySource_BARE_INIT_PL(pk, pkblob);
  1661. BinarySource_BARE_INIT_PL(sig, sigblob);
  1662. /* dmemdump(pkblob, pkblob_len); */
  1663. /* dmemdump(sigblob, sigblob_len); */
  1664. /*
  1665. * See if this is in fact an ssh-rsa signature and a buggy
  1666. * server; otherwise we can just do this the easy way.
  1667. */
  1668. if ((s->ppl.remote_bugs & BUG_SSH2_RSA_PADDING) &&
  1669. ptrlen_eq_string(get_string(pk), "ssh-rsa") &&
  1670. ptrlen_eq_string(get_string(sig), "ssh-rsa")) {
  1671. ptrlen mod_mp, sig_mp;
  1672. size_t sig_prefix_len;
  1673. /*
  1674. * Find the modulus and signature integers.
  1675. */
  1676. get_string(pk); /* skip over exponent */
  1677. mod_mp = get_string(pk); /* remember modulus */
  1678. sig_prefix_len = sig->pos;
  1679. sig_mp = get_string(sig);
  1680. if (get_err(pk) || get_err(sig))
  1681. goto give_up;
  1682. /*
  1683. * Find the byte length of the modulus, not counting leading
  1684. * zeroes.
  1685. */
  1686. while (mod_mp.len > 0 && *(const char *)mod_mp.ptr == 0) {
  1687. mod_mp.len--;
  1688. mod_mp.ptr = (const char *)mod_mp.ptr + 1;
  1689. }
  1690. /* debug("modulus length is %d\n", len); */
  1691. /* debug("signature length is %d\n", siglen); */
  1692. if (mod_mp.len > sig_mp.len) {
  1693. strbuf *substr = strbuf_new();
  1694. put_data(substr, sigblob.ptr, sig_prefix_len);
  1695. put_uint32(substr, mod_mp.len);
  1696. put_padding(substr, mod_mp.len - sig_mp.len, 0);
  1697. put_datapl(substr, sig_mp);
  1698. put_stringsb(pkt, substr);
  1699. return;
  1700. }
  1701. /* Otherwise fall through and do it the easy way. We also come
  1702. * here as a fallback if we discover above that the key blob
  1703. * is misformatted in some way. */
  1704. give_up:;
  1705. }
  1706. put_stringpl(pkt, sigblob);
  1707. }
  1708. #ifndef NO_GSSAPI
  1709. static PktOut *ssh2_userauth_gss_packet(
  1710. struct ssh2_userauth_state *s, const char *authtype)
  1711. {
  1712. strbuf *sb;
  1713. PktOut *p;
  1714. Ssh_gss_buf buf;
  1715. Ssh_gss_buf mic;
  1716. /*
  1717. * The mic is computed over the session id + intended
  1718. * USERAUTH_REQUEST packet.
  1719. */
  1720. sb = strbuf_new();
  1721. put_stringpl(sb, s->session_id);
  1722. put_byte(sb, SSH2_MSG_USERAUTH_REQUEST);
  1723. put_stringz(sb, s->username);
  1724. put_stringz(sb, s->successor_layer->vt->name);
  1725. put_stringz(sb, authtype);
  1726. /* Compute the mic */
  1727. buf.value = sb->s;
  1728. buf.length = sb->len;
  1729. s->shgss->lib->get_mic(s->shgss->lib, s->shgss->ctx, &buf, &mic);
  1730. strbuf_free(sb);
  1731. /* Now we can build the real packet */
  1732. if (strcmp(authtype, "gssapi-with-mic") == 0) {
  1733. p = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_GSSAPI_MIC);
  1734. } else {
  1735. p = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1736. put_stringz(p, s->username);
  1737. put_stringz(p, s->successor_layer->vt->name);
  1738. put_stringz(p, authtype);
  1739. }
  1740. put_string(p, mic.value, mic.length);
  1741. return p;
  1742. }
  1743. #endif
  1744. static bool ssh2_userauth_get_specials(
  1745. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  1746. {
  1747. /* No specials provided by this layer. */
  1748. return false;
  1749. }
  1750. static void ssh2_userauth_special_cmd(PacketProtocolLayer *ppl,
  1751. SessionSpecialCode code, int arg)
  1752. {
  1753. /* No specials provided by this layer. */
  1754. }
  1755. static void ssh2_userauth_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  1756. {
  1757. struct ssh2_userauth_state *s =
  1758. container_of(ppl, struct ssh2_userauth_state, ppl);
  1759. ssh_ppl_reconfigure(s->successor_layer, conf);
  1760. }