ssh1login.c 49 KB

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