ssh2userauth.c 81 KB

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