ssh2userauth.c 80 KB

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