1
0

ssh.h 70 KB

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