ssh.h 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "puttymem.h"
  4. #include "tree234.h"
  5. #ifndef WINSCP_VS
  6. #include "network.h"
  7. #endif
  8. #include "int64.h"
  9. #include "misc.h"
  10. #ifndef WINSCP_VS
  11. struct ssh_channel;
  12. /*
  13. * Buffer management constants. There are several of these for
  14. * various different purposes:
  15. *
  16. * - SSH1_BUFFER_LIMIT is the amount of backlog that must build up
  17. * on a local data stream before we throttle the whole SSH
  18. * connection (in SSH-1 only). Throttling the whole connection is
  19. * pretty drastic so we set this high in the hope it won't
  20. * happen very often.
  21. *
  22. * - SSH_MAX_BACKLOG is the amount of backlog that must build up
  23. * on the SSH connection itself before we defensively throttle
  24. * _all_ local data streams. This is pretty drastic too (though
  25. * thankfully unlikely in SSH-2 since the window mechanism should
  26. * ensure that the server never has any need to throttle its end
  27. * of the connection), so we set this high as well.
  28. *
  29. * - OUR_V2_WINSIZE is the default window size we present on SSH-2
  30. * channels.
  31. *
  32. * - OUR_V2_BIGWIN is the window size we advertise for the only
  33. * channel in a simple connection. It must be <= INT_MAX.
  34. *
  35. * - OUR_V2_MAXPKT is the official "maximum packet size" we send
  36. * to the remote side. This actually has nothing to do with the
  37. * size of the _packet_, but is instead a limit on the amount
  38. * of data we're willing to receive in a single SSH2 channel
  39. * data message.
  40. *
  41. * - OUR_V2_PACKETLIMIT is actually the maximum size of SSH
  42. * _packet_ we're prepared to cope with. It must be a multiple
  43. * of the cipher block size, and must be at least 35000.
  44. */
  45. #define SSH1_BUFFER_LIMIT 32768
  46. #define SSH_MAX_BACKLOG 32768
  47. #define OUR_V2_WINSIZE 16384
  48. #define OUR_V2_BIGWIN 0x7fffffff
  49. #define OUR_V2_MAXPKT 0x4000UL
  50. #define OUR_V2_PACKETLIMIT 0x9000UL
  51. typedef struct PacketQueueNode PacketQueueNode;
  52. struct PacketQueueNode {
  53. PacketQueueNode *next, *prev;
  54. int on_free_queue; /* is this packet scheduled for freeing? */
  55. };
  56. typedef struct PktIn {
  57. int type;
  58. unsigned long sequence; /* SSH-2 incoming sequence number */
  59. PacketQueueNode qnode; /* for linking this packet on to a queue */
  60. BinarySource_IMPLEMENTATION;
  61. } PktIn;
  62. typedef struct PktOut {
  63. long prefix; /* bytes up to and including type field */
  64. long length; /* total bytes, including prefix */
  65. int type;
  66. long minlen; /* SSH-2: ensure wire length is at least this */
  67. unsigned char *data; /* allocated storage */
  68. long maxlen; /* amount of storage allocated for `data' */
  69. /* Extra metadata used in SSH packet logging mode, allowing us to
  70. * log in the packet header line that the packet came from a
  71. * connection-sharing downstream and what if anything unusual was
  72. * done to it. The additional_log_text field is expected to be a
  73. * static string - it will not be freed. */
  74. unsigned downstream_id;
  75. const char *additional_log_text;
  76. PacketQueueNode qnode; /* for linking this packet on to a queue */
  77. BinarySink_IMPLEMENTATION;
  78. } PktOut;
  79. typedef struct PacketQueueBase {
  80. PacketQueueNode end;
  81. struct IdempotentCallback *ic;
  82. Frontend * frontend; // WINSCP
  83. } PacketQueueBase;
  84. typedef struct PktInQueue {
  85. PacketQueueBase pqb;
  86. PktIn *(*after)(PacketQueueBase *, PacketQueueNode *prev, int pop);
  87. } PktInQueue;
  88. typedef struct PktOutQueue {
  89. PacketQueueBase pqb;
  90. PktOut *(*after)(PacketQueueBase *, PacketQueueNode *prev, int pop);
  91. } PktOutQueue;
  92. void pq_base_push(PacketQueueBase *pqb, PacketQueueNode *node);
  93. void pq_base_push_front(PacketQueueBase *pqb, PacketQueueNode *node);
  94. void pq_base_concatenate(PacketQueueBase *dest,
  95. PacketQueueBase *q1, PacketQueueBase *q2);
  96. void pq_in_init(PktInQueue *pq, Frontend * frontend); // WINSCP
  97. void pq_out_init(PktOutQueue *pq, Frontend * frontend); // WINSCP
  98. void pq_in_clear(PktInQueue *pq);
  99. void pq_out_clear(PktOutQueue *pq);
  100. #define pq_push(pq, pkt) \
  101. TYPECHECK((pq)->after(&(pq)->pqb, NULL, FALSE) == pkt, \
  102. pq_base_push(&(pq)->pqb, &(pkt)->qnode))
  103. #define pq_push_front(pq, pkt) \
  104. TYPECHECK((pq)->after(&(pq)->pqb, NULL, FALSE) == pkt, \
  105. pq_base_push_front(&(pq)->pqb, &(pkt)->qnode))
  106. #define pq_peek(pq) ((pq)->after(&(pq)->pqb, &(pq)->pqb.end, FALSE))
  107. #define pq_pop(pq) ((pq)->after(&(pq)->pqb, &(pq)->pqb.end, TRUE))
  108. #define pq_concatenate(dst, q1, q2) \
  109. TYPECHECK((q1)->after(&(q1)->pqb, NULL, FALSE) == \
  110. (dst)->after(&(dst)->pqb, NULL, FALSE) && \
  111. (q2)->after(&(q2)->pqb, NULL, FALSE) == \
  112. (dst)->after(&(dst)->pqb, NULL, FALSE), \
  113. pq_base_concatenate(&(dst)->pqb, &(q1)->pqb, &(q2)->pqb))
  114. #define pq_first(pq) pq_peek(pq)
  115. #define pq_next(pq, pkt) ((pq)->after(&(pq)->pqb, &(pkt)->qnode, FALSE))
  116. /*
  117. * Packet type contexts, so that ssh2_pkt_type can correctly decode
  118. * the ambiguous type numbers back into the correct type strings.
  119. */
  120. typedef enum {
  121. SSH2_PKTCTX_NOKEX,
  122. SSH2_PKTCTX_DHGROUP,
  123. SSH2_PKTCTX_DHGEX,
  124. SSH2_PKTCTX_ECDHKEX,
  125. SSH2_PKTCTX_GSSKEX,
  126. SSH2_PKTCTX_RSAKEX
  127. } Pkt_KCtx;
  128. typedef enum {
  129. SSH2_PKTCTX_NOAUTH,
  130. SSH2_PKTCTX_PUBLICKEY,
  131. SSH2_PKTCTX_PASSWORD,
  132. SSH2_PKTCTX_GSSAPI,
  133. SSH2_PKTCTX_KBDINTER
  134. } Pkt_ACtx;
  135. typedef struct PacketLogSettings {
  136. int omit_passwords, omit_data;
  137. Pkt_KCtx kctx;
  138. Pkt_ACtx actx;
  139. } PacketLogSettings;
  140. #define MAX_BLANKS 4 /* no packet needs more censored sections than this */
  141. int ssh1_censor_packet(
  142. const PacketLogSettings *pls, int type, int sender_is_client,
  143. ptrlen pkt, logblank_t *blanks);
  144. int ssh2_censor_packet(
  145. const PacketLogSettings *pls, int type, int sender_is_client,
  146. ptrlen pkt, logblank_t *blanks);
  147. PktOut *ssh_new_packet(void);
  148. void ssh_free_pktout(PktOut *pkt);
  149. Socket *ssh_connection_sharing_init(
  150. const char *host, int port, Conf *conf, LogContext *logctx,
  151. Plug *sshplug, ssh_sharing_state **state);
  152. void ssh_connshare_provide_connlayer(ssh_sharing_state *sharestate,
  153. ConnectionLayer *cl);
  154. int ssh_share_test_for_upstream(const char *host, int port, Conf *conf);
  155. void share_got_pkt_from_server(ssh_sharing_connstate *ctx, int type,
  156. const void *pkt, int pktlen);
  157. void share_activate(ssh_sharing_state *sharestate,
  158. const char *server_verstring);
  159. void sharestate_free(ssh_sharing_state *state);
  160. int share_ndownstreams(ssh_sharing_state *state);
  161. void ssh_connshare_log(Ssh *ssh, int event, const char *logtext,
  162. const char *ds_err, const char *us_err);
  163. void share_setup_x11_channel(ssh_sharing_connstate *cs, share_channel *chan,
  164. unsigned upstream_id, unsigned server_id,
  165. unsigned server_currwin, unsigned server_maxpkt,
  166. unsigned client_adjusted_window,
  167. const char *peer_addr, int peer_port, int endian,
  168. int protomajor, int protominor,
  169. const void *initial_data, int initial_len);
  170. /* Structure definition centralised here because the SSH-1 and SSH-2
  171. * connection layers both use it. But the client module (portfwd.c)
  172. * should not try to look inside here. */
  173. struct ssh_rportfwd {
  174. unsigned sport, dport;
  175. char *shost, *dhost;
  176. int addressfamily;
  177. char *log_description; /* name of remote listening port, for logging */
  178. ssh_sharing_connstate *share_ctx;
  179. PortFwdRecord *pfr;
  180. };
  181. void free_rportfwd(struct ssh_rportfwd *rpf);
  182. struct ConnectionLayerVtable {
  183. /* Allocate and free remote-to-local port forwardings, called by
  184. * PortFwdManager or by connection sharing */
  185. struct ssh_rportfwd *(*rportfwd_alloc)(
  186. ConnectionLayer *cl,
  187. const char *shost, int sport, const char *dhost, int dport,
  188. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  189. ssh_sharing_connstate *share_ctx);
  190. void (*rportfwd_remove)(ConnectionLayer *cl, struct ssh_rportfwd *rpf);
  191. /* Open a local-to-remote port forwarding channel, called by
  192. * PortFwdManager */
  193. SshChannel *(*lportfwd_open)(
  194. ConnectionLayer *cl, const char *hostname, int port,
  195. const char *org, Channel *chan);
  196. /* Add and remove X11 displays for connection sharing downstreams */
  197. struct X11FakeAuth *(*add_sharing_x11_display)(
  198. ConnectionLayer *cl, int authtype, ssh_sharing_connstate *share_cs,
  199. share_channel *share_chan);
  200. void (*remove_sharing_x11_display)(
  201. ConnectionLayer *cl, struct X11FakeAuth *auth);
  202. /* Pass through an outgoing SSH packet from a downstream */
  203. void (*send_packet_from_downstream)(
  204. ConnectionLayer *cl, unsigned id, int type,
  205. const void *pkt, int pktlen, const char *additional_log_text);
  206. /* Allocate/free an upstream channel number associated with a
  207. * sharing downstream */
  208. unsigned (*alloc_sharing_channel)(ConnectionLayer *cl,
  209. ssh_sharing_connstate *connstate);
  210. void (*delete_sharing_channel)(ConnectionLayer *cl, unsigned localid);
  211. /* Indicate that a downstream has sent a global request with the
  212. * want-reply flag, so that when a reply arrives it will be passed
  213. * back to that downstrean */
  214. void (*sharing_queue_global_request)(
  215. ConnectionLayer *cl, ssh_sharing_connstate *connstate);
  216. /* Indicate that the last downstream has disconnected */
  217. void (*sharing_no_more_downstreams)(ConnectionLayer *cl);
  218. /* Query whether the connection layer is doing agent forwarding */
  219. int (*agent_forwarding_permitted)(ConnectionLayer *cl);
  220. /* Set the size of the main terminal window (if any) */
  221. void (*terminal_size)(ConnectionLayer *cl, int width, int height);
  222. /* Indicate that the backlog on standard output has cleared */
  223. void (*stdout_unthrottle)(ConnectionLayer *cl, int bufsize);
  224. /* Query the size of the backlog on standard _input_ */
  225. int (*stdin_backlog)(ConnectionLayer *cl);
  226. /* Tell the connection layer that the SSH connection itself has
  227. * backed up, so it should tell all currently open channels to
  228. * cease reading from their local input sources if they can. (Or
  229. * tell it that that state of affairs has gone away again.) */
  230. void (*throttle_all_channels)(ConnectionLayer *cl, int throttled);
  231. /* Ask the connection layer about its current preference for
  232. * line-discipline options. */
  233. int (*ldisc_option)(ConnectionLayer *cl, int option);
  234. };
  235. struct ConnectionLayer {
  236. LogContext *logctx;
  237. const struct ConnectionLayerVtable *vt;
  238. };
  239. #define ssh_rportfwd_alloc(cl, sh, sp, dh, dp, af, ld, pfr, share) \
  240. ((cl)->vt->rportfwd_alloc(cl, sh, sp, dh, dp, af, ld, pfr, share))
  241. #define ssh_rportfwd_remove(cl, rpf) ((cl)->vt->rportfwd_remove(cl, rpf))
  242. #define ssh_lportfwd_open(cl, h, p, org, chan) \
  243. ((cl)->vt->lportfwd_open(cl, h, p, org, chan))
  244. #define ssh_add_sharing_x11_display(cl, auth, cs, ch) \
  245. ((cl)->vt->add_sharing_x11_display(cl, auth, cs, ch))
  246. #define ssh_remove_sharing_x11_display(cl, fa) \
  247. ((cl)->vt->remove_sharing_x11_display(cl, fa))
  248. #define ssh_send_packet_from_downstream(cl, id, type, pkt, len, log) \
  249. ((cl)->vt->send_packet_from_downstream(cl, id, type, pkt, len, log))
  250. #define ssh_alloc_sharing_channel(cl, cs) \
  251. ((cl)->vt->alloc_sharing_channel(cl, cs))
  252. #define ssh_delete_sharing_channel(cl, ch) \
  253. ((cl)->vt->delete_sharing_channel(cl, ch))
  254. #define ssh_sharing_queue_global_request(cl, cs) \
  255. ((cl)->vt->sharing_queue_global_request(cl, cs))
  256. #define ssh_sharing_no_more_downstreams(cl) \
  257. ((cl)->vt->sharing_no_more_downstreams(cl))
  258. #define ssh_agent_forwarding_permitted(cl) \
  259. ((cl)->vt->agent_forwarding_permitted(cl))
  260. #define ssh_terminal_size(cl, w, h) ((cl)->vt->terminal_size(cl, w, h))
  261. #define ssh_stdout_unthrottle(cl, bufsize) \
  262. ((cl)->vt->stdout_unthrottle(cl, bufsize))
  263. #define ssh_stdin_backlog(cl) ((cl)->vt->stdin_backlog(cl))
  264. #define ssh_throttle_all_channels(cl, throttled) \
  265. ((cl)->vt->throttle_all_channels(cl, throttled))
  266. #define ssh_ldisc_option(cl, option) ((cl)->vt->ldisc_option(cl, option))
  267. /* Exports from portfwd.c */
  268. PortFwdManager *portfwdmgr_new(ConnectionLayer *cl);
  269. void portfwdmgr_free(PortFwdManager *mgr);
  270. void portfwdmgr_config(PortFwdManager *mgr, Conf *conf);
  271. void portfwdmgr_close(PortFwdManager *mgr, PortFwdRecord *pfr);
  272. void portfwdmgr_close_all(PortFwdManager *mgr);
  273. char *portfwdmgr_connect(PortFwdManager *mgr, Channel **chan_ret,
  274. char *hostname, int port, SshChannel *c,
  275. int addressfamily);
  276. LogContext *ssh_get_logctx(Ssh *ssh);
  277. /* Communications back to ssh.c from connection layers */
  278. void ssh_throttle_conn(Ssh *ssh, int adjust);
  279. void ssh_got_exitcode(Ssh *ssh, int status);
  280. void ssh_ldisc_update(Ssh *ssh);
  281. void ssh_got_fallback_cmd(Ssh *ssh);
  282. /* Functions to abort the connection, for various reasons. */
  283. void ssh_remote_error(Ssh *ssh, const char *fmt, ...);
  284. void ssh_remote_eof(Ssh *ssh, const char *fmt, ...);
  285. void ssh_proto_error(Ssh *ssh, const char *fmt, ...);
  286. void ssh_sw_abort(Ssh *ssh, const char *fmt, ...);
  287. void ssh_user_close(Ssh *ssh, const char *fmt, ...);
  288. #define SSH_CIPHER_IDEA 1
  289. #define SSH_CIPHER_DES 2
  290. #define SSH_CIPHER_3DES 3
  291. #define SSH_CIPHER_BLOWFISH 6
  292. #ifndef BIGNUM_INTERNAL
  293. typedef void *Bignum;
  294. #endif
  295. typedef struct ssh_keyalg ssh_keyalg;
  296. typedef const struct ssh_keyalg *ssh_key;
  297. struct RSAKey {
  298. int bits;
  299. int bytes;
  300. Bignum modulus;
  301. Bignum exponent;
  302. Bignum private_exponent;
  303. Bignum p;
  304. Bignum q;
  305. Bignum iqmp;
  306. char *comment;
  307. ssh_key sshk;
  308. };
  309. struct dss_key {
  310. Bignum p, q, g, y, x;
  311. ssh_key sshk;
  312. };
  313. struct ec_curve;
  314. struct ec_point {
  315. const struct ec_curve *curve;
  316. Bignum x, y;
  317. Bignum z; /* Jacobian denominator */
  318. unsigned char infinity;
  319. };
  320. void ec_point_free(struct ec_point *point);
  321. /* Weierstrass form curve */
  322. struct ec_wcurve
  323. {
  324. Bignum a, b, n;
  325. struct ec_point G;
  326. };
  327. /* Montgomery form curve */
  328. struct ec_mcurve
  329. {
  330. Bignum a, b;
  331. struct ec_point G;
  332. };
  333. /* Edwards form curve */
  334. struct ec_ecurve
  335. {
  336. Bignum l, d;
  337. struct ec_point B;
  338. };
  339. struct ec_curve {
  340. enum { EC_WEIERSTRASS, EC_MONTGOMERY, EC_EDWARDS } type;
  341. /* 'name' is the identifier of the curve when it has to appear in
  342. * wire protocol encodings, as it does in e.g. the public key and
  343. * signature formats for NIST curves. Curves which do not format
  344. * their keys or signatures in this way just have name==NULL.
  345. *
  346. * 'textname' is non-NULL for all curves, and is a human-readable
  347. * identification suitable for putting in log messages. */
  348. const char *name, *textname;
  349. unsigned int fieldBits;
  350. Bignum p;
  351. union {
  352. struct ec_wcurve w;
  353. struct ec_mcurve m;
  354. struct ec_ecurve e;
  355. };
  356. };
  357. const ssh_keyalg *ec_alg_by_oid(int len, const void *oid,
  358. const struct ec_curve **curve);
  359. const unsigned char *ec_alg_oid(const ssh_keyalg *alg, int *oidlen);
  360. extern const int ec_nist_curve_lengths[], n_ec_nist_curve_lengths;
  361. int ec_nist_alg_and_curve_by_bits(int bits,
  362. const struct ec_curve **curve,
  363. const ssh_keyalg **alg);
  364. int ec_ed_alg_and_curve_by_bits(int bits,
  365. const struct ec_curve **curve,
  366. const ssh_keyalg **alg);
  367. struct ec_key {
  368. struct ec_point publicKey;
  369. Bignum privateKey;
  370. ssh_key sshk;
  371. };
  372. struct ec_point *ec_public(const Bignum privateKey, const struct ec_curve *curve);
  373. /*
  374. * SSH-1 never quite decided which order to store the two components
  375. * of an RSA key. During connection setup, the server sends its host
  376. * and server keys with the exponent first; private key files store
  377. * the modulus first. The agent protocol is even more confusing,
  378. * because the client specifies a key to the server in one order and
  379. * the server lists the keys it knows about in the other order!
  380. */
  381. typedef enum { RSA_SSH1_EXPONENT_FIRST, RSA_SSH1_MODULUS_FIRST } RsaSsh1Order;
  382. void BinarySource_get_rsa_ssh1_pub(
  383. BinarySource *src, struct RSAKey *result, RsaSsh1Order order);
  384. void BinarySource_get_rsa_ssh1_priv(
  385. BinarySource *src, struct RSAKey *rsa);
  386. int rsa_ssh1_encrypt(unsigned char *data, int length, struct RSAKey *key);
  387. Bignum rsa_ssh1_decrypt(Bignum input, struct RSAKey *key);
  388. void rsasanitise(struct RSAKey *key);
  389. int rsastr_len(struct RSAKey *key);
  390. void rsastr_fmt(char *str, struct RSAKey *key);
  391. char *rsa_ssh1_fingerprint(struct RSAKey *key);
  392. int rsa_verify(struct RSAKey *key);
  393. void rsa_ssh1_public_blob(BinarySink *bs, struct RSAKey *key,
  394. RsaSsh1Order order);
  395. int rsa_ssh1_public_blob_len(void *data, int maxlen);
  396. void freersakey(struct RSAKey *key);
  397. #endif // WINSCP_VS
  398. typedef uint32 word32;
  399. #ifndef WINSCP_VS
  400. unsigned long crc32_compute(const void *s, size_t len);
  401. unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len);
  402. /* SSH CRC compensation attack detector */
  403. struct crcda_ctx;
  404. struct crcda_ctx *crcda_make_context(void);
  405. void crcda_free_context(struct crcda_ctx *ctx);
  406. int detect_attack(struct crcda_ctx *ctx, unsigned char *buf, uint32 len,
  407. unsigned char *IV);
  408. /*
  409. * SSH2 RSA key exchange functions
  410. */
  411. struct ssh_hashalg;
  412. struct RSAKey *ssh_rsakex_newkey(const void *data, int len);
  413. void ssh_rsakex_freekey(struct RSAKey *key);
  414. int ssh_rsakex_klen(struct RSAKey *key);
  415. void ssh_rsakex_encrypt(const struct ssh_hashalg *h,
  416. unsigned char *in, int inlen,
  417. unsigned char *out, int outlen, struct RSAKey *key);
  418. /*
  419. * SSH2 ECDH key exchange functions
  420. */
  421. struct ssh_kex;
  422. const char *ssh_ecdhkex_curve_textname(const struct ssh_kex *kex);
  423. struct ec_key *ssh_ecdhkex_newkey(const struct ssh_kex *kex);
  424. void ssh_ecdhkex_freekey(struct ec_key *key);
  425. void ssh_ecdhkex_getpublic(struct ec_key *key, BinarySink *bs);
  426. Bignum ssh_ecdhkex_getkey(struct ec_key *key,
  427. const void *remoteKey, int remoteKeyLen);
  428. /*
  429. * Helper function for k generation in DSA, reused in ECDSA
  430. */
  431. Bignum *dss_gen_k(const char *id_string, Bignum modulus, Bignum private_key,
  432. unsigned char *digest, int digest_len);
  433. struct ssh2_cipheralg;
  434. typedef const struct ssh2_cipheralg *ssh2_cipher;
  435. typedef struct {
  436. uint32 h[4];
  437. } MD5_Core_State;
  438. struct MD5Context {
  439. MD5_Core_State core;
  440. unsigned char block[64];
  441. int blkused;
  442. uint32 lenhi, lenlo;
  443. BinarySink_IMPLEMENTATION;
  444. };
  445. void MD5Init(struct MD5Context *context);
  446. void MD5Final(unsigned char digest[16], struct MD5Context *context);
  447. void MD5Simple(void const *p, unsigned len, unsigned char output[16]);
  448. struct hmacmd5_context;
  449. struct hmacmd5_context *hmacmd5_make_context(void);
  450. void hmacmd5_free_context(struct hmacmd5_context *ctx);
  451. void hmacmd5_key(struct hmacmd5_context *ctx, void const *key, int len);
  452. void hmacmd5_do_hmac(struct hmacmd5_context *ctx,
  453. const void *blk, int len, unsigned char *hmac);
  454. int supports_sha_ni(void);
  455. #ifdef MPEXT
  456. // Resolve ambiguity with OpenSSL
  457. #define SHA_Init putty_SHA_Init
  458. #define SHA_Final putty_SHA_Final
  459. #define SHA256_Init putty_SHA256_Init
  460. #define SHA256_Final putty_SHA256_Final
  461. #define SHA512_Init putty_SHA512_Init
  462. #define SHA512_Final putty_SHA512_Final
  463. #endif
  464. typedef struct SHA_State {
  465. uint32 h[5];
  466. unsigned char block[64];
  467. int blkused;
  468. uint32 lenhi, lenlo;
  469. void (*sha1)(struct SHA_State * s, const unsigned char *p, int len);
  470. BinarySink_IMPLEMENTATION;
  471. } SHA_State;
  472. void SHA_Init(SHA_State * s);
  473. void SHA_Final(SHA_State * s, unsigned char *output);
  474. void SHA_Simple(const void *p, int len, unsigned char *output);
  475. void hmac_sha1_simple(const void *key, int keylen,
  476. const void *data, int datalen,
  477. unsigned char *output);
  478. #endif // WINSCP_VS
  479. typedef struct SHA256_State {
  480. uint32 h[8];
  481. unsigned char block[64];
  482. int blkused;
  483. uint32 lenhi, lenlo;
  484. void (*sha256)(struct SHA256_State * s, const unsigned char *p, int len);
  485. BinarySink_IMPLEMENTATION;
  486. } SHA256_State;
  487. #ifndef WINSCP_VS
  488. void SHA256_Init(SHA256_State * s);
  489. void SHA256_Final(SHA256_State * s, unsigned char *output);
  490. void SHA256_Simple(const void *p, int len, unsigned char *output);
  491. typedef struct {
  492. uint64 h[8];
  493. unsigned char block[128];
  494. int blkused;
  495. uint32 len[4];
  496. BinarySink_IMPLEMENTATION;
  497. } SHA512_State;
  498. #define SHA384_State SHA512_State
  499. void SHA512_Init(SHA512_State * s);
  500. void SHA512_Final(SHA512_State * s, unsigned char *output);
  501. void SHA512_Simple(const void *p, int len, unsigned char *output);
  502. void SHA384_Init(SHA384_State * s);
  503. void SHA384_Final(SHA384_State * s, unsigned char *output);
  504. void SHA384_Simple(const void *p, int len, unsigned char *output);
  505. struct ssh2_macalg;
  506. struct ssh1_cipheralg;
  507. typedef const struct ssh1_cipheralg *ssh1_cipher;
  508. struct ssh1_cipheralg {
  509. ssh1_cipher *(*new)(void);
  510. void (*free)(ssh1_cipher *);
  511. void (*sesskey)(ssh1_cipher *, const void *key);
  512. void (*encrypt)(ssh1_cipher *, void *blk, int len);
  513. void (*decrypt)(ssh1_cipher *, void *blk, int len);
  514. int blksize;
  515. const char *text_name;
  516. };
  517. #define ssh1_cipher_new(alg) ((alg)->new())
  518. #define ssh1_cipher_free(ctx) ((*(ctx))->free(ctx))
  519. #define ssh1_cipher_sesskey(ctx, key) ((*(ctx))->sesskey(ctx, key))
  520. #define ssh1_cipher_encrypt(ctx, blk, len) ((*(ctx))->encrypt(ctx, blk, len))
  521. #define ssh1_cipher_decrypt(ctx, blk, len) ((*(ctx))->decrypt(ctx, blk, len))
  522. struct ssh2_cipheralg {
  523. ssh2_cipher *(*new)(const struct ssh2_cipheralg *alg);
  524. void (*free)(ssh2_cipher *);
  525. void (*setiv)(ssh2_cipher *, const void *iv);
  526. void (*setkey)(ssh2_cipher *, const void *key);
  527. void (*encrypt)(ssh2_cipher *, void *blk, int len);
  528. void (*decrypt)(ssh2_cipher *, void *blk, int len);
  529. /* Ignored unless SSH_CIPHER_SEPARATE_LENGTH flag set */
  530. void (*encrypt_length)(ssh2_cipher *, void *blk, int len,
  531. unsigned long seq);
  532. void (*decrypt_length)(ssh2_cipher *, void *blk, int len,
  533. unsigned long seq);
  534. const char *name;
  535. int blksize;
  536. /* real_keybits is the number of bits of entropy genuinely used by
  537. * the cipher scheme; it's used for deciding how big a
  538. * Diffie-Hellman group is needed to exchange a key for the
  539. * cipher. */
  540. int real_keybits;
  541. /* padded_keybytes is the number of bytes of key data expected as
  542. * input to the setkey function; it's used for deciding how much
  543. * data needs to be generated from the post-kex generation of key
  544. * material. In a sensible cipher which uses all its key bytes for
  545. * real work, this will just be real_keybits/8, but in DES-type
  546. * ciphers which ignore one bit in each byte, it'll be slightly
  547. * different. */
  548. int padded_keybytes;
  549. unsigned int flags;
  550. #define SSH_CIPHER_IS_CBC 1
  551. #define SSH_CIPHER_SEPARATE_LENGTH 2
  552. const char *text_name;
  553. /* If set, this takes priority over other MAC. */
  554. const struct ssh2_macalg *required_mac;
  555. };
  556. #define ssh2_cipher_new(alg) ((alg)->new(alg))
  557. #define ssh2_cipher_free(ctx) ((*(ctx))->free(ctx))
  558. #define ssh2_cipher_setiv(ctx, iv) ((*(ctx))->setiv(ctx, iv))
  559. #define ssh2_cipher_setkey(ctx, key) ((*(ctx))->setkey(ctx, key))
  560. #define ssh2_cipher_encrypt(ctx, blk, len) ((*(ctx))->encrypt(ctx, blk, len))
  561. #define ssh2_cipher_decrypt(ctx, blk, len) ((*(ctx))->decrypt(ctx, blk, len))
  562. #define ssh2_cipher_encrypt_length(ctx, blk, len, seq) \
  563. ((*(ctx))->encrypt_length(ctx, blk, len, seq))
  564. #define ssh2_cipher_decrypt_length(ctx, blk, len, seq) \
  565. ((*(ctx))->decrypt_length(ctx, blk, len, seq))
  566. #define ssh2_cipher_alg(ctx) (*(ctx))
  567. struct ssh2_ciphers {
  568. int nciphers;
  569. const struct ssh2_cipheralg *const *list;
  570. };
  571. struct ssh2_macalg;
  572. typedef struct ssh2_mac {
  573. const struct ssh2_macalg *vt;
  574. BinarySink_DELEGATE_IMPLEMENTATION;
  575. } ssh2_mac;
  576. struct ssh2_macalg {
  577. /* Passes in the cipher context */
  578. ssh2_mac *(*new)(const struct ssh2_macalg *alg, ssh2_cipher *cipher);
  579. void (*free)(ssh2_mac *);
  580. void (*setkey)(ssh2_mac *, const void *key);
  581. void (*start)(ssh2_mac *);
  582. void (*genresult)(ssh2_mac *, unsigned char *);
  583. const char *name, *etm_name;
  584. int len, keylen;
  585. const char *text_name;
  586. };
  587. #define ssh2_mac_new(alg, cipher) ((alg)->new(alg, cipher))
  588. #define ssh2_mac_free(ctx) ((ctx)->vt->free(ctx))
  589. #define ssh2_mac_setkey(ctx, key) ((ctx)->vt->free(ctx, key))
  590. #define ssh2_mac_start(ctx) ((ctx)->vt->start(ctx))
  591. #define ssh2_mac_genresult(ctx, out) ((ctx)->vt->genresult(ctx, out))
  592. #define ssh2_mac_alg(ctx) ((ctx)->vt)
  593. /* Centralised 'methods' for ssh2_mac, defined in sshmac.c */
  594. int ssh2_mac_verresult(ssh2_mac *, const void *);
  595. void ssh2_mac_generate(ssh2_mac *, void *, int, unsigned long seq);
  596. int ssh2_mac_verify(ssh2_mac *, const void *, int, unsigned long seq);
  597. typedef struct ssh_hash {
  598. const struct ssh_hashalg *vt;
  599. BinarySink_DELEGATE_IMPLEMENTATION;
  600. } ssh_hash;
  601. struct ssh_hashalg {
  602. ssh_hash *(*new)(const struct ssh_hashalg *alg);
  603. ssh_hash *(*copy)(ssh_hash *);
  604. void (*final)(ssh_hash *, unsigned char *); /* ALSO FREES THE ssh_hash! */
  605. void (*free)(ssh_hash *);
  606. int hlen; /* output length in bytes */
  607. const char *text_name;
  608. };
  609. #define ssh_hash_new(alg) ((alg)->new(alg))
  610. #define ssh_hash_copy(ctx) ((ctx)->vt->copy(ctx))
  611. #define ssh_hash_final(ctx, out) ((ctx)->vt->final(ctx, out))
  612. #define ssh_hash_free(ctx) ((ctx)->vt->free(ctx))
  613. #define ssh_hash_alg(ctx) ((ctx)->vt)
  614. struct ssh_kex {
  615. const char *name, *groupname;
  616. enum { KEXTYPE_DH, KEXTYPE_RSA, KEXTYPE_ECDH, KEXTYPE_GSS } main_type;
  617. const struct ssh_hashalg *hash;
  618. const void *extra; /* private to the kex methods */
  619. };
  620. struct ssh_kexes {
  621. int nkexes;
  622. const struct ssh_kex *const *list;
  623. };
  624. struct ssh_keyalg {
  625. /* Constructors that create an ssh_key */
  626. ssh_key *(*new_pub) (const ssh_keyalg *self, ptrlen pub);
  627. ssh_key *(*new_priv) (const ssh_keyalg *self, ptrlen pub, ptrlen priv);
  628. ssh_key *(*new_priv_openssh) (const ssh_keyalg *self, BinarySource *);
  629. /* Methods that operate on an existing ssh_key */
  630. void (*freekey) (ssh_key *key);
  631. void (*sign) (ssh_key *key, const void *data, int datalen, BinarySink *);
  632. int (*verify) (ssh_key *key, ptrlen sig, ptrlen data);
  633. void (*public_blob)(ssh_key *key, BinarySink *);
  634. void (*private_blob)(ssh_key *key, BinarySink *);
  635. void (*openssh_blob) (ssh_key *key, BinarySink *);
  636. char *(*cache_str) (ssh_key *key);
  637. /* 'Class methods' that don't deal with an ssh_key at all */
  638. int (*pubkey_bits) (const ssh_keyalg *self, ptrlen blob);
  639. /* Constant data fields giving information about the key type */
  640. const char *ssh_id; /* string identifier in the SSH protocol */
  641. const char *cache_id; /* identifier used in PuTTY's host key cache */
  642. const void *extra; /* private to the public key methods */
  643. };
  644. #define ssh_key_new_pub(alg, data) ((alg)->new_pub(alg, data))
  645. #define ssh_key_new_priv(alg, pub, priv) ((alg)->new_priv(alg, pub, priv))
  646. #define ssh_key_new_priv_openssh(alg, bs) ((alg)->new_priv_openssh(alg, bs))
  647. #define ssh_key_free(key) ((*(key))->freekey(key))
  648. #define ssh_key_sign(key, data, len, bs) ((*(key))->sign(key, data, len, bs))
  649. #define ssh_key_verify(key, sig, data) ((*(key))->verify(key, sig, data))
  650. #define ssh_key_public_blob(key, bs) ((*(key))->public_blob(key, bs))
  651. #define ssh_key_private_blob(key, bs) ((*(key))->private_blob(key, bs))
  652. #define ssh_key_openssh_blob(key, bs) ((*(key))->openssh_blob(key, bs))
  653. #define ssh_key_cache_str(key) ((*(key))->cache_str(key))
  654. #define ssh_key_public_bits(alg, blob) ((alg)->pubkey_bits(alg, blob))
  655. #define ssh_key_alg(key) (*(key))
  656. #define ssh_key_ssh_id(key) ((*(key))->ssh_id)
  657. #define ssh_key_cache_id(key) ((*(key))->cache_id)
  658. typedef struct ssh_compressor {
  659. const struct ssh_compression_alg *vt;
  660. } ssh_compressor;
  661. typedef struct ssh_decompressor {
  662. const struct ssh_compression_alg *vt;
  663. } ssh_decompressor;
  664. struct ssh_compression_alg {
  665. const char *name;
  666. /* For [email protected]: if non-NULL, this name will be considered once
  667. * userauth has completed successfully. */
  668. const char *delayed_name;
  669. ssh_compressor *(*compress_new)(void);
  670. void (*compress_free)(ssh_compressor *);
  671. void (*compress)(ssh_compressor *, unsigned char *block, int len,
  672. unsigned char **outblock, int *outlen,
  673. int minlen);
  674. ssh_decompressor *(*decompress_new)(void);
  675. void (*decompress_free)(ssh_decompressor *);
  676. int (*decompress)(ssh_decompressor *, unsigned char *block, int len,
  677. unsigned char **outblock, int *outlen);
  678. const char *text_name;
  679. };
  680. #define ssh_compressor_new(alg) ((alg)->compress_new())
  681. #define ssh_compressor_free(comp) ((comp)->vt->compress_free(comp))
  682. #define ssh_compressor_compress(comp, in, inlen, out, outlen, minlen) \
  683. ((comp)->vt->compress(comp, in, inlen, out, outlen, minlen))
  684. #define ssh_compressor_alg(comp) ((comp)->vt)
  685. #define ssh_decompressor_new(alg) ((alg)->decompress_new())
  686. #define ssh_decompressor_free(comp) ((comp)->vt->decompress_free(comp))
  687. #define ssh_decompressor_decompress(comp, in, inlen, out, outlen) \
  688. ((comp)->vt->decompress(comp, in, inlen, out, outlen))
  689. #define ssh_decompressor_alg(comp) ((comp)->vt)
  690. struct ssh2_userkey {
  691. ssh_key *key; /* the key itself */
  692. char *comment; /* the key comment */
  693. };
  694. /* The maximum length of any hash algorithm used in kex. (bytes) */
  695. #define SSH2_KEX_MAX_HASH_LEN (64) /* SHA-512 */
  696. extern const struct ssh1_cipheralg ssh1_3des;
  697. extern const struct ssh1_cipheralg ssh1_des;
  698. extern const struct ssh1_cipheralg ssh1_blowfish;
  699. extern const struct ssh2_ciphers ssh2_3des;
  700. extern const struct ssh2_ciphers ssh2_des;
  701. extern const struct ssh2_ciphers ssh2_aes;
  702. extern const struct ssh2_ciphers ssh2_blowfish;
  703. extern const struct ssh2_ciphers ssh2_arcfour;
  704. extern const struct ssh2_ciphers ssh2_ccp;
  705. extern const struct ssh_hashalg ssh_sha1;
  706. extern const struct ssh_hashalg ssh_sha256;
  707. extern const struct ssh_hashalg ssh_sha384;
  708. extern const struct ssh_hashalg ssh_sha512;
  709. extern const struct ssh_kexes ssh_diffiehellman_group1;
  710. extern const struct ssh_kexes ssh_diffiehellman_group14;
  711. extern const struct ssh_kexes ssh_diffiehellman_gex;
  712. extern const struct ssh_kexes ssh_gssk5_sha1_kex;
  713. extern const struct ssh_kexes ssh_rsa_kex;
  714. extern const struct ssh_kexes ssh_ecdh_kex;
  715. extern const ssh_keyalg ssh_dss;
  716. extern const ssh_keyalg ssh_rsa;
  717. extern const ssh_keyalg ssh_ecdsa_ed25519;
  718. extern const ssh_keyalg ssh_ecdsa_nistp256;
  719. extern const ssh_keyalg ssh_ecdsa_nistp384;
  720. extern const ssh_keyalg ssh_ecdsa_nistp521;
  721. extern const struct ssh2_macalg ssh_hmac_md5;
  722. extern const struct ssh2_macalg ssh_hmac_sha1;
  723. extern const struct ssh2_macalg ssh_hmac_sha1_buggy;
  724. extern const struct ssh2_macalg ssh_hmac_sha1_96;
  725. extern const struct ssh2_macalg ssh_hmac_sha1_96_buggy;
  726. extern const struct ssh2_macalg ssh_hmac_sha256;
  727. extern const struct ssh_compression_alg ssh_zlib;
  728. typedef struct AESContext AESContext;
  729. AESContext *aes_make_context(void);
  730. void aes_free_context(AESContext *ctx);
  731. void aes128_key(AESContext *ctx, const void *key);
  732. void aes192_key(AESContext *ctx, const void *key);
  733. void aes256_key(AESContext *ctx, const void *key);
  734. void aes_iv(AESContext *ctx, const void *iv);
  735. void aes_ssh2_encrypt_blk(AESContext *ctx, void *blk, int len);
  736. void aes_ssh2_decrypt_blk(AESContext *ctx, void *blk, int len);
  737. void aes_ssh2_sdctr(AESContext *ctx, void *blk, int len);
  738. /*
  739. * PuTTY version number formatted as an SSH version string.
  740. */
  741. extern
  742. #ifndef MPEXT
  743. const
  744. #endif
  745. char sshver[];
  746. /*
  747. * Gross hack: pscp will try to start SFTP but fall back to scp1 if
  748. * that fails. This variable is the means by which scp.c can reach
  749. * into the SSH code and find out which one it got.
  750. */
  751. extern int ssh_fallback_cmd(Backend *backend);
  752. void SHATransform(word32 * digest, word32 * data);
  753. /*
  754. * Check of compiler version
  755. */
  756. #ifdef _FORCE_SHA_NI
  757. # define COMPILER_SUPPORTS_SHA_NI
  758. #elif defined(__clang__)
  759. # if __has_attribute(target) && __has_include(<shaintrin.h>) && (defined(__x86_64__) || defined(__i386))
  760. # define COMPILER_SUPPORTS_SHA_NI
  761. # endif
  762. #elif defined(__GNUC__)
  763. # if ((__GNUC__ >= 5) && (defined(__x86_64__) || defined(__i386)))
  764. # define COMPILER_SUPPORTS_SHA_NI
  765. # endif
  766. #elif defined (_MSC_VER)
  767. # if (defined(_M_X64) || defined(_M_IX86)) && _MSC_VER >= 1900
  768. # define COMPILER_SUPPORTS_SHA_NI
  769. # endif
  770. #endif
  771. #ifdef _FORCE_SOFTWARE_SHA
  772. # undef COMPILER_SUPPORTS_SHA_NI
  773. #endif
  774. int random_byte(void);
  775. void random_add_noise(void *noise, int length);
  776. void random_add_heavynoise(void *noise, int length);
  777. /* Exports from x11fwd.c */
  778. enum {
  779. X11_TRANS_IPV4 = 0, X11_TRANS_IPV6 = 6, X11_TRANS_UNIX = 256
  780. };
  781. struct X11Display {
  782. /* Broken-down components of the display name itself */
  783. int unixdomain;
  784. char *hostname;
  785. int displaynum;
  786. int screennum;
  787. /* OSX sometimes replaces all the above with a full Unix-socket pathname */
  788. char *unixsocketpath;
  789. /* PuTTY networking SockAddr to connect to the display, and associated
  790. * gubbins */
  791. SockAddr *addr;
  792. int port;
  793. char *realhost;
  794. /* Our local auth details for talking to the real X display. */
  795. int localauthproto;
  796. unsigned char *localauthdata;
  797. int localauthdatalen;
  798. };
  799. struct X11FakeAuth {
  800. /* Auth details we invented for a virtual display on the SSH server. */
  801. int proto;
  802. unsigned char *data;
  803. int datalen;
  804. char *protoname;
  805. char *datastring;
  806. /* The encrypted form of the first block, in XDM-AUTHORIZATION-1.
  807. * Used as part of the key when these structures are organised
  808. * into a tree. See x11_invent_fake_auth for explanation. */
  809. unsigned char *xa1_firstblock;
  810. /*
  811. * Used inside x11fwd.c to remember recently seen
  812. * XDM-AUTHORIZATION-1 strings, to avoid replay attacks.
  813. */
  814. tree234 *xdmseen;
  815. /*
  816. * What to do with an X connection matching this auth data.
  817. */
  818. struct X11Display *disp;
  819. ssh_sharing_connstate *share_cs;
  820. share_channel *share_chan;
  821. };
  822. void *x11_make_greeting(int endian, int protomajor, int protominor,
  823. int auth_proto, const void *auth_data, int auth_len,
  824. const char *peer_ip, int peer_port,
  825. int *outlen);
  826. int x11_authcmp(void *av, void *bv); /* for putting X11FakeAuth in a tree234 */
  827. /*
  828. * x11_setup_display() parses the display variable and fills in an
  829. * X11Display structure. Some remote auth details are invented;
  830. * the supplied authtype parameter configures the preferred
  831. * authorisation protocol to use at the remote end. The local auth
  832. * details are looked up by calling platform_get_x11_auth.
  833. *
  834. * If the returned pointer is NULL, then *error_msg will contain a
  835. * dynamically allocated error message string.
  836. */
  837. extern struct X11Display *x11_setup_display(const char *display, Conf *,
  838. char **error_msg);
  839. void x11_free_display(struct X11Display *disp);
  840. struct X11FakeAuth *x11_invent_fake_auth(tree234 *t, int authtype);
  841. void x11_free_fake_auth(struct X11FakeAuth *auth);
  842. Channel *x11_new_channel(tree234 *authtree, SshChannel *c,
  843. const char *peeraddr, int peerport,
  844. int connection_sharing_possible);
  845. char *x11_display(const char *display);
  846. /* Platform-dependent X11 functions */
  847. extern void platform_get_x11_auth(struct X11Display *display, Conf *);
  848. /* examine a mostly-filled-in X11Display and fill in localauth* */
  849. extern const int platform_uses_x11_unix_by_default;
  850. /* choose default X transport in the absence of a specified one */
  851. SockAddr *platform_get_x11_unix_address(const char *path, int displaynum);
  852. /* make up a SockAddr naming the address for displaynum */
  853. char *platform_get_x_display(void);
  854. /* allocated local X display string, if any */
  855. /* Callbacks in x11.c usable _by_ platform X11 functions */
  856. /*
  857. * This function does the job of platform_get_x11_auth, provided
  858. * it is told where to find a normally formatted .Xauthority file:
  859. * it opens that file, parses it to find an auth record which
  860. * matches the display details in "display", and fills in the
  861. * localauth fields.
  862. *
  863. * It is expected that most implementations of
  864. * platform_get_x11_auth() will work by finding their system's
  865. * .Xauthority file, adjusting the display details if necessary
  866. * for local oddities like Unix-domain socket transport, and
  867. * calling this function to do the rest of the work.
  868. */
  869. void x11_get_auth_from_authfile(struct X11Display *display,
  870. const char *authfilename);
  871. int x11_identify_auth_proto(ptrlen protoname);
  872. void *x11_dehexify(ptrlen hex, int *outlen);
  873. Channel *agentf_new(SshChannel *c);
  874. Bignum copybn(Bignum b);
  875. Bignum bn_power_2(int n);
  876. void bn_restore_invariant(Bignum b);
  877. Bignum bignum_from_long(unsigned long n);
  878. void freebn(Bignum b);
  879. Bignum modpow(Bignum base, Bignum exp, Bignum mod);
  880. Bignum modmul(Bignum a, Bignum b, Bignum mod);
  881. Bignum modsub(const Bignum a, const Bignum b, const Bignum n);
  882. void decbn(Bignum n);
  883. extern Bignum Zero, One;
  884. Bignum bignum_from_bytes(const void *data, int nbytes);
  885. Bignum bignum_from_bytes_le(const void *data, int nbytes);
  886. Bignum bignum_random_in_range(const Bignum lower, const Bignum upper);
  887. int bignum_bitcount(Bignum bn);
  888. int bignum_byte(Bignum bn, int i);
  889. int bignum_bit(Bignum bn, int i);
  890. void bignum_set_bit(Bignum bn, int i, int value);
  891. Bignum biggcd(Bignum a, Bignum b);
  892. unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
  893. Bignum bignum_add_long(Bignum number, unsigned long addend);
  894. Bignum bigadd(Bignum a, Bignum b);
  895. Bignum bigsub(Bignum a, Bignum b);
  896. Bignum bigmul(Bignum a, Bignum b);
  897. Bignum bigmuladd(Bignum a, Bignum b, Bignum addend);
  898. Bignum bigdiv(Bignum a, Bignum b);
  899. Bignum bigmod(Bignum a, Bignum b);
  900. Bignum modinv(Bignum number, Bignum modulus);
  901. Bignum bignum_bitmask(Bignum number);
  902. Bignum bignum_rshift(Bignum number, int shift);
  903. Bignum bignum_lshift(Bignum number, int shift);
  904. int bignum_cmp(Bignum a, Bignum b);
  905. char *bignum_decimal(Bignum x);
  906. Bignum bignum_from_decimal(const char *decimal);
  907. void BinarySink_put_mp_ssh1(BinarySink *, Bignum);
  908. void BinarySink_put_mp_ssh2(BinarySink *, Bignum);
  909. Bignum BinarySource_get_mp_ssh1(BinarySource *);
  910. Bignum BinarySource_get_mp_ssh2(BinarySource *);
  911. #ifdef DEBUG
  912. void diagbn(char *prefix, Bignum md);
  913. #endif
  914. int dh_is_gex(const struct ssh_kex *kex);
  915. struct dh_ctx;
  916. struct dh_ctx *dh_setup_group(const struct ssh_kex *kex);
  917. struct dh_ctx *dh_setup_gex(Bignum pval, Bignum gval);
  918. void dh_cleanup(struct dh_ctx *);
  919. Bignum dh_create_e(struct dh_ctx *, int nbits);
  920. const char *dh_validate_f(struct dh_ctx *, Bignum f);
  921. Bignum dh_find_K(struct dh_ctx *, Bignum f);
  922. int rsa_ssh1_encrypted(const Filename *filename, char **comment);
  923. int rsa_ssh1_loadpub(const Filename *filename, BinarySink *bs,
  924. char **commentptr, const char **errorstr);
  925. int rsa_ssh1_loadkey(const Filename *filename, struct RSAKey *key,
  926. const char *passphrase, const char **errorstr);
  927. int rsa_ssh1_savekey(const Filename *filename, struct RSAKey *key,
  928. char *passphrase);
  929. extern int base64_decode_atom(const char *atom, unsigned char *out);
  930. extern int base64_lines(int datalen);
  931. extern void base64_encode_atom(const unsigned char *data, int n, char *out);
  932. extern void base64_encode(FILE *fp, const unsigned char *data, int datalen,
  933. int cpl);
  934. /* ssh2_load_userkey can return this as an error */
  935. extern struct ssh2_userkey ssh2_wrong_passphrase;
  936. #define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
  937. int ssh2_userkey_encrypted(const Filename *filename, char **comment);
  938. struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
  939. const char *passphrase,
  940. const char **errorstr);
  941. int ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
  942. BinarySink *bs,
  943. char **commentptr, const char **errorstr);
  944. int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
  945. char *passphrase);
  946. const ssh_keyalg *find_pubkey_alg(const char *name);
  947. const ssh_keyalg *find_pubkey_alg_len(ptrlen name);
  948. enum {
  949. SSH_KEYTYPE_UNOPENABLE,
  950. SSH_KEYTYPE_UNKNOWN,
  951. SSH_KEYTYPE_SSH1, SSH_KEYTYPE_SSH2,
  952. /*
  953. * The OpenSSH key types deserve a little explanation. OpenSSH has
  954. * two physical formats for private key storage: an old PEM-based
  955. * one largely dictated by their use of OpenSSL and full of ASN.1,
  956. * and a new one using the same private key formats used over the
  957. * wire for talking to ssh-agent. The old format can only support
  958. * a subset of the key types, because it needs redesign for each
  959. * key type, and after a while they decided to move to the new
  960. * format so as not to have to do that.
  961. *
  962. * On input, key files are identified as either
  963. * SSH_KEYTYPE_OPENSSH_PEM or SSH_KEYTYPE_OPENSSH_NEW, describing
  964. * accurately which actual format the keys are stored in.
  965. *
  966. * On output, however, we default to following OpenSSH's own
  967. * policy of writing out PEM-style keys for maximum backwards
  968. * compatibility if the key type supports it, and otherwise
  969. * switching to the new format. So the formats you can select for
  970. * output are SSH_KEYTYPE_OPENSSH_NEW (forcing the new format for
  971. * any key type), and SSH_KEYTYPE_OPENSSH_AUTO to use the oldest
  972. * format supported by whatever key type you're writing out.
  973. *
  974. * So we have three type codes, but only two of them usable in any
  975. * given circumstance. An input key file will never be identified
  976. * as AUTO, only PEM or NEW; key export UIs should not be able to
  977. * select PEM, only AUTO or NEW.
  978. */
  979. SSH_KEYTYPE_OPENSSH_AUTO,
  980. SSH_KEYTYPE_OPENSSH_PEM,
  981. SSH_KEYTYPE_OPENSSH_NEW,
  982. SSH_KEYTYPE_SSHCOM,
  983. /*
  984. * Public-key-only formats, which we still want to be able to read
  985. * for various purposes.
  986. */
  987. SSH_KEYTYPE_SSH1_PUBLIC,
  988. SSH_KEYTYPE_SSH2_PUBLIC_RFC4716,
  989. SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH
  990. };
  991. char *ssh1_pubkey_str(struct RSAKey *ssh1key);
  992. void ssh1_write_pubkey(FILE *fp, struct RSAKey *ssh1key);
  993. char *ssh2_pubkey_openssh_str(struct ssh2_userkey *key);
  994. void ssh2_write_pubkey(FILE *fp, const char *comment,
  995. const void *v_pub_blob, int pub_len,
  996. int keytype);
  997. char *ssh2_fingerprint_blob(const void *blob, int bloblen);
  998. char *ssh2_fingerprint(ssh_key *key);
  999. int key_type(const Filename *filename);
  1000. const char *key_type_to_str(int type);
  1001. #ifdef MPEXT
  1002. int openssh_loadpub_line(char * line, char **algorithm,
  1003. BinarySink *bs,
  1004. char **commentptr, const char **errorstr);
  1005. #endif
  1006. int import_possible(int type);
  1007. int import_target_type(int type);
  1008. int import_encrypted(const Filename *filename, int type, char **comment);
  1009. int import_ssh1(const Filename *filename, int type,
  1010. struct RSAKey *key, char *passphrase, const char **errmsg_p);
  1011. struct ssh2_userkey *import_ssh2(const Filename *filename, int type,
  1012. char *passphrase, const char **errmsg_p);
  1013. int export_ssh1(const Filename *filename, int type,
  1014. struct RSAKey *key, char *passphrase);
  1015. int export_ssh2(const Filename *filename, int type,
  1016. struct ssh2_userkey *key, char *passphrase);
  1017. void des3_decrypt_pubkey(const void *key, void *blk, int len);
  1018. void des3_encrypt_pubkey(const void *key, void *blk, int len);
  1019. void des3_decrypt_pubkey_ossh(const void *key, const void *iv,
  1020. void *blk, int len);
  1021. void des3_encrypt_pubkey_ossh(const void *key, const void *iv,
  1022. void *blk, int len);
  1023. void aes256_encrypt_pubkey(const void *key, void *blk, int len);
  1024. void aes256_decrypt_pubkey(const void *key, void *blk, int len);
  1025. void des_encrypt_xdmauth(const void *key, void *blk, int len);
  1026. void des_decrypt_xdmauth(const void *key, void *blk, int len);
  1027. void openssh_bcrypt(const char *passphrase,
  1028. const unsigned char *salt, int saltbytes,
  1029. int rounds, unsigned char *out, int outbytes);
  1030. /*
  1031. * For progress updates in the key generation utility.
  1032. */
  1033. #define PROGFN_INITIALISE 1
  1034. #define PROGFN_LIN_PHASE 2
  1035. #define PROGFN_EXP_PHASE 3
  1036. #define PROGFN_PHASE_EXTENT 4
  1037. #define PROGFN_READY 5
  1038. #define PROGFN_PROGRESS 6
  1039. typedef void (*progfn_t) (void *param, int action, int phase, int progress);
  1040. int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
  1041. void *pfnparam);
  1042. int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
  1043. void *pfnparam);
  1044. int ec_generate(struct ec_key *key, int bits, progfn_t pfn,
  1045. void *pfnparam);
  1046. int ec_edgenerate(struct ec_key *key, int bits, progfn_t pfn,
  1047. void *pfnparam);
  1048. Bignum primegen(int bits, int modulus, int residue, Bignum factor,
  1049. int phase, progfn_t pfn, void *pfnparam, unsigned firstbits);
  1050. void invent_firstbits(unsigned *one, unsigned *two);
  1051. /*
  1052. * Connection-sharing API provided by platforms. This function must
  1053. * either:
  1054. * - return SHARE_NONE and do nothing
  1055. * - return SHARE_DOWNSTREAM and set *sock to a Socket connected to
  1056. * downplug
  1057. * - return SHARE_UPSTREAM and set *sock to a Socket connected to
  1058. * upplug.
  1059. */
  1060. enum { SHARE_NONE, SHARE_DOWNSTREAM, SHARE_UPSTREAM };
  1061. int platform_ssh_share(const char *name, Conf *conf,
  1062. Plug *downplug, Plug *upplug, Socket **sock,
  1063. char **logtext, char **ds_err, char **us_err,
  1064. int can_upstream, int can_downstream);
  1065. void platform_ssh_share_cleanup(const char *name);
  1066. /*
  1067. * List macro defining the SSH-1 message type codes.
  1068. */
  1069. #define SSH1_MESSAGE_TYPES(X, y) \
  1070. X(y, SSH1_MSG_DISCONNECT, 1) \
  1071. X(y, SSH1_SMSG_PUBLIC_KEY, 2) \
  1072. X(y, SSH1_CMSG_SESSION_KEY, 3) \
  1073. X(y, SSH1_CMSG_USER, 4) \
  1074. X(y, SSH1_CMSG_AUTH_RSA, 6) \
  1075. X(y, SSH1_SMSG_AUTH_RSA_CHALLENGE, 7) \
  1076. X(y, SSH1_CMSG_AUTH_RSA_RESPONSE, 8) \
  1077. X(y, SSH1_CMSG_AUTH_PASSWORD, 9) \
  1078. X(y, SSH1_CMSG_REQUEST_PTY, 10) \
  1079. X(y, SSH1_CMSG_WINDOW_SIZE, 11) \
  1080. X(y, SSH1_CMSG_EXEC_SHELL, 12) \
  1081. X(y, SSH1_CMSG_EXEC_CMD, 13) \
  1082. X(y, SSH1_SMSG_SUCCESS, 14) \
  1083. X(y, SSH1_SMSG_FAILURE, 15) \
  1084. X(y, SSH1_CMSG_STDIN_DATA, 16) \
  1085. X(y, SSH1_SMSG_STDOUT_DATA, 17) \
  1086. X(y, SSH1_SMSG_STDERR_DATA, 18) \
  1087. X(y, SSH1_CMSG_EOF, 19) \
  1088. X(y, SSH1_SMSG_EXIT_STATUS, 20) \
  1089. X(y, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, 21) \
  1090. X(y, SSH1_MSG_CHANNEL_OPEN_FAILURE, 22) \
  1091. X(y, SSH1_MSG_CHANNEL_DATA, 23) \
  1092. X(y, SSH1_MSG_CHANNEL_CLOSE, 24) \
  1093. X(y, SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION, 25) \
  1094. X(y, SSH1_SMSG_X11_OPEN, 27) \
  1095. X(y, SSH1_CMSG_PORT_FORWARD_REQUEST, 28) \
  1096. X(y, SSH1_MSG_PORT_OPEN, 29) \
  1097. X(y, SSH1_CMSG_AGENT_REQUEST_FORWARDING, 30) \
  1098. X(y, SSH1_SMSG_AGENT_OPEN, 31) \
  1099. X(y, SSH1_MSG_IGNORE, 32) \
  1100. X(y, SSH1_CMSG_EXIT_CONFIRMATION, 33) \
  1101. X(y, SSH1_CMSG_X11_REQUEST_FORWARDING, 34) \
  1102. X(y, SSH1_CMSG_AUTH_RHOSTS_RSA, 35) \
  1103. X(y, SSH1_MSG_DEBUG, 36) \
  1104. X(y, SSH1_CMSG_REQUEST_COMPRESSION, 37) \
  1105. X(y, SSH1_CMSG_AUTH_TIS, 39) \
  1106. X(y, SSH1_SMSG_AUTH_TIS_CHALLENGE, 40) \
  1107. X(y, SSH1_CMSG_AUTH_TIS_RESPONSE, 41) \
  1108. X(y, SSH1_CMSG_AUTH_CCARD, 70) \
  1109. X(y, SSH1_SMSG_AUTH_CCARD_CHALLENGE, 71) \
  1110. X(y, SSH1_CMSG_AUTH_CCARD_RESPONSE, 72) \
  1111. /* end of list */
  1112. #define SSH1_AUTH_RHOSTS 1 /* 0x1 */
  1113. #define SSH1_AUTH_RSA 2 /* 0x2 */
  1114. #define SSH1_AUTH_PASSWORD 3 /* 0x3 */
  1115. #define SSH1_AUTH_RHOSTS_RSA 4 /* 0x4 */
  1116. #define SSH1_AUTH_TIS 5 /* 0x5 */
  1117. #define SSH1_AUTH_CCARD 16 /* 0x10 */
  1118. #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */
  1119. /* Mask for protoflags we will echo back to server if seen */
  1120. #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */
  1121. /*
  1122. * List macro defining SSH-2 message type codes. Some of these depend
  1123. * on particular contexts (i.e. a previously negotiated kex or auth
  1124. * method)
  1125. */
  1126. #define SSH2_MESSAGE_TYPES(X, K, A, y) \
  1127. X(y, SSH2_MSG_DISCONNECT, 1) \
  1128. X(y, SSH2_MSG_IGNORE, 2) \
  1129. X(y, SSH2_MSG_UNIMPLEMENTED, 3) \
  1130. X(y, SSH2_MSG_DEBUG, 4) \
  1131. X(y, SSH2_MSG_SERVICE_REQUEST, 5) \
  1132. X(y, SSH2_MSG_SERVICE_ACCEPT, 6) \
  1133. X(y, SSH2_MSG_KEXINIT, 20) \
  1134. X(y, SSH2_MSG_NEWKEYS, 21) \
  1135. K(y, SSH2_MSG_KEXDH_INIT, 30, SSH2_PKTCTX_DHGROUP) \
  1136. K(y, SSH2_MSG_KEXDH_REPLY, 31, SSH2_PKTCTX_DHGROUP) \
  1137. K(y, SSH2_MSG_KEX_DH_GEX_REQUEST_OLD, 30, SSH2_PKTCTX_DHGEX) \
  1138. K(y, SSH2_MSG_KEX_DH_GEX_REQUEST, 34, SSH2_PKTCTX_DHGEX) \
  1139. K(y, SSH2_MSG_KEX_DH_GEX_GROUP, 31, SSH2_PKTCTX_DHGEX) \
  1140. K(y, SSH2_MSG_KEX_DH_GEX_INIT, 32, SSH2_PKTCTX_DHGEX) \
  1141. K(y, SSH2_MSG_KEX_DH_GEX_REPLY, 33, SSH2_PKTCTX_DHGEX) \
  1142. K(y, SSH2_MSG_KEXGSS_INIT, 30, SSH2_PKTCTX_GSSKEX) \
  1143. K(y, SSH2_MSG_KEXGSS_CONTINUE, 31, SSH2_PKTCTX_GSSKEX) \
  1144. K(y, SSH2_MSG_KEXGSS_COMPLETE, 32, SSH2_PKTCTX_GSSKEX) \
  1145. K(y, SSH2_MSG_KEXGSS_HOSTKEY, 33, SSH2_PKTCTX_GSSKEX) \
  1146. K(y, SSH2_MSG_KEXGSS_ERROR, 34, SSH2_PKTCTX_GSSKEX) \
  1147. K(y, SSH2_MSG_KEXGSS_GROUPREQ, 40, SSH2_PKTCTX_GSSKEX) \
  1148. K(y, SSH2_MSG_KEXGSS_GROUP, 41, SSH2_PKTCTX_GSSKEX) \
  1149. K(y, SSH2_MSG_KEXRSA_PUBKEY, 30, SSH2_PKTCTX_RSAKEX) \
  1150. K(y, SSH2_MSG_KEXRSA_SECRET, 31, SSH2_PKTCTX_RSAKEX) \
  1151. K(y, SSH2_MSG_KEXRSA_DONE, 32, SSH2_PKTCTX_RSAKEX) \
  1152. K(y, SSH2_MSG_KEX_ECDH_INIT, 30, SSH2_PKTCTX_ECDHKEX) \
  1153. K(y, SSH2_MSG_KEX_ECDH_REPLY, 31, SSH2_PKTCTX_ECDHKEX) \
  1154. X(y, SSH2_MSG_USERAUTH_REQUEST, 50) \
  1155. X(y, SSH2_MSG_USERAUTH_FAILURE, 51) \
  1156. X(y, SSH2_MSG_USERAUTH_SUCCESS, 52) \
  1157. X(y, SSH2_MSG_USERAUTH_BANNER, 53) \
  1158. A(y, SSH2_MSG_USERAUTH_PK_OK, 60, SSH2_PKTCTX_PUBLICKEY) \
  1159. A(y, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, 60, SSH2_PKTCTX_PASSWORD) \
  1160. A(y, SSH2_MSG_USERAUTH_INFO_REQUEST, 60, SSH2_PKTCTX_KBDINTER) \
  1161. A(y, SSH2_MSG_USERAUTH_INFO_RESPONSE, 61, SSH2_PKTCTX_KBDINTER) \
  1162. A(y, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, 60, SSH2_PKTCTX_GSSAPI) \
  1163. A(y, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, 61, SSH2_PKTCTX_GSSAPI) \
  1164. A(y, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, 63, SSH2_PKTCTX_GSSAPI) \
  1165. A(y, SSH2_MSG_USERAUTH_GSSAPI_ERROR, 64, SSH2_PKTCTX_GSSAPI) \
  1166. A(y, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, 65, SSH2_PKTCTX_GSSAPI) \
  1167. A(y, SSH2_MSG_USERAUTH_GSSAPI_MIC, 66, SSH2_PKTCTX_GSSAPI) \
  1168. X(y, SSH2_MSG_GLOBAL_REQUEST, 80) \
  1169. X(y, SSH2_MSG_REQUEST_SUCCESS, 81) \
  1170. X(y, SSH2_MSG_REQUEST_FAILURE, 82) \
  1171. X(y, SSH2_MSG_CHANNEL_OPEN, 90) \
  1172. X(y, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, 91) \
  1173. X(y, SSH2_MSG_CHANNEL_OPEN_FAILURE, 92) \
  1174. X(y, SSH2_MSG_CHANNEL_WINDOW_ADJUST, 93) \
  1175. X(y, SSH2_MSG_CHANNEL_DATA, 94) \
  1176. X(y, SSH2_MSG_CHANNEL_EXTENDED_DATA, 95) \
  1177. X(y, SSH2_MSG_CHANNEL_EOF, 96) \
  1178. X(y, SSH2_MSG_CHANNEL_CLOSE, 97) \
  1179. X(y, SSH2_MSG_CHANNEL_REQUEST, 98) \
  1180. X(y, SSH2_MSG_CHANNEL_SUCCESS, 99) \
  1181. X(y, SSH2_MSG_CHANNEL_FAILURE, 100) \
  1182. /* end of list */
  1183. #define DEF_ENUM_UNIVERSAL(y, name, value) name = value,
  1184. #define DEF_ENUM_CONTEXTUAL(y, name, value, context) name = value,
  1185. enum {
  1186. SSH1_MESSAGE_TYPES(DEF_ENUM_UNIVERSAL, y)
  1187. SSH2_MESSAGE_TYPES(DEF_ENUM_UNIVERSAL,
  1188. DEF_ENUM_CONTEXTUAL, DEF_ENUM_CONTEXTUAL, y)
  1189. /* Virtual packet type, for packets too short to even have a type */
  1190. SSH_MSG_NO_TYPE_CODE = 256
  1191. };
  1192. #undef DEF_ENUM_UNIVERSAL
  1193. #undef DEF_ENUM_CONTEXTUAL
  1194. /*
  1195. * SSH-1 agent messages.
  1196. */
  1197. #define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
  1198. #define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
  1199. #define SSH1_AGENTC_RSA_CHALLENGE 3
  1200. #define SSH1_AGENT_RSA_RESPONSE 4
  1201. #define SSH1_AGENTC_ADD_RSA_IDENTITY 7
  1202. #define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
  1203. #define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
  1204. /*
  1205. * Messages common to SSH-1 and OpenSSH's SSH-2.
  1206. */
  1207. #define SSH_AGENT_FAILURE 5
  1208. #define SSH_AGENT_SUCCESS 6
  1209. /*
  1210. * OpenSSH's SSH-2 agent messages.
  1211. */
  1212. #define SSH2_AGENTC_REQUEST_IDENTITIES 11
  1213. #define SSH2_AGENT_IDENTITIES_ANSWER 12
  1214. #define SSH2_AGENTC_SIGN_REQUEST 13
  1215. #define SSH2_AGENT_SIGN_RESPONSE 14
  1216. #define SSH2_AGENTC_ADD_IDENTITY 17
  1217. #define SSH2_AGENTC_REMOVE_IDENTITY 18
  1218. #define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
  1219. /*
  1220. * Assorted other SSH-related enumerations.
  1221. */
  1222. #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */
  1223. #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */
  1224. #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */
  1225. #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */
  1226. #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */
  1227. #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */
  1228. #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */
  1229. #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */
  1230. #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */
  1231. #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */
  1232. #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */
  1233. #define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */
  1234. #define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */
  1235. #define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */
  1236. #define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */
  1237. #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */
  1238. #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */
  1239. #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */
  1240. #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */
  1241. #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */
  1242. const char *ssh1_pkt_type(int type);
  1243. const char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type);
  1244. int ssh2_pkt_type_code_valid(unsigned type);
  1245. /*
  1246. * Need this to warn about support for the original SSH-2 keyfile
  1247. * format.
  1248. */
  1249. void old_keyfile_warning(void);
  1250. /*
  1251. * Flags indicating implementation bugs that we know how to mitigate
  1252. * if we think the other end has them.
  1253. */
  1254. #define SSH_IMPL_BUG_LIST(X) \
  1255. X(BUG_CHOKES_ON_SSH1_IGNORE) \
  1256. X(BUG_SSH2_HMAC) \
  1257. X(BUG_NEEDS_SSH1_PLAIN_PASSWORD) \
  1258. X(BUG_CHOKES_ON_RSA) \
  1259. X(BUG_SSH2_RSA_PADDING) \
  1260. X(BUG_SSH2_DERIVEKEY) \
  1261. X(BUG_SSH2_REKEY) \
  1262. X(BUG_SSH2_PK_SESSIONID) \
  1263. X(BUG_SSH2_MAXPKT) \
  1264. X(BUG_CHOKES_ON_SSH2_IGNORE) \
  1265. X(BUG_CHOKES_ON_WINADJ) \
  1266. X(BUG_SENDS_LATE_REQUEST_REPLY) \
  1267. X(BUG_SSH2_OLDGEX) \
  1268. /* end of list */
  1269. #define TMP_DECLARE_LOG2_ENUM(thing) log2_##thing,
  1270. enum { SSH_IMPL_BUG_LIST(TMP_DECLARE_LOG2_ENUM) };
  1271. #undef TMP_DECLARE_LOG2_ENUM
  1272. #define TMP_DECLARE_REAL_ENUM(thing) thing = 1 << log2_##thing,
  1273. enum { SSH_IMPL_BUG_LIST(TMP_DECLARE_REAL_ENUM) };
  1274. #undef TMP_DECLARE_REAL_ENUM
  1275. /* Shared function that writes tty modes into a pty request */
  1276. void write_ttymodes_to_packet_from_conf(
  1277. BinarySink *bs, Frontend *frontend, Conf *conf,
  1278. int ssh_version, int ospeed, int ispeed);
  1279. /* Shared system for allocating local SSH channel ids. Expects to be
  1280. * passed a tree full of structs that have a field called 'localid' of
  1281. * type unsigned, and will check that! */
  1282. unsigned alloc_channel_id_general(tree234 *channels, size_t localid_offset);
  1283. #define alloc_channel_id(tree, type) \
  1284. TYPECHECK(&((type *)0)->localid == (unsigned *)0, \
  1285. alloc_channel_id_general(tree, offsetof(type, localid)))
  1286. int first_in_commasep_string(char const *needle, char const *haystack,
  1287. int haylen);
  1288. int in_commasep_string(char const *needle, char const *haystack, int haylen);
  1289. void add_to_commasep(strbuf *buf, const char *data);
  1290. int verify_ssh_manual_host_key(
  1291. Conf *conf, const char *fingerprint, ssh_key *key);
  1292. #endif // WINSCP_VS