ssh2userauth.c 84 KB

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