ssh.h 66 KB

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