ssh.h 70 KB

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