ssh2userauth.c 69 KB

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