ssh2userauth.c 74 KB

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