ssh.h 62 KB

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