ssh1login.c 47 KB

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