ssh2userauth.c 70 KB

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