ssh2userauth.c 85 KB

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