ssh2userauth.c 77 KB

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