ssh2userauth.c 82 KB

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