ssh2userauth.c 70 KB

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