ssh2userauth.c 82 KB

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