1
0

ssh2userauth.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. /*
  2. * Packet protocol layer for the SSH-2 userauth protocol (RFC 4252).
  3. */
  4. #include <assert.h>
  5. #include "putty.h"
  6. #include "ssh.h"
  7. #include "sshbpp.h"
  8. #include "sshppl.h"
  9. #include "sshcr.h"
  10. #ifndef NO_GSSAPI
  11. #include "sshgssc.h"
  12. #include "sshgss.h"
  13. #endif
  14. #define BANNER_LIMIT 131072
  15. struct ssh2_userauth_state {
  16. int crState;
  17. PacketProtocolLayer *transport_layer, *successor_layer;
  18. Filename *keyfile;
  19. int tryagent, change_username;
  20. char *hostname, *fullhostname;
  21. char *default_username;
  22. int try_ki_auth, try_gssapi_auth, try_gssapi_kex_auth, gssapi_fwd;
  23. char *loghost; // WINSCP
  24. int change_password; // WINSCP
  25. ptrlen session_id;
  26. enum {
  27. AUTH_TYPE_NONE,
  28. AUTH_TYPE_PUBLICKEY,
  29. AUTH_TYPE_PUBLICKEY_OFFER_LOUD,
  30. AUTH_TYPE_PUBLICKEY_OFFER_QUIET,
  31. AUTH_TYPE_PASSWORD,
  32. AUTH_TYPE_GSSAPI, /* always QUIET */
  33. AUTH_TYPE_KEYBOARD_INTERACTIVE,
  34. AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET
  35. } type;
  36. int done_service_req;
  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. s->done_service_req = FALSE;
  188. #ifndef NO_GSSAPI
  189. s->tried_gssapi = FALSE;
  190. s->tried_gssapi_keyex_auth = FALSE;
  191. #endif
  192. /*
  193. * Misc one-time setup for authentication.
  194. */
  195. s->publickey_blob = NULL;
  196. s->session_id = ssh2_transport_get_session_id(s->transport_layer);
  197. /*
  198. * Load the public half of any configured public key file for
  199. * later use.
  200. */
  201. if (!filename_is_null(s->keyfile)) {
  202. int keytype;
  203. ppl_logevent((WINSCP_BOM "Reading key file \"%.150s\"",
  204. filename_to_str(s->keyfile)));
  205. keytype = key_type(s->keyfile);
  206. if (keytype == SSH_KEYTYPE_SSH2 ||
  207. keytype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
  208. keytype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH) {
  209. const char *error;
  210. s->publickey_blob = strbuf_new();
  211. if (ssh2_userkey_loadpub(s->keyfile,
  212. &s->publickey_algorithm,
  213. BinarySink_UPCAST(s->publickey_blob),
  214. &s->publickey_comment, &error)) {
  215. s->privatekey_available = (keytype == SSH_KEYTYPE_SSH2);
  216. if (!s->privatekey_available)
  217. ppl_logevent(("Key file contains public key only"));
  218. s->privatekey_encrypted =
  219. ssh2_userkey_encrypted(s->keyfile, NULL);
  220. } else {
  221. ppl_logevent(("Unable to load key (%s)", error));
  222. ppl_printf((WINSCP_BOM "Unable to load key file \"%s\" (%s)\r\n",
  223. filename_to_str(s->keyfile), error));
  224. strbuf_free(s->publickey_blob);
  225. s->publickey_blob = NULL;
  226. }
  227. } else {
  228. ppl_logevent(("Unable to use this key file (%s)",
  229. key_type_to_str(keytype)));
  230. ppl_printf((WINSCP_BOM "Unable to use key file \"%s\" (%s)\r\n",
  231. filename_to_str(s->keyfile),
  232. key_type_to_str(keytype)));
  233. s->publickey_blob = NULL;
  234. }
  235. }
  236. /*
  237. * Find out about any keys Pageant has (but if there's a public
  238. * key configured, filter out all others).
  239. */
  240. s->nkeys = 0;
  241. s->pkblob_pos_in_agent = 0;
  242. if (s->tryagent && agent_exists()) {
  243. ppl_logevent(("Pageant is running. Requesting keys."));
  244. /* Request the keys held by the agent. */
  245. {
  246. strbuf *request = strbuf_new_for_agent_query();
  247. put_byte(request, SSH2_AGENTC_REQUEST_IDENTITIES);
  248. ssh2_userauth_agent_query(s, request);
  249. strbuf_free(request);
  250. crWaitUntilV(!s->auth_agent_query);
  251. }
  252. BinarySource_BARE_INIT(
  253. s->asrc, s->agent_response.ptr, s->agent_response.len);
  254. get_uint32(s->asrc); /* skip length field */
  255. if (get_byte(s->asrc) == SSH2_AGENT_IDENTITIES_ANSWER) {
  256. int keyi;
  257. s->nkeys = toint(get_uint32(s->asrc));
  258. /*
  259. * Vet the Pageant response to ensure that the key count
  260. * and blob lengths make sense.
  261. */
  262. if (s->nkeys < 0) {
  263. ppl_logevent(("Pageant response contained a negative"
  264. " key count %d", s->nkeys));
  265. s->nkeys = 0;
  266. goto done_agent_query;
  267. } else {
  268. ppl_logevent(("Pageant has %d SSH-2 keys", s->nkeys));
  269. /* See if configured key is in agent. */
  270. for (keyi = 0; keyi < s->nkeys; keyi++) {
  271. size_t pos = s->asrc->pos;
  272. ptrlen blob = get_string(s->asrc);
  273. get_string(s->asrc); /* skip comment */
  274. if (get_err(s->asrc)) {
  275. ppl_logevent(("Pageant response was truncated"));
  276. s->nkeys = 0;
  277. goto done_agent_query;
  278. }
  279. if (s->publickey_blob &&
  280. blob.len == s->publickey_blob->len &&
  281. !memcmp(blob.ptr, s->publickey_blob->s,
  282. s->publickey_blob->len)) {
  283. ppl_logevent(("Pageant key #%d matches "
  284. "configured key file", keyi));
  285. s->keyi = keyi;
  286. s->pkblob_pos_in_agent = pos;
  287. break;
  288. }
  289. }
  290. if (s->publickey_blob && !s->pkblob_pos_in_agent) {
  291. ppl_logevent(("Configured key file not in Pageant"));
  292. s->nkeys = 0;
  293. }
  294. }
  295. } else {
  296. ppl_logevent(("Failed to get reply from Pageant"));
  297. }
  298. done_agent_query:;
  299. }
  300. /*
  301. * We repeat this whole loop, including the username prompt,
  302. * until we manage a successful authentication. If the user
  303. * types the wrong _password_, they can be sent back to the
  304. * beginning to try another username, if this is configured on.
  305. * (If they specify a username in the config, they are never
  306. * asked, even if they do give a wrong password.)
  307. *
  308. * I think this best serves the needs of
  309. *
  310. * - the people who have no configuration, no keys, and just
  311. * want to try repeated (username,password) pairs until they
  312. * type both correctly
  313. *
  314. * - people who have keys and configuration but occasionally
  315. * need to fall back to passwords
  316. *
  317. * - people with a key held in Pageant, who might not have
  318. * logged in to a particular machine before; so they want to
  319. * type a username, and then _either_ their key will be
  320. * accepted, _or_ they will type a password. If they mistype
  321. * the username they will want to be able to get back and
  322. * retype it!
  323. */
  324. s->got_username = FALSE;
  325. while (1) {
  326. /*
  327. * Get a username.
  328. */
  329. if (s->got_username && s->change_username) {
  330. /*
  331. * We got a username last time round this loop, and
  332. * with change_username turned off we don't try to get
  333. * it again.
  334. */
  335. } else if ((s->username = s->default_username) == NULL) {
  336. s->cur_prompt = new_prompts(s->ppl.frontend);
  337. s->cur_prompt->to_server = TRUE;
  338. s->cur_prompt->name = dupstr("SSH login name");
  339. add_prompt(s->cur_prompt, dupstr("login as: "), TRUE);
  340. s->userpass_ret = get_userpass_input(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 = get_userpass_input(
  345. 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. * 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, "ssh-connection");/* service requested */
  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. * from_backend.
  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.frontend, &s->banner, len); // WINSCP
  426. from_backend(s->ppl.frontend, TRUE, 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, "ssh-connection");
  579. /* service requested */
  580. put_stringz(s->pktout, "publickey");
  581. /* method */
  582. put_bool(s->pktout, FALSE); /* no signature included */
  583. put_stringpl(s->pktout, s->alg);
  584. put_stringpl(s->pktout, s->pk);
  585. pq_push(s->ppl.out_pq, s->pktout);
  586. s->type = AUTH_TYPE_PUBLICKEY_OFFER_QUIET;
  587. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  588. if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {
  589. /* Offer of key refused, presumably via
  590. * USERAUTH_FAILURE. Requeue for the next iteration. */
  591. pq_push_front(s->ppl.in_pq, pktin);
  592. } else {
  593. strbuf *agentreq, *sigdata;
  594. if (flags & FLAG_VERBOSE)
  595. ppl_printf(("Authenticating with public key "
  596. "\"%.*s\" from agent\r\n",
  597. PTRLEN_PRINTF(s->comment)));
  598. /*
  599. * Server is willing to accept the key.
  600. * Construct a SIGN_REQUEST.
  601. */
  602. s->pktout = ssh_bpp_new_pktout(
  603. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  604. put_stringz(s->pktout, s->username);
  605. put_stringz(s->pktout, "ssh-connection");
  606. /* service requested */
  607. put_stringz(s->pktout, "publickey");
  608. /* method */
  609. put_bool(s->pktout, TRUE); /* signature included */
  610. put_stringpl(s->pktout, s->alg);
  611. put_stringpl(s->pktout, s->pk);
  612. /* Ask agent for signature. */
  613. agentreq = strbuf_new_for_agent_query();
  614. put_byte(agentreq, SSH2_AGENTC_SIGN_REQUEST);
  615. put_stringpl(agentreq, s->pk);
  616. /* Now the data to be signed... */
  617. sigdata = strbuf_new();
  618. ssh2_userauth_add_session_id(s, sigdata);
  619. put_data(sigdata, s->pktout->data + 5,
  620. s->pktout->length - 5);
  621. put_stringsb(agentreq, sigdata);
  622. /* And finally the (zero) flags word. */
  623. put_uint32(agentreq, 0);
  624. ssh2_userauth_agent_query(s, agentreq);
  625. strbuf_free(agentreq);
  626. crWaitUntilV(!s->auth_agent_query);
  627. if (s->agent_response.ptr) {
  628. ptrlen sigblob;
  629. BinarySource src[1];
  630. BinarySource_BARE_INIT(src, s->agent_response.ptr,
  631. s->agent_response.len);
  632. get_uint32(src); /* skip length field */
  633. if (get_byte(src) == SSH2_AGENT_SIGN_RESPONSE &&
  634. (sigblob = get_string(src), !get_err(src))) {
  635. ppl_logevent(("Sending Pageant's response"));
  636. ssh2_userauth_add_sigblob(s, s->pktout,
  637. s->pk, sigblob);
  638. pq_push(s->ppl.out_pq, s->pktout);
  639. s->type = AUTH_TYPE_PUBLICKEY;
  640. } else {
  641. /* FIXME: less drastic response */
  642. ssh_sw_abort(s->ppl.ssh, "Pageant failed to "
  643. "provide a signature");
  644. return;
  645. }
  646. }
  647. }
  648. /* Do we have any keys left to try? */
  649. if (s->pkblob_pos_in_agent) {
  650. s->done_agent = TRUE;
  651. s->tried_pubkey_config = TRUE;
  652. } else {
  653. s->keyi++;
  654. if (s->keyi >= s->nkeys)
  655. s->done_agent = TRUE;
  656. }
  657. } else if (s->can_pubkey && s->publickey_blob &&
  658. s->privatekey_available && !s->tried_pubkey_config) {
  659. struct ssh2_userkey *key; /* not live over crReturn */
  660. char *passphrase; /* not live over crReturn */
  661. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PUBLICKEY;
  662. s->tried_pubkey_config = TRUE;
  663. /*
  664. * Try the public key supplied in the configuration.
  665. *
  666. * First, offer the public blob to see if the server is
  667. * willing to accept it.
  668. */
  669. s->pktout = ssh_bpp_new_pktout(
  670. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  671. put_stringz(s->pktout, s->username);
  672. put_stringz(s->pktout, "ssh-connection");
  673. /* service requested */
  674. put_stringz(s->pktout, "publickey"); /* method */
  675. put_bool(s->pktout, FALSE);
  676. /* no signature included */
  677. put_stringz(s->pktout, s->publickey_algorithm);
  678. put_string(s->pktout, s->publickey_blob->s,
  679. s->publickey_blob->len);
  680. pq_push(s->ppl.out_pq, s->pktout);
  681. ppl_logevent(("Offered public key"));
  682. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  683. if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {
  684. /* Key refused. Give up. */
  685. pq_push_front(s->ppl.in_pq, pktin);
  686. s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD;
  687. continue; /* process this new message */
  688. }
  689. ppl_logevent(("Offer of public key accepted"));
  690. /*
  691. * Actually attempt a serious authentication using
  692. * the key.
  693. */
  694. if (flags & FLAG_VERBOSE)
  695. ppl_printf(("Authenticating with public key \"%s\"\r\n",
  696. s->publickey_comment));
  697. key = NULL;
  698. while (!key) {
  699. const char *error; /* not live over crReturn */
  700. if (s->privatekey_encrypted) {
  701. /*
  702. * Get a passphrase from the user.
  703. */
  704. s->cur_prompt = new_prompts(s->ppl.frontend);
  705. s->cur_prompt->to_server = FALSE;
  706. s->cur_prompt->name = dupstr("SSH key passphrase");
  707. add_prompt(s->cur_prompt,
  708. dupprintf("Passphrase for key \"%.100s\": ",
  709. s->publickey_comment),
  710. FALSE);
  711. s->userpass_ret = get_userpass_input(
  712. s->cur_prompt, NULL);
  713. while (1) {
  714. while (s->userpass_ret < 0 &&
  715. bufchain_size(s->ppl.user_input) > 0)
  716. s->userpass_ret = get_userpass_input(
  717. s->cur_prompt, s->ppl.user_input);
  718. if (s->userpass_ret >= 0)
  719. break;
  720. s->want_user_input = TRUE;
  721. crReturnV;
  722. s->want_user_input = FALSE;
  723. }
  724. if (!s->userpass_ret) {
  725. /* Failed to get a passphrase. Terminate. */
  726. free_prompts(s->cur_prompt);
  727. ssh_bpp_queue_disconnect(
  728. s->ppl.bpp, "Unable to authenticate",
  729. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  730. ssh_user_close(s->ppl.ssh, "User aborted at "
  731. "passphrase prompt");
  732. return;
  733. }
  734. passphrase =
  735. dupstr(s->cur_prompt->prompts[0]->result);
  736. free_prompts(s->cur_prompt);
  737. } else {
  738. passphrase = NULL; /* no passphrase needed */
  739. }
  740. /*
  741. * Try decrypting the key.
  742. */
  743. key = ssh2_load_userkey(s->keyfile, passphrase, &error);
  744. if (passphrase) {
  745. /* burn the evidence */
  746. smemclr(passphrase, strlen(passphrase));
  747. sfree(passphrase);
  748. }
  749. if (key == SSH2_WRONG_PASSPHRASE || key == NULL) {
  750. if (passphrase &&
  751. (key == SSH2_WRONG_PASSPHRASE)) {
  752. ppl_printf(("Wrong passphrase\r\n"));
  753. key = NULL;
  754. /* and loop again */
  755. } else {
  756. ppl_printf(("Unable to load private key (%s)\r\n",
  757. error));
  758. key = NULL;
  759. break; /* try something else */
  760. }
  761. }
  762. }
  763. if (key) {
  764. strbuf *pkblob, *sigdata, *sigblob;
  765. /*
  766. * We have loaded the private key and the server
  767. * has announced that it's willing to accept it.
  768. * Hallelujah. Generate a signature and send it.
  769. */
  770. s->pktout = ssh_bpp_new_pktout(
  771. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  772. put_stringz(s->pktout, s->username);
  773. put_stringz(s->pktout, "ssh-connection");
  774. /* service requested */
  775. put_stringz(s->pktout, "publickey"); /* method */
  776. put_bool(s->pktout, TRUE); /* signature follows */
  777. put_stringz(s->pktout, ssh_key_ssh_id(key->key));
  778. pkblob = strbuf_new();
  779. ssh_key_public_blob(key->key, BinarySink_UPCAST(pkblob));
  780. put_string(s->pktout, pkblob->s, pkblob->len);
  781. /*
  782. * The data to be signed is:
  783. *
  784. * string session-id
  785. *
  786. * followed by everything so far placed in the
  787. * outgoing packet.
  788. */
  789. sigdata = strbuf_new();
  790. ssh2_userauth_add_session_id(s, sigdata);
  791. put_data(sigdata, s->pktout->data + 5,
  792. s->pktout->length - 5);
  793. sigblob = strbuf_new();
  794. ssh_key_sign(key->key, sigdata->s, sigdata->len,
  795. BinarySink_UPCAST(sigblob));
  796. strbuf_free(sigdata);
  797. ssh2_userauth_add_sigblob(
  798. s, s->pktout, make_ptrlen(pkblob->s, pkblob->len),
  799. make_ptrlen(sigblob->s, sigblob->len));
  800. strbuf_free(pkblob);
  801. strbuf_free(sigblob);
  802. pq_push(s->ppl.out_pq, s->pktout);
  803. ppl_logevent(("Sent public key signature"));
  804. s->type = AUTH_TYPE_PUBLICKEY;
  805. ssh_key_free(key->key);
  806. sfree(key->comment);
  807. sfree(key);
  808. }
  809. #ifndef NO_GSSAPI
  810. } else if (s->can_gssapi && !s->tried_gssapi) {
  811. /* gssapi-with-mic authentication */
  812. ptrlen data;
  813. s->type = AUTH_TYPE_GSSAPI;
  814. s->tried_gssapi = TRUE;
  815. s->ppl.bpp->pls->actx = SSH2_PKTCTX_GSSAPI;
  816. if (s->shgss->lib->gsslogmsg)
  817. ppl_logevent(("%s", s->shgss->lib->gsslogmsg));
  818. /* Sending USERAUTH_REQUEST with "gssapi-with-mic" method */
  819. ppl_logevent(("Trying gssapi-with-mic..."));
  820. s->pktout = ssh_bpp_new_pktout(
  821. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  822. put_stringz(s->pktout, s->username);
  823. put_stringz(s->pktout, "ssh-connection");
  824. put_stringz(s->pktout, "gssapi-with-mic");
  825. ppl_logevent(("Attempting GSSAPI authentication"));
  826. /* add mechanism info */
  827. s->shgss->lib->indicate_mech(s->shgss->lib, &s->gss_buf);
  828. /* number of GSSAPI mechanisms */
  829. put_uint32(s->pktout, 1);
  830. /* length of OID + 2 */
  831. put_uint32(s->pktout, s->gss_buf.length + 2);
  832. put_byte(s->pktout, SSH2_GSS_OIDTYPE);
  833. /* length of OID */
  834. put_byte(s->pktout, s->gss_buf.length);
  835. put_data(s->pktout, s->gss_buf.value, s->gss_buf.length);
  836. pq_push(s->ppl.out_pq, s->pktout);
  837. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  838. if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_RESPONSE) {
  839. ppl_logevent(("GSSAPI authentication request refused"));
  840. pq_push_front(s->ppl.in_pq, pktin);
  841. continue;
  842. }
  843. /* check returned packet ... */
  844. data = get_string(pktin);
  845. s->gss_rcvtok.value = (char *)data.ptr;
  846. s->gss_rcvtok.length = data.len;
  847. if (s->gss_rcvtok.length != s->gss_buf.length + 2 ||
  848. ((char *)s->gss_rcvtok.value)[0] != SSH2_GSS_OIDTYPE ||
  849. ((char *)s->gss_rcvtok.value)[1] != s->gss_buf.length ||
  850. memcmp((char *)s->gss_rcvtok.value + 2,
  851. s->gss_buf.value,s->gss_buf.length) ) {
  852. ppl_logevent(("GSSAPI authentication - wrong response "
  853. "from server"));
  854. continue;
  855. }
  856. /* Import server name if not cached from KEX */
  857. if (s->shgss->srv_name == GSS_C_NO_NAME) {
  858. // WINSCP
  859. const char * fullhostname = s->fullhostname;
  860. if (s->loghost[0] != '\0')
  861. {
  862. fullhostname = s->loghost;
  863. }
  864. // /WINSCP
  865. s->gss_stat = s->shgss->lib->import_name(
  866. s->shgss->lib, fullhostname, &s->shgss->srv_name); // WINSCP
  867. if (s->gss_stat != SSH_GSS_OK) {
  868. if (s->gss_stat == SSH_GSS_BAD_HOST_NAME)
  869. ppl_logevent(("GSSAPI import name failed -"
  870. " Bad service name"));
  871. else
  872. ppl_logevent(("GSSAPI import name failed"));
  873. continue;
  874. }
  875. }
  876. /* Allocate our gss_ctx */
  877. s->gss_stat = s->shgss->lib->acquire_cred(
  878. s->shgss->lib, &s->shgss->ctx, NULL);
  879. if (s->gss_stat != SSH_GSS_OK) {
  880. ppl_logevent(("GSSAPI authentication failed to get "
  881. "credentials"));
  882. continue;
  883. }
  884. /* initial tokens are empty */
  885. SSH_GSS_CLEAR_BUF(&s->gss_rcvtok);
  886. SSH_GSS_CLEAR_BUF(&s->gss_sndtok);
  887. /* now enter the loop */
  888. do {
  889. /*
  890. * When acquire_cred yields no useful expiration, go with
  891. * the service ticket expiration.
  892. */
  893. s->gss_stat = s->shgss->lib->init_sec_context
  894. (s->shgss->lib,
  895. &s->shgss->ctx,
  896. s->shgss->srv_name,
  897. s->gssapi_fwd,
  898. &s->gss_rcvtok,
  899. &s->gss_sndtok,
  900. NULL,
  901. NULL);
  902. if (s->gss_stat!=SSH_GSS_S_COMPLETE &&
  903. s->gss_stat!=SSH_GSS_S_CONTINUE_NEEDED) {
  904. ppl_logevent(("GSSAPI authentication initialisation "
  905. "failed"));
  906. if (s->shgss->lib->display_status(s->shgss->lib,
  907. s->shgss->ctx, &s->gss_buf) == SSH_GSS_OK) {
  908. ppl_logevent(("%s", (char *)s->gss_buf.value));
  909. sfree(s->gss_buf.value);
  910. }
  911. pq_push_front(s->ppl.in_pq, pktin);
  912. break;
  913. }
  914. ppl_logevent(("GSSAPI authentication initialised"));
  915. /*
  916. * Client and server now exchange tokens until GSSAPI
  917. * no longer says CONTINUE_NEEDED
  918. */
  919. if (s->gss_sndtok.length != 0) {
  920. s->pktout =
  921. ssh_bpp_new_pktout(
  922. s->ppl.bpp, SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
  923. put_string(s->pktout,
  924. s->gss_sndtok.value, s->gss_sndtok.length);
  925. pq_push(s->ppl.out_pq, s->pktout);
  926. s->shgss->lib->free_tok(s->shgss->lib, &s->gss_sndtok);
  927. }
  928. if (s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED) {
  929. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  930. if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_TOKEN) {
  931. ppl_logevent(("GSSAPI authentication -"
  932. " bad server response"));
  933. s->gss_stat = SSH_GSS_FAILURE;
  934. pq_push_front(s->ppl.in_pq, pktin);
  935. break;
  936. }
  937. data = get_string(pktin);
  938. s->gss_rcvtok.value = (char *)data.ptr;
  939. s->gss_rcvtok.length = data.len;
  940. }
  941. } while (s-> gss_stat == SSH_GSS_S_CONTINUE_NEEDED);
  942. if (s->gss_stat != SSH_GSS_OK) {
  943. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  944. continue;
  945. }
  946. ppl_logevent(("GSSAPI authentication loop finished OK"));
  947. /* Now send the MIC */
  948. s->pktout = ssh2_userauth_gss_packet(s, "gssapi-with-mic");
  949. pq_push(s->ppl.out_pq, s->pktout);
  950. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  951. continue;
  952. #endif
  953. } else if (s->can_keyb_inter && !s->kbd_inter_refused) {
  954. /*
  955. * Keyboard-interactive authentication.
  956. */
  957. s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE;
  958. s->ppl.bpp->pls->actx = SSH2_PKTCTX_KBDINTER;
  959. s->pktout = ssh_bpp_new_pktout(
  960. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  961. put_stringz(s->pktout, s->username);
  962. put_stringz(s->pktout, "ssh-connection");
  963. /* service requested */
  964. put_stringz(s->pktout, "keyboard-interactive");
  965. /* method */
  966. put_stringz(s->pktout, ""); /* lang */
  967. put_stringz(s->pktout, ""); /* submethods */
  968. pq_push(s->ppl.out_pq, s->pktout);
  969. ppl_logevent(("Attempting keyboard-interactive authentication"));
  970. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  971. if (pktin->type != SSH2_MSG_USERAUTH_INFO_REQUEST) {
  972. /* Server is not willing to do keyboard-interactive
  973. * at all (or, bizarrely but legally, accepts the
  974. * user without actually issuing any prompts).
  975. * Give up on it entirely. */
  976. pq_push_front(s->ppl.in_pq, pktin);
  977. s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET;
  978. s->kbd_inter_refused = TRUE; /* don't try it again */
  979. continue;
  980. }
  981. /*
  982. * Loop while the server continues to send INFO_REQUESTs.
  983. */
  984. while (pktin->type == SSH2_MSG_USERAUTH_INFO_REQUEST) {
  985. ptrlen name, inst;
  986. int i;
  987. /*
  988. * We've got a fresh USERAUTH_INFO_REQUEST.
  989. * Get the preamble and start building a prompt.
  990. */
  991. name = get_string(pktin);
  992. inst = get_string(pktin);
  993. get_string(pktin); /* skip language tag */
  994. s->cur_prompt = new_prompts(s->ppl.frontend);
  995. s->cur_prompt->to_server = TRUE;
  996. /*
  997. * Get any prompt(s) from the packet.
  998. */
  999. s->num_prompts = get_uint32(pktin);
  1000. for (i = 0; i < s->num_prompts; i++) {
  1001. ptrlen prompt;
  1002. int echo;
  1003. static char noprompt[] =
  1004. "<server failed to send prompt>: ";
  1005. prompt = get_string(pktin);
  1006. echo = get_bool(pktin);
  1007. if (!prompt.len) {
  1008. prompt.ptr = noprompt;
  1009. prompt.len = lenof(noprompt)-1;
  1010. }
  1011. add_prompt(s->cur_prompt, mkstr(prompt), echo);
  1012. }
  1013. if (name.len) {
  1014. /* FIXME: better prefix to distinguish from
  1015. * local prompts? */
  1016. s->cur_prompt->name =
  1017. dupprintf("SSH server: %.*s", PTRLEN_PRINTF(name));
  1018. s->cur_prompt->name_reqd = TRUE;
  1019. } else {
  1020. s->cur_prompt->name =
  1021. dupstr("SSH server authentication");
  1022. s->cur_prompt->name_reqd = FALSE;
  1023. }
  1024. /* We add a prefix to try to make it clear that a prompt
  1025. * has come from the server.
  1026. * FIXME: ugly to print "Using..." in prompt _every_
  1027. * time round. Can this be done more subtly? */
  1028. /* Special case: for reasons best known to themselves,
  1029. * some servers send k-i requests with no prompts and
  1030. * nothing to display. Keep quiet in this case. */
  1031. if (s->num_prompts || name.len || inst.len) {
  1032. s->cur_prompt->instruction =
  1033. dupprintf("Using keyboard-interactive "
  1034. "authentication.%s%.*s",
  1035. inst.len ? "\n" : "",
  1036. PTRLEN_PRINTF(inst));
  1037. s->cur_prompt->instr_reqd = TRUE;
  1038. } else {
  1039. s->cur_prompt->instr_reqd = FALSE;
  1040. }
  1041. /*
  1042. * Display any instructions, and get the user's
  1043. * response(s).
  1044. */
  1045. s->userpass_ret = get_userpass_input(s->cur_prompt, NULL);
  1046. while (1) {
  1047. while (s->userpass_ret < 0 &&
  1048. bufchain_size(s->ppl.user_input) > 0)
  1049. s->userpass_ret = get_userpass_input(
  1050. s->cur_prompt, s->ppl.user_input);
  1051. if (s->userpass_ret >= 0)
  1052. break;
  1053. s->want_user_input = TRUE;
  1054. crReturnV;
  1055. s->want_user_input = FALSE;
  1056. }
  1057. if (!s->userpass_ret) {
  1058. /*
  1059. * Failed to get responses. Terminate.
  1060. */
  1061. free_prompts(s->cur_prompt);
  1062. ssh_bpp_queue_disconnect(
  1063. s->ppl.bpp, "Unable to authenticate",
  1064. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1065. ssh_user_close(s->ppl.ssh, "User aborted during "
  1066. "keyboard-interactive authentication");
  1067. return;
  1068. }
  1069. /*
  1070. * Send the response(s) to the server.
  1071. */
  1072. s->pktout = ssh_bpp_new_pktout(
  1073. s->ppl.bpp, SSH2_MSG_USERAUTH_INFO_RESPONSE);
  1074. put_uint32(s->pktout, s->num_prompts);
  1075. for (i=0; i < s->num_prompts; i++) {
  1076. put_stringz(s->pktout,
  1077. s->cur_prompt->prompts[i]->result);
  1078. }
  1079. s->pktout->minlen = 256;
  1080. pq_push(s->ppl.out_pq, s->pktout);
  1081. /*
  1082. * Free the prompts structure from this iteration.
  1083. * If there's another, a new one will be allocated
  1084. * when we return to the top of this while loop.
  1085. */
  1086. free_prompts(s->cur_prompt);
  1087. /*
  1088. * Get the next packet in case it's another
  1089. * INFO_REQUEST.
  1090. */
  1091. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1092. }
  1093. /*
  1094. * We should have SUCCESS or FAILURE now.
  1095. */
  1096. pq_push_front(s->ppl.in_pq, pktin);
  1097. } else if (s->can_passwd) {
  1098. /*
  1099. * Plain old password authentication.
  1100. */
  1101. int changereq_first_time; /* not live over crReturn */
  1102. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PASSWORD;
  1103. // WINSCP
  1104. if (s->change_password != 0)
  1105. {
  1106. s->password = dupstr("");
  1107. s->type = AUTH_TYPE_PASSWORD;
  1108. }
  1109. else
  1110. {
  1111. // no indentation to ease merges
  1112. // /WINSCP
  1113. s->cur_prompt = new_prompts(s->ppl.frontend);
  1114. s->cur_prompt->to_server = TRUE;
  1115. s->cur_prompt->name = dupstr("SSH password");
  1116. add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ",
  1117. s->username, s->hostname),
  1118. FALSE);
  1119. s->userpass_ret = get_userpass_input(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 = get_userpass_input(
  1124. 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 password "
  1140. "authentication");
  1141. return;
  1142. }
  1143. /*
  1144. * Squirrel away the password. (We may need it later if
  1145. * asked to change it.)
  1146. */
  1147. s->password = dupstr(s->cur_prompt->prompts[0]->result);
  1148. free_prompts(s->cur_prompt);
  1149. /*
  1150. * Send the password packet.
  1151. *
  1152. * We pad out the password packet to 256 bytes to make
  1153. * it harder for an attacker to find the length of the
  1154. * user's password.
  1155. *
  1156. * Anyone using a password longer than 256 bytes
  1157. * probably doesn't have much to worry about from
  1158. * people who find out how long their password is!
  1159. */
  1160. s->pktout = ssh_bpp_new_pktout(
  1161. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1162. put_stringz(s->pktout, s->username);
  1163. put_stringz(s->pktout, "ssh-connection");
  1164. /* service requested */
  1165. put_stringz(s->pktout, "password");
  1166. put_bool(s->pktout, FALSE);
  1167. put_stringz(s->pktout, s->password);
  1168. s->pktout->minlen = 256;
  1169. pq_push(s->ppl.out_pq, s->pktout);
  1170. ppl_logevent(("Sent password"));
  1171. s->type = AUTH_TYPE_PASSWORD;
  1172. /*
  1173. * Wait for next packet, in case it's a password change
  1174. * request.
  1175. */
  1176. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1177. } // WINSCP
  1178. changereq_first_time = TRUE;
  1179. while ((pktin->type == SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ) ||
  1180. (s->change_password != 0)) { // WINSCP
  1181. /*
  1182. * We're being asked for a new password
  1183. * (perhaps not for the first time).
  1184. * Loop until the server accepts it.
  1185. */
  1186. int got_new = FALSE; /* not live over crReturn */
  1187. ptrlen prompt; /* not live over crReturn */
  1188. if (s->change_password == 0) // WINSCP
  1189. {
  1190. const char *msg;
  1191. if (changereq_first_time)
  1192. msg = "Server requested password change";
  1193. else
  1194. msg = "Server rejected new password";
  1195. ppl_logevent(("%s", msg));
  1196. ppl_printf(("%s\r\n", msg));
  1197. }
  1198. s->change_password = 0; // WINSCP
  1199. prompt = get_string(pktin);
  1200. s->cur_prompt = new_prompts(s->ppl.frontend);
  1201. s->cur_prompt->to_server = TRUE;
  1202. s->cur_prompt->name = dupstr("New SSH password");
  1203. s->cur_prompt->instruction = mkstr(prompt);
  1204. s->cur_prompt->instr_reqd = TRUE;
  1205. /*
  1206. * There's no explicit requirement in the protocol
  1207. * for the "old" passwords in the original and
  1208. * password-change messages to be the same, and
  1209. * apparently some Cisco kit supports password change
  1210. * by the user entering a blank password originally
  1211. * and the real password subsequently, so,
  1212. * reluctantly, we prompt for the old password again.
  1213. *
  1214. * (On the other hand, some servers don't even bother
  1215. * to check this field.)
  1216. */
  1217. add_prompt(s->cur_prompt,
  1218. dupstr("Current password (blank for previously entered password): "),
  1219. FALSE);
  1220. add_prompt(s->cur_prompt, dupstr("Enter new password: "),
  1221. FALSE);
  1222. add_prompt(s->cur_prompt, dupstr("Confirm new password: "),
  1223. FALSE);
  1224. /*
  1225. * Loop until the user manages to enter the same
  1226. * password twice.
  1227. */
  1228. while (!got_new) {
  1229. s->userpass_ret = get_userpass_input(
  1230. s->cur_prompt, NULL);
  1231. while (1) {
  1232. while (s->userpass_ret < 0 &&
  1233. bufchain_size(s->ppl.user_input) > 0)
  1234. s->userpass_ret = get_userpass_input(
  1235. s->cur_prompt, s->ppl.user_input);
  1236. if (s->userpass_ret >= 0)
  1237. break;
  1238. s->want_user_input = TRUE;
  1239. crReturnV;
  1240. s->want_user_input = FALSE;
  1241. }
  1242. if (!s->userpass_ret) {
  1243. /*
  1244. * Failed to get responses. Terminate.
  1245. */
  1246. /* burn the evidence */
  1247. free_prompts(s->cur_prompt);
  1248. smemclr(s->password, strlen(s->password));
  1249. sfree(s->password);
  1250. ssh_bpp_queue_disconnect(
  1251. s->ppl.bpp, "Unable to authenticate",
  1252. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1253. ssh_user_close(s->ppl.ssh, "User aborted during "
  1254. "password changing");
  1255. return;
  1256. }
  1257. /*
  1258. * If the user specified a new original password
  1259. * (IYSWIM), overwrite any previously specified
  1260. * one.
  1261. * (A side effect is that the user doesn't have to
  1262. * re-enter it if they louse up the new password.)
  1263. */
  1264. if (s->cur_prompt->prompts[0]->result[0]) {
  1265. smemclr(s->password, strlen(s->password));
  1266. /* burn the evidence */
  1267. sfree(s->password);
  1268. s->password =
  1269. dupstr(s->cur_prompt->prompts[0]->result);
  1270. }
  1271. /*
  1272. * Check the two new passwords match.
  1273. */
  1274. got_new = (strcmp(s->cur_prompt->prompts[1]->result,
  1275. s->cur_prompt->prompts[2]->result)
  1276. == 0);
  1277. if (!got_new)
  1278. /* They don't. Silly user. */
  1279. ppl_printf(("Passwords do not match\r\n"));
  1280. }
  1281. /*
  1282. * Send the new password (along with the old one).
  1283. * (see above for padding rationale)
  1284. */
  1285. s->pktout = ssh_bpp_new_pktout(
  1286. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1287. put_stringz(s->pktout, s->username);
  1288. put_stringz(s->pktout, "ssh-connection");
  1289. /* service requested */
  1290. put_stringz(s->pktout, "password");
  1291. put_bool(s->pktout, TRUE);
  1292. put_stringz(s->pktout, s->password);
  1293. put_stringz(s->pktout,
  1294. s->cur_prompt->prompts[1]->result);
  1295. free_prompts(s->cur_prompt);
  1296. s->pktout->minlen = 256;
  1297. pq_push(s->ppl.out_pq, s->pktout);
  1298. ppl_logevent(("Sent new password"));
  1299. /*
  1300. * Now see what the server has to say about it.
  1301. * (If it's CHANGEREQ again, it's not happy with the
  1302. * new password.)
  1303. */
  1304. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1305. changereq_first_time = FALSE;
  1306. }
  1307. /*
  1308. * We need to reexamine the current pktin at the top
  1309. * of the loop. Either:
  1310. * - we weren't asked to change password at all, in
  1311. * which case it's a SUCCESS or FAILURE with the
  1312. * usual meaning
  1313. * - we sent a new password, and the server was
  1314. * either OK with it (SUCCESS or FAILURE w/partial
  1315. * success) or unhappy with the _old_ password
  1316. * (FAILURE w/o partial success)
  1317. * In any of these cases, we go back to the top of
  1318. * the loop and start again.
  1319. */
  1320. pq_push_front(s->ppl.in_pq, pktin);
  1321. /*
  1322. * We don't need the old password any more, in any
  1323. * case. Burn the evidence.
  1324. */
  1325. smemclr(s->password, strlen(s->password));
  1326. sfree(s->password);
  1327. } else {
  1328. ssh_bpp_queue_disconnect(
  1329. s->ppl.bpp,
  1330. "No supported authentication methods available",
  1331. SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE);
  1332. ssh_sw_abort(s->ppl.ssh, "No supported authentication methods "
  1333. "available (server sent: %.*s)",
  1334. PTRLEN_PRINTF(methods));
  1335. return;
  1336. }
  1337. }
  1338. try_new_username:;
  1339. }
  1340. userauth_success:
  1341. /*
  1342. * We've just received USERAUTH_SUCCESS, and we haven't sent
  1343. * any packets since. Signal the transport layer to consider
  1344. * doing an immediate rekey, if it has any reason to want to.
  1345. */
  1346. ssh2_transport_notify_auth_done(s->transport_layer);
  1347. /*
  1348. * Finally, hand over to our successor layer, and return
  1349. * immediately without reaching the crFinishV: ssh_ppl_replace
  1350. * will have freed us, so crFinishV's zeroing-out of crState would
  1351. * be a use-after-free bug.
  1352. */
  1353. {
  1354. PacketProtocolLayer *successor = s->successor_layer;
  1355. s->successor_layer = NULL; /* avoid freeing it ourself */
  1356. ssh_ppl_replace(&s->ppl, successor);
  1357. return; /* we've just freed s, so avoid even touching s->crState */
  1358. }
  1359. crFinishV;
  1360. }
  1361. static void ssh2_userauth_add_session_id(
  1362. struct ssh2_userauth_state *s, strbuf *sigdata)
  1363. {
  1364. if (s->ppl.remote_bugs & BUG_SSH2_PK_SESSIONID) {
  1365. put_data(sigdata, s->session_id.ptr, s->session_id.len);
  1366. } else {
  1367. put_stringpl(sigdata, s->session_id);
  1368. }
  1369. }
  1370. static void ssh2_userauth_agent_query(
  1371. struct ssh2_userauth_state *s, strbuf *req)
  1372. {
  1373. void *response;
  1374. int response_len;
  1375. sfree(s->agent_response_to_free);
  1376. s->agent_response_to_free = NULL;
  1377. s->auth_agent_query = agent_query(req, &response, &response_len,
  1378. ssh2_userauth_agent_callback, s);
  1379. if (!s->auth_agent_query)
  1380. ssh2_userauth_agent_callback(s, response, response_len);
  1381. }
  1382. static void ssh2_userauth_agent_callback(void *uav, void *reply, int replylen)
  1383. {
  1384. struct ssh2_userauth_state *s = (struct ssh2_userauth_state *)uav;
  1385. s->auth_agent_query = NULL;
  1386. s->agent_response_to_free = reply;
  1387. s->agent_response = make_ptrlen(reply, replylen);
  1388. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1389. }
  1390. /*
  1391. * Helper function to add an SSH-2 signature blob to a packet. Expects
  1392. * to be shown the public key blob as well as the signature blob.
  1393. * Normally just appends the sig blob unmodified as a string, except
  1394. * that it optionally breaks it open and fiddle with it to work around
  1395. * BUG_SSH2_RSA_PADDING.
  1396. */
  1397. static void ssh2_userauth_add_sigblob(
  1398. struct ssh2_userauth_state *s, PktOut *pkt, ptrlen pkblob, ptrlen sigblob)
  1399. {
  1400. BinarySource pk[1], sig[1];
  1401. BinarySource_BARE_INIT(pk, pkblob.ptr, pkblob.len);
  1402. BinarySource_BARE_INIT(sig, sigblob.ptr, sigblob.len);
  1403. /* dmemdump(pkblob, pkblob_len); */
  1404. /* dmemdump(sigblob, sigblob_len); */
  1405. /*
  1406. * See if this is in fact an ssh-rsa signature and a buggy
  1407. * server; otherwise we can just do this the easy way.
  1408. */
  1409. if ((s->ppl.remote_bugs & BUG_SSH2_RSA_PADDING) &&
  1410. ptrlen_eq_string(get_string(pk), "ssh-rsa") &&
  1411. ptrlen_eq_string(get_string(sig), "ssh-rsa")) {
  1412. ptrlen mod_mp, sig_mp;
  1413. size_t sig_prefix_len;
  1414. /*
  1415. * Find the modulus and signature integers.
  1416. */
  1417. get_string(pk); /* skip over exponent */
  1418. mod_mp = get_string(pk); /* remember modulus */
  1419. sig_prefix_len = sig->pos;
  1420. sig_mp = get_string(sig);
  1421. if (get_err(pk) || get_err(sig))
  1422. goto give_up;
  1423. /*
  1424. * Find the byte length of the modulus, not counting leading
  1425. * zeroes.
  1426. */
  1427. while (mod_mp.len > 0 && *(const char *)mod_mp.ptr == 0) {
  1428. mod_mp.len--;
  1429. mod_mp.ptr = (const char *)mod_mp.ptr + 1;
  1430. }
  1431. /* debug(("modulus length is %d\n", len)); */
  1432. /* debug(("signature length is %d\n", siglen)); */
  1433. if (mod_mp.len != sig_mp.len) {
  1434. strbuf *substr = strbuf_new();
  1435. put_data(substr, sigblob.ptr, sig_prefix_len);
  1436. put_uint32(substr, mod_mp.len);
  1437. put_padding(substr, mod_mp.len - sig_mp.len, 0);
  1438. put_data(substr, sig_mp.ptr, sig_mp.len);
  1439. put_stringsb(pkt, substr);
  1440. return;
  1441. }
  1442. /* Otherwise fall through and do it the easy way. We also come
  1443. * here as a fallback if we discover above that the key blob
  1444. * is misformatted in some way. */
  1445. give_up:;
  1446. }
  1447. put_stringpl(pkt, sigblob);
  1448. }
  1449. #ifndef NO_GSSAPI
  1450. static PktOut *ssh2_userauth_gss_packet(
  1451. struct ssh2_userauth_state *s, const char *authtype)
  1452. {
  1453. strbuf *sb;
  1454. PktOut *p;
  1455. Ssh_gss_buf buf;
  1456. Ssh_gss_buf mic;
  1457. /*
  1458. * The mic is computed over the session id + intended
  1459. * USERAUTH_REQUEST packet.
  1460. */
  1461. sb = strbuf_new();
  1462. put_stringpl(sb, s->session_id);
  1463. put_byte(sb, SSH2_MSG_USERAUTH_REQUEST);
  1464. put_stringz(sb, s->username);
  1465. put_stringz(sb, "ssh-connection");
  1466. put_stringz(sb, authtype);
  1467. /* Compute the mic */
  1468. buf.value = sb->s;
  1469. buf.length = sb->len;
  1470. s->shgss->lib->get_mic(s->shgss->lib, s->shgss->ctx, &buf, &mic);
  1471. strbuf_free(sb);
  1472. /* Now we can build the real packet */
  1473. if (strcmp(authtype, "gssapi-with-mic") == 0) {
  1474. p = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_GSSAPI_MIC);
  1475. } else {
  1476. p = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1477. put_stringz(p, s->username);
  1478. put_stringz(p, "ssh-connection");
  1479. put_stringz(p, authtype);
  1480. }
  1481. put_string(p, mic.value, mic.length);
  1482. return p;
  1483. }
  1484. #endif
  1485. static int ssh2_userauth_get_specials(
  1486. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  1487. {
  1488. /* No specials provided by this layer. */
  1489. return FALSE;
  1490. }
  1491. static void ssh2_userauth_special_cmd(PacketProtocolLayer *ppl,
  1492. SessionSpecialCode code, int arg)
  1493. {
  1494. /* No specials provided by this layer. */
  1495. }
  1496. static int ssh2_userauth_want_user_input(PacketProtocolLayer *ppl)
  1497. {
  1498. struct ssh2_userauth_state *s =
  1499. container_of(ppl, struct ssh2_userauth_state, ppl);
  1500. return s->want_user_input;
  1501. }
  1502. static void ssh2_userauth_got_user_input(PacketProtocolLayer *ppl)
  1503. {
  1504. struct ssh2_userauth_state *s =
  1505. container_of(ppl, struct ssh2_userauth_state, ppl);
  1506. if (s->want_user_input)
  1507. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1508. }
  1509. static void ssh2_userauth_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  1510. {
  1511. struct ssh2_userauth_state *s =
  1512. container_of(ppl, struct ssh2_userauth_state, ppl);
  1513. ssh_ppl_reconfigure(s->successor_layer, conf);
  1514. }