ssh1login.c 50 KB

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