ssh.h 72 KB

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