ssh2userauth.c 72 KB

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