ssh.h 62 KB

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