ssh2userauth.c 85 KB

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