ssh2userauth.c 80 KB

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