ssh.h 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "puttymem.h"
  4. #include "tree234.h"
  5. #ifndef WINSCP_VS
  6. #include "network.h"
  7. #endif
  8. #include "int64.h"
  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. };
  55. typedef struct PktIn {
  56. int refcount;
  57. int type;
  58. unsigned long sequence; /* SSH-2 incoming sequence number */
  59. PacketQueueNode qnode; /* for linking this packet on to a queue */
  60. BinarySource_IMPLEMENTATION;
  61. } PktIn;
  62. typedef struct PktOut {
  63. long prefix; /* bytes up to and including type field */
  64. long length; /* total bytes, including prefix */
  65. int type;
  66. long minlen; /* SSH-2: ensure wire length is at least this */
  67. unsigned char *data; /* allocated storage */
  68. long maxlen; /* amount of storage allocated for `data' */
  69. /* Extra metadata used in SSH packet logging mode, allowing us to
  70. * log in the packet header line that the packet came from a
  71. * connection-sharing downstream and what if anything unusual was
  72. * done to it. The additional_log_text field is expected to be a
  73. * static string - it will not be freed. */
  74. unsigned downstream_id;
  75. const char *additional_log_text;
  76. PacketQueueNode qnode; /* for linking this packet on to a queue */
  77. BinarySink_IMPLEMENTATION;
  78. } PktOut;
  79. typedef struct PacketQueueBase {
  80. PacketQueueNode end;
  81. } PacketQueueBase;
  82. typedef struct PktInQueue {
  83. PacketQueueBase pqb;
  84. PktIn *(*get)(PacketQueueBase *, int pop);
  85. } PktInQueue;
  86. typedef struct PktOutQueue {
  87. PacketQueueBase pqb;
  88. PktOut *(*get)(PacketQueueBase *, int pop);
  89. } PktOutQueue;
  90. void pq_base_push(PacketQueueBase *pqb, PacketQueueNode *node);
  91. void pq_base_push_front(PacketQueueBase *pqb, PacketQueueNode *node);
  92. void pq_base_concatenate(PacketQueueBase *dest,
  93. PacketQueueBase *q1, PacketQueueBase *q2);
  94. void pq_in_init(PktInQueue *pq);
  95. void pq_out_init(PktOutQueue *pq);
  96. void pq_in_clear(PktInQueue *pq);
  97. void pq_out_clear(PktOutQueue *pq);
  98. #define pq_push(pq, pkt) \
  99. TYPECHECK((pq)->get(&(pq)->pqb, FALSE) == pkt, \
  100. pq_base_push(&(pq)->pqb, &(pkt)->qnode))
  101. #define pq_push_front(pq, pkt) \
  102. TYPECHECK((pq)->get(&(pq)->pqb, FALSE) == pkt, \
  103. pq_base_push_front(&(pq)->pqb, &(pkt)->qnode))
  104. #define pq_peek(pq) ((pq)->get(&(pq)->pqb, FALSE))
  105. #define pq_pop(pq) ((pq)->get(&(pq)->pqb, TRUE))
  106. #define pq_concatenate(dst, q1, q2) \
  107. TYPECHECK((q1)->get(&(q1)->pqb, FALSE) == \
  108. (dst)->get(&(dst)->pqb, FALSE) && \
  109. (q2)->get(&(q2)->pqb, FALSE) == \
  110. (dst)->get(&(dst)->pqb, FALSE), \
  111. pq_base_concatenate(&(dst)->pqb, &(q1)->pqb, &(q2)->pqb))
  112. /*
  113. * Packet type contexts, so that ssh2_pkt_type can correctly decode
  114. * the ambiguous type numbers back into the correct type strings.
  115. */
  116. typedef enum {
  117. SSH2_PKTCTX_NOKEX,
  118. SSH2_PKTCTX_DHGROUP,
  119. SSH2_PKTCTX_DHGEX,
  120. SSH2_PKTCTX_ECDHKEX,
  121. SSH2_PKTCTX_GSSKEX,
  122. SSH2_PKTCTX_RSAKEX
  123. } Pkt_KCtx;
  124. typedef enum {
  125. SSH2_PKTCTX_NOAUTH,
  126. SSH2_PKTCTX_PUBLICKEY,
  127. SSH2_PKTCTX_PASSWORD,
  128. SSH2_PKTCTX_GSSAPI,
  129. SSH2_PKTCTX_KBDINTER
  130. } Pkt_ACtx;
  131. typedef struct PacketLogSettings {
  132. int omit_passwords, omit_data;
  133. Pkt_KCtx kctx;
  134. Pkt_ACtx actx;
  135. } PacketLogSettings;
  136. #define MAX_BLANKS 4 /* no packet needs more censored sections than this */
  137. int ssh1_censor_packet(
  138. const PacketLogSettings *pls, int type, int sender_is_client,
  139. ptrlen pkt, logblank_t *blanks);
  140. int ssh2_censor_packet(
  141. const PacketLogSettings *pls, int type, int sender_is_client,
  142. ptrlen pkt, logblank_t *blanks);
  143. PktOut *ssh_new_packet(void);
  144. void ssh_unref_packet(PktIn *pkt);
  145. void ssh_free_pktout(PktOut *pkt);
  146. extern Socket ssh_connection_sharing_init(
  147. const char *host, int port, Conf *conf, ConnectionLayer *cl,
  148. Plug sshplug, ssh_sharing_state **state);
  149. int ssh_share_test_for_upstream(const char *host, int port, Conf *conf);
  150. void share_got_pkt_from_server(ssh_sharing_connstate *ctx, int type,
  151. const void *pkt, int pktlen);
  152. void share_activate(ssh_sharing_state *sharestate,
  153. const char *server_verstring);
  154. void sharestate_free(ssh_sharing_state *state);
  155. int share_ndownstreams(ssh_sharing_state *state);
  156. void ssh_connshare_log(Ssh ssh, int event, const char *logtext,
  157. const char *ds_err, const char *us_err);
  158. void share_setup_x11_channel(ssh_sharing_connstate *cs, share_channel *chan,
  159. unsigned upstream_id, unsigned server_id,
  160. unsigned server_currwin, unsigned server_maxpkt,
  161. unsigned client_adjusted_window,
  162. const char *peer_addr, int peer_port, int endian,
  163. int protomajor, int protominor,
  164. const void *initial_data, int initial_len);
  165. struct ssh_rportfwd;
  166. struct ConnectionLayerVtable {
  167. /* Allocate and free remote-to-local port forwardings, called by
  168. * PortFwdManager or by connection sharing */
  169. struct ssh_rportfwd *(*rportfwd_alloc)(
  170. ConnectionLayer *cl,
  171. const char *shost, int sport, const char *dhost, int dport,
  172. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  173. ssh_sharing_connstate *share_ctx);
  174. void (*rportfwd_remove)(ConnectionLayer *cl, struct ssh_rportfwd *rpf);
  175. /* Open a local-to-remote port forwarding channel, called by
  176. * PortFwdManager */
  177. SshChannel *(*lportfwd_open)(
  178. ConnectionLayer *cl, const char *hostname, int port,
  179. const char *org, Channel *chan);
  180. /* Add and remove X11 displays for connection sharing downstreams */
  181. struct X11FakeAuth *(*add_sharing_x11_display)(
  182. ConnectionLayer *cl, int authtype, ssh_sharing_connstate *share_cs,
  183. share_channel *share_chan);
  184. void (*remove_sharing_x11_display)(
  185. ConnectionLayer *cl, struct X11FakeAuth *auth);
  186. /* Pass through an outgoing SSH packet from a downstream */
  187. void (*send_packet_from_downstream)(
  188. ConnectionLayer *cl, unsigned id, int type,
  189. const void *pkt, int pktlen, const char *additional_log_text);
  190. /* Allocate/free an upstream channel number associated with a
  191. * sharing downstream */
  192. unsigned (*alloc_sharing_channel)(ConnectionLayer *cl,
  193. ssh_sharing_connstate *connstate);
  194. void (*delete_sharing_channel)(ConnectionLayer *cl, unsigned localid);
  195. /* Indicate that a downstream has sent a global request with the
  196. * want-reply flag, so that when a reply arrives it will be passed
  197. * back to that downstrean */
  198. void (*sharing_queue_global_request)(
  199. ConnectionLayer *cl, ssh_sharing_connstate *connstate);
  200. /* Query whether the connection layer is doing agent forwarding */
  201. int (*agent_forwarding_permitted)(ConnectionLayer *cl);
  202. };
  203. struct ConnectionLayer {
  204. Frontend *frontend;
  205. const struct ConnectionLayerVtable *vt;
  206. };
  207. #define ssh_rportfwd_alloc(cl, sh, sp, dh, dp, af, ld, pfr, share) \
  208. ((cl)->vt->rportfwd_alloc(cl, sh, sp, dh, dp, af, ld, pfr, share))
  209. #define ssh_rportfwd_remove(cl, rpf) ((cl)->vt->rportfwd_remove(cl, rpf))
  210. #define ssh_lportfwd_open(cl, h, p, org, chan) \
  211. ((cl)->vt->lportfwd_open(cl, h, p, org, chan))
  212. #define ssh_add_sharing_x11_display(cl, auth, cs, ch) \
  213. ((cl)->vt->add_sharing_x11_display(cl, auth, cs, ch))
  214. #define ssh_remove_sharing_x11_display(cl, fa) \
  215. ((cl)->vt->remove_sharing_x11_display(cl, fa))
  216. #define ssh_send_packet_from_downstream(cl, id, type, pkt, len, log) \
  217. ((cl)->vt->send_packet_from_downstream(cl, id, type, pkt, len, log))
  218. #define ssh_alloc_sharing_channel(cl, cs) \
  219. ((cl)->vt->alloc_sharing_channel(cl, cs))
  220. #define ssh_delete_sharing_channel(cl, ch) \
  221. ((cl)->vt->delete_sharing_channel(cl, ch))
  222. #define ssh_sharing_queue_global_request(cl, cs) \
  223. ((cl)->vt->sharing_queue_global_request(cl, cs))
  224. #define ssh_agent_forwarding_permitted(cl) \
  225. ((cl)->vt->agent_forwarding_permitted(cl))
  226. /* Exports from portfwd.c */
  227. PortFwdManager *portfwdmgr_new(ConnectionLayer *cl);
  228. void portfwdmgr_free(PortFwdManager *mgr);
  229. void portfwdmgr_config(PortFwdManager *mgr, Conf *conf);
  230. void portfwdmgr_close(PortFwdManager *mgr, PortFwdRecord *pfr);
  231. void portfwdmgr_close_all(PortFwdManager *mgr);
  232. char *portfwdmgr_connect(PortFwdManager *mgr, Channel **chan_ret,
  233. char *hostname, int port, SshChannel *c,
  234. int addressfamily);
  235. Frontend *ssh_get_frontend(Ssh ssh);
  236. #define SSH_CIPHER_IDEA 1
  237. #define SSH_CIPHER_DES 2
  238. #define SSH_CIPHER_3DES 3
  239. #define SSH_CIPHER_BLOWFISH 6
  240. #ifndef BIGNUM_INTERNAL
  241. typedef void *Bignum;
  242. #endif
  243. typedef struct ssh_keyalg ssh_keyalg;
  244. typedef const struct ssh_keyalg *ssh_key;
  245. struct RSAKey {
  246. int bits;
  247. int bytes;
  248. Bignum modulus;
  249. Bignum exponent;
  250. Bignum private_exponent;
  251. Bignum p;
  252. Bignum q;
  253. Bignum iqmp;
  254. char *comment;
  255. ssh_key sshk;
  256. };
  257. struct dss_key {
  258. Bignum p, q, g, y, x;
  259. ssh_key sshk;
  260. };
  261. struct ec_curve;
  262. struct ec_point {
  263. const struct ec_curve *curve;
  264. Bignum x, y;
  265. Bignum z; /* Jacobian denominator */
  266. unsigned char infinity;
  267. };
  268. void ec_point_free(struct ec_point *point);
  269. /* Weierstrass form curve */
  270. struct ec_wcurve
  271. {
  272. Bignum a, b, n;
  273. struct ec_point G;
  274. };
  275. /* Montgomery form curve */
  276. struct ec_mcurve
  277. {
  278. Bignum a, b;
  279. struct ec_point G;
  280. };
  281. /* Edwards form curve */
  282. struct ec_ecurve
  283. {
  284. Bignum l, d;
  285. struct ec_point B;
  286. };
  287. struct ec_curve {
  288. enum { EC_WEIERSTRASS, EC_MONTGOMERY, EC_EDWARDS } type;
  289. /* 'name' is the identifier of the curve when it has to appear in
  290. * wire protocol encodings, as it does in e.g. the public key and
  291. * signature formats for NIST curves. Curves which do not format
  292. * their keys or signatures in this way just have name==NULL.
  293. *
  294. * 'textname' is non-NULL for all curves, and is a human-readable
  295. * identification suitable for putting in log messages. */
  296. const char *name, *textname;
  297. unsigned int fieldBits;
  298. Bignum p;
  299. union {
  300. struct ec_wcurve w;
  301. struct ec_mcurve m;
  302. struct ec_ecurve e;
  303. };
  304. };
  305. const ssh_keyalg *ec_alg_by_oid(int len, const void *oid,
  306. const struct ec_curve **curve);
  307. const unsigned char *ec_alg_oid(const ssh_keyalg *alg, int *oidlen);
  308. extern const int ec_nist_curve_lengths[], n_ec_nist_curve_lengths;
  309. int ec_nist_alg_and_curve_by_bits(int bits,
  310. const struct ec_curve **curve,
  311. const ssh_keyalg **alg);
  312. int ec_ed_alg_and_curve_by_bits(int bits,
  313. const struct ec_curve **curve,
  314. const ssh_keyalg **alg);
  315. struct ec_key {
  316. struct ec_point publicKey;
  317. Bignum privateKey;
  318. ssh_key sshk;
  319. };
  320. struct ec_point *ec_public(const Bignum privateKey, const struct ec_curve *curve);
  321. /*
  322. * SSH-1 never quite decided which order to store the two components
  323. * of an RSA key. During connection setup, the server sends its host
  324. * and server keys with the exponent first; private key files store
  325. * the modulus first. The agent protocol is even more confusing,
  326. * because the client specifies a key to the server in one order and
  327. * the server lists the keys it knows about in the other order!
  328. */
  329. typedef enum { RSA_SSH1_EXPONENT_FIRST, RSA_SSH1_MODULUS_FIRST } RsaSsh1Order;
  330. void BinarySource_get_rsa_ssh1_pub(
  331. BinarySource *src, struct RSAKey *result, RsaSsh1Order order);
  332. void BinarySource_get_rsa_ssh1_priv(
  333. BinarySource *src, struct RSAKey *rsa);
  334. int rsa_ssh1_encrypt(unsigned char *data, int length, struct RSAKey *key);
  335. Bignum rsa_ssh1_decrypt(Bignum input, struct RSAKey *key);
  336. void rsasanitise(struct RSAKey *key);
  337. int rsastr_len(struct RSAKey *key);
  338. void rsastr_fmt(char *str, struct RSAKey *key);
  339. char *rsa_ssh1_fingerprint(struct RSAKey *key);
  340. int rsa_verify(struct RSAKey *key);
  341. void rsa_ssh1_public_blob(BinarySink *bs, struct RSAKey *key,
  342. RsaSsh1Order order);
  343. int rsa_ssh1_public_blob_len(void *data, int maxlen);
  344. void freersakey(struct RSAKey *key);
  345. #endif // WINSCP_VS
  346. typedef uint32 word32;
  347. #ifndef WINSCP_VS
  348. unsigned long crc32_compute(const void *s, size_t len);
  349. unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len);
  350. /* SSH CRC compensation attack detector */
  351. struct crcda_ctx;
  352. struct crcda_ctx *crcda_make_context(void);
  353. void crcda_free_context(struct crcda_ctx *ctx);
  354. int detect_attack(struct crcda_ctx *ctx, unsigned char *buf, uint32 len,
  355. unsigned char *IV);
  356. /*
  357. * SSH2 RSA key exchange functions
  358. */
  359. struct ssh_hashalg;
  360. struct RSAKey *ssh_rsakex_newkey(const void *data, int len);
  361. void ssh_rsakex_freekey(struct RSAKey *key);
  362. int ssh_rsakex_klen(struct RSAKey *key);
  363. void ssh_rsakex_encrypt(const struct ssh_hashalg *h,
  364. unsigned char *in, int inlen,
  365. unsigned char *out, int outlen, struct RSAKey *key);
  366. /*
  367. * SSH2 ECDH key exchange functions
  368. */
  369. struct ssh_kex;
  370. const char *ssh_ecdhkex_curve_textname(const struct ssh_kex *kex);
  371. struct ec_key *ssh_ecdhkex_newkey(const struct ssh_kex *kex);
  372. void ssh_ecdhkex_freekey(struct ec_key *key);
  373. void ssh_ecdhkex_getpublic(struct ec_key *key, BinarySink *bs);
  374. Bignum ssh_ecdhkex_getkey(struct ec_key *key,
  375. const void *remoteKey, int remoteKeyLen);
  376. /*
  377. * Helper function for k generation in DSA, reused in ECDSA
  378. */
  379. Bignum *dss_gen_k(const char *id_string, Bignum modulus, Bignum private_key,
  380. unsigned char *digest, int digest_len);
  381. struct ssh2_cipheralg;
  382. typedef const struct ssh2_cipheralg *ssh2_cipher;
  383. typedef struct {
  384. uint32 h[4];
  385. } MD5_Core_State;
  386. struct MD5Context {
  387. MD5_Core_State core;
  388. unsigned char block[64];
  389. int blkused;
  390. uint32 lenhi, lenlo;
  391. BinarySink_IMPLEMENTATION;
  392. };
  393. void MD5Init(struct MD5Context *context);
  394. void MD5Final(unsigned char digest[16], struct MD5Context *context);
  395. void MD5Simple(void const *p, unsigned len, unsigned char output[16]);
  396. struct hmacmd5_context;
  397. struct hmacmd5_context *hmacmd5_make_context(void);
  398. void hmacmd5_free_context(struct hmacmd5_context *ctx);
  399. void hmacmd5_key(struct hmacmd5_context *ctx, void const *key, int len);
  400. void hmacmd5_do_hmac(struct hmacmd5_context *ctx,
  401. const void *blk, int len, unsigned char *hmac);
  402. int supports_sha_ni(void);
  403. #ifdef MPEXT
  404. // Resolve ambiguity with OpenSSL
  405. #define SHA_Init putty_SHA_Init
  406. #define SHA_Final putty_SHA_Final
  407. #define SHA256_Init putty_SHA256_Init
  408. #define SHA256_Final putty_SHA256_Final
  409. #define SHA512_Init putty_SHA512_Init
  410. #define SHA512_Final putty_SHA512_Final
  411. #endif
  412. typedef struct SHA_State {
  413. uint32 h[5];
  414. unsigned char block[64];
  415. int blkused;
  416. uint32 lenhi, lenlo;
  417. void (*sha1)(struct SHA_State * s, const unsigned char *p, int len);
  418. BinarySink_IMPLEMENTATION;
  419. } SHA_State;
  420. void SHA_Init(SHA_State * s);
  421. void SHA_Final(SHA_State * s, unsigned char *output);
  422. void SHA_Simple(const void *p, int len, unsigned char *output);
  423. void hmac_sha1_simple(const void *key, int keylen,
  424. const void *data, int datalen,
  425. unsigned char *output);
  426. #endif // WINSCP_VS
  427. typedef struct SHA256_State {
  428. uint32 h[8];
  429. unsigned char block[64];
  430. int blkused;
  431. uint32 lenhi, lenlo;
  432. void (*sha256)(struct SHA256_State * s, const unsigned char *p, int len);
  433. BinarySink_IMPLEMENTATION;
  434. } SHA256_State;
  435. #ifndef WINSCP_VS
  436. void SHA256_Init(SHA256_State * s);
  437. void SHA256_Final(SHA256_State * s, unsigned char *output);
  438. void SHA256_Simple(const void *p, int len, unsigned char *output);
  439. typedef struct {
  440. uint64 h[8];
  441. unsigned char block[128];
  442. int blkused;
  443. uint32 len[4];
  444. BinarySink_IMPLEMENTATION;
  445. } SHA512_State;
  446. #define SHA384_State SHA512_State
  447. void SHA512_Init(SHA512_State * s);
  448. void SHA512_Final(SHA512_State * s, unsigned char *output);
  449. void SHA512_Simple(const void *p, int len, unsigned char *output);
  450. void SHA384_Init(SHA384_State * s);
  451. void SHA384_Final(SHA384_State * s, unsigned char *output);
  452. void SHA384_Simple(const void *p, int len, unsigned char *output);
  453. struct ssh2_macalg;
  454. struct ssh1_cipheralg;
  455. typedef const struct ssh1_cipheralg *ssh1_cipher;
  456. struct ssh1_cipheralg {
  457. ssh1_cipher *(*new)(void);
  458. void (*free)(ssh1_cipher *);
  459. void (*sesskey)(ssh1_cipher *, const void *key);
  460. void (*encrypt)(ssh1_cipher *, void *blk, int len);
  461. void (*decrypt)(ssh1_cipher *, void *blk, int len);
  462. int blksize;
  463. const char *text_name;
  464. };
  465. #define ssh1_cipher_new(alg) ((alg)->new())
  466. #define ssh1_cipher_free(ctx) ((*(ctx))->free(ctx))
  467. #define ssh1_cipher_sesskey(ctx, key) ((*(ctx))->sesskey(ctx, key))
  468. #define ssh1_cipher_encrypt(ctx, blk, len) ((*(ctx))->encrypt(ctx, blk, len))
  469. #define ssh1_cipher_decrypt(ctx, blk, len) ((*(ctx))->decrypt(ctx, blk, len))
  470. struct ssh2_cipheralg {
  471. ssh2_cipher *(*new)(const struct ssh2_cipheralg *alg);
  472. void (*free)(ssh2_cipher *);
  473. void (*setiv)(ssh2_cipher *, const void *iv);
  474. void (*setkey)(ssh2_cipher *, const void *key);
  475. void (*encrypt)(ssh2_cipher *, void *blk, int len);
  476. void (*decrypt)(ssh2_cipher *, void *blk, int len);
  477. /* Ignored unless SSH_CIPHER_SEPARATE_LENGTH flag set */
  478. void (*encrypt_length)(ssh2_cipher *, void *blk, int len,
  479. unsigned long seq);
  480. void (*decrypt_length)(ssh2_cipher *, void *blk, int len,
  481. unsigned long seq);
  482. const char *name;
  483. int blksize;
  484. /* real_keybits is the number of bits of entropy genuinely used by
  485. * the cipher scheme; it's used for deciding how big a
  486. * Diffie-Hellman group is needed to exchange a key for the
  487. * cipher. */
  488. int real_keybits;
  489. /* padded_keybytes is the number of bytes of key data expected as
  490. * input to the setkey function; it's used for deciding how much
  491. * data needs to be generated from the post-kex generation of key
  492. * material. In a sensible cipher which uses all its key bytes for
  493. * real work, this will just be real_keybits/8, but in DES-type
  494. * ciphers which ignore one bit in each byte, it'll be slightly
  495. * different. */
  496. int padded_keybytes;
  497. unsigned int flags;
  498. #define SSH_CIPHER_IS_CBC 1
  499. #define SSH_CIPHER_SEPARATE_LENGTH 2
  500. const char *text_name;
  501. /* If set, this takes priority over other MAC. */
  502. const struct ssh2_macalg *required_mac;
  503. };
  504. #define ssh2_cipher_new(alg) ((alg)->new(alg))
  505. #define ssh2_cipher_free(ctx) ((*(ctx))->free(ctx))
  506. #define ssh2_cipher_setiv(ctx, iv) ((*(ctx))->setiv(ctx, iv))
  507. #define ssh2_cipher_setkey(ctx, key) ((*(ctx))->setkey(ctx, key))
  508. #define ssh2_cipher_encrypt(ctx, blk, len) ((*(ctx))->encrypt(ctx, blk, len))
  509. #define ssh2_cipher_decrypt(ctx, blk, len) ((*(ctx))->decrypt(ctx, blk, len))
  510. #define ssh2_cipher_encrypt_length(ctx, blk, len, seq) \
  511. ((*(ctx))->encrypt_length(ctx, blk, len, seq))
  512. #define ssh2_cipher_decrypt_length(ctx, blk, len, seq) \
  513. ((*(ctx))->decrypt_length(ctx, blk, len, seq))
  514. #define ssh2_cipher_alg(ctx) (*(ctx))
  515. struct ssh2_ciphers {
  516. int nciphers;
  517. const struct ssh2_cipheralg *const *list;
  518. };
  519. struct ssh2_macalg;
  520. typedef struct ssh2_mac {
  521. const struct ssh2_macalg *vt;
  522. BinarySink_DELEGATE_IMPLEMENTATION;
  523. } ssh2_mac;
  524. struct ssh2_macalg {
  525. /* Passes in the cipher context */
  526. ssh2_mac *(*new)(const struct ssh2_macalg *alg, ssh2_cipher *cipher);
  527. void (*free)(ssh2_mac *);
  528. void (*setkey)(ssh2_mac *, const void *key);
  529. void (*start)(ssh2_mac *);
  530. void (*genresult)(ssh2_mac *, unsigned char *);
  531. const char *name, *etm_name;
  532. int len, keylen;
  533. const char *text_name;
  534. };
  535. #define ssh2_mac_new(alg, cipher) ((alg)->new(alg, cipher))
  536. #define ssh2_mac_free(ctx) ((ctx)->vt->free(ctx))
  537. #define ssh2_mac_setkey(ctx, key) ((ctx)->vt->free(ctx, key))
  538. #define ssh2_mac_start(ctx) ((ctx)->vt->start(ctx))
  539. #define ssh2_mac_genresult(ctx, out) ((ctx)->vt->genresult(ctx, out))
  540. #define ssh2_mac_alg(ctx) ((ctx)->vt)
  541. /* Centralised 'methods' for ssh2_mac, defined in sshmac.c */
  542. int ssh2_mac_verresult(ssh2_mac *, const void *);
  543. void ssh2_mac_generate(ssh2_mac *, void *, int, unsigned long seq);
  544. int ssh2_mac_verify(ssh2_mac *, const void *, int, unsigned long seq);
  545. typedef struct ssh_hash {
  546. const struct ssh_hashalg *vt;
  547. BinarySink_DELEGATE_IMPLEMENTATION;
  548. } ssh_hash;
  549. struct ssh_hashalg {
  550. ssh_hash *(*new)(const struct ssh_hashalg *alg);
  551. ssh_hash *(*copy)(ssh_hash *);
  552. void (*final)(ssh_hash *, unsigned char *); /* ALSO FREES THE ssh_hash! */
  553. void (*free)(ssh_hash *);
  554. int hlen; /* output length in bytes */
  555. const char *text_name;
  556. };
  557. #define ssh_hash_new(alg) ((alg)->new(alg))
  558. #define ssh_hash_copy(ctx) ((ctx)->vt->copy(ctx))
  559. #define ssh_hash_final(ctx, out) ((ctx)->vt->final(ctx, out))
  560. #define ssh_hash_free(ctx) ((ctx)->vt->free(ctx))
  561. #define ssh_hash_alg(ctx) ((ctx)->vt)
  562. struct ssh_kex {
  563. const char *name, *groupname;
  564. enum { KEXTYPE_DH, KEXTYPE_RSA, KEXTYPE_ECDH, KEXTYPE_GSS } main_type;
  565. const struct ssh_hashalg *hash;
  566. const void *extra; /* private to the kex methods */
  567. };
  568. struct ssh_kexes {
  569. int nkexes;
  570. const struct ssh_kex *const *list;
  571. };
  572. struct ssh_keyalg {
  573. /* Constructors that create an ssh_key */
  574. ssh_key *(*new_pub) (const ssh_keyalg *self, ptrlen pub);
  575. ssh_key *(*new_priv) (const ssh_keyalg *self, ptrlen pub, ptrlen priv);
  576. ssh_key *(*new_priv_openssh) (const ssh_keyalg *self, BinarySource *);
  577. /* Methods that operate on an existing ssh_key */
  578. void (*freekey) (ssh_key *key);
  579. void (*sign) (ssh_key *key, const void *data, int datalen, BinarySink *);
  580. int (*verify) (ssh_key *key, ptrlen sig, ptrlen data);
  581. void (*public_blob)(ssh_key *key, BinarySink *);
  582. void (*private_blob)(ssh_key *key, BinarySink *);
  583. void (*openssh_blob) (ssh_key *key, BinarySink *);
  584. char *(*cache_str) (ssh_key *key);
  585. /* 'Class methods' that don't deal with an ssh_key at all */
  586. int (*pubkey_bits) (const ssh_keyalg *self, ptrlen blob);
  587. /* Constant data fields giving information about the key type */
  588. const char *ssh_id; /* string identifier in the SSH protocol */
  589. const char *cache_id; /* identifier used in PuTTY's host key cache */
  590. const void *extra; /* private to the public key methods */
  591. };
  592. #define ssh_key_new_pub(alg, data) ((alg)->new_pub(alg, data))
  593. #define ssh_key_new_priv(alg, pub, priv) ((alg)->new_priv(alg, pub, priv))
  594. #define ssh_key_new_priv_openssh(alg, bs) ((alg)->new_priv_openssh(alg, bs))
  595. #define ssh_key_free(key) ((*(key))->freekey(key))
  596. #define ssh_key_sign(key, data, len, bs) ((*(key))->sign(key, data, len, bs))
  597. #define ssh_key_verify(key, sig, data) ((*(key))->verify(key, sig, data))
  598. #define ssh_key_public_blob(key, bs) ((*(key))->public_blob(key, bs))
  599. #define ssh_key_private_blob(key, bs) ((*(key))->private_blob(key, bs))
  600. #define ssh_key_openssh_blob(key, bs) ((*(key))->openssh_blob(key, bs))
  601. #define ssh_key_cache_str(key) ((*(key))->cache_str(key))
  602. #define ssh_key_public_bits(alg, blob) ((alg)->pubkey_bits(alg, blob))
  603. #define ssh_key_alg(key) (*(key))
  604. #define ssh_key_ssh_id(key) ((*(key))->ssh_id)
  605. #define ssh_key_cache_id(key) ((*(key))->cache_id)
  606. typedef struct ssh_compressor {
  607. const struct ssh_compression_alg *vt;
  608. } ssh_compressor;
  609. typedef struct ssh_decompressor {
  610. const struct ssh_compression_alg *vt;
  611. } ssh_decompressor;
  612. struct ssh_compression_alg {
  613. const char *name;
  614. /* For [email protected]: if non-NULL, this name will be considered once
  615. * userauth has completed successfully. */
  616. const char *delayed_name;
  617. ssh_compressor *(*compress_new)(void);
  618. void (*compress_free)(ssh_compressor *);
  619. void (*compress)(ssh_compressor *, unsigned char *block, int len,
  620. unsigned char **outblock, int *outlen,
  621. int minlen);
  622. ssh_decompressor *(*decompress_new)(void);
  623. void (*decompress_free)(ssh_decompressor *);
  624. int (*decompress)(ssh_decompressor *, unsigned char *block, int len,
  625. unsigned char **outblock, int *outlen);
  626. const char *text_name;
  627. };
  628. #define ssh_compressor_new(alg) ((alg)->compress_new())
  629. #define ssh_compressor_free(comp) ((comp)->vt->compress_free(comp))
  630. #define ssh_compressor_compress(comp, in, inlen, out, outlen, minlen) \
  631. ((comp)->vt->compress(comp, in, inlen, out, outlen, minlen))
  632. #define ssh_decompressor_new(alg) ((alg)->decompress_new())
  633. #define ssh_decompressor_free(comp) ((comp)->vt->decompress_free(comp))
  634. #define ssh_decompressor_decompress(comp, in, inlen, out, outlen) \
  635. ((comp)->vt->decompress(comp, in, inlen, out, outlen))
  636. struct ssh2_userkey {
  637. ssh_key *key; /* the key itself */
  638. char *comment; /* the key comment */
  639. };
  640. /* The maximum length of any hash algorithm used in kex. (bytes) */
  641. #define SSH2_KEX_MAX_HASH_LEN (64) /* SHA-512 */
  642. extern const struct ssh1_cipheralg ssh1_3des;
  643. extern const struct ssh1_cipheralg ssh1_des;
  644. extern const struct ssh1_cipheralg ssh1_blowfish;
  645. extern const struct ssh2_ciphers ssh2_3des;
  646. extern const struct ssh2_ciphers ssh2_des;
  647. extern const struct ssh2_ciphers ssh2_aes;
  648. extern const struct ssh2_ciphers ssh2_blowfish;
  649. extern const struct ssh2_ciphers ssh2_arcfour;
  650. extern const struct ssh2_ciphers ssh2_ccp;
  651. extern const struct ssh_hashalg ssh_sha1;
  652. extern const struct ssh_hashalg ssh_sha256;
  653. extern const struct ssh_hashalg ssh_sha384;
  654. extern const struct ssh_hashalg ssh_sha512;
  655. extern const struct ssh_kexes ssh_diffiehellman_group1;
  656. extern const struct ssh_kexes ssh_diffiehellman_group14;
  657. extern const struct ssh_kexes ssh_diffiehellman_gex;
  658. extern const struct ssh_kexes ssh_gssk5_sha1_kex;
  659. extern const struct ssh_kexes ssh_rsa_kex;
  660. extern const struct ssh_kexes ssh_ecdh_kex;
  661. extern const ssh_keyalg ssh_dss;
  662. extern const ssh_keyalg ssh_rsa;
  663. extern const ssh_keyalg ssh_ecdsa_ed25519;
  664. extern const ssh_keyalg ssh_ecdsa_nistp256;
  665. extern const ssh_keyalg ssh_ecdsa_nistp384;
  666. extern const ssh_keyalg ssh_ecdsa_nistp521;
  667. extern const struct ssh2_macalg ssh_hmac_md5;
  668. extern const struct ssh2_macalg ssh_hmac_sha1;
  669. extern const struct ssh2_macalg ssh_hmac_sha1_buggy;
  670. extern const struct ssh2_macalg ssh_hmac_sha1_96;
  671. extern const struct ssh2_macalg ssh_hmac_sha1_96_buggy;
  672. extern const struct ssh2_macalg ssh_hmac_sha256;
  673. extern const struct ssh_compression_alg ssh_zlib;
  674. typedef struct AESContext AESContext;
  675. AESContext *aes_make_context(void);
  676. void aes_free_context(AESContext *ctx);
  677. void aes128_key(AESContext *ctx, const void *key);
  678. void aes192_key(AESContext *ctx, const void *key);
  679. void aes256_key(AESContext *ctx, const void *key);
  680. void aes_iv(AESContext *ctx, const void *iv);
  681. void aes_ssh2_encrypt_blk(AESContext *ctx, void *blk, int len);
  682. void aes_ssh2_decrypt_blk(AESContext *ctx, void *blk, int len);
  683. void aes_ssh2_sdctr(AESContext *ctx, void *blk, int len);
  684. /*
  685. * PuTTY version number formatted as an SSH version string.
  686. */
  687. extern
  688. #ifndef MPEXT
  689. const
  690. #endif
  691. char sshver[];
  692. /*
  693. * Gross hack: pscp will try to start SFTP but fall back to scp1 if
  694. * that fails. This variable is the means by which scp.c can reach
  695. * into the SSH code and find out which one it got.
  696. */
  697. extern int ssh_fallback_cmd(Backend *backend);
  698. void SHATransform(word32 * digest, word32 * data);
  699. /*
  700. * Check of compiler version
  701. */
  702. #ifdef _FORCE_SHA_NI
  703. # define COMPILER_SUPPORTS_SHA_NI
  704. #elif defined(__clang__)
  705. # if __has_attribute(target) && __has_include(<shaintrin.h>) && (defined(__x86_64__) || defined(__i386))
  706. # define COMPILER_SUPPORTS_SHA_NI
  707. # endif
  708. #elif defined(__GNUC__)
  709. # if ((__GNUC__ >= 5) && (defined(__x86_64__) || defined(__i386)))
  710. # define COMPILER_SUPPORTS_SHA_NI
  711. # endif
  712. #elif defined (_MSC_VER)
  713. # if (defined(_M_X64) || defined(_M_IX86)) && _MSC_VER >= 1900
  714. # define COMPILER_SUPPORTS_SHA_NI
  715. # endif
  716. #endif
  717. #ifdef _FORCE_SOFTWARE_SHA
  718. # undef COMPILER_SUPPORTS_SHA_NI
  719. #endif
  720. int random_byte(void);
  721. void random_add_noise(void *noise, int length);
  722. void random_add_heavynoise(void *noise, int length);
  723. /* Exports from x11fwd.c */
  724. enum {
  725. X11_TRANS_IPV4 = 0, X11_TRANS_IPV6 = 6, X11_TRANS_UNIX = 256
  726. };
  727. struct X11Display {
  728. /* Broken-down components of the display name itself */
  729. int unixdomain;
  730. char *hostname;
  731. int displaynum;
  732. int screennum;
  733. /* OSX sometimes replaces all the above with a full Unix-socket pathname */
  734. char *unixsocketpath;
  735. /* PuTTY networking SockAddr to connect to the display, and associated
  736. * gubbins */
  737. SockAddr addr;
  738. int port;
  739. char *realhost;
  740. /* Our local auth details for talking to the real X display. */
  741. int localauthproto;
  742. unsigned char *localauthdata;
  743. int localauthdatalen;
  744. };
  745. struct X11FakeAuth {
  746. /* Auth details we invented for a virtual display on the SSH server. */
  747. int proto;
  748. unsigned char *data;
  749. int datalen;
  750. char *protoname;
  751. char *datastring;
  752. /* The encrypted form of the first block, in XDM-AUTHORIZATION-1.
  753. * Used as part of the key when these structures are organised
  754. * into a tree. See x11_invent_fake_auth for explanation. */
  755. unsigned char *xa1_firstblock;
  756. /*
  757. * Used inside x11fwd.c to remember recently seen
  758. * XDM-AUTHORIZATION-1 strings, to avoid replay attacks.
  759. */
  760. tree234 *xdmseen;
  761. /*
  762. * What to do with an X connection matching this auth data.
  763. */
  764. struct X11Display *disp;
  765. ssh_sharing_connstate *share_cs;
  766. share_channel *share_chan;
  767. };
  768. void *x11_make_greeting(int endian, int protomajor, int protominor,
  769. int auth_proto, const void *auth_data, int auth_len,
  770. const char *peer_ip, int peer_port,
  771. int *outlen);
  772. int x11_authcmp(void *av, void *bv); /* for putting X11FakeAuth in a tree234 */
  773. /*
  774. * x11_setup_display() parses the display variable and fills in an
  775. * X11Display structure. Some remote auth details are invented;
  776. * the supplied authtype parameter configures the preferred
  777. * authorisation protocol to use at the remote end. The local auth
  778. * details are looked up by calling platform_get_x11_auth.
  779. */
  780. extern struct X11Display *x11_setup_display(const char *display, Conf *);
  781. void x11_free_display(struct X11Display *disp);
  782. struct X11FakeAuth *x11_invent_fake_auth(tree234 *t, int authtype);
  783. void x11_free_fake_auth(struct X11FakeAuth *auth);
  784. Channel *x11_new_channel(tree234 *authtree, SshChannel *c,
  785. const char *peeraddr, int peerport,
  786. int connection_sharing_possible);
  787. char *x11_display(const char *display);
  788. /* Platform-dependent X11 functions */
  789. extern void platform_get_x11_auth(struct X11Display *display, Conf *);
  790. /* examine a mostly-filled-in X11Display and fill in localauth* */
  791. extern const int platform_uses_x11_unix_by_default;
  792. /* choose default X transport in the absence of a specified one */
  793. SockAddr platform_get_x11_unix_address(const char *path, int displaynum);
  794. /* make up a SockAddr naming the address for displaynum */
  795. char *platform_get_x_display(void);
  796. /* allocated local X display string, if any */
  797. /* Callbacks in x11.c usable _by_ platform X11 functions */
  798. /*
  799. * This function does the job of platform_get_x11_auth, provided
  800. * it is told where to find a normally formatted .Xauthority file:
  801. * it opens that file, parses it to find an auth record which
  802. * matches the display details in "display", and fills in the
  803. * localauth fields.
  804. *
  805. * It is expected that most implementations of
  806. * platform_get_x11_auth() will work by finding their system's
  807. * .Xauthority file, adjusting the display details if necessary
  808. * for local oddities like Unix-domain socket transport, and
  809. * calling this function to do the rest of the work.
  810. */
  811. void x11_get_auth_from_authfile(struct X11Display *display,
  812. const char *authfilename);
  813. int x11_identify_auth_proto(ptrlen protoname);
  814. void *x11_dehexify(ptrlen hex, int *outlen);
  815. Channel *agentf_new(SshChannel *c);
  816. Bignum copybn(Bignum b);
  817. Bignum bn_power_2(int n);
  818. void bn_restore_invariant(Bignum b);
  819. Bignum bignum_from_long(unsigned long n);
  820. void freebn(Bignum b);
  821. Bignum modpow(Bignum base, Bignum exp, Bignum mod);
  822. Bignum modmul(Bignum a, Bignum b, Bignum mod);
  823. Bignum modsub(const Bignum a, const Bignum b, const Bignum n);
  824. void decbn(Bignum n);
  825. extern Bignum Zero, One;
  826. Bignum bignum_from_bytes(const void *data, int nbytes);
  827. Bignum bignum_from_bytes_le(const void *data, int nbytes);
  828. Bignum bignum_random_in_range(const Bignum lower, const Bignum upper);
  829. int bignum_bitcount(Bignum bn);
  830. int bignum_byte(Bignum bn, int i);
  831. int bignum_bit(Bignum bn, int i);
  832. void bignum_set_bit(Bignum bn, int i, int value);
  833. Bignum biggcd(Bignum a, Bignum b);
  834. unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
  835. Bignum bignum_add_long(Bignum number, unsigned long addend);
  836. Bignum bigadd(Bignum a, Bignum b);
  837. Bignum bigsub(Bignum a, Bignum b);
  838. Bignum bigmul(Bignum a, Bignum b);
  839. Bignum bigmuladd(Bignum a, Bignum b, Bignum addend);
  840. Bignum bigdiv(Bignum a, Bignum b);
  841. Bignum bigmod(Bignum a, Bignum b);
  842. Bignum modinv(Bignum number, Bignum modulus);
  843. Bignum bignum_bitmask(Bignum number);
  844. Bignum bignum_rshift(Bignum number, int shift);
  845. Bignum bignum_lshift(Bignum number, int shift);
  846. int bignum_cmp(Bignum a, Bignum b);
  847. char *bignum_decimal(Bignum x);
  848. Bignum bignum_from_decimal(const char *decimal);
  849. void BinarySink_put_mp_ssh1(BinarySink *, Bignum);
  850. void BinarySink_put_mp_ssh2(BinarySink *, Bignum);
  851. Bignum BinarySource_get_mp_ssh1(BinarySource *);
  852. Bignum BinarySource_get_mp_ssh2(BinarySource *);
  853. #ifdef DEBUG
  854. void diagbn(char *prefix, Bignum md);
  855. #endif
  856. int dh_is_gex(const struct ssh_kex *kex);
  857. struct dh_ctx;
  858. struct dh_ctx *dh_setup_group(const struct ssh_kex *kex);
  859. struct dh_ctx *dh_setup_gex(Bignum pval, Bignum gval);
  860. void dh_cleanup(struct dh_ctx *);
  861. Bignum dh_create_e(struct dh_ctx *, int nbits);
  862. const char *dh_validate_f(struct dh_ctx *, Bignum f);
  863. Bignum dh_find_K(struct dh_ctx *, Bignum f);
  864. int rsa_ssh1_encrypted(const Filename *filename, char **comment);
  865. int rsa_ssh1_loadpub(const Filename *filename, BinarySink *bs,
  866. char **commentptr, const char **errorstr);
  867. int rsa_ssh1_loadkey(const Filename *filename, struct RSAKey *key,
  868. const char *passphrase, const char **errorstr);
  869. int rsa_ssh1_savekey(const Filename *filename, struct RSAKey *key,
  870. char *passphrase);
  871. extern int base64_decode_atom(const char *atom, unsigned char *out);
  872. extern int base64_lines(int datalen);
  873. extern void base64_encode_atom(const unsigned char *data, int n, char *out);
  874. extern void base64_encode(FILE *fp, const unsigned char *data, int datalen,
  875. int cpl);
  876. /* ssh2_load_userkey can return this as an error */
  877. extern struct ssh2_userkey ssh2_wrong_passphrase;
  878. #define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
  879. int ssh2_userkey_encrypted(const Filename *filename, char **comment);
  880. struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
  881. const char *passphrase,
  882. const char **errorstr);
  883. int ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
  884. BinarySink *bs,
  885. char **commentptr, const char **errorstr);
  886. int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
  887. char *passphrase);
  888. const ssh_keyalg *find_pubkey_alg(const char *name);
  889. const ssh_keyalg *find_pubkey_alg_len(ptrlen name);
  890. enum {
  891. SSH_KEYTYPE_UNOPENABLE,
  892. SSH_KEYTYPE_UNKNOWN,
  893. SSH_KEYTYPE_SSH1, SSH_KEYTYPE_SSH2,
  894. /*
  895. * The OpenSSH key types deserve a little explanation. OpenSSH has
  896. * two physical formats for private key storage: an old PEM-based
  897. * one largely dictated by their use of OpenSSL and full of ASN.1,
  898. * and a new one using the same private key formats used over the
  899. * wire for talking to ssh-agent. The old format can only support
  900. * a subset of the key types, because it needs redesign for each
  901. * key type, and after a while they decided to move to the new
  902. * format so as not to have to do that.
  903. *
  904. * On input, key files are identified as either
  905. * SSH_KEYTYPE_OPENSSH_PEM or SSH_KEYTYPE_OPENSSH_NEW, describing
  906. * accurately which actual format the keys are stored in.
  907. *
  908. * On output, however, we default to following OpenSSH's own
  909. * policy of writing out PEM-style keys for maximum backwards
  910. * compatibility if the key type supports it, and otherwise
  911. * switching to the new format. So the formats you can select for
  912. * output are SSH_KEYTYPE_OPENSSH_NEW (forcing the new format for
  913. * any key type), and SSH_KEYTYPE_OPENSSH_AUTO to use the oldest
  914. * format supported by whatever key type you're writing out.
  915. *
  916. * So we have three type codes, but only two of them usable in any
  917. * given circumstance. An input key file will never be identified
  918. * as AUTO, only PEM or NEW; key export UIs should not be able to
  919. * select PEM, only AUTO or NEW.
  920. */
  921. SSH_KEYTYPE_OPENSSH_AUTO,
  922. SSH_KEYTYPE_OPENSSH_PEM,
  923. SSH_KEYTYPE_OPENSSH_NEW,
  924. SSH_KEYTYPE_SSHCOM,
  925. /*
  926. * Public-key-only formats, which we still want to be able to read
  927. * for various purposes.
  928. */
  929. SSH_KEYTYPE_SSH1_PUBLIC,
  930. SSH_KEYTYPE_SSH2_PUBLIC_RFC4716,
  931. SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH
  932. };
  933. char *ssh1_pubkey_str(struct RSAKey *ssh1key);
  934. void ssh1_write_pubkey(FILE *fp, struct RSAKey *ssh1key);
  935. char *ssh2_pubkey_openssh_str(struct ssh2_userkey *key);
  936. void ssh2_write_pubkey(FILE *fp, const char *comment,
  937. const void *v_pub_blob, int pub_len,
  938. int keytype);
  939. char *ssh2_fingerprint_blob(const void *blob, int bloblen);
  940. char *ssh2_fingerprint(ssh_key *key);
  941. int key_type(const Filename *filename);
  942. const char *key_type_to_str(int type);
  943. #ifdef MPEXT
  944. int openssh_loadpub_line(char * line, char **algorithm,
  945. BinarySink *bs,
  946. char **commentptr, const char **errorstr);
  947. #endif
  948. int import_possible(int type);
  949. int import_target_type(int type);
  950. int import_encrypted(const Filename *filename, int type, char **comment);
  951. int import_ssh1(const Filename *filename, int type,
  952. struct RSAKey *key, char *passphrase, const char **errmsg_p);
  953. struct ssh2_userkey *import_ssh2(const Filename *filename, int type,
  954. char *passphrase, const char **errmsg_p);
  955. int export_ssh1(const Filename *filename, int type,
  956. struct RSAKey *key, char *passphrase);
  957. int export_ssh2(const Filename *filename, int type,
  958. struct ssh2_userkey *key, char *passphrase);
  959. void des3_decrypt_pubkey(const void *key, void *blk, int len);
  960. void des3_encrypt_pubkey(const void *key, void *blk, int len);
  961. void des3_decrypt_pubkey_ossh(const void *key, const void *iv,
  962. void *blk, int len);
  963. void des3_encrypt_pubkey_ossh(const void *key, const void *iv,
  964. void *blk, int len);
  965. void aes256_encrypt_pubkey(const void *key, void *blk, int len);
  966. void aes256_decrypt_pubkey(const void *key, void *blk, int len);
  967. void des_encrypt_xdmauth(const void *key, void *blk, int len);
  968. void des_decrypt_xdmauth(const void *key, void *blk, int len);
  969. void openssh_bcrypt(const char *passphrase,
  970. const unsigned char *salt, int saltbytes,
  971. int rounds, unsigned char *out, int outbytes);
  972. /*
  973. * For progress updates in the key generation utility.
  974. */
  975. #define PROGFN_INITIALISE 1
  976. #define PROGFN_LIN_PHASE 2
  977. #define PROGFN_EXP_PHASE 3
  978. #define PROGFN_PHASE_EXTENT 4
  979. #define PROGFN_READY 5
  980. #define PROGFN_PROGRESS 6
  981. typedef void (*progfn_t) (void *param, int action, int phase, int progress);
  982. int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
  983. void *pfnparam);
  984. int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
  985. void *pfnparam);
  986. int ec_generate(struct ec_key *key, int bits, progfn_t pfn,
  987. void *pfnparam);
  988. int ec_edgenerate(struct ec_key *key, int bits, progfn_t pfn,
  989. void *pfnparam);
  990. Bignum primegen(int bits, int modulus, int residue, Bignum factor,
  991. int phase, progfn_t pfn, void *pfnparam, unsigned firstbits);
  992. void invent_firstbits(unsigned *one, unsigned *two);
  993. /*
  994. * Connection-sharing API provided by platforms. This function must
  995. * either:
  996. * - return SHARE_NONE and do nothing
  997. * - return SHARE_DOWNSTREAM and set *sock to a Socket connected to
  998. * downplug
  999. * - return SHARE_UPSTREAM and set *sock to a Socket connected to
  1000. * upplug.
  1001. */
  1002. enum { SHARE_NONE, SHARE_DOWNSTREAM, SHARE_UPSTREAM };
  1003. int platform_ssh_share(const char *name, Conf *conf,
  1004. Plug downplug, Plug upplug, Socket *sock,
  1005. char **logtext, char **ds_err, char **us_err,
  1006. int can_upstream, int can_downstream);
  1007. void platform_ssh_share_cleanup(const char *name);
  1008. /*
  1009. * SSH-1 message type codes.
  1010. */
  1011. #define SSH1_MSG_DISCONNECT 1 /* 0x1 */
  1012. #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */
  1013. #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */
  1014. #define SSH1_CMSG_USER 4 /* 0x4 */
  1015. #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */
  1016. #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */
  1017. #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */
  1018. #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */
  1019. #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */
  1020. #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */
  1021. #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */
  1022. #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */
  1023. #define SSH1_SMSG_SUCCESS 14 /* 0xe */
  1024. #define SSH1_SMSG_FAILURE 15 /* 0xf */
  1025. #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */
  1026. #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */
  1027. #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */
  1028. #define SSH1_CMSG_EOF 19 /* 0x13 */
  1029. #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */
  1030. #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */
  1031. #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */
  1032. #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */
  1033. #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */
  1034. #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */
  1035. #define SSH1_SMSG_X11_OPEN 27 /* 0x1b */
  1036. #define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */
  1037. #define SSH1_MSG_PORT_OPEN 29 /* 0x1d */
  1038. #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */
  1039. #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */
  1040. #define SSH1_MSG_IGNORE 32 /* 0x20 */
  1041. #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */
  1042. #define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */
  1043. #define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */
  1044. #define SSH1_MSG_DEBUG 36 /* 0x24 */
  1045. #define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */
  1046. #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */
  1047. #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */
  1048. #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */
  1049. #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */
  1050. #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */
  1051. #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */
  1052. #define SSH1_AUTH_RHOSTS 1 /* 0x1 */
  1053. #define SSH1_AUTH_RSA 2 /* 0x2 */
  1054. #define SSH1_AUTH_PASSWORD 3 /* 0x3 */
  1055. #define SSH1_AUTH_RHOSTS_RSA 4 /* 0x4 */
  1056. #define SSH1_AUTH_TIS 5 /* 0x5 */
  1057. #define SSH1_AUTH_CCARD 16 /* 0x10 */
  1058. #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */
  1059. /* Mask for protoflags we will echo back to server if seen */
  1060. #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */
  1061. /*
  1062. * SSH-2 message type codes.
  1063. */
  1064. #define SSH2_MSG_DISCONNECT 1 /* 0x1 */
  1065. #define SSH2_MSG_IGNORE 2 /* 0x2 */
  1066. #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */
  1067. #define SSH2_MSG_DEBUG 4 /* 0x4 */
  1068. #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */
  1069. #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */
  1070. #define SSH2_MSG_KEXINIT 20 /* 0x14 */
  1071. #define SSH2_MSG_NEWKEYS 21 /* 0x15 */
  1072. #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */
  1073. #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */
  1074. #define SSH2_MSG_KEX_DH_GEX_REQUEST_OLD 30 /* 0x1e */
  1075. #define SSH2_MSG_KEX_DH_GEX_REQUEST 34 /* 0x22 */
  1076. #define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */
  1077. #define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */
  1078. #define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */
  1079. #define SSH2_MSG_KEXGSS_INIT 30 /* 0x1e */
  1080. #define SSH2_MSG_KEXGSS_CONTINUE 31 /* 0x1f */
  1081. #define SSH2_MSG_KEXGSS_COMPLETE 32 /* 0x20 */
  1082. #define SSH2_MSG_KEXGSS_HOSTKEY 33 /* 0x21 */
  1083. #define SSH2_MSG_KEXGSS_ERROR 34 /* 0x22 */
  1084. #define SSH2_MSG_KEXGSS_GROUPREQ 40 /* 0x28 */
  1085. #define SSH2_MSG_KEXGSS_GROUP 41 /* 0x29 */
  1086. #define SSH2_MSG_KEXRSA_PUBKEY 30 /* 0x1e */
  1087. #define SSH2_MSG_KEXRSA_SECRET 31 /* 0x1f */
  1088. #define SSH2_MSG_KEXRSA_DONE 32 /* 0x20 */
  1089. #define SSH2_MSG_KEX_ECDH_INIT 30 /* 0x1e */
  1090. #define SSH2_MSG_KEX_ECDH_REPLY 31 /* 0x1f */
  1091. #define SSH2_MSG_KEX_ECMQV_INIT 30 /* 0x1e */
  1092. #define SSH2_MSG_KEX_ECMQV_REPLY 31 /* 0x1f */
  1093. #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */
  1094. #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */
  1095. #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */
  1096. #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */
  1097. #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */
  1098. #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */
  1099. #define SSH2_MSG_USERAUTH_INFO_REQUEST 60 /* 0x3c */
  1100. #define SSH2_MSG_USERAUTH_INFO_RESPONSE 61 /* 0x3d */
  1101. #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */
  1102. #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */
  1103. #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */
  1104. #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */
  1105. #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */
  1106. #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */
  1107. #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */
  1108. #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */
  1109. #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */
  1110. #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */
  1111. #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */
  1112. #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */
  1113. #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */
  1114. #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */
  1115. #define SSH2_MSG_USERAUTH_GSSAPI_RESPONSE 60
  1116. #define SSH2_MSG_USERAUTH_GSSAPI_TOKEN 61
  1117. #define SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE 63
  1118. #define SSH2_MSG_USERAUTH_GSSAPI_ERROR 64
  1119. #define SSH2_MSG_USERAUTH_GSSAPI_ERRTOK 65
  1120. #define SSH2_MSG_USERAUTH_GSSAPI_MIC 66
  1121. /* Virtual packet type, for packets too short to even have a type */
  1122. #define SSH_MSG_NO_TYPE_CODE 0x100
  1123. /* Given that virtual packet types exist, this is how big the dispatch
  1124. * table has to be */
  1125. #define SSH_MAX_MSG 0x101
  1126. /*
  1127. * SSH-1 agent messages.
  1128. */
  1129. #define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
  1130. #define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
  1131. #define SSH1_AGENTC_RSA_CHALLENGE 3
  1132. #define SSH1_AGENT_RSA_RESPONSE 4
  1133. #define SSH1_AGENTC_ADD_RSA_IDENTITY 7
  1134. #define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
  1135. #define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
  1136. /*
  1137. * Messages common to SSH-1 and OpenSSH's SSH-2.
  1138. */
  1139. #define SSH_AGENT_FAILURE 5
  1140. #define SSH_AGENT_SUCCESS 6
  1141. /*
  1142. * OpenSSH's SSH-2 agent messages.
  1143. */
  1144. #define SSH2_AGENTC_REQUEST_IDENTITIES 11
  1145. #define SSH2_AGENT_IDENTITIES_ANSWER 12
  1146. #define SSH2_AGENTC_SIGN_REQUEST 13
  1147. #define SSH2_AGENT_SIGN_RESPONSE 14
  1148. #define SSH2_AGENTC_ADD_IDENTITY 17
  1149. #define SSH2_AGENTC_REMOVE_IDENTITY 18
  1150. #define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
  1151. /*
  1152. * Assorted other SSH-related enumerations.
  1153. */
  1154. #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */
  1155. #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */
  1156. #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */
  1157. #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */
  1158. #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */
  1159. #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */
  1160. #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */
  1161. #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */
  1162. #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */
  1163. #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */
  1164. #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */
  1165. #define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */
  1166. #define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */
  1167. #define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */
  1168. #define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */
  1169. #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */
  1170. #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */
  1171. #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */
  1172. #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */
  1173. #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */
  1174. const char *ssh1_pkt_type(int type);
  1175. const char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type);
  1176. /*
  1177. * Need this to warn about support for the original SSH-2 keyfile
  1178. * format.
  1179. */
  1180. void old_keyfile_warning(void);
  1181. /*
  1182. * Flags indicating implementation bugs that we know how to mitigate
  1183. * if we think the other end has them.
  1184. */
  1185. #define SSH_IMPL_BUG_LIST(X) \
  1186. X(BUG_CHOKES_ON_SSH1_IGNORE) \
  1187. X(BUG_SSH2_HMAC) \
  1188. X(BUG_NEEDS_SSH1_PLAIN_PASSWORD) \
  1189. X(BUG_CHOKES_ON_RSA) \
  1190. X(BUG_SSH2_RSA_PADDING) \
  1191. X(BUG_SSH2_DERIVEKEY) \
  1192. X(BUG_SSH2_REKEY) \
  1193. X(BUG_SSH2_PK_SESSIONID) \
  1194. X(BUG_SSH2_MAXPKT) \
  1195. X(BUG_CHOKES_ON_SSH2_IGNORE) \
  1196. X(BUG_CHOKES_ON_WINADJ) \
  1197. X(BUG_SENDS_LATE_REQUEST_REPLY) \
  1198. X(BUG_SSH2_OLDGEX) \
  1199. /* end of list */
  1200. #define TMP_DECLARE_LOG2_ENUM(thing) log2_##thing,
  1201. enum { SSH_IMPL_BUG_LIST(TMP_DECLARE_LOG2_ENUM) };
  1202. #undef TMP_DECLARE_LOG2_ENUM
  1203. #define TMP_DECLARE_REAL_ENUM(thing) thing = 1 << log2_##thing,
  1204. enum { SSH_IMPL_BUG_LIST(TMP_DECLARE_REAL_ENUM) };
  1205. #undef TMP_DECLARE_REAL_ENUM
  1206. /* Shared function that writes tty modes into a pty request */
  1207. void write_ttymodes_to_packet_from_conf(
  1208. BinarySink *bs, Frontend *frontend, Conf *conf,
  1209. int ssh_version, int ospeed, int ispeed);
  1210. /* Shared system for allocating local SSH channel ids. Expects to be
  1211. * passed a tree full of structs that have a field called 'localid' of
  1212. * type unsigned, and will check that! */
  1213. unsigned alloc_channel_id_general(tree234 *channels, size_t localid_offset);
  1214. #define alloc_channel_id(tree, type) \
  1215. TYPECHECK(&((type *)0)->localid == (unsigned *)0, \
  1216. alloc_channel_id_general(tree, offsetof(type, localid)))
  1217. int first_in_commasep_string(char const *needle, char const *haystack,
  1218. int haylen);
  1219. int in_commasep_string(char const *needle, char const *haystack, int haylen);
  1220. void add_to_commasep(strbuf *buf, const char *data);
  1221. #endif // WINSCP_VS