ssh.h 63 KB

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