ssh.h 62 KB

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