ssh1login.c 47 KB

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