ssh2userauth.c 78 KB

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