ssh.h 64 KB

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