ssh1login.c 48 KB

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