ssh1login.c 49 KB

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