ssh.h 64 KB

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