userauth2-client.c 80 KB

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