ssh1login.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * Packet protocol layer for the SSH-1 login phase (combining what
  3. * SSH-2 would think of as key exchange and user authentication).
  4. */
  5. #include <assert.h>
  6. #include "putty.h"
  7. #include "ssh.h"
  8. #include "mpint.h"
  9. #include "sshbpp.h"
  10. #include "sshppl.h"
  11. #include "sshcr.h"
  12. typedef struct agent_key {
  13. RSAKey key;
  14. strbuf *comment;
  15. ptrlen blob; /* only used during initial parsing of agent response */
  16. } agent_key;
  17. struct ssh1_login_state {
  18. int crState;
  19. PacketProtocolLayer *successor_layer;
  20. Conf *conf;
  21. char *savedhost;
  22. int savedport;
  23. bool try_agent_auth, is_trivial_auth;
  24. int remote_protoflags;
  25. int local_protoflags;
  26. unsigned char session_key[32];
  27. char *username;
  28. agent_pending_query *auth_agent_query;
  29. int len;
  30. unsigned char *rsabuf;
  31. unsigned long supported_ciphers_mask, supported_auths_mask;
  32. bool tried_publickey, tried_agent;
  33. bool tis_auth_refused, ccard_auth_refused;
  34. unsigned char cookie[8];
  35. unsigned char session_id[16];
  36. int cipher_type;
  37. strbuf *publickey_blob;
  38. char *publickey_comment;
  39. bool privatekey_available, privatekey_encrypted;
  40. prompts_t *cur_prompt;
  41. int userpass_ret;
  42. char c;
  43. int pwpkt_type;
  44. void *agent_response_to_free;
  45. ptrlen agent_response;
  46. BinarySource asrc[1]; /* response from SSH agent */
  47. size_t agent_keys_len;
  48. agent_key *agent_keys;
  49. size_t agent_key_index, agent_key_limit;
  50. bool authed;
  51. RSAKey key;
  52. int dlgret;
  53. Filename *keyfile;
  54. RSAKey servkey, hostkey;
  55. bool want_user_input;
  56. StripCtrlChars *tis_scc;
  57. bool tis_scc_initialised;
  58. PacketProtocolLayer ppl;
  59. };
  60. static void ssh1_login_free(PacketProtocolLayer *);
  61. static void ssh1_login_process_queue(PacketProtocolLayer *);
  62. static void ssh1_login_dialog_callback(void *, int);
  63. static void ssh1_login_special_cmd(PacketProtocolLayer *ppl,
  64. SessionSpecialCode code, int arg);
  65. static bool ssh1_login_want_user_input(PacketProtocolLayer *ppl);
  66. static void ssh1_login_got_user_input(PacketProtocolLayer *ppl);
  67. static void ssh1_login_reconfigure(PacketProtocolLayer *ppl, Conf *conf);
  68. static const PacketProtocolLayerVtable ssh1_login_vtable = {
  69. .free = ssh1_login_free,
  70. .process_queue = ssh1_login_process_queue,
  71. .get_specials = ssh1_common_get_specials,
  72. .special_cmd = ssh1_login_special_cmd,
  73. .want_user_input = ssh1_login_want_user_input,
  74. .got_user_input = ssh1_login_got_user_input,
  75. .reconfigure = ssh1_login_reconfigure,
  76. .queued_data_size = ssh_ppl_default_queued_data_size,
  77. .name = NULL, /* no layer names in SSH-1 */
  78. };
  79. static void ssh1_login_agent_query(struct ssh1_login_state *s, strbuf *req);
  80. static void ssh1_login_agent_callback(void *loginv, void *reply, int replylen);
  81. PacketProtocolLayer *ssh1_login_new(
  82. Conf *conf, const char *host, int port,
  83. PacketProtocolLayer *successor_layer)
  84. {
  85. struct ssh1_login_state *s = snew(struct ssh1_login_state);
  86. memset(s, 0, sizeof(*s));
  87. s->ppl.vt = &ssh1_login_vtable;
  88. s->conf = conf_copy(conf);
  89. s->savedhost = dupstr(host);
  90. s->savedport = port;
  91. s->successor_layer = successor_layer;
  92. s->is_trivial_auth = true;
  93. return &s->ppl;
  94. }
  95. static void ssh1_login_free(PacketProtocolLayer *ppl)
  96. {
  97. struct ssh1_login_state *s =
  98. container_of(ppl, struct ssh1_login_state, ppl);
  99. if (s->successor_layer)
  100. ssh_ppl_free(s->successor_layer);
  101. conf_free(s->conf);
  102. sfree(s->savedhost);
  103. sfree(s->rsabuf);
  104. sfree(s->username);
  105. if (s->publickey_blob)
  106. strbuf_free(s->publickey_blob);
  107. sfree(s->publickey_comment);
  108. if (s->cur_prompt)
  109. free_prompts(s->cur_prompt);
  110. if (s->agent_keys) {
  111. for (size_t i = 0; i < s->agent_keys_len; i++) {
  112. freersakey(&s->agent_keys[i].key);
  113. strbuf_free(s->agent_keys[i].comment);
  114. }
  115. sfree(s->agent_keys);
  116. }
  117. sfree(s->agent_response_to_free);
  118. if (s->auth_agent_query)
  119. agent_cancel_query(s->auth_agent_query);
  120. sfree(s);
  121. }
  122. static bool ssh1_login_filter_queue(struct ssh1_login_state *s)
  123. {
  124. return ssh1_common_filter_queue(&s->ppl);
  125. }
  126. static PktIn *ssh1_login_pop(struct ssh1_login_state *s)
  127. {
  128. if (ssh1_login_filter_queue(s))
  129. return NULL;
  130. return pq_pop(s->ppl.in_pq);
  131. }
  132. static void ssh1_login_setup_tis_scc(struct ssh1_login_state *s);
  133. static void ssh1_login_process_queue(PacketProtocolLayer *ppl)
  134. {
  135. struct ssh1_login_state *s =
  136. container_of(ppl, struct ssh1_login_state, ppl);
  137. PktIn *pktin;
  138. PktOut *pkt;
  139. int i;
  140. /* Filter centrally handled messages off the front of the queue on
  141. * every entry to this coroutine, no matter where we're resuming
  142. * from, even if we're _not_ looping on pq_pop. That way we can
  143. * still proactively handle those messages even if we're waiting
  144. * for a user response. */
  145. if (ssh1_login_filter_queue(s))
  146. return;
  147. crBegin(s->crState);
  148. crMaybeWaitUntilV((pktin = ssh1_login_pop(s)) != NULL);
  149. if (pktin->type != SSH1_SMSG_PUBLIC_KEY) {
  150. ssh_proto_error(s->ppl.ssh, "Public key packet not received");
  151. return;
  152. }
  153. ppl_logevent("Received public keys");
  154. {
  155. ptrlen pl = get_data(pktin, 8);
  156. memcpy(s->cookie, pl.ptr, pl.len);
  157. }
  158. get_rsa_ssh1_pub(pktin, &s->servkey, RSA_SSH1_EXPONENT_FIRST);
  159. get_rsa_ssh1_pub(pktin, &s->hostkey, RSA_SSH1_EXPONENT_FIRST);
  160. s->hostkey.comment = NULL; /* avoid confusing rsa_ssh1_fingerprint */
  161. /*
  162. * Log the host key fingerprint.
  163. */
  164. if (!get_err(pktin)) {
  165. char *fingerprint = rsa_ssh1_fingerprint(&s->hostkey);
  166. ppl_logevent("Host key fingerprint is:");
  167. ppl_logevent(" %s", fingerprint);
  168. sfree(fingerprint);
  169. }
  170. s->remote_protoflags = get_uint32(pktin);
  171. s->supported_ciphers_mask = get_uint32(pktin);
  172. s->supported_auths_mask = get_uint32(pktin);
  173. if (get_err(pktin)) {
  174. ssh_proto_error(s->ppl.ssh, "Bad SSH-1 public key packet");
  175. return;
  176. }
  177. if ((s->ppl.remote_bugs & BUG_CHOKES_ON_RSA))
  178. s->supported_auths_mask &= ~(1 << SSH1_AUTH_RSA);
  179. s->local_protoflags =
  180. s->remote_protoflags & SSH1_PROTOFLAGS_SUPPORTED;
  181. s->local_protoflags |= SSH1_PROTOFLAG_SCREEN_NUMBER;
  182. ssh1_compute_session_id(s->session_id, s->cookie,
  183. &s->hostkey, &s->servkey);
  184. random_read(s->session_key, 32);
  185. /*
  186. * Verify that the `bits' and `bytes' parameters match.
  187. */
  188. if (s->hostkey.bits > s->hostkey.bytes * 8 ||
  189. s->servkey.bits > s->servkey.bytes * 8) {
  190. ssh_proto_error(s->ppl.ssh, "SSH-1 public keys were badly formatted");
  191. return;
  192. }
  193. s->len = 32;
  194. if (s->len < s->hostkey.bytes)
  195. s->len = s->hostkey.bytes;
  196. if (s->len < s->servkey.bytes)
  197. s->len = s->servkey.bytes;
  198. s->rsabuf = snewn(s->len, unsigned char);
  199. /*
  200. * Verify the host key.
  201. */
  202. {
  203. /*
  204. * First format the key into a string.
  205. */
  206. char *keystr = rsastr_fmt(&s->hostkey);
  207. char **fingerprints = rsa_ssh1_fake_all_fingerprints(&s->hostkey);
  208. /* First check against manually configured host keys. */
  209. s->dlgret = verify_ssh_manual_host_key(s->conf, fingerprints, NULL);
  210. if (s->dlgret == 0) { /* did not match */
  211. ssh2_free_all_fingerprints(fingerprints);
  212. sfree(keystr);
  213. ssh_proto_error(s->ppl.ssh, "Host key did not appear in manually "
  214. "configured list");
  215. return;
  216. } else if (s->dlgret < 0) { /* none configured; use standard handling */
  217. char *keydisp = ssh1_pubkey_str(&s->hostkey);
  218. s->dlgret = seat_verify_ssh_host_key(
  219. s->ppl.seat, s->savedhost, s->savedport, "rsa", keystr,
  220. keydisp, fingerprints, ssh1_login_dialog_callback, s);
  221. sfree(keydisp);
  222. ssh2_free_all_fingerprints(fingerprints);
  223. sfree(keystr);
  224. #ifdef FUZZING
  225. s->dlgret = 1;
  226. #endif
  227. crMaybeWaitUntilV(s->dlgret >= 0);
  228. if (s->dlgret == 0) {
  229. ssh_user_close(s->ppl.ssh,
  230. "User aborted at host key verification");
  231. return;
  232. }
  233. } else {
  234. ssh2_free_all_fingerprints(fingerprints);
  235. sfree(keystr);
  236. }
  237. }
  238. for (i = 0; i < 32; i++) {
  239. s->rsabuf[i] = s->session_key[i];
  240. if (i < 16)
  241. s->rsabuf[i] ^= s->session_id[i];
  242. }
  243. {
  244. RSAKey *smaller = (s->hostkey.bytes > s->servkey.bytes ?
  245. &s->servkey : &s->hostkey);
  246. RSAKey *larger = (s->hostkey.bytes > s->servkey.bytes ?
  247. &s->hostkey : &s->servkey);
  248. if (!rsa_ssh1_encrypt(s->rsabuf, 32, smaller) ||
  249. !rsa_ssh1_encrypt(s->rsabuf, smaller->bytes, larger)) {
  250. ssh_proto_error(s->ppl.ssh, "SSH-1 public key encryptions failed "
  251. "due to bad formatting");
  252. return;
  253. }
  254. }
  255. ppl_logevent("Encrypted session key");
  256. {
  257. bool cipher_chosen = false, warn = false;
  258. const char *cipher_string = NULL;
  259. int i;
  260. for (i = 0; !cipher_chosen && i < CIPHER_MAX; i++) {
  261. int next_cipher = conf_get_int_int(
  262. s->conf, CONF_ssh_cipherlist, i);
  263. if (next_cipher == CIPHER_WARN) {
  264. /* If/when we choose a cipher, warn about it */
  265. warn = true;
  266. } else if (next_cipher == CIPHER_AES) {
  267. /* XXX Probably don't need to mention this. */
  268. ppl_logevent("AES not supported in SSH-1, skipping");
  269. } else {
  270. switch (next_cipher) {
  271. case CIPHER_3DES: s->cipher_type = SSH1_CIPHER_3DES;
  272. cipher_string = "3DES"; break;
  273. case CIPHER_BLOWFISH: s->cipher_type = SSH1_CIPHER_BLOWFISH;
  274. cipher_string = "Blowfish"; break;
  275. case CIPHER_DES: s->cipher_type = SSH1_CIPHER_DES;
  276. cipher_string = "single-DES"; break;
  277. }
  278. if (s->supported_ciphers_mask & (1 << s->cipher_type))
  279. cipher_chosen = true;
  280. }
  281. }
  282. if (!cipher_chosen) {
  283. if ((s->supported_ciphers_mask & (1 << SSH1_CIPHER_3DES)) == 0) {
  284. ssh_proto_error(s->ppl.ssh, "Server violates SSH-1 protocol "
  285. "by not supporting 3DES encryption");
  286. } else {
  287. /* shouldn't happen */
  288. ssh_sw_abort(s->ppl.ssh, "No supported ciphers found");
  289. }
  290. return;
  291. }
  292. /* Warn about chosen cipher if necessary. */
  293. if (warn) {
  294. s->dlgret = seat_confirm_weak_crypto_primitive(
  295. s->ppl.seat, "cipher", cipher_string,
  296. ssh1_login_dialog_callback, s);
  297. crMaybeWaitUntilV(s->dlgret >= 0);
  298. if (s->dlgret == 0) {
  299. ssh_user_close(s->ppl.ssh, "User aborted at cipher warning");
  300. return;
  301. }
  302. }
  303. }
  304. switch (s->cipher_type) {
  305. case SSH1_CIPHER_3DES:
  306. ppl_logevent("Using 3DES encryption");
  307. break;
  308. case SSH1_CIPHER_DES:
  309. ppl_logevent("Using single-DES encryption");
  310. break;
  311. case SSH1_CIPHER_BLOWFISH:
  312. ppl_logevent("Using Blowfish encryption");
  313. break;
  314. }
  315. pkt = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_SESSION_KEY);
  316. put_byte(pkt, s->cipher_type);
  317. put_data(pkt, s->cookie, 8);
  318. put_uint16(pkt, s->len * 8);
  319. put_data(pkt, s->rsabuf, s->len);
  320. put_uint32(pkt, s->local_protoflags);
  321. pq_push(s->ppl.out_pq, pkt);
  322. ppl_logevent("Trying to enable encryption...");
  323. sfree(s->rsabuf);
  324. s->rsabuf = NULL;
  325. /*
  326. * Force the BPP to synchronously marshal all packets up to and
  327. * including the SESSION_KEY into wire format, before we turn on
  328. * crypto.
  329. */
  330. ssh_bpp_handle_output(s->ppl.bpp);
  331. {
  332. const ssh_cipheralg *cipher =
  333. (s->cipher_type == SSH1_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 :
  334. s->cipher_type == SSH1_CIPHER_DES ? &ssh_des : &ssh_3des_ssh1);
  335. ssh1_bpp_new_cipher(s->ppl.bpp, cipher, s->session_key);
  336. }
  337. freersakey(&s->servkey);
  338. freersakey(&s->hostkey);
  339. crMaybeWaitUntilV((pktin = ssh1_login_pop(s)) != NULL);
  340. if (pktin->type != SSH1_SMSG_SUCCESS) {
  341. ssh_proto_error(s->ppl.ssh, "Encryption not successfully enabled");
  342. return;
  343. }
  344. ppl_logevent("Successfully started encryption");
  345. if ((s->username = get_remote_username(s->conf)) == NULL) {
  346. s->cur_prompt = new_prompts();
  347. s->cur_prompt->to_server = true;
  348. s->cur_prompt->from_server = false;
  349. s->cur_prompt->name = dupstr("SSH login name");
  350. add_prompt(s->cur_prompt, dupstr("login as: "), true);
  351. s->userpass_ret = seat_get_userpass_input(
  352. s->ppl.seat, s->cur_prompt, NULL);
  353. while (1) {
  354. while (s->userpass_ret < 0 &&
  355. bufchain_size(s->ppl.user_input) > 0)
  356. s->userpass_ret = seat_get_userpass_input(
  357. s->ppl.seat, s->cur_prompt, s->ppl.user_input);
  358. if (s->userpass_ret >= 0)
  359. break;
  360. s->want_user_input = true;
  361. crReturnV;
  362. s->want_user_input = false;
  363. }
  364. if (!s->userpass_ret) {
  365. /*
  366. * Failed to get a username. Terminate.
  367. */
  368. ssh_user_close(s->ppl.ssh, "No username provided");
  369. return;
  370. }
  371. s->username = prompt_get_result(s->cur_prompt->prompts[0]);
  372. free_prompts(s->cur_prompt);
  373. s->cur_prompt = NULL;
  374. }
  375. pkt = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_USER);
  376. put_stringz(pkt, s->username);
  377. pq_push(s->ppl.out_pq, pkt);
  378. ppl_logevent("Sent username \"%s\"", s->username);
  379. if (seat_verbose(s->ppl.seat) || seat_interactive(s->ppl.seat))
  380. ppl_printf("Sent username \"%s\"\r\n", s->username);
  381. crMaybeWaitUntilV((pktin = ssh1_login_pop(s)) != NULL);
  382. if (!(s->supported_auths_mask & (1 << SSH1_AUTH_RSA))) {
  383. /* We must not attempt PK auth. Pretend we've already tried it. */
  384. s->tried_publickey = s->tried_agent = true;
  385. } else {
  386. s->tried_publickey = s->tried_agent = false;
  387. }
  388. s->tis_auth_refused = s->ccard_auth_refused = false;
  389. /*
  390. * Load the public half of any configured keyfile for later use.
  391. */
  392. s->keyfile = conf_get_filename(s->conf, CONF_keyfile);
  393. if (!filename_is_null(s->keyfile)) {
  394. int keytype;
  395. ppl_logevent("Reading key file \"%s\"", filename_to_str(s->keyfile));
  396. keytype = key_type(s->keyfile);
  397. if (keytype == SSH_KEYTYPE_SSH1 ||
  398. keytype == SSH_KEYTYPE_SSH1_PUBLIC) {
  399. const char *error;
  400. s->publickey_blob = strbuf_new();
  401. if (rsa1_loadpub_f(s->keyfile,
  402. BinarySink_UPCAST(s->publickey_blob),
  403. &s->publickey_comment, &error)) {
  404. s->privatekey_available = (keytype == SSH_KEYTYPE_SSH1);
  405. if (!s->privatekey_available)
  406. ppl_logevent("Key file contains public key only");
  407. s->privatekey_encrypted = rsa1_encrypted_f(s->keyfile, NULL);
  408. } else {
  409. ppl_logevent("Unable to load key (%s)", error);
  410. ppl_printf("Unable to load key file \"%s\" (%s)\r\n",
  411. filename_to_str(s->keyfile), error);
  412. strbuf_free(s->publickey_blob);
  413. s->publickey_blob = NULL;
  414. }
  415. } else {
  416. ppl_logevent("Unable to use this key file (%s)",
  417. key_type_to_str(keytype));
  418. ppl_printf("Unable to use key file \"%s\" (%s)\r\n",
  419. filename_to_str(s->keyfile),
  420. key_type_to_str(keytype));
  421. }
  422. }
  423. /* Check whether we're configured to try Pageant, and also whether
  424. * it's available. */
  425. s->try_agent_auth = (conf_get_bool(s->conf, CONF_tryagent) &&
  426. agent_exists());
  427. while (pktin->type == SSH1_SMSG_FAILURE) {
  428. s->pwpkt_type = SSH1_CMSG_AUTH_PASSWORD;
  429. if (s->try_agent_auth && !s->tried_agent) {
  430. /*
  431. * Attempt RSA authentication using Pageant.
  432. */
  433. s->authed = false;
  434. s->tried_agent = true;
  435. ppl_logevent("Pageant is running. Requesting keys.");
  436. /* Request the keys held by the agent. */
  437. {
  438. strbuf *request = strbuf_new_for_agent_query();
  439. put_byte(request, SSH1_AGENTC_REQUEST_RSA_IDENTITIES);
  440. ssh1_login_agent_query(s, request);
  441. strbuf_free(request);
  442. crMaybeWaitUntilV(!s->auth_agent_query);
  443. }
  444. BinarySource_BARE_INIT_PL(s->asrc, s->agent_response);
  445. get_uint32(s->asrc); /* skip length field */
  446. if (get_byte(s->asrc) == SSH1_AGENT_RSA_IDENTITIES_ANSWER) {
  447. size_t nkeys = get_uint32(s->asrc);
  448. size_t origpos = s->asrc->pos;
  449. /*
  450. * Check that the agent response is well formed.
  451. */
  452. for (size_t i = 0; i < nkeys; i++) {
  453. get_rsa_ssh1_pub(s->asrc, NULL, RSA_SSH1_EXPONENT_FIRST);
  454. get_string(s->asrc); /* comment */
  455. if (get_err(s->asrc)) {
  456. ppl_logevent("Pageant's response was truncated");
  457. goto parsed_agent_query;
  458. }
  459. }
  460. /*
  461. * Copy the list of public-key blobs out of the Pageant
  462. * response.
  463. */
  464. BinarySource_REWIND_TO(s->asrc, origpos);
  465. s->agent_keys_len = nkeys;
  466. s->agent_keys = snewn(s->agent_keys_len, agent_key);
  467. for (size_t i = 0; i < nkeys; i++) {
  468. memset(&s->agent_keys[i].key, 0,
  469. sizeof(s->agent_keys[i].key));
  470. const char *blobstart = get_ptr(s->asrc);
  471. get_rsa_ssh1_pub(s->asrc, &s->agent_keys[i].key,
  472. RSA_SSH1_EXPONENT_FIRST);
  473. const char *blobend = get_ptr(s->asrc);
  474. s->agent_keys[i].comment = strbuf_new();
  475. put_datapl(s->agent_keys[i].comment, get_string(s->asrc));
  476. s->agent_keys[i].blob = make_ptrlen(
  477. blobstart, blobend - blobstart);
  478. }
  479. ppl_logevent("Pageant has %"SIZEu" SSH-1 keys", nkeys);
  480. if (s->publickey_blob) {
  481. /*
  482. * If we've been given a specific public key blob,
  483. * filter the list of keys to try from the agent
  484. * down to only that one, or none if it's not
  485. * there.
  486. */
  487. ptrlen our_blob = ptrlen_from_strbuf(s->publickey_blob);
  488. size_t i;
  489. for (i = 0; i < nkeys; i++) {
  490. if (ptrlen_eq_ptrlen(our_blob, s->agent_keys[i].blob))
  491. break;
  492. }
  493. if (i < nkeys) {
  494. ppl_logevent("Pageant key #%"SIZEu" matches "
  495. "configured key file", i);
  496. s->agent_key_index = i;
  497. s->agent_key_limit = i+1;
  498. } else {
  499. ppl_logevent("Configured key file not in Pageant");
  500. s->agent_key_index = 0;
  501. s->agent_key_limit = 0;
  502. }
  503. } else {
  504. /*
  505. * Otherwise, try them all.
  506. */
  507. s->agent_key_index = 0;
  508. s->agent_key_limit = nkeys;
  509. }
  510. } else {
  511. ppl_logevent("Failed to get reply from Pageant");
  512. }
  513. parsed_agent_query:;
  514. for (; s->agent_key_index < s->agent_key_limit;
  515. s->agent_key_index++) {
  516. ppl_logevent("Trying Pageant key #%"SIZEu, s->agent_key_index);
  517. pkt = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_AUTH_RSA);
  518. put_mp_ssh1(pkt,
  519. s->agent_keys[s->agent_key_index].key.modulus);
  520. pq_push(s->ppl.out_pq, pkt);
  521. crMaybeWaitUntilV((pktin = ssh1_login_pop(s))
  522. != NULL);
  523. if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) {
  524. ppl_logevent("Key refused");
  525. continue;
  526. }
  527. ppl_logevent("Received RSA challenge");
  528. {
  529. mp_int *challenge = get_mp_ssh1(pktin);
  530. if (get_err(pktin)) {
  531. mp_free(challenge);
  532. ssh_proto_error(s->ppl.ssh, "Server's RSA challenge "
  533. "was badly formatted");
  534. return;
  535. }
  536. strbuf *agentreq = strbuf_new_for_agent_query();
  537. put_byte(agentreq, SSH1_AGENTC_RSA_CHALLENGE);
  538. rsa_ssh1_public_blob(
  539. BinarySink_UPCAST(agentreq),
  540. &s->agent_keys[s->agent_key_index].key,
  541. RSA_SSH1_EXPONENT_FIRST);
  542. put_mp_ssh1(agentreq, challenge);
  543. mp_free(challenge);
  544. put_data(agentreq, s->session_id, 16);
  545. put_uint32(agentreq, 1); /* response format */
  546. ssh1_login_agent_query(s, agentreq);
  547. strbuf_free(agentreq);
  548. crMaybeWaitUntilV(!s->auth_agent_query);
  549. }
  550. {
  551. const unsigned char *ret = s->agent_response.ptr;
  552. if (ret) {
  553. if (s->agent_response.len >= 5+16 &&
  554. ret[4] == SSH1_AGENT_RSA_RESPONSE) {
  555. ppl_logevent("Sending Pageant's response");
  556. pkt = ssh_bpp_new_pktout(
  557. s->ppl.bpp, SSH1_CMSG_AUTH_RSA_RESPONSE);
  558. put_data(pkt, ret + 5, 16);
  559. pq_push(s->ppl.out_pq, pkt);
  560. s->is_trivial_auth = false;
  561. crMaybeWaitUntilV(
  562. (pktin = ssh1_login_pop(s))
  563. != NULL);
  564. if (pktin->type == SSH1_SMSG_SUCCESS) {
  565. ppl_logevent("Pageant's response "
  566. "accepted");
  567. if (seat_verbose(s->ppl.seat)) {
  568. ptrlen comment = ptrlen_from_strbuf(
  569. s->agent_keys[s->agent_key_index].
  570. comment);
  571. ppl_printf("Authenticated using RSA "
  572. "key \"%.*s\" from "
  573. "agent\r\n",
  574. PTRLEN_PRINTF(comment));
  575. }
  576. s->authed = true;
  577. } else
  578. ppl_logevent("Pageant's response not "
  579. "accepted");
  580. } else {
  581. ppl_logevent("Pageant failed to answer "
  582. "challenge");
  583. sfree((char *)ret);
  584. }
  585. } else {
  586. ppl_logevent("No reply received from Pageant");
  587. }
  588. }
  589. if (s->authed)
  590. break;
  591. }
  592. if (s->authed)
  593. break;
  594. }
  595. if (s->publickey_blob && s->privatekey_available &&
  596. !s->tried_publickey) {
  597. /*
  598. * Try public key authentication with the specified
  599. * key file.
  600. */
  601. bool got_passphrase; /* need not be kept over crReturn */
  602. if (seat_verbose(s->ppl.seat))
  603. ppl_printf("Trying public key authentication.\r\n");
  604. ppl_logevent("Trying public key \"%s\"",
  605. filename_to_str(s->keyfile));
  606. s->tried_publickey = true;
  607. got_passphrase = false;
  608. while (!got_passphrase) {
  609. /*
  610. * Get a passphrase, if necessary.
  611. */
  612. int retd;
  613. char *passphrase = NULL; /* only written after crReturn */
  614. const char *error;
  615. if (!s->privatekey_encrypted) {
  616. if (seat_verbose(s->ppl.seat))
  617. ppl_printf("No passphrase required.\r\n");
  618. passphrase = NULL;
  619. } else {
  620. s->cur_prompt = new_prompts();
  621. s->cur_prompt->to_server = false;
  622. s->cur_prompt->from_server = false;
  623. s->cur_prompt->name = dupstr("SSH key passphrase");
  624. add_prompt(s->cur_prompt,
  625. dupprintf("Passphrase for key \"%s\": ",
  626. s->publickey_comment), false);
  627. s->userpass_ret = seat_get_userpass_input(
  628. s->ppl.seat, s->cur_prompt, NULL);
  629. while (1) {
  630. while (s->userpass_ret < 0 &&
  631. bufchain_size(s->ppl.user_input) > 0)
  632. s->userpass_ret = seat_get_userpass_input(
  633. s->ppl.seat, s->cur_prompt, s->ppl.user_input);
  634. if (s->userpass_ret >= 0)
  635. break;
  636. s->want_user_input = true;
  637. crReturnV;
  638. s->want_user_input = false;
  639. }
  640. if (!s->userpass_ret) {
  641. /* Failed to get a passphrase. Terminate. */
  642. ssh_user_close(s->ppl.ssh,
  643. "User aborted at passphrase prompt");
  644. return;
  645. }
  646. passphrase = prompt_get_result(s->cur_prompt->prompts[0]);
  647. free_prompts(s->cur_prompt);
  648. s->cur_prompt = NULL;
  649. }
  650. /*
  651. * Try decrypting key with passphrase.
  652. */
  653. retd = rsa1_load_f(s->keyfile, &s->key, passphrase, &error);
  654. if (passphrase) {
  655. smemclr(passphrase, strlen(passphrase));
  656. sfree(passphrase);
  657. }
  658. if (retd == 1) {
  659. /* Correct passphrase. */
  660. got_passphrase = true;
  661. } else if (retd == 0) {
  662. ppl_printf("Couldn't load private key from %s (%s).\r\n",
  663. filename_to_str(s->keyfile), error);
  664. got_passphrase = false;
  665. break; /* go and try something else */
  666. } else if (retd == -1) {
  667. ppl_printf("Wrong passphrase.\r\n");
  668. got_passphrase = false;
  669. /* and try again */
  670. } else {
  671. unreachable("unexpected return from rsa1_load_f()");
  672. }
  673. }
  674. if (got_passphrase) {
  675. /*
  676. * Send a public key attempt.
  677. */
  678. pkt = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_AUTH_RSA);
  679. put_mp_ssh1(pkt, s->key.modulus);
  680. pq_push(s->ppl.out_pq, pkt);
  681. crMaybeWaitUntilV((pktin = ssh1_login_pop(s))
  682. != NULL);
  683. if (pktin->type == SSH1_SMSG_FAILURE) {
  684. ppl_printf("Server refused our public key.\r\n");
  685. continue; /* go and try something else */
  686. }
  687. if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) {
  688. ssh_proto_error(s->ppl.ssh, "Received unexpected packet"
  689. " in response to offer of public key, "
  690. "type %d (%s)", pktin->type,
  691. ssh1_pkt_type(pktin->type));
  692. return;
  693. }
  694. {
  695. int i;
  696. unsigned char buffer[32];
  697. mp_int *challenge, *response;
  698. challenge = get_mp_ssh1(pktin);
  699. if (get_err(pktin)) {
  700. mp_free(challenge);
  701. ssh_proto_error(s->ppl.ssh, "Server's RSA challenge "
  702. "was badly formatted");
  703. return;
  704. }
  705. response = rsa_ssh1_decrypt(challenge, &s->key);
  706. freersapriv(&s->key); /* burn the evidence */
  707. for (i = 0; i < 32; i++) {
  708. buffer[i] = mp_get_byte(response, 31 - i);
  709. }
  710. {
  711. ssh_hash *h = ssh_hash_new(&ssh_md5);
  712. put_data(h, buffer, 32);
  713. put_data(h, s->session_id, 16);
  714. ssh_hash_final(h, buffer);
  715. }
  716. pkt = ssh_bpp_new_pktout(
  717. s->ppl.bpp, SSH1_CMSG_AUTH_RSA_RESPONSE);
  718. put_data(pkt, buffer, 16);
  719. pq_push(s->ppl.out_pq, pkt);
  720. s->is_trivial_auth = false;
  721. mp_free(challenge);
  722. mp_free(response);
  723. }
  724. crMaybeWaitUntilV((pktin = ssh1_login_pop(s))
  725. != NULL);
  726. if (pktin->type == SSH1_SMSG_FAILURE) {
  727. if (seat_verbose(s->ppl.seat))
  728. ppl_printf("Failed to authenticate with"
  729. " our public key.\r\n");
  730. continue; /* go and try something else */
  731. } else if (pktin->type != SSH1_SMSG_SUCCESS) {
  732. ssh_proto_error(s->ppl.ssh, "Received unexpected packet"
  733. " in response to RSA authentication, "
  734. "type %d (%s)", pktin->type,
  735. ssh1_pkt_type(pktin->type));
  736. return;
  737. }
  738. break; /* we're through! */
  739. }
  740. }
  741. /*
  742. * Otherwise, try various forms of password-like authentication.
  743. */
  744. s->cur_prompt = new_prompts();
  745. if (conf_get_bool(s->conf, CONF_try_tis_auth) &&
  746. (s->supported_auths_mask & (1 << SSH1_AUTH_TIS)) &&
  747. !s->tis_auth_refused) {
  748. ssh1_login_setup_tis_scc(s);
  749. s->pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE;
  750. ppl_logevent("Requested TIS authentication");
  751. pkt = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_AUTH_TIS);
  752. pq_push(s->ppl.out_pq, pkt);
  753. crMaybeWaitUntilV((pktin = ssh1_login_pop(s)) != NULL);
  754. if (pktin->type == SSH1_SMSG_FAILURE) {
  755. ppl_logevent("TIS authentication declined");
  756. if (seat_interactive(s->ppl.seat))
  757. ppl_printf("TIS authentication refused.\r\n");
  758. s->tis_auth_refused = true;
  759. continue;
  760. } else if (pktin->type == SSH1_SMSG_AUTH_TIS_CHALLENGE) {
  761. ptrlen challenge = get_string(pktin);
  762. if (get_err(pktin)) {
  763. ssh_proto_error(s->ppl.ssh, "TIS challenge packet was "
  764. "badly formed");
  765. return;
  766. }
  767. ppl_logevent("Received TIS challenge");
  768. s->cur_prompt->to_server = true;
  769. s->cur_prompt->from_server = true;
  770. s->cur_prompt->name = dupstr("SSH TIS authentication");
  771. strbuf *sb = strbuf_new();
  772. put_datapl(sb, PTRLEN_LITERAL("\
  773. -- TIS authentication challenge from server: ---------------------------------\
  774. \r\n"));
  775. if (s->tis_scc) {
  776. stripctrl_retarget(s->tis_scc, BinarySink_UPCAST(sb));
  777. put_datapl(s->tis_scc, challenge);
  778. stripctrl_retarget(s->tis_scc, NULL);
  779. } else {
  780. put_datapl(sb, challenge);
  781. }
  782. if (!ptrlen_endswith(challenge, PTRLEN_LITERAL("\n"), NULL))
  783. put_datapl(sb, PTRLEN_LITERAL("\r\n"));
  784. put_datapl(sb, PTRLEN_LITERAL("\
  785. -- End of TIS authentication challenge from server: --------------------------\
  786. \r\n"));
  787. s->cur_prompt->instruction = strbuf_to_str(sb);
  788. s->cur_prompt->instr_reqd = true;
  789. add_prompt(s->cur_prompt, dupstr(
  790. "TIS authentication response: "), false);
  791. } else {
  792. ssh_proto_error(s->ppl.ssh, "Received unexpected packet"
  793. " in response to TIS authentication, "
  794. "type %d (%s)", pktin->type,
  795. ssh1_pkt_type(pktin->type));
  796. return;
  797. }
  798. } else if (conf_get_bool(s->conf, CONF_try_tis_auth) &&
  799. (s->supported_auths_mask & (1 << SSH1_AUTH_CCARD)) &&
  800. !s->ccard_auth_refused) {
  801. ssh1_login_setup_tis_scc(s);
  802. s->pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE;
  803. ppl_logevent("Requested CryptoCard authentication");
  804. pkt = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_AUTH_CCARD);
  805. pq_push(s->ppl.out_pq, pkt);
  806. crMaybeWaitUntilV((pktin = ssh1_login_pop(s)) != NULL);
  807. if (pktin->type == SSH1_SMSG_FAILURE) {
  808. ppl_logevent("CryptoCard authentication declined");
  809. ppl_printf("CryptoCard authentication refused.\r\n");
  810. s->ccard_auth_refused = true;
  811. continue;
  812. } else if (pktin->type == SSH1_SMSG_AUTH_CCARD_CHALLENGE) {
  813. ptrlen challenge = get_string(pktin);
  814. if (get_err(pktin)) {
  815. ssh_proto_error(s->ppl.ssh, "CryptoCard challenge packet "
  816. "was badly formed");
  817. return;
  818. }
  819. ppl_logevent("Received CryptoCard challenge");
  820. s->cur_prompt->to_server = true;
  821. s->cur_prompt->from_server = true;
  822. s->cur_prompt->name = dupstr("SSH CryptoCard authentication");
  823. strbuf *sb = strbuf_new();
  824. put_datapl(sb, PTRLEN_LITERAL("\
  825. -- CryptoCard authentication challenge from server: --------------------------\
  826. \r\n"));
  827. if (s->tis_scc) {
  828. stripctrl_retarget(s->tis_scc, BinarySink_UPCAST(sb));
  829. put_datapl(s->tis_scc, challenge);
  830. stripctrl_retarget(s->tis_scc, NULL);
  831. } else {
  832. put_datapl(sb, challenge);
  833. }
  834. if (!ptrlen_endswith(challenge, PTRLEN_LITERAL("\n"), NULL))
  835. put_datapl(sb, PTRLEN_LITERAL("\r\n"));
  836. put_datapl(sb, PTRLEN_LITERAL("\
  837. -- End of CryptoCard authentication challenge from server: -------------------\
  838. \r\n"));
  839. s->cur_prompt->instruction = strbuf_to_str(sb);
  840. s->cur_prompt->instr_reqd = true;
  841. add_prompt(s->cur_prompt, dupstr(
  842. "CryptoCard authentication response: "), false);
  843. } else {
  844. ssh_proto_error(s->ppl.ssh, "Received unexpected packet"
  845. " in response to TIS authentication, "
  846. "type %d (%s)", pktin->type,
  847. ssh1_pkt_type(pktin->type));
  848. return;
  849. }
  850. }
  851. if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) {
  852. if ((s->supported_auths_mask & (1 << SSH1_AUTH_PASSWORD)) == 0) {
  853. ssh_sw_abort(s->ppl.ssh, "No supported authentication methods "
  854. "available");
  855. return;
  856. }
  857. s->cur_prompt->to_server = true;
  858. s->cur_prompt->from_server = false;
  859. s->cur_prompt->name = dupstr("SSH password");
  860. add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ",
  861. s->username, s->savedhost),
  862. false);
  863. }
  864. /*
  865. * Show password prompt, having first obtained it via a TIS
  866. * or CryptoCard exchange if we're doing TIS or CryptoCard
  867. * authentication.
  868. */
  869. s->userpass_ret = seat_get_userpass_input(
  870. s->ppl.seat, s->cur_prompt, NULL);
  871. while (1) {
  872. while (s->userpass_ret < 0 &&
  873. bufchain_size(s->ppl.user_input) > 0)
  874. s->userpass_ret = seat_get_userpass_input(
  875. s->ppl.seat, s->cur_prompt, s->ppl.user_input);
  876. if (s->userpass_ret >= 0)
  877. break;
  878. s->want_user_input = true;
  879. crReturnV;
  880. s->want_user_input = false;
  881. }
  882. if (!s->userpass_ret) {
  883. /*
  884. * Failed to get a password (for example
  885. * because one was supplied on the command line
  886. * which has already failed to work). Terminate.
  887. */
  888. ssh_user_close(s->ppl.ssh, "User aborted at password prompt");
  889. return;
  890. }
  891. if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) {
  892. /*
  893. * Defence against traffic analysis: we send a
  894. * whole bunch of packets containing strings of
  895. * different lengths. One of these strings is the
  896. * password, in a SSH1_CMSG_AUTH_PASSWORD packet.
  897. * The others are all random data in
  898. * SSH1_MSG_IGNORE packets. This way a passive
  899. * listener can't tell which is the password, and
  900. * hence can't deduce the password length.
  901. *
  902. * Anybody with a password length greater than 16
  903. * bytes is going to have enough entropy in their
  904. * password that a listener won't find it _that_
  905. * much help to know how long it is. So what we'll
  906. * do is:
  907. *
  908. * - if password length < 16, we send 15 packets
  909. * containing string lengths 1 through 15
  910. *
  911. * - otherwise, we let N be the nearest multiple
  912. * of 8 below the password length, and send 8
  913. * packets containing string lengths N through
  914. * N+7. This won't obscure the order of
  915. * magnitude of the password length, but it will
  916. * introduce a bit of extra uncertainty.
  917. *
  918. * A few servers can't deal with SSH1_MSG_IGNORE, at
  919. * least in this context. For these servers, we need
  920. * an alternative defence. We make use of the fact
  921. * that the password is interpreted as a C string:
  922. * so we can append a NUL, then some random data.
  923. *
  924. * A few servers can deal with neither SSH1_MSG_IGNORE
  925. * here _nor_ a padded password string.
  926. * For these servers we are left with no defences
  927. * against password length sniffing.
  928. */
  929. if (!(s->ppl.remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE) &&
  930. !(s->ppl.remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) {
  931. /*
  932. * The server can deal with SSH1_MSG_IGNORE, so
  933. * we can use the primary defence.
  934. */
  935. int bottom, top, pwlen, i;
  936. const char *pw = prompt_get_result_ref(
  937. s->cur_prompt->prompts[0]);
  938. pwlen = strlen(pw);
  939. if (pwlen < 16) {
  940. bottom = 0; /* zero length passwords are OK! :-) */
  941. top = 15;
  942. } else {
  943. bottom = pwlen & ~7;
  944. top = bottom + 7;
  945. }
  946. assert(pwlen >= bottom && pwlen <= top);
  947. for (i = bottom; i <= top; i++) {
  948. if (i == pwlen) {
  949. pkt = ssh_bpp_new_pktout(s->ppl.bpp, s->pwpkt_type);
  950. put_stringz(pkt, pw);
  951. pq_push(s->ppl.out_pq, pkt);
  952. } else {
  953. strbuf *random_data = strbuf_new_nm();
  954. random_read(strbuf_append(random_data, i), i);
  955. pkt = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_IGNORE);
  956. put_stringsb(pkt, random_data);
  957. pq_push(s->ppl.out_pq, pkt);
  958. }
  959. }
  960. ppl_logevent("Sending password with camouflage packets");
  961. }
  962. else if (!(s->ppl.remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) {
  963. /*
  964. * The server can't deal with SSH1_MSG_IGNORE
  965. * but can deal with padded passwords, so we
  966. * can use the secondary defence.
  967. */
  968. strbuf *padded_pw = strbuf_new_nm();
  969. ppl_logevent("Sending length-padded password");
  970. pkt = ssh_bpp_new_pktout(s->ppl.bpp, s->pwpkt_type);
  971. put_asciz(padded_pw, prompt_get_result_ref(
  972. s->cur_prompt->prompts[0]));
  973. size_t pad = 63 & -padded_pw->len;
  974. random_read(strbuf_append(padded_pw, pad), pad);
  975. put_stringsb(pkt, padded_pw);
  976. pq_push(s->ppl.out_pq, pkt);
  977. } else {
  978. /*
  979. * The server is believed unable to cope with
  980. * any of our password camouflage methods.
  981. */
  982. ppl_logevent("Sending unpadded password");
  983. pkt = ssh_bpp_new_pktout(s->ppl.bpp, s->pwpkt_type);
  984. put_stringz(pkt, prompt_get_result_ref(
  985. s->cur_prompt->prompts[0]));
  986. pq_push(s->ppl.out_pq, pkt);
  987. }
  988. } else {
  989. pkt = ssh_bpp_new_pktout(s->ppl.bpp, s->pwpkt_type);
  990. put_stringz(pkt, prompt_get_result_ref(s->cur_prompt->prompts[0]));
  991. pq_push(s->ppl.out_pq, pkt);
  992. }
  993. s->is_trivial_auth = false;
  994. ppl_logevent("Sent password");
  995. free_prompts(s->cur_prompt);
  996. s->cur_prompt = NULL;
  997. crMaybeWaitUntilV((pktin = ssh1_login_pop(s)) != NULL);
  998. if (pktin->type == SSH1_SMSG_FAILURE) {
  999. if (seat_verbose(s->ppl.seat))
  1000. ppl_printf("Access denied\r\n");
  1001. ppl_logevent("Authentication refused");
  1002. } else if (pktin->type != SSH1_SMSG_SUCCESS) {
  1003. ssh_proto_error(s->ppl.ssh, "Received unexpected packet"
  1004. " in response to password authentication, type %d "
  1005. "(%s)", pktin->type, ssh1_pkt_type(pktin->type));
  1006. return;
  1007. }
  1008. }
  1009. if (conf_get_bool(s->conf, CONF_ssh_no_trivial_userauth) &&
  1010. s->is_trivial_auth) {
  1011. ssh_proto_error(s->ppl.ssh, "Authentication was trivial! "
  1012. "Abandoning session as specified in configuration.");
  1013. return;
  1014. }
  1015. ppl_logevent("Authentication successful");
  1016. if (conf_get_bool(s->conf, CONF_compression)) {
  1017. ppl_logevent("Requesting compression");
  1018. pkt = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_REQUEST_COMPRESSION);
  1019. put_uint32(pkt, 6); /* gzip compression level */
  1020. pq_push(s->ppl.out_pq, pkt);
  1021. crMaybeWaitUntilV((pktin = ssh1_login_pop(s)) != NULL);
  1022. if (pktin->type == SSH1_SMSG_SUCCESS) {
  1023. /*
  1024. * We don't have to actually do anything here: the SSH-1
  1025. * BPP will take care of automatically starting the
  1026. * compression, by recognising our outgoing request packet
  1027. * and the success response. (Horrible, but it's the
  1028. * easiest way to avoid race conditions if other packets
  1029. * cross in transit.)
  1030. */
  1031. } else if (pktin->type == SSH1_SMSG_FAILURE) {
  1032. ppl_logevent("Server refused to enable compression");
  1033. ppl_printf("Server refused to compress\r\n");
  1034. } else {
  1035. ssh_proto_error(s->ppl.ssh, "Received unexpected packet"
  1036. " in response to compression request, type %d "
  1037. "(%s)", pktin->type, ssh1_pkt_type(pktin->type));
  1038. return;
  1039. }
  1040. }
  1041. ssh1_connection_set_protoflags(
  1042. s->successor_layer, s->local_protoflags, s->remote_protoflags);
  1043. {
  1044. PacketProtocolLayer *successor = s->successor_layer;
  1045. s->successor_layer = NULL; /* avoid freeing it ourself */
  1046. ssh_ppl_replace(&s->ppl, successor);
  1047. return; /* we've just freed s, so avoid even touching s->crState */
  1048. }
  1049. crFinishV;
  1050. }
  1051. static void ssh1_login_setup_tis_scc(struct ssh1_login_state *s)
  1052. {
  1053. if (s->tis_scc_initialised)
  1054. return;
  1055. s->tis_scc = seat_stripctrl_new(s->ppl.seat, NULL, SIC_KI_PROMPTS);
  1056. if (s->tis_scc)
  1057. stripctrl_enable_line_limiting(s->tis_scc);
  1058. s->tis_scc_initialised = true;
  1059. }
  1060. static void ssh1_login_dialog_callback(void *loginv, int ret)
  1061. {
  1062. struct ssh1_login_state *s = (struct ssh1_login_state *)loginv;
  1063. s->dlgret = ret;
  1064. ssh_ppl_process_queue(&s->ppl);
  1065. }
  1066. static void ssh1_login_agent_query(struct ssh1_login_state *s, strbuf *req)
  1067. {
  1068. void *response;
  1069. int response_len;
  1070. sfree(s->agent_response_to_free);
  1071. s->agent_response_to_free = NULL;
  1072. s->auth_agent_query = agent_query(req, &response, &response_len,
  1073. ssh1_login_agent_callback, s);
  1074. if (!s->auth_agent_query)
  1075. ssh1_login_agent_callback(s, response, response_len);
  1076. }
  1077. static void ssh1_login_agent_callback(void *loginv, void *reply, int replylen)
  1078. {
  1079. struct ssh1_login_state *s = (struct ssh1_login_state *)loginv;
  1080. s->auth_agent_query = NULL;
  1081. s->agent_response_to_free = reply;
  1082. s->agent_response = make_ptrlen(reply, replylen);
  1083. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1084. }
  1085. static void ssh1_login_special_cmd(PacketProtocolLayer *ppl,
  1086. SessionSpecialCode code, int arg)
  1087. {
  1088. struct ssh1_login_state *s =
  1089. container_of(ppl, struct ssh1_login_state, ppl);
  1090. PktOut *pktout;
  1091. if (code == SS_PING || code == SS_NOP) {
  1092. if (!(s->ppl.remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) {
  1093. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_IGNORE);
  1094. put_stringz(pktout, "");
  1095. pq_push(s->ppl.out_pq, pktout);
  1096. }
  1097. }
  1098. }
  1099. static bool ssh1_login_want_user_input(PacketProtocolLayer *ppl)
  1100. {
  1101. struct ssh1_login_state *s =
  1102. container_of(ppl, struct ssh1_login_state, ppl);
  1103. return s->want_user_input;
  1104. }
  1105. static void ssh1_login_got_user_input(PacketProtocolLayer *ppl)
  1106. {
  1107. struct ssh1_login_state *s =
  1108. container_of(ppl, struct ssh1_login_state, ppl);
  1109. if (s->want_user_input)
  1110. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1111. }
  1112. static void ssh1_login_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  1113. {
  1114. struct ssh1_login_state *s =
  1115. container_of(ppl, struct ssh1_login_state, ppl);
  1116. ssh_ppl_reconfigure(s->successor_layer, conf);
  1117. }