ssh.h 88 KB

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