ssh2userauth.c 70 KB

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