ssh2userauth.c 78 KB

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