ssh1login.c 47 KB

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