ssh.h 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  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_HYBRIDKEX,
  123. SSH2_PKTCTX_GSSKEX,
  124. SSH2_PKTCTX_RSAKEX
  125. } Pkt_KCtx;
  126. typedef enum {
  127. SSH2_PKTCTX_NOAUTH,
  128. SSH2_PKTCTX_PUBLICKEY,
  129. SSH2_PKTCTX_PASSWORD,
  130. SSH2_PKTCTX_GSSAPI,
  131. SSH2_PKTCTX_KBDINTER
  132. } Pkt_ACtx;
  133. typedef struct PacketLogSettings {
  134. bool omit_passwords, omit_data;
  135. Pkt_KCtx kctx;
  136. Pkt_ACtx actx;
  137. } PacketLogSettings;
  138. #define MAX_BLANKS 4 /* no packet needs more censored sections than this */
  139. int ssh1_censor_packet(
  140. const PacketLogSettings *pls, int type, bool sender_is_client,
  141. ptrlen pkt, logblank_t *blanks);
  142. int ssh2_censor_packet(
  143. const PacketLogSettings *pls, int type, bool sender_is_client,
  144. ptrlen pkt, logblank_t *blanks);
  145. PktOut *ssh_new_packet(void);
  146. void ssh_free_pktout(PktOut *pkt);
  147. Socket *ssh_connection_sharing_init(
  148. const char *host, int port, Conf *conf, LogContext *logctx,
  149. Plug *sshplug, ssh_sharing_state **state);
  150. void ssh_connshare_provide_connlayer(ssh_sharing_state *sharestate,
  151. ConnectionLayer *cl);
  152. bool ssh_share_test_for_upstream(const char *host, int port, Conf *conf);
  153. void share_got_pkt_from_server(ssh_sharing_connstate *ctx, int type,
  154. const void *pkt, int pktlen);
  155. void share_activate(ssh_sharing_state *sharestate,
  156. const char *server_verstring);
  157. void sharestate_free(ssh_sharing_state *state);
  158. int share_ndownstreams(ssh_sharing_state *state);
  159. void ssh_connshare_log(Ssh *ssh, int event, const char *logtext,
  160. const char *ds_err, const char *us_err);
  161. void share_setup_x11_channel(ssh_sharing_connstate *cs, share_channel *chan,
  162. unsigned upstream_id, unsigned server_id,
  163. unsigned server_currwin, unsigned server_maxpkt,
  164. unsigned client_adjusted_window,
  165. const char *peer_addr, int peer_port, int endian,
  166. int protomajor, int protominor,
  167. const void *initial_data, int initial_len);
  168. /* Per-application overrides for what roles we can take in connection
  169. * sharing, regardless of user configuration (e.g. pscp will never be
  170. * an upstream) */
  171. extern const bool share_can_be_downstream;
  172. extern const bool share_can_be_upstream;
  173. struct X11Display;
  174. struct X11FakeAuth;
  175. /* Structure definition centralised here because the SSH-1 and SSH-2
  176. * connection layers both use it. But the client module (portfwd.c)
  177. * should not try to look inside here. */
  178. struct ssh_rportfwd {
  179. unsigned sport, dport;
  180. char *shost, *dhost;
  181. int addressfamily;
  182. char *log_description; /* name of remote listening port, for logging */
  183. ssh_sharing_connstate *share_ctx;
  184. PortFwdRecord *pfr;
  185. };
  186. void free_rportfwd(struct ssh_rportfwd *rpf);
  187. typedef struct ConnectionLayerVtable ConnectionLayerVtable;
  188. struct ConnectionLayerVtable {
  189. /* Allocate and free remote-to-local port forwardings, called by
  190. * PortFwdManager or by connection sharing */
  191. struct ssh_rportfwd *(*rportfwd_alloc)(
  192. ConnectionLayer *cl,
  193. const char *shost, int sport, const char *dhost, int dport,
  194. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  195. ssh_sharing_connstate *share_ctx);
  196. void (*rportfwd_remove)(ConnectionLayer *cl, struct ssh_rportfwd *rpf);
  197. /* Open a local-to-remote port forwarding channel, called by
  198. * PortFwdManager */
  199. SshChannel *(*lportfwd_open)(
  200. ConnectionLayer *cl, const char *hostname, int port,
  201. const char *description, const SocketEndpointInfo *peerinfo,
  202. Channel *chan);
  203. /* Initiate opening of a 'session'-type channel */
  204. SshChannel *(*session_open)(ConnectionLayer *cl, Channel *chan);
  205. /* Open outgoing channels for X and agent forwarding. (Used in the
  206. * SSH server.) */
  207. SshChannel *(*serverside_x11_open)(ConnectionLayer *cl, Channel *chan,
  208. const SocketEndpointInfo *pi);
  209. SshChannel *(*serverside_agent_open)(ConnectionLayer *cl, Channel *chan);
  210. /* Add an X11 display for ordinary X forwarding */
  211. struct X11FakeAuth *(*add_x11_display)(
  212. ConnectionLayer *cl, int authtype, struct X11Display *x11disp);
  213. /* Add and remove X11 displays for connection sharing downstreams */
  214. struct X11FakeAuth *(*add_sharing_x11_display)(
  215. ConnectionLayer *cl, int authtype, ssh_sharing_connstate *share_cs,
  216. share_channel *share_chan);
  217. void (*remove_sharing_x11_display)(
  218. ConnectionLayer *cl, struct X11FakeAuth *auth);
  219. /* Pass through an outgoing SSH packet from a downstream */
  220. void (*send_packet_from_downstream)(
  221. ConnectionLayer *cl, unsigned id, int type,
  222. const void *pkt, int pktlen, const char *additional_log_text);
  223. /* Allocate/free an upstream channel number associated with a
  224. * sharing downstream */
  225. unsigned (*alloc_sharing_channel)(ConnectionLayer *cl,
  226. ssh_sharing_connstate *connstate);
  227. void (*delete_sharing_channel)(ConnectionLayer *cl, unsigned localid);
  228. /* Indicate that a downstream has sent a global request with the
  229. * want-reply flag, so that when a reply arrives it will be passed
  230. * back to that downstrean */
  231. void (*sharing_queue_global_request)(
  232. ConnectionLayer *cl, ssh_sharing_connstate *connstate);
  233. /* Indicate that the last downstream has disconnected */
  234. void (*sharing_no_more_downstreams)(ConnectionLayer *cl);
  235. /* Query whether the connection layer is doing agent forwarding */
  236. bool (*agent_forwarding_permitted)(ConnectionLayer *cl);
  237. /* Set the size of the main terminal window (if any) */
  238. void (*terminal_size)(ConnectionLayer *cl, int width, int height);
  239. /* Indicate that the backlog on standard output has cleared */
  240. void (*stdout_unthrottle)(ConnectionLayer *cl, size_t bufsize);
  241. /* Query the size of the backlog on standard _input_ */
  242. size_t (*stdin_backlog)(ConnectionLayer *cl);
  243. /* Tell the connection layer that the SSH connection itself has
  244. * backed up, so it should tell all currently open channels to
  245. * cease reading from their local input sources if they can. (Or
  246. * tell it that that state of affairs has gone away again.) */
  247. void (*throttle_all_channels)(ConnectionLayer *cl, bool throttled);
  248. /* Ask the connection layer about its current preference for
  249. * line-discipline options. */
  250. bool (*ldisc_option)(ConnectionLayer *cl, int option);
  251. /* Communicate _to_ the connection layer (from the main session
  252. * channel) what its preference for line-discipline options is. */
  253. void (*set_ldisc_option)(ConnectionLayer *cl, int option, bool value);
  254. /* Communicate to the connection layer whether X forwarding was
  255. * successfully enabled (for purposes of knowing whether to accept
  256. * subsequent channel-opens). */
  257. void (*enable_x_fwd)(ConnectionLayer *cl);
  258. /* Communicate / query whether the main session channel currently
  259. * wants user input. The set function is called by mainchan; the
  260. * query function is called by the top-level ssh.c. */
  261. void (*set_wants_user_input)(ConnectionLayer *cl, bool wanted);
  262. bool (*get_wants_user_input)(ConnectionLayer *cl);
  263. /* Notify the connection layer that more data has been added to
  264. * the user input queue. */
  265. void (*got_user_input)(ConnectionLayer *cl);
  266. };
  267. struct ConnectionLayer {
  268. LogContext *logctx;
  269. const struct ConnectionLayerVtable *vt;
  270. };
  271. static inline struct ssh_rportfwd *ssh_rportfwd_alloc(
  272. ConnectionLayer *cl, const char *sh, int sp, const char *dh, int dp,
  273. int af, const char *log, PortFwdRecord *pfr, ssh_sharing_connstate *cs)
  274. { return cl->vt->rportfwd_alloc(cl, sh, sp, dh, dp, af, log, pfr, cs); }
  275. static inline void ssh_rportfwd_remove(
  276. ConnectionLayer *cl, struct ssh_rportfwd *rpf)
  277. { cl->vt->rportfwd_remove(cl, rpf); }
  278. static inline SshChannel *ssh_lportfwd_open(
  279. ConnectionLayer *cl, const char *host, int port,
  280. const char *desc, const SocketEndpointInfo *pi, Channel *chan)
  281. { return cl->vt->lportfwd_open(cl, host, port, desc, pi, chan); }
  282. static inline SshChannel *ssh_session_open(ConnectionLayer *cl, Channel *chan)
  283. { return cl->vt->session_open(cl, chan); }
  284. static inline SshChannel *ssh_serverside_x11_open(
  285. ConnectionLayer *cl, Channel *chan, const SocketEndpointInfo *pi)
  286. { return cl->vt->serverside_x11_open(cl, chan, pi); }
  287. static inline SshChannel *ssh_serverside_agent_open(
  288. ConnectionLayer *cl, Channel *chan)
  289. { return cl->vt->serverside_agent_open(cl, chan); }
  290. static inline struct X11FakeAuth *ssh_add_x11_display(
  291. ConnectionLayer *cl, int authtype, struct X11Display *x11disp)
  292. { return cl->vt->add_x11_display(cl, authtype, x11disp); }
  293. static inline struct X11FakeAuth *ssh_add_sharing_x11_display(
  294. ConnectionLayer *cl, int authtype, ssh_sharing_connstate *share_cs,
  295. share_channel *share_chan)
  296. { return cl->vt->add_sharing_x11_display(cl, authtype, share_cs, share_chan); }
  297. static inline void ssh_remove_sharing_x11_display(
  298. ConnectionLayer *cl, struct X11FakeAuth *auth)
  299. { cl->vt->remove_sharing_x11_display(cl, auth); }
  300. static inline void ssh_send_packet_from_downstream(
  301. ConnectionLayer *cl, unsigned id, int type,
  302. const void *pkt, int len, const char *log)
  303. { cl->vt->send_packet_from_downstream(cl, id, type, pkt, len, log); }
  304. static inline unsigned ssh_alloc_sharing_channel(
  305. ConnectionLayer *cl, ssh_sharing_connstate *connstate)
  306. { return cl->vt->alloc_sharing_channel(cl, connstate); }
  307. static inline void ssh_delete_sharing_channel(
  308. ConnectionLayer *cl, unsigned localid)
  309. { cl->vt->delete_sharing_channel(cl, localid); }
  310. static inline void ssh_sharing_queue_global_request(
  311. ConnectionLayer *cl, ssh_sharing_connstate *connstate)
  312. { cl->vt->sharing_queue_global_request(cl, connstate); }
  313. static inline void ssh_sharing_no_more_downstreams(ConnectionLayer *cl)
  314. { cl->vt->sharing_no_more_downstreams(cl); }
  315. static inline bool ssh_agent_forwarding_permitted(ConnectionLayer *cl)
  316. { return cl->vt->agent_forwarding_permitted(cl); }
  317. static inline void ssh_terminal_size(ConnectionLayer *cl, int w, int h)
  318. { cl->vt->terminal_size(cl, w, h); }
  319. static inline void ssh_stdout_unthrottle(ConnectionLayer *cl, size_t bufsize)
  320. { cl->vt->stdout_unthrottle(cl, bufsize); }
  321. static inline size_t ssh_stdin_backlog(ConnectionLayer *cl)
  322. { return cl->vt->stdin_backlog(cl); }
  323. static inline void ssh_throttle_all_channels(ConnectionLayer *cl, bool thr)
  324. { cl->vt->throttle_all_channels(cl, thr); }
  325. static inline bool ssh_ldisc_option(ConnectionLayer *cl, int option)
  326. { return cl->vt->ldisc_option(cl, option); }
  327. static inline void ssh_set_ldisc_option(ConnectionLayer *cl, int opt, bool val)
  328. { cl->vt->set_ldisc_option(cl, opt, val); }
  329. static inline void ssh_enable_x_fwd(ConnectionLayer *cl)
  330. { cl->vt->enable_x_fwd(cl); }
  331. static inline void ssh_set_wants_user_input(ConnectionLayer *cl, bool wanted)
  332. { cl->vt->set_wants_user_input(cl, wanted); }
  333. static inline bool ssh_get_wants_user_input(ConnectionLayer *cl)
  334. { return cl->vt->get_wants_user_input(cl); }
  335. static inline void ssh_got_user_input(ConnectionLayer *cl)
  336. { cl->vt->got_user_input(cl); }
  337. /* Exports from portfwd.c */
  338. PortFwdManager *portfwdmgr_new(ConnectionLayer *cl);
  339. void portfwdmgr_free(PortFwdManager *mgr);
  340. void portfwdmgr_config(PortFwdManager *mgr, Conf *conf);
  341. void portfwdmgr_close(PortFwdManager *mgr, PortFwdRecord *pfr);
  342. void portfwdmgr_close_all(PortFwdManager *mgr);
  343. char *portfwdmgr_connect(PortFwdManager *mgr, Channel **chan_ret,
  344. char *hostname, int port, SshChannel *c,
  345. int addressfamily);
  346. bool portfwdmgr_listen(PortFwdManager *mgr, const char *host, int port,
  347. const char *keyhost, int keyport, Conf *conf);
  348. bool portfwdmgr_unlisten(PortFwdManager *mgr, const char *host, int port);
  349. Channel *portfwd_raw_new(ConnectionLayer *cl, Plug **plug, bool start_ready);
  350. void portfwd_raw_free(Channel *pfchan);
  351. void portfwd_raw_setup(Channel *pfchan, Socket *s, SshChannel *sc);
  352. Socket *platform_make_agent_socket(Plug *plug, const char *dirprefix,
  353. char **error, char **name);
  354. LogContext *ssh_get_logctx(Ssh *ssh);
  355. /* Communications back to ssh.c from connection layers */
  356. void ssh_throttle_conn(Ssh *ssh, int adjust);
  357. void ssh_got_exitcode(Ssh *ssh, int status);
  358. void ssh_ldisc_update(Ssh *ssh);
  359. void ssh_check_sendok(Ssh *ssh);
  360. void ssh_got_fallback_cmd(Ssh *ssh);
  361. bool ssh_is_bare(Ssh *ssh);
  362. /* Communications back to ssh.c from the BPP */
  363. void ssh_conn_processed_data(Ssh *ssh);
  364. void ssh_sendbuffer_changed(Ssh *ssh);
  365. void ssh_check_frozen(Ssh *ssh);
  366. /* Functions to abort the connection, for various reasons. */
  367. void ssh_remote_error(Ssh *ssh, const char *fmt, ...) PRINTF_LIKE(2, 3);
  368. void ssh_remote_eof(Ssh *ssh, const char *fmt, ...) PRINTF_LIKE(2, 3);
  369. void ssh_proto_error(Ssh *ssh, const char *fmt, ...) PRINTF_LIKE(2, 3);
  370. void ssh_sw_abort(Ssh *ssh, const char *fmt, ...) PRINTF_LIKE(2, 3);
  371. void ssh_sw_abort_deferred(Ssh *ssh, const char *fmt, ...) PRINTF_LIKE(2, 3);
  372. void ssh_user_close(Ssh *ssh, const char *fmt, ...) PRINTF_LIKE(2, 3);
  373. void ssh_spr_close(Ssh *ssh, SeatPromptResult spr, const char *context);
  374. /* Bit positions in the SSH-1 cipher protocol word */
  375. #define SSH1_CIPHER_IDEA 1
  376. #define SSH1_CIPHER_DES 2
  377. #define SSH1_CIPHER_3DES 3
  378. #define SSH1_CIPHER_BLOWFISH 6
  379. /* The subset of those that we support, with names for selecting them
  380. * on Uppity's command line */
  381. #define SSH1_SUPPORTED_CIPHER_LIST(X) \
  382. X(SSH1_CIPHER_3DES, "3des") \
  383. X(SSH1_CIPHER_BLOWFISH, "blowfish") \
  384. X(SSH1_CIPHER_DES, "des") \
  385. /* end of list */
  386. #define SSH1_CIPHER_LIST_MAKE_MASK(bitpos, name) | (1U << bitpos)
  387. #define SSH1_SUPPORTED_CIPHER_MASK \
  388. (0 SSH1_SUPPORTED_CIPHER_LIST(SSH1_CIPHER_LIST_MAKE_MASK))
  389. struct ssh_key {
  390. const ssh_keyalg *vt;
  391. };
  392. struct RSAKey {
  393. int bits;
  394. int bytes;
  395. mp_int *modulus;
  396. mp_int *exponent;
  397. mp_int *private_exponent;
  398. mp_int *p;
  399. mp_int *q;
  400. mp_int *iqmp;
  401. char *comment;
  402. ssh_key sshk;
  403. };
  404. struct dsa_key {
  405. mp_int *p, *q, *g, *y, *x;
  406. ssh_key sshk;
  407. };
  408. struct ec_curve;
  409. /* Weierstrass form curve */
  410. struct ec_wcurve
  411. {
  412. WeierstrassCurve *wc;
  413. WeierstrassPoint *G;
  414. mp_int *G_order;
  415. };
  416. /* Montgomery form curve */
  417. struct ec_mcurve
  418. {
  419. MontgomeryCurve *mc;
  420. MontgomeryPoint *G;
  421. unsigned log2_cofactor;
  422. };
  423. /* Edwards form curve */
  424. struct ec_ecurve
  425. {
  426. EdwardsCurve *ec;
  427. EdwardsPoint *G;
  428. mp_int *G_order;
  429. unsigned log2_cofactor;
  430. };
  431. typedef enum EllipticCurveType {
  432. EC_WEIERSTRASS, EC_MONTGOMERY, EC_EDWARDS
  433. } EllipticCurveType;
  434. struct ec_curve {
  435. EllipticCurveType type;
  436. /* 'name' is the identifier of the curve when it has to appear in
  437. * wire protocol encodings, as it does in e.g. the public key and
  438. * signature formats for NIST curves. Curves which do not format
  439. * their keys or signatures in this way just have name==NULL.
  440. *
  441. * 'textname' is non-NULL for all curves, and is a human-readable
  442. * identification suitable for putting in log messages. */
  443. const char *name, *textname;
  444. size_t fieldBits, fieldBytes;
  445. mp_int *p;
  446. union {
  447. struct ec_wcurve w;
  448. struct ec_mcurve m;
  449. struct ec_ecurve e;
  450. };
  451. };
  452. const ssh_keyalg *ec_alg_by_oid(int len, const void *oid,
  453. const struct ec_curve **curve);
  454. const unsigned char *ec_alg_oid(const ssh_keyalg *alg, int *oidlen);
  455. extern const int ec_nist_curve_lengths[], n_ec_nist_curve_lengths;
  456. extern const int ec_ed_curve_lengths[], n_ec_ed_curve_lengths;
  457. bool ec_nist_alg_and_curve_by_bits(int bits,
  458. const struct ec_curve **curve,
  459. const ssh_keyalg **alg);
  460. bool ec_ed_alg_and_curve_by_bits(int bits,
  461. const struct ec_curve **curve,
  462. const ssh_keyalg **alg);
  463. struct ecdsa_key {
  464. const struct ec_curve *curve;
  465. WeierstrassPoint *publicKey;
  466. mp_int *privateKey;
  467. ssh_key sshk;
  468. };
  469. struct eddsa_key {
  470. const struct ec_curve *curve;
  471. EdwardsPoint *publicKey;
  472. mp_int *privateKey;
  473. ssh_key sshk;
  474. };
  475. WeierstrassPoint *ecdsa_public(mp_int *private_key, const ssh_keyalg *alg);
  476. EdwardsPoint *eddsa_public(mp_int *private_key, const ssh_keyalg *alg);
  477. typedef enum KeyComponentType {
  478. KCT_TEXT, KCT_BINARY, KCT_MPINT
  479. } KeyComponentType;
  480. typedef struct key_component {
  481. char *name;
  482. KeyComponentType type;
  483. union {
  484. strbuf *str; /* used for KCT_TEXT and KCT_BINARY */
  485. mp_int *mp; /* used for KCT_MPINT */
  486. };
  487. } key_component;
  488. typedef struct key_components {
  489. size_t ncomponents, componentsize;
  490. key_component *components;
  491. } key_components;
  492. key_components *key_components_new(void);
  493. void key_components_add_text(key_components *kc,
  494. const char *name, const char *value);
  495. void key_components_add_text_pl(key_components *kc,
  496. const char *name, ptrlen value);
  497. void key_components_add_binary(key_components *kc,
  498. const char *name, ptrlen value);
  499. void key_components_add_mp(key_components *kc,
  500. const char *name, mp_int *value);
  501. void key_components_add_uint(key_components *kc,
  502. const char *name, uintmax_t value);
  503. void key_components_add_copy(key_components *kc,
  504. const char *name, const key_component *value);
  505. void key_components_free(key_components *kc);
  506. /*
  507. * SSH-1 never quite decided which order to store the two components
  508. * of an RSA key. During connection setup, the server sends its host
  509. * and server keys with the exponent first; private key files store
  510. * the modulus first. The agent protocol is even more confusing,
  511. * because the client specifies a key to the server in one order and
  512. * the server lists the keys it knows about in the other order!
  513. */
  514. typedef enum { RSA_SSH1_EXPONENT_FIRST, RSA_SSH1_MODULUS_FIRST } RsaSsh1Order;
  515. void BinarySource_get_rsa_ssh1_pub(
  516. BinarySource *src, RSAKey *result, RsaSsh1Order order);
  517. void BinarySource_get_rsa_ssh1_priv(
  518. BinarySource *src, RSAKey *rsa);
  519. RSAKey *BinarySource_get_rsa_ssh1_priv_agent(BinarySource *src);
  520. bool rsa_ssh1_encrypt(unsigned char *data, int length, RSAKey *key);
  521. mp_int *rsa_ssh1_decrypt(mp_int *input, RSAKey *key);
  522. bool rsa_ssh1_decrypt_pkcs1(mp_int *input, RSAKey *key, strbuf *outbuf);
  523. char *rsastr_fmt(RSAKey *key);
  524. char *rsa_ssh1_fingerprint(RSAKey *key);
  525. char **rsa_ssh1_fake_all_fingerprints(RSAKey *key);
  526. bool rsa_verify(RSAKey *key);
  527. void rsa_ssh1_public_blob(BinarySink *bs, RSAKey *key, RsaSsh1Order order);
  528. int rsa_ssh1_public_blob_len(ptrlen data);
  529. void rsa_ssh1_private_blob_agent(BinarySink *bs, RSAKey *key);
  530. void duprsakey(RSAKey *dst, const RSAKey *src);
  531. void freersapriv(RSAKey *key);
  532. void freersakey(RSAKey *key);
  533. key_components *rsa_components(RSAKey *key);
  534. uint32_t crc32_rfc1662(ptrlen data);
  535. uint32_t crc32_ssh1(ptrlen data);
  536. uint32_t crc32_update(uint32_t crc_input, ptrlen data);
  537. /* SSH CRC compensation attack detector */
  538. struct crcda_ctx;
  539. struct crcda_ctx *crcda_make_context(void);
  540. void crcda_free_context(struct crcda_ctx *ctx);
  541. bool detect_attack(struct crcda_ctx *ctx,
  542. const unsigned char *buf, uint32_t len,
  543. const unsigned char *IV);
  544. /*
  545. * SSH2 RSA key exchange functions
  546. */
  547. struct ssh_rsa_kex_extra {
  548. int minklen;
  549. };
  550. RSAKey *ssh_rsakex_newkey(ptrlen data);
  551. void ssh_rsakex_freekey(RSAKey *key);
  552. int ssh_rsakex_klen(RSAKey *key);
  553. strbuf *ssh_rsakex_encrypt(
  554. RSAKey *key, const ssh_hashalg *h, ptrlen plaintext);
  555. mp_int *ssh_rsakex_decrypt(
  556. RSAKey *key, const ssh_hashalg *h, ptrlen ciphertext);
  557. /*
  558. * System for generating k in DSA and ECDSA.
  559. */
  560. struct RFC6979Result {
  561. mp_int *k;
  562. unsigned ok;
  563. };
  564. RFC6979 *rfc6979_new(const ssh_hashalg *hashalg, mp_int *q, mp_int *x);
  565. void rfc6979_setup(RFC6979 *s, ptrlen message);
  566. RFC6979Result rfc6979_attempt(RFC6979 *s);
  567. void rfc6979_free(RFC6979 *s);
  568. mp_int *rfc6979(const ssh_hashalg *hashalg, mp_int *modulus,
  569. mp_int *private_key, ptrlen message);
  570. struct ssh_cipher {
  571. const ssh_cipheralg *vt;
  572. };
  573. struct ssh_cipheralg {
  574. ssh_cipher *(*new)(const ssh_cipheralg *alg);
  575. void (*free)(ssh_cipher *);
  576. void (*setiv)(ssh_cipher *, const void *iv);
  577. void (*setkey)(ssh_cipher *, const void *key);
  578. void (*encrypt)(ssh_cipher *, void *blk, int len);
  579. void (*decrypt)(ssh_cipher *, void *blk, int len);
  580. /* Ignored unless SSH_CIPHER_SEPARATE_LENGTH flag set */
  581. void (*encrypt_length)(ssh_cipher *, void *blk, int len,
  582. unsigned long seq);
  583. void (*decrypt_length)(ssh_cipher *, void *blk, int len,
  584. unsigned long seq);
  585. /* For ciphers that update their state per logical message
  586. * (typically, per unit independently MACed) */
  587. void (*next_message)(ssh_cipher *);
  588. const char *ssh2_id;
  589. int blksize;
  590. /* real_keybits is the number of bits of entropy genuinely used by
  591. * the cipher scheme; it's used for deciding how big a
  592. * Diffie-Hellman group is needed to exchange a key for the
  593. * cipher. */
  594. int real_keybits;
  595. /* padded_keybytes is the number of bytes of key data expected as
  596. * input to the setkey function; it's used for deciding how much
  597. * data needs to be generated from the post-kex generation of key
  598. * material. In a sensible cipher which uses all its key bytes for
  599. * real work, this will just be real_keybits/8, but in DES-type
  600. * ciphers which ignore one bit in each byte, it'll be slightly
  601. * different. */
  602. int padded_keybytes;
  603. unsigned int flags;
  604. #define SSH_CIPHER_IS_CBC 1
  605. #define SSH_CIPHER_SEPARATE_LENGTH 2
  606. const char *text_name;
  607. /* If set, this takes priority over other MAC. */
  608. const ssh2_macalg *required_mac;
  609. /* Pointer to any extra data used by a particular implementation. */
  610. const void *extra;
  611. };
  612. static inline ssh_cipher *ssh_cipher_new(const ssh_cipheralg *alg)
  613. { return alg->new(alg); }
  614. static inline void ssh_cipher_free(ssh_cipher *c)
  615. { c->vt->free(c); }
  616. static inline void ssh_cipher_setiv(ssh_cipher *c, const void *iv)
  617. { c->vt->setiv(c, iv); }
  618. static inline void ssh_cipher_setkey(ssh_cipher *c, const void *key)
  619. { c->vt->setkey(c, key); }
  620. static inline void ssh_cipher_encrypt(ssh_cipher *c, void *blk, int len)
  621. { c->vt->encrypt(c, blk, len); }
  622. static inline void ssh_cipher_decrypt(ssh_cipher *c, void *blk, int len)
  623. { c->vt->decrypt(c, blk, len); }
  624. static inline void ssh_cipher_encrypt_length(
  625. ssh_cipher *c, void *blk, int len, unsigned long seq)
  626. { c->vt->encrypt_length(c, blk, len, seq); }
  627. static inline void ssh_cipher_decrypt_length(
  628. ssh_cipher *c, void *blk, int len, unsigned long seq)
  629. { c->vt->decrypt_length(c, blk, len, seq); }
  630. static inline void ssh_cipher_next_message(ssh_cipher *c)
  631. { c->vt->next_message(c); }
  632. static inline const struct ssh_cipheralg *ssh_cipher_alg(ssh_cipher *c)
  633. { return c->vt; }
  634. void nullcipher_next_message(ssh_cipher *);
  635. struct ssh2_ciphers {
  636. int nciphers;
  637. const ssh_cipheralg *const *list;
  638. };
  639. struct ssh2_mac {
  640. const ssh2_macalg *vt;
  641. BinarySink_DELEGATE_IMPLEMENTATION;
  642. };
  643. struct ssh2_macalg {
  644. /* Passes in the cipher context */
  645. ssh2_mac *(*new)(const ssh2_macalg *alg, ssh_cipher *cipher);
  646. void (*free)(ssh2_mac *);
  647. void (*setkey)(ssh2_mac *, ptrlen key);
  648. void (*start)(ssh2_mac *);
  649. void (*genresult)(ssh2_mac *, unsigned char *);
  650. void (*next_message)(ssh2_mac *);
  651. const char *(*text_name)(ssh2_mac *);
  652. const char *name, *etm_name;
  653. int len, keylen;
  654. /* Pointer to any extra data used by a particular implementation. */
  655. const void *extra;
  656. };
  657. static inline ssh2_mac *ssh2_mac_new(
  658. const ssh2_macalg *alg, ssh_cipher *cipher)
  659. { return alg->new(alg, cipher); }
  660. static inline void ssh2_mac_free(ssh2_mac *m)
  661. { m->vt->free(m); }
  662. static inline void ssh2_mac_setkey(ssh2_mac *m, ptrlen key)
  663. { m->vt->setkey(m, key); }
  664. static inline void ssh2_mac_start(ssh2_mac *m)
  665. { m->vt->start(m); }
  666. static inline void ssh2_mac_genresult(ssh2_mac *m, unsigned char *out)
  667. { m->vt->genresult(m, out); }
  668. static inline void ssh2_mac_next_message(ssh2_mac *m)
  669. { m->vt->next_message(m); }
  670. static inline const char *ssh2_mac_text_name(ssh2_mac *m)
  671. { return m->vt->text_name(m); }
  672. static inline const ssh2_macalg *ssh2_mac_alg(ssh2_mac *m)
  673. { return m->vt; }
  674. /* Centralised 'methods' for ssh2_mac, defined in mac.c. These run
  675. * the MAC in a specifically SSH-2 style, i.e. taking account of a
  676. * packet sequence number as well as the data to be authenticated. */
  677. bool ssh2_mac_verresult(ssh2_mac *, const void *);
  678. void ssh2_mac_generate(ssh2_mac *, void *, int, unsigned long seq);
  679. bool ssh2_mac_verify(ssh2_mac *, const void *, int, unsigned long seq);
  680. void nullmac_next_message(ssh2_mac *m);
  681. /* Use a MAC in its raw form, outside SSH-2 context, to MAC a given
  682. * string with a given key in the most obvious way. */
  683. void mac_simple(const ssh2_macalg *alg, ptrlen key, ptrlen data, void *output);
  684. /* Constructor that makes an HMAC object given just a MAC. This object
  685. * will have empty 'name' and 'etm_name' fields, so it's not suitable
  686. * for use in SSH. It's used as a subroutine in RFC 6979. */
  687. ssh2_mac *hmac_new_from_hash(const ssh_hashalg *hash);
  688. struct ssh_hash {
  689. const ssh_hashalg *vt;
  690. BinarySink_DELEGATE_IMPLEMENTATION;
  691. };
  692. struct ssh_hashalg {
  693. ssh_hash *(*new)(const ssh_hashalg *alg);
  694. void (*reset)(ssh_hash *);
  695. void (*copyfrom)(ssh_hash *dest, ssh_hash *src);
  696. void (*digest)(ssh_hash *, unsigned char *);
  697. void (*free)(ssh_hash *);
  698. size_t hlen; /* output length in bytes */
  699. size_t blocklen; /* length of the hash's input block, or 0 for N/A */
  700. const char *text_basename; /* the semantic name of the hash */
  701. const char *annotation; /* extra info, e.g. which of multiple impls */
  702. const char *text_name; /* both combined, e.g. "SHA-n (unaccelerated)" */
  703. const void *extra; /* private to the hash implementation */
  704. };
  705. static inline ssh_hash *ssh_hash_new(const ssh_hashalg *alg)
  706. { ssh_hash *h = alg->new(alg); if (h) h->vt->reset(h); return h; }
  707. static inline ssh_hash *ssh_hash_copy(ssh_hash *orig)
  708. { ssh_hash *h = orig->vt->new(orig->vt); h->vt->copyfrom(h, orig); return h; }
  709. static inline void ssh_hash_digest(ssh_hash *h, unsigned char *out)
  710. { h->vt->digest(h, out); }
  711. static inline void ssh_hash_free(ssh_hash *h)
  712. { h->vt->free(h); }
  713. static inline const ssh_hashalg *ssh_hash_alg(ssh_hash *h)
  714. { return h->vt; }
  715. /* The reset and copyfrom vtable methods return void. But for call-site
  716. * convenience, these wrappers return their input pointer. */
  717. static inline ssh_hash *ssh_hash_reset(ssh_hash *h)
  718. { h->vt->reset(h); return h; }
  719. static inline ssh_hash *ssh_hash_copyfrom(ssh_hash *dest, ssh_hash *src)
  720. { dest->vt->copyfrom(dest, src); return dest; }
  721. /* ssh_hash_final emits the digest _and_ frees the ssh_hash */
  722. static inline void ssh_hash_final(ssh_hash *h, unsigned char *out)
  723. { h->vt->digest(h, out); h->vt->free(h); }
  724. /* ssh_hash_digest_nondestructive generates a finalised hash from the
  725. * given object without changing its state, so you can continue
  726. * appending data to get a hash of an extended string. */
  727. static inline void ssh_hash_digest_nondestructive(ssh_hash *h,
  728. unsigned char *out)
  729. { ssh_hash_final(ssh_hash_copy(h), out); }
  730. /* Handy macros for defining all those text-name fields at once */
  731. #define HASHALG_NAMES_BARE(base) \
  732. .text_basename = base, .annotation = NULL, .text_name = base
  733. #define HASHALG_NAMES_ANNOTATED(base, ann) \
  734. .text_basename = base, .annotation = ann, .text_name = base " (" ann ")"
  735. void hash_simple(const ssh_hashalg *alg, ptrlen data, void *output);
  736. struct ssh_kex {
  737. const char *name, *groupname;
  738. enum { KEXTYPE_DH, KEXTYPE_RSA, KEXTYPE_ECDH,
  739. KEXTYPE_GSS, KEXTYPE_GSS_ECDH } main_type;
  740. const ssh_hashalg *hash;
  741. union { /* publicly visible data for each type */
  742. const ecdh_keyalg *ecdh_vt; /* for KEXTYPE_ECDH, KEXTYPE_GSS_ECDH */
  743. };
  744. const void *extra; /* private to the kex methods */
  745. };
  746. static inline bool kex_is_gss(const struct ssh_kex *kex)
  747. {
  748. return kex->main_type == KEXTYPE_GSS || kex->main_type == KEXTYPE_GSS_ECDH;
  749. }
  750. struct ssh_kexes {
  751. int nkexes;
  752. const ssh_kex *const *list;
  753. };
  754. /* Indices of the negotiation strings in the KEXINIT packet */
  755. enum kexlist {
  756. KEXLIST_KEX, KEXLIST_HOSTKEY, KEXLIST_CSCIPHER, KEXLIST_SCCIPHER,
  757. KEXLIST_CSMAC, KEXLIST_SCMAC, KEXLIST_CSCOMP, KEXLIST_SCCOMP,
  758. NKEXLIST
  759. };
  760. struct ssh_keyalg {
  761. /* Constructors that create an ssh_key */
  762. ssh_key *(*new_pub) (const ssh_keyalg *self, ptrlen pub);
  763. ssh_key *(*new_priv) (const ssh_keyalg *self, ptrlen pub, ptrlen priv);
  764. ssh_key *(*new_priv_openssh) (const ssh_keyalg *self, BinarySource *);
  765. /* Methods that operate on an existing ssh_key */
  766. void (*freekey) (ssh_key *key);
  767. char *(*invalid) (ssh_key *key, unsigned flags);
  768. void (*sign) (ssh_key *key, ptrlen data, unsigned flags, BinarySink *);
  769. bool (*verify) (ssh_key *key, ptrlen sig, ptrlen data);
  770. void (*public_blob)(ssh_key *key, BinarySink *);
  771. void (*private_blob)(ssh_key *key, BinarySink *);
  772. void (*openssh_blob) (ssh_key *key, BinarySink *);
  773. bool (*has_private) (ssh_key *key);
  774. char *(*cache_str) (ssh_key *key);
  775. key_components *(*components) (ssh_key *key);
  776. ssh_key *(*base_key) (ssh_key *key); /* does not confer ownership */
  777. /* The following methods can be NULL if !is_certificate */
  778. void (*ca_public_blob)(ssh_key *key, BinarySink *);
  779. bool (*check_cert)(ssh_key *key, bool host, ptrlen principal,
  780. uint64_t time, const ca_options *opts,
  781. BinarySink *error);
  782. void (*cert_id_string)(ssh_key *key, BinarySink *);
  783. SeatDialogText *(*cert_info)(ssh_key *key);
  784. /* 'Class methods' that don't deal with an ssh_key at all */
  785. int (*pubkey_bits) (const ssh_keyalg *self, ptrlen blob);
  786. unsigned (*supported_flags) (const ssh_keyalg *self);
  787. const char *(*alternate_ssh_id) (const ssh_keyalg *self, unsigned flags);
  788. char *(*alg_desc)(const ssh_keyalg *self);
  789. bool (*variable_size)(const ssh_keyalg *self);
  790. /* The following methods can be NULL if !is_certificate */
  791. const ssh_keyalg *(*related_alg)(const ssh_keyalg *self,
  792. const ssh_keyalg *base);
  793. /* Constant data fields giving information about the key type */
  794. const char *ssh_id; /* string identifier in the SSH protocol */
  795. const char *cache_id; /* identifier used in PuTTY's host key cache */
  796. const void *extra; /* private to the public key methods */
  797. bool is_certificate; /* is this a certified key type? */
  798. const ssh_keyalg *base_alg; /* if so, for what underlying key alg? */
  799. };
  800. static inline ssh_key *ssh_key_new_pub(const ssh_keyalg *self, ptrlen pub)
  801. { return self->new_pub(self, pub); }
  802. static inline ssh_key *ssh_key_new_priv(
  803. const ssh_keyalg *self, ptrlen pub, ptrlen priv)
  804. { return self->new_priv(self, pub, priv); }
  805. static inline ssh_key *ssh_key_new_priv_openssh(
  806. const ssh_keyalg *self, BinarySource *src)
  807. { return self->new_priv_openssh(self, src); }
  808. static inline void ssh_key_free(ssh_key *key)
  809. { key->vt->freekey(key); }
  810. static inline char *ssh_key_invalid(ssh_key *key, unsigned flags)
  811. { return key->vt->invalid(key, flags); }
  812. static inline void ssh_key_sign(
  813. ssh_key *key, ptrlen data, unsigned flags, BinarySink *bs)
  814. { key->vt->sign(key, data, flags, bs); }
  815. static inline bool ssh_key_verify(ssh_key *key, ptrlen sig, ptrlen data)
  816. { return key->vt->verify(key, sig, data); }
  817. static inline void ssh_key_public_blob(ssh_key *key, BinarySink *bs)
  818. { key->vt->public_blob(key, bs); }
  819. static inline void ssh_key_private_blob(ssh_key *key, BinarySink *bs)
  820. { key->vt->private_blob(key, bs); }
  821. static inline void ssh_key_openssh_blob(ssh_key *key, BinarySink *bs)
  822. { key->vt->openssh_blob(key, bs); }
  823. static inline bool ssh_key_has_private(ssh_key *key)
  824. { return key->vt->has_private(key); }
  825. static inline char *ssh_key_cache_str(ssh_key *key)
  826. { return key->vt->cache_str(key); }
  827. static inline key_components *ssh_key_components(ssh_key *key)
  828. { return key->vt->components(key); }
  829. static inline ssh_key *ssh_key_base_key(ssh_key *key)
  830. { return key->vt->base_key(key); }
  831. static inline void ssh_key_ca_public_blob(ssh_key *key, BinarySink *bs)
  832. { key->vt->ca_public_blob(key, bs); }
  833. static inline void ssh_key_cert_id_string(ssh_key *key, BinarySink *bs)
  834. { key->vt->cert_id_string(key, bs); }
  835. static inline SeatDialogText *ssh_key_cert_info(ssh_key *key)
  836. { return key->vt->cert_info(key); }
  837. static inline bool ssh_key_check_cert(
  838. ssh_key *key, bool host, ptrlen principal, uint64_t time,
  839. const ca_options *opts, BinarySink *error)
  840. { return key->vt->check_cert(key, host, principal, time, opts, error); }
  841. static inline int ssh_key_public_bits(const ssh_keyalg *self, ptrlen blob)
  842. { return self->pubkey_bits(self, blob); }
  843. static inline const ssh_keyalg *ssh_key_alg(ssh_key *key)
  844. { return key->vt; }
  845. static inline const char *ssh_key_ssh_id(ssh_key *key)
  846. { return key->vt->ssh_id; }
  847. static inline const char *ssh_key_cache_id(ssh_key *key)
  848. { return key->vt->cache_id; }
  849. static inline unsigned ssh_key_supported_flags(ssh_key *key)
  850. { return key->vt->supported_flags(key->vt); }
  851. static inline unsigned ssh_keyalg_supported_flags(const ssh_keyalg *self)
  852. { return self->supported_flags(self); }
  853. static inline const char *ssh_keyalg_alternate_ssh_id(
  854. const ssh_keyalg *self, unsigned flags)
  855. { return self->alternate_ssh_id(self, flags); }
  856. static inline char *ssh_keyalg_desc(const ssh_keyalg *self)
  857. { return self->alg_desc(self); }
  858. static inline bool ssh_keyalg_variable_size(const ssh_keyalg *self)
  859. { return self->variable_size(self); }
  860. static inline const ssh_keyalg *ssh_keyalg_related_alg(
  861. const ssh_keyalg *self, const ssh_keyalg *base)
  862. { return self->related_alg(self, base); }
  863. /* Stub functions shared between multiple key types */
  864. unsigned nullkey_supported_flags(const ssh_keyalg *self);
  865. const char *nullkey_alternate_ssh_id(const ssh_keyalg *self, unsigned flags);
  866. ssh_key *nullkey_base_key(ssh_key *key);
  867. bool nullkey_variable_size_no(const ssh_keyalg *self);
  868. bool nullkey_variable_size_yes(const ssh_keyalg *self);
  869. /* Utility functions implemented centrally */
  870. ssh_key *ssh_key_clone(ssh_key *key);
  871. /*
  872. * SSH2 ECDH key exchange vtable
  873. */
  874. struct ecdh_key {
  875. const ecdh_keyalg *vt;
  876. };
  877. struct ecdh_keyalg {
  878. /* Unusually, the 'new' method here doesn't directly take a vt
  879. * pointer, because it will also need the containing ssh_kex
  880. * structure for top-level parameters, and since that contains a
  881. * vt pointer anyway, we might as well _only_ pass that. */
  882. ecdh_key *(*new)(const ssh_kex *kex, bool is_server);
  883. void (*free)(ecdh_key *key);
  884. void (*getpublic)(ecdh_key *key, BinarySink *bs);
  885. bool (*getkey)(ecdh_key *key, ptrlen remoteKey, BinarySink *bs);
  886. char *(*description)(const ssh_kex *kex);
  887. /* Some things that use this vtable are genuinely elliptic-curve
  888. * Diffie-Hellman. Others are hybrid PQ + classical kex methods.
  889. * Provide a packet-naming context for use in the SSH log. (Purely
  890. * cosmetic.) */
  891. Pkt_KCtx packet_naming_ctx;
  892. };
  893. static inline ecdh_key *ecdh_key_new(const ssh_kex *kex, bool is_server)
  894. { return kex->ecdh_vt->new(kex, is_server); }
  895. static inline void ecdh_key_free(ecdh_key *key)
  896. { key->vt->free(key); }
  897. static inline void ecdh_key_getpublic(ecdh_key *key, BinarySink *bs)
  898. { key->vt->getpublic(key, bs); }
  899. static inline bool ecdh_key_getkey(ecdh_key *key, ptrlen remoteKey,
  900. BinarySink *bs)
  901. { return key->vt->getkey(key, remoteKey, bs); }
  902. static inline char *ecdh_keyalg_description(const ssh_kex *kex)
  903. { return kex->ecdh_vt->description(kex); }
  904. /*
  905. * vtable for post-quantum key encapsulation methods (things like NTRU
  906. * and ML-KEM).
  907. *
  908. * These work in an asymmetric way that's conceptually more like the
  909. * old RSA kex (either SSH-1 or SSH-2) than like Diffie-Hellman. One
  910. * party generates a keypair and sends the public key; the other party
  911. * invents a secret and encrypts it with the public key; the first
  912. * party receives the ciphertext and decrypts it, and now both parties
  913. * have the secret.
  914. */
  915. struct pq_kem_dk {
  916. const pq_kemalg *vt;
  917. };
  918. struct pq_kemalg {
  919. /* Generate a key pair, writing the public encryption key in wire
  920. * format to ek. Return the decryption key. */
  921. pq_kem_dk *(*keygen)(const pq_kemalg *alg, BinarySink *ek);
  922. /* Invent and encrypt a secret, writing the ciphertext in wire
  923. * format to c and the secret itself to k. Returns false on any
  924. * kind of really obvious validation failure of the encryption key. */
  925. bool (*encaps)(const pq_kemalg *alg, BinarySink *c, BinarySink *k,
  926. ptrlen ek);
  927. /* Decrypt the secret and write it to k. Returns false on
  928. * validation failure. However, more competent cryptographic
  929. * attacks are rejected in a way that's not obvious, returning a
  930. * useless pseudorandom secret. */
  931. bool (*decaps)(pq_kem_dk *dk, BinarySink *k, ptrlen c);
  932. /* Free a decryption key. */
  933. void (*free_dk)(pq_kem_dk *dk);
  934. const void *extra;
  935. const char *description;
  936. size_t ek_len, c_len;
  937. };
  938. static inline pq_kem_dk *pq_kem_keygen(const pq_kemalg *alg, BinarySink *ek)
  939. { return alg->keygen(alg, ek); }
  940. static inline bool pq_kem_encaps(const pq_kemalg *alg, BinarySink *c,
  941. BinarySink *k, ptrlen ek)
  942. { return alg->encaps(alg, c, k, ek); }
  943. static inline bool pq_kem_decaps(pq_kem_dk *dk, BinarySink *k, ptrlen c)
  944. { return dk->vt->decaps(dk, k, c); }
  945. static inline void pq_kem_free_dk(pq_kem_dk *dk)
  946. { dk->vt->free_dk(dk); }
  947. /*
  948. * Suffix on GSSAPI SSH protocol identifiers that indicates Kerberos 5
  949. * as the mechanism.
  950. *
  951. * This suffix is the base64-encoded MD5 hash of the byte sequence
  952. * 06 09 2A 86 48 86 F7 12 01 02 02, which in turn is the ASN.1 DER
  953. * encoding of the object ID 1.2.840.113554.1.2.2 which designates
  954. * Kerberos v5.
  955. *
  956. * (The same encoded OID, minus the two-byte DER header, is defined in
  957. * ssh/pgssapi.c as GSS_MECH_KRB5.)
  958. */
  959. #define GSS_KRB5_OID_HASH "toWM5Slw5Ew8Mqkay+al2g=="
  960. /*
  961. * Enumeration of signature flags from draft-miller-ssh-agent-02
  962. */
  963. #define SSH_AGENT_RSA_SHA2_256 2
  964. #define SSH_AGENT_RSA_SHA2_512 4
  965. struct ssh_compressor {
  966. const ssh_compression_alg *vt;
  967. };
  968. struct ssh_decompressor {
  969. const ssh_compression_alg *vt;
  970. };
  971. struct ssh_compression_alg {
  972. const char *name;
  973. /* For zlib@openssh.com: if non-NULL, this name will be considered once
  974. * userauth has completed successfully. */
  975. const char *delayed_name;
  976. ssh_compressor *(*compress_new)(void);
  977. void (*compress_free)(ssh_compressor *);
  978. void (*compress)(ssh_compressor *, const unsigned char *block, int len,
  979. unsigned char **outblock, int *outlen,
  980. int minlen);
  981. ssh_decompressor *(*decompress_new)(void);
  982. void (*decompress_free)(ssh_decompressor *);
  983. bool (*decompress)(ssh_decompressor *, const unsigned char *block, int len,
  984. unsigned char **outblock, int *outlen);
  985. const char *text_name;
  986. };
  987. static inline ssh_compressor *ssh_compressor_new(
  988. const ssh_compression_alg *alg)
  989. { return alg->compress_new(); }
  990. static inline ssh_decompressor *ssh_decompressor_new(
  991. const ssh_compression_alg *alg)
  992. { return alg->decompress_new(); }
  993. static inline void ssh_compressor_free(ssh_compressor *c)
  994. { c->vt->compress_free(c); }
  995. static inline void ssh_decompressor_free(ssh_decompressor *d)
  996. { d->vt->decompress_free(d); }
  997. static inline void ssh_compressor_compress(
  998. ssh_compressor *c, const unsigned char *block, int len,
  999. unsigned char **outblock, int *outlen, int minlen)
  1000. { c->vt->compress(c, block, len, outblock, outlen, minlen); }
  1001. static inline bool ssh_decompressor_decompress(
  1002. ssh_decompressor *d, const unsigned char *block, int len,
  1003. unsigned char **outblock, int *outlen)
  1004. { return d->vt->decompress(d, block, len, outblock, outlen); }
  1005. static inline const ssh_compression_alg *ssh_compressor_alg(
  1006. ssh_compressor *c)
  1007. { return c->vt; }
  1008. static inline const ssh_compression_alg *ssh_decompressor_alg(
  1009. ssh_decompressor *d)
  1010. { return d->vt; }
  1011. struct ssh2_userkey {
  1012. ssh_key *key; /* the key itself */
  1013. char *comment; /* the key comment */
  1014. };
  1015. /* Argon2 password hashing function */
  1016. typedef enum { Argon2d = 0, Argon2i = 1, Argon2id = 2 } Argon2Flavour;
  1017. void argon2(Argon2Flavour, uint32_t mem, uint32_t passes,
  1018. uint32_t parallel, uint32_t taglen,
  1019. ptrlen P, ptrlen S, ptrlen K, ptrlen X, strbuf *out);
  1020. void argon2_choose_passes(
  1021. Argon2Flavour, uint32_t mem, uint32_t milliseconds, uint32_t *passes,
  1022. uint32_t parallel, uint32_t taglen, ptrlen P, ptrlen S, ptrlen K, ptrlen X,
  1023. strbuf *out);
  1024. /* The H' hash defined in Argon2, exposed just for testcrypt */
  1025. strbuf *argon2_long_hash(unsigned length, ptrlen data);
  1026. /* The maximum length of any hash algorithm. (bytes) */
  1027. #define MAX_HASH_LEN (114) /* longest is SHAKE256 with 114-byte output */
  1028. extern const ssh_cipheralg ssh_3des_ssh1;
  1029. extern const ssh_cipheralg ssh_blowfish_ssh1;
  1030. extern const ssh_cipheralg ssh_3des_ssh2_ctr;
  1031. extern const ssh_cipheralg ssh_3des_ssh2;
  1032. extern const ssh_cipheralg ssh_des;
  1033. extern const ssh_cipheralg ssh_des_sshcom_ssh2;
  1034. extern const ssh_cipheralg ssh_aes256_sdctr;
  1035. extern const ssh_cipheralg ssh_aes256_sdctr_ni;
  1036. extern const ssh_cipheralg ssh_aes256_sdctr_neon;
  1037. extern const ssh_cipheralg ssh_aes256_sdctr_sw;
  1038. extern const ssh_cipheralg ssh_aes256_gcm;
  1039. extern const ssh_cipheralg ssh_aes256_gcm_ni;
  1040. extern const ssh_cipheralg ssh_aes256_gcm_neon;
  1041. extern const ssh_cipheralg ssh_aes256_gcm_sw;
  1042. extern const ssh_cipheralg ssh_aes256_cbc;
  1043. extern const ssh_cipheralg ssh_aes256_cbc_ni;
  1044. extern const ssh_cipheralg ssh_aes256_cbc_neon;
  1045. extern const ssh_cipheralg ssh_aes256_cbc_sw;
  1046. extern const ssh_cipheralg ssh_aes192_sdctr;
  1047. extern const ssh_cipheralg ssh_aes192_sdctr_ni;
  1048. extern const ssh_cipheralg ssh_aes192_sdctr_neon;
  1049. extern const ssh_cipheralg ssh_aes192_sdctr_sw;
  1050. extern const ssh_cipheralg ssh_aes192_gcm;
  1051. extern const ssh_cipheralg ssh_aes192_gcm_ni;
  1052. extern const ssh_cipheralg ssh_aes192_gcm_neon;
  1053. extern const ssh_cipheralg ssh_aes192_gcm_sw;
  1054. extern const ssh_cipheralg ssh_aes192_cbc;
  1055. extern const ssh_cipheralg ssh_aes192_cbc_ni;
  1056. extern const ssh_cipheralg ssh_aes192_cbc_neon;
  1057. extern const ssh_cipheralg ssh_aes192_cbc_sw;
  1058. extern const ssh_cipheralg ssh_aes128_sdctr;
  1059. extern const ssh_cipheralg ssh_aes128_sdctr_ni;
  1060. extern const ssh_cipheralg ssh_aes128_sdctr_neon;
  1061. extern const ssh_cipheralg ssh_aes128_sdctr_sw;
  1062. extern const ssh_cipheralg ssh_aes128_gcm;
  1063. extern const ssh_cipheralg ssh_aes128_gcm_ni;
  1064. extern const ssh_cipheralg ssh_aes128_gcm_neon;
  1065. extern const ssh_cipheralg ssh_aes128_gcm_sw;
  1066. extern const ssh_cipheralg ssh_aes128_cbc;
  1067. extern const ssh_cipheralg ssh_aes128_cbc_ni;
  1068. extern const ssh_cipheralg ssh_aes128_cbc_neon;
  1069. extern const ssh_cipheralg ssh_aes128_cbc_sw;
  1070. extern const ssh_cipheralg ssh_blowfish_ssh2_ctr;
  1071. extern const ssh_cipheralg ssh_blowfish_ssh2;
  1072. extern const ssh_cipheralg ssh_arcfour256_ssh2;
  1073. extern const ssh_cipheralg ssh_arcfour128_ssh2;
  1074. extern const ssh_cipheralg ssh2_chacha20_poly1305;
  1075. extern const ssh2_ciphers ssh2_3des;
  1076. extern const ssh2_ciphers ssh2_des;
  1077. extern const ssh2_ciphers ssh2_aes;
  1078. extern const ssh2_ciphers ssh2_blowfish;
  1079. extern const ssh2_ciphers ssh2_arcfour;
  1080. extern const ssh2_ciphers ssh2_ccp;
  1081. extern const ssh2_ciphers ssh2_aesgcm;
  1082. extern const ssh_hashalg ssh_md5;
  1083. extern const ssh_hashalg ssh_sha1;
  1084. extern const ssh_hashalg ssh_sha1_ni;
  1085. extern const ssh_hashalg ssh_sha1_neon;
  1086. extern const ssh_hashalg ssh_sha1_sw;
  1087. extern const ssh_hashalg ssh_sha256;
  1088. extern const ssh_hashalg ssh_sha256_ni;
  1089. extern const ssh_hashalg ssh_sha256_neon;
  1090. extern const ssh_hashalg ssh_sha256_sw;
  1091. extern const ssh_hashalg ssh_sha384;
  1092. extern const ssh_hashalg ssh_sha384_neon;
  1093. extern const ssh_hashalg ssh_sha384_sw;
  1094. extern const ssh_hashalg ssh_sha512;
  1095. extern const ssh_hashalg ssh_sha512_neon;
  1096. extern const ssh_hashalg ssh_sha512_sw;
  1097. extern const ssh_hashalg ssh_sha3_224;
  1098. extern const ssh_hashalg ssh_sha3_256;
  1099. extern const ssh_hashalg ssh_sha3_384;
  1100. extern const ssh_hashalg ssh_sha3_512;
  1101. extern const ssh_hashalg ssh_shake256_32bytes;
  1102. extern const ssh_hashalg ssh_shake256_114bytes;
  1103. extern const ssh_hashalg ssh_blake2b;
  1104. extern const ssh_kexes ssh_diffiehellman_group1;
  1105. extern const ssh_kexes ssh_diffiehellman_group14;
  1106. extern const ssh_kexes ssh_diffiehellman_group15;
  1107. extern const ssh_kexes ssh_diffiehellman_group16;
  1108. extern const ssh_kexes ssh_diffiehellman_group17;
  1109. extern const ssh_kexes ssh_diffiehellman_group18;
  1110. extern const ssh_kexes ssh_diffiehellman_gex;
  1111. extern const ssh_kex ssh_diffiehellman_group1_sha1;
  1112. extern const ssh_kex ssh_diffiehellman_group14_sha256;
  1113. extern const ssh_kex ssh_diffiehellman_group14_sha1;
  1114. extern const ssh_kex ssh_diffiehellman_group15_sha512;
  1115. extern const ssh_kex ssh_diffiehellman_group16_sha512;
  1116. extern const ssh_kex ssh_diffiehellman_group17_sha512;
  1117. extern const ssh_kex ssh_diffiehellman_group18_sha512;
  1118. extern const ssh_kexes ssh_gssk5_sha1_kex;
  1119. extern const ssh_kexes ssh_gssk5_sha2_kex;
  1120. extern const ssh_kexes ssh_gssk5_ecdh_kex;
  1121. extern const ssh_kexes ssh_rsa_kex;
  1122. extern const ssh_kex ssh_ec_kex_curve25519;
  1123. extern const ssh_kex ssh_ec_kex_curve448;
  1124. extern const ssh_kex ssh_ec_kex_nistp256;
  1125. extern const ssh_kex ssh_ec_kex_nistp384;
  1126. extern const ssh_kex ssh_ec_kex_nistp521;
  1127. extern const ssh_kexes ssh_ecdh_kex;
  1128. extern const ssh_kexes ssh_ntru_hybrid_kex;
  1129. extern const pq_kemalg ssh_ntru;
  1130. extern const ssh_kexes ssh_mlkem_curve25519_hybrid_kex;
  1131. extern const ssh_kexes ssh_mlkem_nist_hybrid_kex;
  1132. extern const pq_kemalg ssh_mlkem512;
  1133. extern const pq_kemalg ssh_mlkem768;
  1134. extern const pq_kemalg ssh_mlkem1024;
  1135. extern const ssh_keyalg ssh_dsa;
  1136. extern const ssh_keyalg ssh_rsa;
  1137. extern const ssh_keyalg ssh_rsa_sha256;
  1138. extern const ssh_keyalg ssh_rsa_sha512;
  1139. extern const ssh_keyalg ssh_ecdsa_ed25519;
  1140. extern const ssh_keyalg ssh_ecdsa_ed448;
  1141. extern const ssh_keyalg ssh_ecdsa_nistp256;
  1142. extern const ssh_keyalg ssh_ecdsa_nistp384;
  1143. extern const ssh_keyalg ssh_ecdsa_nistp521;
  1144. extern const ssh_keyalg opensshcert_ssh_dsa;
  1145. extern const ssh_keyalg opensshcert_ssh_rsa;
  1146. extern const ssh_keyalg opensshcert_ssh_rsa_sha256;
  1147. extern const ssh_keyalg opensshcert_ssh_rsa_sha512;
  1148. extern const ssh_keyalg opensshcert_ssh_ecdsa_ed25519;
  1149. extern const ssh_keyalg opensshcert_ssh_ecdsa_nistp256;
  1150. extern const ssh_keyalg opensshcert_ssh_ecdsa_nistp384;
  1151. extern const ssh_keyalg opensshcert_ssh_ecdsa_nistp521;
  1152. extern const ssh2_macalg ssh_hmac_md5;
  1153. extern const ssh2_macalg ssh_hmac_sha1;
  1154. extern const ssh2_macalg ssh_hmac_sha1_buggy;
  1155. extern const ssh2_macalg ssh_hmac_sha1_96;
  1156. extern const ssh2_macalg ssh_hmac_sha1_96_buggy;
  1157. extern const ssh2_macalg ssh_hmac_sha256;
  1158. extern const ssh2_macalg ssh_hmac_sha384;
  1159. extern const ssh2_macalg ssh_hmac_sha512;
  1160. extern const ssh2_macalg ssh2_poly1305;
  1161. extern const ssh2_macalg ssh2_aesgcm_mac;
  1162. extern const ssh2_macalg ssh2_aesgcm_mac_sw;
  1163. extern const ssh2_macalg ssh2_aesgcm_mac_ref_poly;
  1164. extern const ssh2_macalg ssh2_aesgcm_mac_clmul;
  1165. extern const ssh2_macalg ssh2_aesgcm_mac_neon;
  1166. extern const ssh_compression_alg ssh_zlib;
  1167. /* Special constructor: BLAKE2b can be instantiated with any hash
  1168. * length up to 128 bytes */
  1169. ssh_hash *blake2b_new_general(unsigned hashlen);
  1170. /* Special test function for AES-GCM */
  1171. void aesgcm_set_prefix_lengths(ssh2_mac *mac, size_t skip, size_t aad);
  1172. /* Shake128/256 extendable output functions (like a hash except you don't
  1173. * commit up front to how much data you want to get out of it) */
  1174. ShakeXOF *shake128_xof_from_input(ptrlen data);
  1175. ShakeXOF *shake256_xof_from_input(ptrlen data);
  1176. void shake_xof_read(ShakeXOF *sx, void *output_v, size_t size);
  1177. void shake_xof_free(ShakeXOF *sx);
  1178. /*
  1179. * On some systems, you have to detect hardware crypto acceleration by
  1180. * asking the local OS API rather than OS-agnostically asking the CPU
  1181. * itself. If so, then this function should be implemented in each
  1182. * platform subdirectory.
  1183. */
  1184. bool platform_aes_neon_available(void);
  1185. bool platform_pmull_neon_available(void);
  1186. bool platform_sha256_neon_available(void);
  1187. bool platform_sha1_neon_available(void);
  1188. bool platform_sha512_neon_available(void);
  1189. bool platform_dit_available(void);
  1190. /*
  1191. * PuTTY version number formatted as an SSH version string.
  1192. */
  1193. extern const char sshver[];
  1194. /*
  1195. * Gross hack: pscp will try to start SFTP but fall back to scp1 if
  1196. * that fails. This variable is the means by which pscp.c can reach
  1197. * into the SSH code and find out which one it got.
  1198. */
  1199. extern bool ssh_fallback_cmd(Backend *backend);
  1200. /*
  1201. * The PRNG type, defined in prng.c. Visible data fields are
  1202. * 'savesize', which suggests how many random bytes you should request
  1203. * from a particular PRNG instance to write to putty.rnd, and a
  1204. * BinarySink implementation which you can use to write seed data in
  1205. * between calling prng_seed_{begin,finish}.
  1206. */
  1207. struct prng {
  1208. size_t savesize;
  1209. BinarySink_IMPLEMENTATION;
  1210. /* (also there's a surrounding implementation struct in prng.c) */
  1211. };
  1212. prng *prng_new(const ssh_hashalg *hashalg);
  1213. void prng_free(prng *p);
  1214. void prng_seed_begin(prng *p);
  1215. void prng_seed_finish(prng *p);
  1216. void prng_read(prng *p, void *vout, size_t size);
  1217. void prng_add_entropy(prng *p, unsigned source_id, ptrlen data);
  1218. size_t prng_seed_bits(prng *p);
  1219. /* This function must be implemented by the platform, and returns a
  1220. * timer in milliseconds that the PRNG can use to know whether it's
  1221. * been reseeded too recently to do it again.
  1222. *
  1223. * The PRNG system has its own special timing function not because its
  1224. * timing needs are unusual in the real applications, but simply so
  1225. * that testcrypt can mock it to keep the tests deterministic. */
  1226. uint64_t prng_reseed_time_ms(void);
  1227. void random_read(void *out, size_t size);
  1228. /* Exports from x11fwd.c */
  1229. enum {
  1230. X11_TRANS_IPV4 = 0, X11_TRANS_IPV6 = 6, X11_TRANS_UNIX = 256
  1231. };
  1232. struct X11Display {
  1233. /* Broken-down components of the display name itself */
  1234. bool unixdomain;
  1235. char *hostname;
  1236. int displaynum;
  1237. int screennum;
  1238. /* OSX sometimes replaces all the above with a full Unix-socket pathname */
  1239. char *unixsocketpath;
  1240. /* PuTTY networking SockAddr to connect to the display, and associated
  1241. * gubbins */
  1242. SockAddr *addr;
  1243. int port;
  1244. char *realhost;
  1245. /* Our local auth details for talking to the real X display. */
  1246. int localauthproto;
  1247. unsigned char *localauthdata;
  1248. int localauthdatalen;
  1249. };
  1250. struct X11FakeAuth {
  1251. /* Auth details we invented for a virtual display on the SSH server. */
  1252. int proto;
  1253. unsigned char *data;
  1254. int datalen;
  1255. char *protoname;
  1256. char *datastring;
  1257. /* The encrypted form of the first block, in XDM-AUTHORIZATION-1.
  1258. * Used as part of the key when these structures are organised
  1259. * into a tree. See x11_invent_fake_auth for explanation. */
  1260. unsigned char *xa1_firstblock;
  1261. /*
  1262. * Used inside x11fwd.c to remember recently seen
  1263. * XDM-AUTHORIZATION-1 strings, to avoid replay attacks.
  1264. */
  1265. tree234 *xdmseen;
  1266. /*
  1267. * What to do with an X connection matching this auth data.
  1268. */
  1269. struct X11Display *disp;
  1270. ssh_sharing_connstate *share_cs;
  1271. share_channel *share_chan;
  1272. };
  1273. int x11_authcmp(void *av, void *bv); /* for putting X11FakeAuth in a tree234 */
  1274. /*
  1275. * x11_setup_display() parses the display variable and fills in an
  1276. * X11Display structure. Some remote auth details are invented;
  1277. * the supplied authtype parameter configures the preferred
  1278. * authorisation protocol to use at the remote end. The local auth
  1279. * details are looked up by calling platform_get_x11_auth.
  1280. *
  1281. * If the returned pointer is NULL, then *error_msg will contain a
  1282. * dynamically allocated error message string.
  1283. */
  1284. extern struct X11Display *x11_setup_display(const char *display, Conf *,
  1285. char **error_msg);
  1286. void x11_free_display(struct X11Display *disp);
  1287. struct X11FakeAuth *x11_invent_fake_auth(tree234 *t, int authtype);
  1288. void x11_free_fake_auth(struct X11FakeAuth *auth);
  1289. Channel *x11_new_channel(tree234 *authtree, SshChannel *c,
  1290. const char *peeraddr, int peerport,
  1291. bool connection_sharing_possible);
  1292. char *x11_display(const char *display);
  1293. /* Platform-dependent X11 functions */
  1294. extern void platform_get_x11_auth(struct X11Display *display, Conf *);
  1295. /* examine a mostly-filled-in X11Display and fill in localauth* */
  1296. extern const bool platform_uses_x11_unix_by_default;
  1297. /* choose default X transport in the absence of a specified one */
  1298. SockAddr *platform_get_x11_unix_address(const char *path, int displaynum);
  1299. /* make up a SockAddr naming the address for displaynum */
  1300. char *platform_get_x_display(void);
  1301. /* allocated local X display string, if any */
  1302. /* X11-related helper functions in utils */
  1303. /*
  1304. * This function does the job of platform_get_x11_auth, provided
  1305. * it is told where to find a normally formatted .Xauthority file:
  1306. * it opens that file, parses it to find an auth record which
  1307. * matches the display details in "display", and fills in the
  1308. * localauth fields.
  1309. *
  1310. * It is expected that most implementations of
  1311. * platform_get_x11_auth() will work by finding their system's
  1312. * .Xauthority file, adjusting the display details if necessary
  1313. * for local oddities like Unix-domain socket transport, and
  1314. * calling this function to do the rest of the work.
  1315. */
  1316. void x11_get_auth_from_authfile(struct X11Display *display,
  1317. Filename *authfilename);
  1318. void x11_format_auth_for_authfile(
  1319. BinarySink *bs, SockAddr *addr, int display_no,
  1320. ptrlen authproto, ptrlen authdata);
  1321. void *x11_make_greeting(int endian, int protomajor, int protominor,
  1322. int auth_proto, const void *auth_data, int auth_len,
  1323. const char *peer_ip, int peer_port,
  1324. int *outlen);
  1325. int x11_identify_auth_proto(ptrlen protoname);
  1326. void *x11_dehexify(ptrlen hex, int *outlen);
  1327. bool x11_parse_ip(const char *addr_string, unsigned long *ip);
  1328. Channel *agentf_new(SshChannel *c);
  1329. bool dh_is_gex(const ssh_kex *kex);
  1330. dh_ctx *dh_setup_group(const ssh_kex *kex);
  1331. dh_ctx *dh_setup_gex(mp_int *pval, mp_int *gval);
  1332. int dh_modulus_bit_size(const dh_ctx *ctx);
  1333. void dh_cleanup(dh_ctx *);
  1334. mp_int *dh_create_e(dh_ctx *);
  1335. const char *dh_validate_f(dh_ctx *, mp_int *f);
  1336. mp_int *dh_find_K(dh_ctx *, mp_int *f);
  1337. static inline bool is_base64_char(char c)
  1338. {
  1339. return ((c >= '0' && c <= '9') ||
  1340. (c >= 'a' && c <= 'z') ||
  1341. (c >= 'A' && c <= 'Z') ||
  1342. c == '+' || c == '/' || c == '=');
  1343. }
  1344. extern int base64_lines(int datalen);
  1345. /* ppk_load_* can return this as an error */
  1346. extern ssh2_userkey ssh2_wrong_passphrase;
  1347. #define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
  1348. bool ppk_encrypted_s(BinarySource *src, char **comment);
  1349. bool ppk_encrypted_f(const Filename *filename, char **comment);
  1350. bool rsa1_encrypted_s(BinarySource *src, char **comment);
  1351. bool rsa1_encrypted_f(const Filename *filename, char **comment);
  1352. ssh2_userkey *ppk_load_s(BinarySource *src, const char *passphrase,
  1353. const char **errorstr);
  1354. ssh2_userkey *ppk_load_f(const Filename *filename, const char *passphrase,
  1355. const char **errorstr);
  1356. int rsa1_load_s(BinarySource *src, RSAKey *key,
  1357. const char *passphrase, const char **errorstr);
  1358. int rsa1_load_f(const Filename *filename, RSAKey *key,
  1359. const char *passphrase, const char **errorstr);
  1360. typedef struct ppk_save_parameters {
  1361. unsigned fmt_version; /* currently 2 or 3 */
  1362. /*
  1363. * Parameters for fmt_version == 3
  1364. */
  1365. Argon2Flavour argon2_flavour;
  1366. uint32_t argon2_mem; /* in Kbyte */
  1367. bool argon2_passes_auto;
  1368. union {
  1369. uint32_t argon2_passes; /* if auto == false */
  1370. uint32_t argon2_milliseconds; /* if auto == true */
  1371. };
  1372. uint32_t argon2_parallelism;
  1373. /* The ability to choose a specific salt is only intended for the
  1374. * use of the automated test of PuTTYgen. It's a (mild) security
  1375. * risk to do it with any passphrase you actually care about,
  1376. * because it invalidates the entire point of having a salt in the
  1377. * first place. */
  1378. const uint8_t *salt;
  1379. size_t saltlen;
  1380. } ppk_save_parameters;
  1381. extern const ppk_save_parameters ppk_save_default_parameters;
  1382. strbuf *ppk_save_sb(ssh2_userkey *key, const char *passphrase,
  1383. const ppk_save_parameters *params);
  1384. bool ppk_save_f(const Filename *filename, ssh2_userkey *key,
  1385. const char *passphrase, const ppk_save_parameters *params);
  1386. strbuf *rsa1_save_sb(RSAKey *key, const char *passphrase);
  1387. bool rsa1_save_f(const Filename *filename, RSAKey *key,
  1388. const char *passphrase);
  1389. bool ppk_loadpub_s(BinarySource *src, char **algorithm, BinarySink *bs,
  1390. char **commentptr, const char **errorstr);
  1391. bool ppk_loadpub_f(const Filename *filename, char **algorithm, BinarySink *bs,
  1392. char **commentptr, const char **errorstr);
  1393. int rsa1_loadpub_s(BinarySource *src, BinarySink *bs,
  1394. char **commentptr, const char **errorstr);
  1395. int rsa1_loadpub_f(const Filename *filename, BinarySink *bs,
  1396. char **commentptr, const char **errorstr);
  1397. extern const ssh_keyalg *const all_keyalgs[];
  1398. extern const size_t n_keyalgs;
  1399. const ssh_keyalg *find_pubkey_alg(const char *name);
  1400. const ssh_keyalg *find_pubkey_alg_len(ptrlen name);
  1401. ptrlen pubkey_blob_to_alg_name(ptrlen blob);
  1402. const ssh_keyalg *pubkey_blob_to_alg(ptrlen blob);
  1403. /* Convenient wrappers on the LoadedFile mechanism suitable for key files */
  1404. LoadedFile *lf_load_keyfile(const Filename *filename, const char **errptr);
  1405. LoadedFile *lf_load_keyfile_fp(FILE *fp, const char **errptr);
  1406. enum {
  1407. SSH_KEYTYPE_UNOPENABLE,
  1408. SSH_KEYTYPE_UNKNOWN,
  1409. SSH_KEYTYPE_SSH1, SSH_KEYTYPE_SSH2,
  1410. /*
  1411. * The OpenSSH key types deserve a little explanation. OpenSSH has
  1412. * two physical formats for private key storage: an old PEM-based
  1413. * one largely dictated by their use of OpenSSL and full of ASN.1,
  1414. * and a new one using the same private key formats used over the
  1415. * wire for talking to ssh-agent. The old format can only support
  1416. * a subset of the key types, because it needs redesign for each
  1417. * key type, and after a while they decided to move to the new
  1418. * format so as not to have to do that.
  1419. *
  1420. * On input, key files are identified as either
  1421. * SSH_KEYTYPE_OPENSSH_PEM or SSH_KEYTYPE_OPENSSH_NEW, describing
  1422. * accurately which actual format the keys are stored in.
  1423. *
  1424. * On output, however, we default to following OpenSSH's own
  1425. * policy of writing out PEM-style keys for maximum backwards
  1426. * compatibility if the key type supports it, and otherwise
  1427. * switching to the new format. So the formats you can select for
  1428. * output are SSH_KEYTYPE_OPENSSH_NEW (forcing the new format for
  1429. * any key type), and SSH_KEYTYPE_OPENSSH_AUTO to use the oldest
  1430. * format supported by whatever key type you're writing out.
  1431. *
  1432. * So we have three type codes, but only two of them usable in any
  1433. * given circumstance. An input key file will never be identified
  1434. * as AUTO, only PEM or NEW; key export UIs should not be able to
  1435. * select PEM, only AUTO or NEW.
  1436. */
  1437. SSH_KEYTYPE_OPENSSH_AUTO,
  1438. SSH_KEYTYPE_OPENSSH_PEM,
  1439. SSH_KEYTYPE_OPENSSH_NEW,
  1440. SSH_KEYTYPE_SSHCOM,
  1441. /*
  1442. * Public-key-only formats, which we still want to be able to read
  1443. * for various purposes.
  1444. */
  1445. SSH_KEYTYPE_SSH1_PUBLIC,
  1446. SSH_KEYTYPE_SSH2_PUBLIC_RFC4716,
  1447. SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH
  1448. };
  1449. typedef enum {
  1450. /* Default fingerprint types strip off a certificate to show you
  1451. * the fingerprint of the underlying public key */
  1452. SSH_FPTYPE_MD5,
  1453. SSH_FPTYPE_SHA256,
  1454. /* Non-default version of each fingerprint type which is 'raw',
  1455. * giving you the true hash of the public key blob even if it
  1456. * includes a certificate */
  1457. SSH_FPTYPE_MD5_CERT,
  1458. SSH_FPTYPE_SHA256_CERT,
  1459. } FingerprintType;
  1460. static inline bool ssh_fptype_is_cert(FingerprintType fptype)
  1461. {
  1462. return fptype >= SSH_FPTYPE_MD5_CERT;
  1463. }
  1464. static inline FingerprintType ssh_fptype_from_cert(FingerprintType fptype)
  1465. {
  1466. if (ssh_fptype_is_cert(fptype))
  1467. fptype -= (SSH_FPTYPE_MD5_CERT - SSH_FPTYPE_MD5);
  1468. return fptype;
  1469. }
  1470. static inline FingerprintType ssh_fptype_to_cert(FingerprintType fptype)
  1471. {
  1472. if (!ssh_fptype_is_cert(fptype))
  1473. fptype += (SSH_FPTYPE_MD5_CERT - SSH_FPTYPE_MD5);
  1474. return fptype;
  1475. }
  1476. #define SSH_N_FPTYPES (SSH_FPTYPE_SHA256_CERT + 1)
  1477. #define SSH_FPTYPE_DEFAULT SSH_FPTYPE_SHA256
  1478. FingerprintType ssh2_pick_fingerprint(char **fingerprints,
  1479. FingerprintType preferred_type);
  1480. FingerprintType ssh2_pick_default_fingerprint(char **fingerprints);
  1481. char *ssh1_pubkey_str(RSAKey *ssh1key);
  1482. void ssh1_write_pubkey(FILE *fp, RSAKey *ssh1key);
  1483. char *ssh2_pubkey_openssh_str(ssh2_userkey *key);
  1484. void ssh2_write_pubkey(FILE *fp, const char *comment,
  1485. const void *v_pub_blob, int pub_len,
  1486. int keytype);
  1487. char *ssh2_fingerprint_blob(ptrlen, FingerprintType);
  1488. char *ssh2_fingerprint(ssh_key *key, FingerprintType);
  1489. char *ssh2_double_fingerprint_blob(ptrlen, FingerprintType);
  1490. char *ssh2_double_fingerprint(ssh_key *key, FingerprintType);
  1491. char **ssh2_all_fingerprints_for_blob(ptrlen);
  1492. char **ssh2_all_fingerprints(ssh_key *key);
  1493. void ssh2_free_all_fingerprints(char **);
  1494. int key_type(const Filename *filename);
  1495. int key_type_s(BinarySource *src);
  1496. const char *key_type_to_str(int type);
  1497. bool import_possible(int type);
  1498. int import_target_type(int type);
  1499. bool import_encrypted(const Filename *filename, int type, char **comment);
  1500. bool import_encrypted_s(const Filename *filename, BinarySource *src,
  1501. int type, char **comment);
  1502. int import_ssh1(const Filename *filename, int type,
  1503. RSAKey *key, char *passphrase, const char **errmsg_p);
  1504. int import_ssh1_s(BinarySource *src, int type,
  1505. RSAKey *key, char *passphrase, const char **errmsg_p);
  1506. ssh2_userkey *import_ssh2(const Filename *filename, int type,
  1507. char *passphrase, const char **errmsg_p);
  1508. ssh2_userkey *import_ssh2_s(BinarySource *src, int type,
  1509. char *passphrase, const char **errmsg_p);
  1510. bool export_ssh1(const Filename *filename, int type,
  1511. RSAKey *key, char *passphrase);
  1512. bool export_ssh2(const Filename *filename, int type,
  1513. ssh2_userkey *key, char *passphrase);
  1514. void des3_decrypt_pubkey(const void *key, void *blk, int len);
  1515. void des3_encrypt_pubkey(const void *key, void *blk, int len);
  1516. void des3_decrypt_pubkey_ossh(const void *key, const void *iv,
  1517. void *blk, int len);
  1518. void des3_encrypt_pubkey_ossh(const void *key, const void *iv,
  1519. void *blk, int len);
  1520. void aes256_encrypt_pubkey(const void *key, const void *iv,
  1521. void *blk, int len);
  1522. void aes256_decrypt_pubkey(const void *key, const void *iv,
  1523. void *blk, int len);
  1524. void des_encrypt_xdmauth(const void *key, void *blk, int len);
  1525. void des_decrypt_xdmauth(const void *key, void *blk, int len);
  1526. void openssh_bcrypt(ptrlen passphrase, ptrlen salt,
  1527. int rounds, unsigned char *out, int outbytes);
  1528. /*
  1529. * Connection-sharing API provided by platforms. This function must
  1530. * either:
  1531. * - return SHARE_NONE and do nothing
  1532. * - return SHARE_DOWNSTREAM and set *sock to a Socket connected to
  1533. * downplug
  1534. * - return SHARE_UPSTREAM and set *sock to a Socket connected to
  1535. * upplug.
  1536. */
  1537. enum { SHARE_NONE, SHARE_DOWNSTREAM, SHARE_UPSTREAM };
  1538. int platform_ssh_share(const char *name, Conf *conf,
  1539. Plug *downplug, Plug *upplug, Socket **sock,
  1540. char **logtext, char **ds_err, char **us_err,
  1541. bool can_upstream, bool can_downstream);
  1542. void platform_ssh_share_cleanup(const char *name);
  1543. /*
  1544. * List macro defining the SSH-1 message type codes.
  1545. */
  1546. #define SSH1_MESSAGE_TYPES(X, y) \
  1547. X(y, SSH1_MSG_DISCONNECT, 1) \
  1548. X(y, SSH1_SMSG_PUBLIC_KEY, 2) \
  1549. X(y, SSH1_CMSG_SESSION_KEY, 3) \
  1550. X(y, SSH1_CMSG_USER, 4) \
  1551. X(y, SSH1_CMSG_AUTH_RSA, 6) \
  1552. X(y, SSH1_SMSG_AUTH_RSA_CHALLENGE, 7) \
  1553. X(y, SSH1_CMSG_AUTH_RSA_RESPONSE, 8) \
  1554. X(y, SSH1_CMSG_AUTH_PASSWORD, 9) \
  1555. X(y, SSH1_CMSG_REQUEST_PTY, 10) \
  1556. X(y, SSH1_CMSG_WINDOW_SIZE, 11) \
  1557. X(y, SSH1_CMSG_EXEC_SHELL, 12) \
  1558. X(y, SSH1_CMSG_EXEC_CMD, 13) \
  1559. X(y, SSH1_SMSG_SUCCESS, 14) \
  1560. X(y, SSH1_SMSG_FAILURE, 15) \
  1561. X(y, SSH1_CMSG_STDIN_DATA, 16) \
  1562. X(y, SSH1_SMSG_STDOUT_DATA, 17) \
  1563. X(y, SSH1_SMSG_STDERR_DATA, 18) \
  1564. X(y, SSH1_CMSG_EOF, 19) \
  1565. X(y, SSH1_SMSG_EXIT_STATUS, 20) \
  1566. X(y, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, 21) \
  1567. X(y, SSH1_MSG_CHANNEL_OPEN_FAILURE, 22) \
  1568. X(y, SSH1_MSG_CHANNEL_DATA, 23) \
  1569. X(y, SSH1_MSG_CHANNEL_CLOSE, 24) \
  1570. X(y, SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION, 25) \
  1571. X(y, SSH1_SMSG_X11_OPEN, 27) \
  1572. X(y, SSH1_CMSG_PORT_FORWARD_REQUEST, 28) \
  1573. X(y, SSH1_MSG_PORT_OPEN, 29) \
  1574. X(y, SSH1_CMSG_AGENT_REQUEST_FORWARDING, 30) \
  1575. X(y, SSH1_SMSG_AGENT_OPEN, 31) \
  1576. X(y, SSH1_MSG_IGNORE, 32) \
  1577. X(y, SSH1_CMSG_EXIT_CONFIRMATION, 33) \
  1578. X(y, SSH1_CMSG_X11_REQUEST_FORWARDING, 34) \
  1579. X(y, SSH1_CMSG_AUTH_RHOSTS_RSA, 35) \
  1580. X(y, SSH1_MSG_DEBUG, 36) \
  1581. X(y, SSH1_CMSG_REQUEST_COMPRESSION, 37) \
  1582. X(y, SSH1_CMSG_AUTH_TIS, 39) \
  1583. X(y, SSH1_SMSG_AUTH_TIS_CHALLENGE, 40) \
  1584. X(y, SSH1_CMSG_AUTH_TIS_RESPONSE, 41) \
  1585. X(y, SSH1_CMSG_AUTH_CCARD, 70) \
  1586. X(y, SSH1_SMSG_AUTH_CCARD_CHALLENGE, 71) \
  1587. X(y, SSH1_CMSG_AUTH_CCARD_RESPONSE, 72) \
  1588. /* end of list */
  1589. #define SSH1_AUTH_RHOSTS 1 /* 0x1 */
  1590. #define SSH1_AUTH_RSA 2 /* 0x2 */
  1591. #define SSH1_AUTH_PASSWORD 3 /* 0x3 */
  1592. #define SSH1_AUTH_RHOSTS_RSA 4 /* 0x4 */
  1593. #define SSH1_AUTH_TIS 5 /* 0x5 */
  1594. #define SSH1_AUTH_CCARD 16 /* 0x10 */
  1595. #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */
  1596. /* Mask for protoflags we will echo back to server if seen */
  1597. #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */
  1598. /*
  1599. * List macro defining SSH-2 message type codes. Some of these depend
  1600. * on particular contexts (i.e. a previously negotiated kex or auth
  1601. * method)
  1602. */
  1603. #define SSH2_MESSAGE_TYPES(X, K, A, y) \
  1604. X(y, SSH2_MSG_DISCONNECT, 1) \
  1605. X(y, SSH2_MSG_IGNORE, 2) \
  1606. X(y, SSH2_MSG_UNIMPLEMENTED, 3) \
  1607. X(y, SSH2_MSG_DEBUG, 4) \
  1608. X(y, SSH2_MSG_SERVICE_REQUEST, 5) \
  1609. X(y, SSH2_MSG_SERVICE_ACCEPT, 6) \
  1610. X(y, SSH2_MSG_EXT_INFO, 7) \
  1611. X(y, SSH2_MSG_KEXINIT, 20) \
  1612. X(y, SSH2_MSG_NEWKEYS, 21) \
  1613. K(y, SSH2_MSG_KEXDH_INIT, 30, SSH2_PKTCTX_DHGROUP) \
  1614. K(y, SSH2_MSG_KEXDH_REPLY, 31, SSH2_PKTCTX_DHGROUP) \
  1615. K(y, SSH2_MSG_KEX_DH_GEX_REQUEST_OLD, 30, SSH2_PKTCTX_DHGEX) \
  1616. K(y, SSH2_MSG_KEX_DH_GEX_REQUEST, 34, SSH2_PKTCTX_DHGEX) \
  1617. K(y, SSH2_MSG_KEX_DH_GEX_GROUP, 31, SSH2_PKTCTX_DHGEX) \
  1618. K(y, SSH2_MSG_KEX_DH_GEX_INIT, 32, SSH2_PKTCTX_DHGEX) \
  1619. K(y, SSH2_MSG_KEX_DH_GEX_REPLY, 33, SSH2_PKTCTX_DHGEX) \
  1620. K(y, SSH2_MSG_KEXGSS_INIT, 30, SSH2_PKTCTX_GSSKEX) \
  1621. K(y, SSH2_MSG_KEXGSS_CONTINUE, 31, SSH2_PKTCTX_GSSKEX) \
  1622. K(y, SSH2_MSG_KEXGSS_COMPLETE, 32, SSH2_PKTCTX_GSSKEX) \
  1623. K(y, SSH2_MSG_KEXGSS_HOSTKEY, 33, SSH2_PKTCTX_GSSKEX) \
  1624. K(y, SSH2_MSG_KEXGSS_ERROR, 34, SSH2_PKTCTX_GSSKEX) \
  1625. K(y, SSH2_MSG_KEXGSS_GROUPREQ, 40, SSH2_PKTCTX_GSSKEX) \
  1626. K(y, SSH2_MSG_KEXGSS_GROUP, 41, SSH2_PKTCTX_GSSKEX) \
  1627. K(y, SSH2_MSG_KEXRSA_PUBKEY, 30, SSH2_PKTCTX_RSAKEX) \
  1628. K(y, SSH2_MSG_KEXRSA_SECRET, 31, SSH2_PKTCTX_RSAKEX) \
  1629. K(y, SSH2_MSG_KEXRSA_DONE, 32, SSH2_PKTCTX_RSAKEX) \
  1630. K(y, SSH2_MSG_KEX_ECDH_INIT, 30, SSH2_PKTCTX_ECDHKEX) \
  1631. K(y, SSH2_MSG_KEX_ECDH_REPLY, 31, SSH2_PKTCTX_ECDHKEX) \
  1632. K(y, SSH2_MSG_KEX_HYBRID_INIT, 30, SSH2_PKTCTX_HYBRIDKEX) \
  1633. K(y, SSH2_MSG_KEX_HYBRID_REPLY, 31, SSH2_PKTCTX_HYBRIDKEX) \
  1634. X(y, SSH2_MSG_USERAUTH_REQUEST, 50) \
  1635. X(y, SSH2_MSG_USERAUTH_FAILURE, 51) \
  1636. X(y, SSH2_MSG_USERAUTH_SUCCESS, 52) \
  1637. X(y, SSH2_MSG_USERAUTH_BANNER, 53) \
  1638. A(y, SSH2_MSG_USERAUTH_PK_OK, 60, SSH2_PKTCTX_PUBLICKEY) \
  1639. A(y, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, 60, SSH2_PKTCTX_PASSWORD) \
  1640. A(y, SSH2_MSG_USERAUTH_INFO_REQUEST, 60, SSH2_PKTCTX_KBDINTER) \
  1641. A(y, SSH2_MSG_USERAUTH_INFO_RESPONSE, 61, SSH2_PKTCTX_KBDINTER) \
  1642. A(y, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, 60, SSH2_PKTCTX_GSSAPI) \
  1643. A(y, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, 61, SSH2_PKTCTX_GSSAPI) \
  1644. A(y, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, 63, SSH2_PKTCTX_GSSAPI) \
  1645. A(y, SSH2_MSG_USERAUTH_GSSAPI_ERROR, 64, SSH2_PKTCTX_GSSAPI) \
  1646. A(y, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, 65, SSH2_PKTCTX_GSSAPI) \
  1647. A(y, SSH2_MSG_USERAUTH_GSSAPI_MIC, 66, SSH2_PKTCTX_GSSAPI) \
  1648. X(y, SSH2_MSG_GLOBAL_REQUEST, 80) \
  1649. X(y, SSH2_MSG_REQUEST_SUCCESS, 81) \
  1650. X(y, SSH2_MSG_REQUEST_FAILURE, 82) \
  1651. X(y, SSH2_MSG_CHANNEL_OPEN, 90) \
  1652. X(y, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, 91) \
  1653. X(y, SSH2_MSG_CHANNEL_OPEN_FAILURE, 92) \
  1654. X(y, SSH2_MSG_CHANNEL_WINDOW_ADJUST, 93) \
  1655. X(y, SSH2_MSG_CHANNEL_DATA, 94) \
  1656. X(y, SSH2_MSG_CHANNEL_EXTENDED_DATA, 95) \
  1657. X(y, SSH2_MSG_CHANNEL_EOF, 96) \
  1658. X(y, SSH2_MSG_CHANNEL_CLOSE, 97) \
  1659. X(y, SSH2_MSG_CHANNEL_REQUEST, 98) \
  1660. X(y, SSH2_MSG_CHANNEL_SUCCESS, 99) \
  1661. X(y, SSH2_MSG_CHANNEL_FAILURE, 100) \
  1662. /* end of list */
  1663. #define DEF_ENUM_UNIVERSAL(y, name, value) name = value,
  1664. #define DEF_ENUM_CONTEXTUAL(y, name, value, context) name = value,
  1665. enum {
  1666. SSH1_MESSAGE_TYPES(DEF_ENUM_UNIVERSAL, y)
  1667. SSH2_MESSAGE_TYPES(DEF_ENUM_UNIVERSAL,
  1668. DEF_ENUM_CONTEXTUAL, DEF_ENUM_CONTEXTUAL, y)
  1669. /* Virtual packet type, for packets too short to even have a type */
  1670. SSH_MSG_NO_TYPE_CODE = 256
  1671. };
  1672. #undef DEF_ENUM_UNIVERSAL
  1673. #undef DEF_ENUM_CONTEXTUAL
  1674. /*
  1675. * SSH-1 agent messages.
  1676. */
  1677. #define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
  1678. #define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
  1679. #define SSH1_AGENTC_RSA_CHALLENGE 3
  1680. #define SSH1_AGENT_RSA_RESPONSE 4
  1681. #define SSH1_AGENTC_ADD_RSA_IDENTITY 7
  1682. #define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
  1683. #define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
  1684. /*
  1685. * Messages common to SSH-1 and OpenSSH's SSH-2.
  1686. */
  1687. #define SSH_AGENT_FAILURE 5
  1688. #define SSH_AGENT_SUCCESS 6
  1689. /*
  1690. * OpenSSH's SSH-2 agent messages.
  1691. */
  1692. #define SSH2_AGENTC_REQUEST_IDENTITIES 11
  1693. #define SSH2_AGENT_IDENTITIES_ANSWER 12
  1694. #define SSH2_AGENTC_SIGN_REQUEST 13
  1695. #define SSH2_AGENT_SIGN_RESPONSE 14
  1696. #define SSH2_AGENTC_ADD_IDENTITY 17
  1697. #define SSH2_AGENTC_REMOVE_IDENTITY 18
  1698. #define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
  1699. #define SSH2_AGENTC_EXTENSION 27
  1700. #define SSH_AGENT_EXTENSION_FAILURE 28
  1701. /*
  1702. * Assorted other SSH-related enumerations.
  1703. */
  1704. #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */
  1705. #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */
  1706. #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */
  1707. #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */
  1708. #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */
  1709. #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */
  1710. #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */
  1711. #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */
  1712. #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */
  1713. #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */
  1714. #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */
  1715. #define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */
  1716. #define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */
  1717. #define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */
  1718. #define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */
  1719. #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */
  1720. #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */
  1721. #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */
  1722. #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */
  1723. #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */
  1724. enum {
  1725. /* TTY modes with opcodes defined consistently in the SSH specs. */
  1726. #define TTYMODE_CHAR(name, val, index) SSH_TTYMODE_##name = val,
  1727. #define TTYMODE_FLAG(name, val, field, mask) SSH_TTYMODE_##name = val,
  1728. #include "ssh/ttymode-list.h"
  1729. #undef TTYMODE_CHAR
  1730. #undef TTYMODE_FLAG
  1731. /* Modes encoded differently between SSH-1 and SSH-2, for which we
  1732. * make up our own dummy opcodes to avoid confusion. */
  1733. TTYMODE_dummy = 255,
  1734. TTYMODE_ISPEED, TTYMODE_OSPEED,
  1735. /* Limiting value that we can use as an array bound below */
  1736. TTYMODE_LIMIT,
  1737. /* The real opcodes for terminal speeds. */
  1738. TTYMODE_ISPEED_SSH1 = 192,
  1739. TTYMODE_OSPEED_SSH1 = 193,
  1740. TTYMODE_ISPEED_SSH2 = 128,
  1741. TTYMODE_OSPEED_SSH2 = 129,
  1742. /* And the opcode that ends a list. */
  1743. TTYMODE_END_OF_LIST = 0
  1744. };
  1745. struct ssh_ttymodes {
  1746. /* A boolean per mode, indicating whether it's set. */
  1747. bool have_mode[TTYMODE_LIMIT];
  1748. /* The actual value for each mode. */
  1749. unsigned mode_val[TTYMODE_LIMIT];
  1750. };
  1751. struct ssh_ttymodes get_ttymodes_from_conf(Seat *seat, Conf *conf);
  1752. struct ssh_ttymodes read_ttymodes_from_packet(
  1753. BinarySource *bs, int ssh_version);
  1754. void write_ttymodes_to_packet(BinarySink *bs, int ssh_version,
  1755. struct ssh_ttymodes modes);
  1756. const char *ssh1_pkt_type(int type);
  1757. const char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type);
  1758. bool ssh2_pkt_type_code_valid(unsigned type);
  1759. /*
  1760. * Need this to warn about support for the original SSH-2 keyfile
  1761. * format.
  1762. */
  1763. void old_keyfile_warning(void);
  1764. /*
  1765. * Flags indicating implementation bugs that we know how to mitigate
  1766. * if we think the other end has them.
  1767. */
  1768. #define SSH_IMPL_BUG_LIST(X) \
  1769. X(BUG_CHOKES_ON_SSH1_IGNORE) \
  1770. X(BUG_SSH2_HMAC) \
  1771. X(BUG_NEEDS_SSH1_PLAIN_PASSWORD) \
  1772. X(BUG_CHOKES_ON_RSA) \
  1773. X(BUG_SSH2_RSA_PADDING) \
  1774. X(BUG_SSH2_DERIVEKEY) \
  1775. X(BUG_SSH2_REKEY) \
  1776. X(BUG_SSH2_PK_SESSIONID) \
  1777. X(BUG_SSH2_MAXPKT) \
  1778. X(BUG_CHOKES_ON_SSH2_IGNORE) \
  1779. X(BUG_CHOKES_ON_WINADJ) \
  1780. X(BUG_SENDS_LATE_REQUEST_REPLY) \
  1781. X(BUG_SSH2_OLDGEX) \
  1782. X(BUG_REQUIRES_FILTERED_KEXINIT) \
  1783. X(BUG_RSA_SHA2_CERT_USERAUTH) \
  1784. /* end of list */
  1785. #define TMP_DECLARE_LOG2_ENUM(thing) log2_##thing,
  1786. enum { SSH_IMPL_BUG_LIST(TMP_DECLARE_LOG2_ENUM) };
  1787. #undef TMP_DECLARE_LOG2_ENUM
  1788. #define TMP_DECLARE_REAL_ENUM(thing) thing = 1 << log2_##thing,
  1789. enum { SSH_IMPL_BUG_LIST(TMP_DECLARE_REAL_ENUM) };
  1790. #undef TMP_DECLARE_REAL_ENUM
  1791. /* Shared system for allocating local SSH channel ids. Expects to be
  1792. * passed a tree full of structs that have a field called 'localid' of
  1793. * type unsigned, and will check that! */
  1794. unsigned alloc_channel_id_general(tree234 *channels, size_t localid_offset);
  1795. #define alloc_channel_id(tree, type) \
  1796. TYPECHECK(&((type *)0)->localid == (unsigned *)0, \
  1797. alloc_channel_id_general(tree, offsetof(type, localid)))
  1798. void add_to_commasep(strbuf *buf, const char *data);
  1799. void add_to_commasep_pl(strbuf *buf, ptrlen data);
  1800. bool get_commasep_word(ptrlen *list, ptrlen *word);
  1801. /* Reasons why something warned by confirm_weak_crypto_primitive might
  1802. * be considered weak */
  1803. typedef enum WeakCryptoReason {
  1804. WCR_BELOW_THRESHOLD, /* user has told us to consider it weak */
  1805. WCR_TERRAPIN, /* known vulnerability CVE-2023-48795 */
  1806. WCR_TERRAPIN_AVOIDABLE, /* same, but demoting ChaCha20 can avoid it */
  1807. } WeakCryptoReason;
  1808. SeatPromptResult verify_ssh_host_key(
  1809. InteractionReadySeat iseat, Conf *conf, const char *host, int port,
  1810. ssh_key *key, const char *keytype, char *keystr, const char *keydisp,
  1811. char **fingerprints, int ca_count,
  1812. void (*callback)(void *ctx, SeatPromptResult result), void *ctx);
  1813. SeatPromptResult confirm_weak_crypto_primitive(
  1814. InteractionReadySeat iseat, const char *algtype, const char *algname,
  1815. void (*callback)(void *ctx, SeatPromptResult result), void *ctx,
  1816. WeakCryptoReason wcr);
  1817. SeatPromptResult confirm_weak_cached_hostkey(
  1818. InteractionReadySeat iseat, const char *algname, const char **betteralgs,
  1819. void (*callback)(void *ctx, SeatPromptResult result), void *ctx);
  1820. typedef struct ssh_transient_hostkey_cache ssh_transient_hostkey_cache;
  1821. ssh_transient_hostkey_cache *ssh_transient_hostkey_cache_new(void);
  1822. void ssh_transient_hostkey_cache_free(ssh_transient_hostkey_cache *thc);
  1823. void ssh_transient_hostkey_cache_add(
  1824. ssh_transient_hostkey_cache *thc, ssh_key *key);
  1825. bool ssh_transient_hostkey_cache_verify(
  1826. ssh_transient_hostkey_cache *thc, ssh_key *key);
  1827. bool ssh_transient_hostkey_cache_has(
  1828. ssh_transient_hostkey_cache *thc, const ssh_keyalg *alg);
  1829. bool ssh_transient_hostkey_cache_non_empty(ssh_transient_hostkey_cache *thc);
  1830. /*
  1831. * Protocol definitions for authentication helper plugins
  1832. */
  1833. #define AUTHPLUGIN_MSG_NAMES(X) \
  1834. X(PLUGIN_INIT, 1) \
  1835. X(PLUGIN_INIT_RESPONSE, 2) \
  1836. X(PLUGIN_PROTOCOL, 3) \
  1837. X(PLUGIN_PROTOCOL_ACCEPT, 4) \
  1838. X(PLUGIN_PROTOCOL_REJECT, 5) \
  1839. X(PLUGIN_AUTH_SUCCESS, 6) \
  1840. X(PLUGIN_AUTH_FAILURE, 7) \
  1841. X(PLUGIN_INIT_FAILURE, 8) \
  1842. X(PLUGIN_KI_SERVER_REQUEST, 20) \
  1843. X(PLUGIN_KI_SERVER_RESPONSE, 21) \
  1844. X(PLUGIN_KI_USER_REQUEST, 22) \
  1845. X(PLUGIN_KI_USER_RESPONSE, 23) \
  1846. /* end of list */
  1847. #define PLUGIN_PROTOCOL_MAX_VERSION 2 /* the highest version we speak */
  1848. enum {
  1849. #define ENUMDECL(name, value) name = value,
  1850. AUTHPLUGIN_MSG_NAMES(ENUMDECL)
  1851. #undef ENUMDECL
  1852. /* Error codes internal to this implementation, indicating failure
  1853. * to receive a meaningful packet at all */
  1854. PLUGIN_NOTYPE = 256, /* packet too short to have a type */
  1855. PLUGIN_EOF = 257 /* EOF from auth plugin */
  1856. };
  1857. /*
  1858. * CPU features for security
  1859. */
  1860. #if HAVE_ARM_DIT
  1861. void enable_dit(void);
  1862. #else
  1863. #define enable_dit() ((void)0)
  1864. #endif