ssh.h 48 KB

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