ssh.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. #ifndef WINSCP_VS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "puttymem.h"
  5. #include "tree234.h"
  6. #include "network.h"
  7. #include "int64.h"
  8. #include "misc.h"
  9. struct ssh_channel;
  10. typedef struct ssh_tag *Ssh;
  11. extern int sshfwd_write(struct ssh_channel *c, char *, int);
  12. extern void sshfwd_write_eof(struct ssh_channel *c);
  13. extern void sshfwd_unclean_close(struct ssh_channel *c, const char *err);
  14. extern void sshfwd_unthrottle(struct ssh_channel *c, int bufsize);
  15. Conf *sshfwd_get_conf(struct ssh_channel *c);
  16. void sshfwd_x11_sharing_handover(struct ssh_channel *c,
  17. void *share_cs, void *share_chan,
  18. const char *peer_addr, int peer_port,
  19. int endian, int protomajor, int protominor,
  20. const void *initial_data, int initial_len);
  21. void sshfwd_x11_is_local(struct ssh_channel *c);
  22. extern Socket ssh_connection_sharing_init(const char *host, int port,
  23. Conf *conf, Ssh ssh, void **state);
  24. void share_got_pkt_from_server(void *ctx, int type,
  25. unsigned char *pkt, int pktlen);
  26. void share_activate(void *state, const char *server_verstring);
  27. void sharestate_free(void *state);
  28. int share_ndownstreams(void *state);
  29. void ssh_connshare_log(Ssh ssh, int event, const char *logtext,
  30. const char *ds_err, const char *us_err);
  31. unsigned ssh_alloc_sharing_channel(Ssh ssh, void *sharing_ctx);
  32. void ssh_delete_sharing_channel(Ssh ssh, unsigned localid);
  33. int ssh_alloc_sharing_rportfwd(Ssh ssh, const char *shost, int sport,
  34. void *share_ctx);
  35. void ssh_sharing_queue_global_request(Ssh ssh, void *share_ctx);
  36. struct X11FakeAuth *ssh_sharing_add_x11_display(Ssh ssh, int authtype,
  37. void *share_cs,
  38. void *share_chan);
  39. void ssh_sharing_remove_x11_display(Ssh ssh, struct X11FakeAuth *auth);
  40. void ssh_send_packet_from_downstream(Ssh ssh, unsigned id, int type,
  41. const void *pkt, int pktlen,
  42. const char *additional_log_text);
  43. void ssh_sharing_downstream_connected(Ssh ssh, unsigned id,
  44. const char *peerinfo);
  45. void ssh_sharing_downstream_disconnected(Ssh ssh, unsigned id);
  46. void ssh_sharing_logf(Ssh ssh, unsigned id, const char *logfmt, ...);
  47. int ssh_agent_forwarding_permitted(Ssh ssh);
  48. void share_setup_x11_channel(void *csv, void *chanv,
  49. unsigned upstream_id, unsigned server_id,
  50. unsigned server_currwin, unsigned server_maxpkt,
  51. unsigned client_adjusted_window,
  52. const char *peer_addr, int peer_port, int endian,
  53. int protomajor, int protominor,
  54. const void *initial_data, int initial_len);
  55. /*
  56. * Useful thing.
  57. */
  58. #ifndef lenof
  59. #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
  60. #endif
  61. #define SSH_CIPHER_IDEA 1
  62. #define SSH_CIPHER_DES 2
  63. #define SSH_CIPHER_3DES 3
  64. #define SSH_CIPHER_BLOWFISH 6
  65. #ifdef MSCRYPTOAPI
  66. #define APIEXTRA 8
  67. #else
  68. #define APIEXTRA 0
  69. #endif
  70. #ifndef BIGNUM_INTERNAL
  71. typedef void *Bignum;
  72. #endif
  73. struct RSAKey {
  74. int bits;
  75. int bytes;
  76. #ifdef MSCRYPTOAPI
  77. unsigned long exponent;
  78. unsigned char *modulus;
  79. #else
  80. Bignum modulus;
  81. Bignum exponent;
  82. Bignum private_exponent;
  83. Bignum p;
  84. Bignum q;
  85. Bignum iqmp;
  86. #endif
  87. char *comment;
  88. };
  89. struct dss_key {
  90. Bignum p, q, g, y, x;
  91. };
  92. int makekey(unsigned char *data, int len, struct RSAKey *result,
  93. unsigned char **keystr, int order);
  94. int makeprivate(unsigned char *data, int len, struct RSAKey *result);
  95. int rsaencrypt(unsigned char *data, int length, struct RSAKey *key);
  96. Bignum rsadecrypt(Bignum input, struct RSAKey *key);
  97. void rsasign(unsigned char *data, int length, struct RSAKey *key);
  98. void rsasanitise(struct RSAKey *key);
  99. int rsastr_len(struct RSAKey *key);
  100. void rsastr_fmt(char *str, struct RSAKey *key);
  101. void rsa_fingerprint(char *str, int len, struct RSAKey *key);
  102. int rsa_verify(struct RSAKey *key);
  103. unsigned char *rsa_public_blob(struct RSAKey *key, int *len);
  104. int rsa_public_blob_len(void *data, int maxlen);
  105. void freersakey(struct RSAKey *key);
  106. #endif // WINSCP_VS
  107. #ifndef PUTTY_UINT32_DEFINED
  108. /* This makes assumptions about the int type. */
  109. typedef unsigned int uint32;
  110. #define PUTTY_UINT32_DEFINED
  111. #endif
  112. typedef uint32 word32;
  113. #ifndef WINSCP_VS
  114. unsigned long crc32_compute(const void *s, size_t len);
  115. unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len);
  116. /* SSH CRC compensation attack detector */
  117. void *crcda_make_context(void);
  118. void crcda_free_context(void *handle);
  119. int detect_attack(void *handle, unsigned char *buf, uint32 len,
  120. unsigned char *IV);
  121. /*
  122. * SSH2 RSA key exchange functions
  123. */
  124. struct ssh_hash;
  125. void *ssh_rsakex_newkey(char *data, int len);
  126. void ssh_rsakex_freekey(void *key);
  127. int ssh_rsakex_klen(void *key);
  128. void ssh_rsakex_encrypt(const struct ssh_hash *h, unsigned char *in, int inlen,
  129. unsigned char *out, int outlen,
  130. void *key);
  131. typedef struct {
  132. uint32 h[4];
  133. } MD5_Core_State;
  134. struct MD5Context {
  135. #ifdef MSCRYPTOAPI
  136. unsigned long hHash;
  137. #else
  138. MD5_Core_State core;
  139. unsigned char block[64];
  140. int blkused;
  141. uint32 lenhi, lenlo;
  142. #endif
  143. };
  144. void MD5Init(struct MD5Context *context);
  145. void MD5Update(struct MD5Context *context, unsigned char const *buf,
  146. unsigned len);
  147. void MD5Final(unsigned char digest[16], struct MD5Context *context);
  148. void MD5Simple(void const *p, unsigned len, unsigned char output[16]);
  149. void *hmacmd5_make_context(void);
  150. void hmacmd5_free_context(void *handle);
  151. void hmacmd5_key(void *handle, void const *key, int len);
  152. void hmacmd5_do_hmac(void *handle, unsigned char const *blk, int len,
  153. unsigned char *hmac);
  154. #ifdef MPEXT
  155. // Resolve ambiguity with OpenSSL
  156. #define SHA_Init putty_SHA_Init
  157. #define SHA_Final putty_SHA_Final
  158. #define SHA256_Init putty_SHA256_Init
  159. #define SHA256_Final putty_SHA256_Final
  160. #define SHA512_Init putty_SHA512_Init
  161. #define SHA512_Final putty_SHA512_Final
  162. #endif
  163. typedef struct {
  164. uint32 h[5];
  165. unsigned char block[64];
  166. int blkused;
  167. uint32 lenhi, lenlo;
  168. } SHA_State;
  169. void SHA_Init(SHA_State * s);
  170. void SHA_Bytes(SHA_State * s, const void *p, int len);
  171. void SHA_Final(SHA_State * s, unsigned char *output);
  172. void SHA_Simple(const void *p, int len, unsigned char *output);
  173. void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
  174. unsigned char *output);
  175. #endif // WINSCP_VS
  176. typedef struct {
  177. uint32 h[8];
  178. unsigned char block[64];
  179. int blkused;
  180. uint32 lenhi, lenlo;
  181. } SHA256_State;
  182. #ifndef WINSCP_VS
  183. void SHA256_Init(SHA256_State * s);
  184. void SHA256_Bytes(SHA256_State * s, const void *p, int len);
  185. void SHA256_Final(SHA256_State * s, unsigned char *output);
  186. void SHA256_Simple(const void *p, int len, unsigned char *output);
  187. typedef struct {
  188. uint64 h[8];
  189. unsigned char block[128];
  190. int blkused;
  191. uint32 len[4];
  192. } SHA512_State;
  193. void SHA512_Init(SHA512_State * s);
  194. void SHA512_Bytes(SHA512_State * s, const void *p, int len);
  195. void SHA512_Final(SHA512_State * s, unsigned char *output);
  196. void SHA512_Simple(const void *p, int len, unsigned char *output);
  197. struct ssh_cipher {
  198. void *(*make_context)(void);
  199. void (*free_context)(void *);
  200. void (*sesskey) (void *, unsigned char *key); /* for SSH-1 */
  201. void (*encrypt) (void *, unsigned char *blk, int len);
  202. void (*decrypt) (void *, unsigned char *blk, int len);
  203. int blksize;
  204. char *text_name;
  205. };
  206. struct ssh2_cipher {
  207. void *(*make_context)(void);
  208. void (*free_context)(void *);
  209. void (*setiv) (void *, unsigned char *key); /* for SSH-2 */
  210. void (*setkey) (void *, unsigned char *key);/* for SSH-2 */
  211. void (*encrypt) (void *, unsigned char *blk, int len);
  212. void (*decrypt) (void *, unsigned char *blk, int len);
  213. char *name;
  214. int blksize;
  215. int keylen;
  216. unsigned int flags;
  217. #define SSH_CIPHER_IS_CBC 1
  218. char *text_name;
  219. };
  220. struct ssh2_ciphers {
  221. int nciphers;
  222. const struct ssh2_cipher *const *list;
  223. };
  224. struct ssh_mac {
  225. void *(*make_context)(void);
  226. void (*free_context)(void *);
  227. void (*setkey) (void *, unsigned char *key);
  228. /* whole-packet operations */
  229. void (*generate) (void *, unsigned char *blk, int len, unsigned long seq);
  230. int (*verify) (void *, unsigned char *blk, int len, unsigned long seq);
  231. /* partial-packet operations */
  232. void (*start) (void *);
  233. void (*bytes) (void *, unsigned char const *, int);
  234. void (*genresult) (void *, unsigned char *);
  235. int (*verresult) (void *, unsigned char const *);
  236. char *name;
  237. int len;
  238. char *text_name;
  239. };
  240. struct ssh_hash {
  241. void *(*init)(void); /* also allocates context */
  242. void (*bytes)(void *, void *, int);
  243. void (*final)(void *, unsigned char *); /* also frees context */
  244. int hlen; /* output length in bytes */
  245. char *text_name;
  246. };
  247. struct ssh_kex {
  248. char *name, *groupname;
  249. enum { KEXTYPE_DH, KEXTYPE_RSA } main_type;
  250. /* For DH */
  251. const unsigned char *pdata, *gdata; /* NULL means group exchange */
  252. int plen, glen;
  253. const struct ssh_hash *hash;
  254. };
  255. struct ssh_kexes {
  256. int nkexes;
  257. const struct ssh_kex *const *list;
  258. };
  259. struct ssh_signkey {
  260. void *(*newkey) (char *data, int len);
  261. void (*freekey) (void *key);
  262. char *(*fmtkey) (void *key);
  263. unsigned char *(*public_blob) (void *key, int *len);
  264. unsigned char *(*private_blob) (void *key, int *len);
  265. void *(*createkey) (unsigned char *pub_blob, int pub_len,
  266. unsigned char *priv_blob, int priv_len);
  267. void *(*openssh_createkey) (unsigned char **blob, int *len);
  268. int (*openssh_fmtkey) (void *key, unsigned char *blob, int len);
  269. int (*pubkey_bits) (void *blob, int len);
  270. char *(*fingerprint) (void *key);
  271. int (*verifysig) (void *key, char *sig, int siglen,
  272. char *data, int datalen);
  273. unsigned char *(*sign) (void *key, char *data, int datalen,
  274. int *siglen);
  275. char *name;
  276. char *keytype; /* for host key cache */
  277. };
  278. struct ssh_compress {
  279. char *name;
  280. /* For [email protected]: if non-NULL, this name will be considered once
  281. * userauth has completed successfully. */
  282. char *delayed_name;
  283. void *(*compress_init) (void);
  284. void (*compress_cleanup) (void *);
  285. int (*compress) (void *, unsigned char *block, int len,
  286. unsigned char **outblock, int *outlen);
  287. void *(*decompress_init) (void);
  288. void (*decompress_cleanup) (void *);
  289. int (*decompress) (void *, unsigned char *block, int len,
  290. unsigned char **outblock, int *outlen);
  291. int (*disable_compression) (void *);
  292. char *text_name;
  293. };
  294. struct ssh2_userkey {
  295. const struct ssh_signkey *alg; /* the key algorithm */
  296. void *data; /* the key data */
  297. char *comment; /* the key comment */
  298. };
  299. /* The maximum length of any hash algorithm used in kex. (bytes) */
  300. #define SSH2_KEX_MAX_HASH_LEN (32) /* SHA-256 */
  301. extern const struct ssh_cipher ssh_3des;
  302. extern const struct ssh_cipher ssh_des;
  303. extern const struct ssh_cipher ssh_blowfish_ssh1;
  304. extern const struct ssh2_ciphers ssh2_3des;
  305. extern const struct ssh2_ciphers ssh2_des;
  306. extern const struct ssh2_ciphers ssh2_aes;
  307. extern const struct ssh2_ciphers ssh2_blowfish;
  308. extern const struct ssh2_ciphers ssh2_arcfour;
  309. extern const struct ssh_hash ssh_sha1;
  310. extern const struct ssh_hash ssh_sha256;
  311. extern const struct ssh_kexes ssh_diffiehellman_group1;
  312. extern const struct ssh_kexes ssh_diffiehellman_group14;
  313. extern const struct ssh_kexes ssh_diffiehellman_gex;
  314. extern const struct ssh_kexes ssh_rsa_kex;
  315. extern const struct ssh_signkey ssh_dss;
  316. extern const struct ssh_signkey ssh_rsa;
  317. extern const struct ssh_mac ssh_hmac_md5;
  318. extern const struct ssh_mac ssh_hmac_sha1;
  319. extern const struct ssh_mac ssh_hmac_sha1_buggy;
  320. extern const struct ssh_mac ssh_hmac_sha1_96;
  321. extern const struct ssh_mac ssh_hmac_sha1_96_buggy;
  322. extern const struct ssh_mac ssh_hmac_sha256;
  323. void *aes_make_context(void);
  324. void aes_free_context(void *handle);
  325. void aes128_key(void *handle, unsigned char *key);
  326. void aes192_key(void *handle, unsigned char *key);
  327. void aes256_key(void *handle, unsigned char *key);
  328. void aes_iv(void *handle, unsigned char *iv);
  329. void aes_ssh2_encrypt_blk(void *handle, unsigned char *blk, int len);
  330. void aes_ssh2_decrypt_blk(void *handle, unsigned char *blk, int len);
  331. /*
  332. * PuTTY version number formatted as an SSH version string.
  333. */
  334. extern char sshver[];
  335. /*
  336. * Gross hack: pscp will try to start SFTP but fall back to scp1 if
  337. * that fails. This variable is the means by which scp.c can reach
  338. * into the SSH code and find out which one it got.
  339. */
  340. extern int ssh_fallback_cmd(void *handle);
  341. #ifndef MSCRYPTOAPI
  342. void SHATransform(word32 * digest, word32 * data);
  343. #endif
  344. int random_byte(void);
  345. void random_add_noise(void *noise, int length);
  346. void random_add_heavynoise(void *noise, int length);
  347. void logevent(void *, const char *);
  348. struct PortForwarding;
  349. /* Allocate and register a new channel for port forwarding */
  350. void *new_sock_channel(void *handle, struct PortForwarding *pf);
  351. void ssh_send_port_open(void *channel, char *hostname, int port, char *org);
  352. /* Exports from portfwd.c */
  353. extern char *pfd_connect(struct PortForwarding **pf, char *hostname, int port,
  354. void *c, Conf *conf, int addressfamily);
  355. extern void pfd_close(struct PortForwarding *);
  356. extern int pfd_send(struct PortForwarding *, char *data, int len);
  357. extern void pfd_send_eof(struct PortForwarding *);
  358. extern void pfd_confirm(struct PortForwarding *);
  359. extern void pfd_unthrottle(struct PortForwarding *);
  360. extern void pfd_override_throttle(struct PortForwarding *, int enable);
  361. struct PortListener;
  362. /* desthost == NULL indicates dynamic (SOCKS) port forwarding */
  363. extern char *pfl_listen(char *desthost, int destport, char *srcaddr,
  364. int port, void *backhandle, Conf *conf,
  365. struct PortListener **pl, int address_family);
  366. extern void pfl_terminate(struct PortListener *);
  367. /* Exports from x11fwd.c */
  368. enum {
  369. X11_TRANS_IPV4 = 0, X11_TRANS_IPV6 = 6, X11_TRANS_UNIX = 256
  370. };
  371. struct X11Display {
  372. /* Broken-down components of the display name itself */
  373. int unixdomain;
  374. char *hostname;
  375. int displaynum;
  376. int screennum;
  377. /* OSX sometimes replaces all the above with a full Unix-socket pathname */
  378. char *unixsocketpath;
  379. /* PuTTY networking SockAddr to connect to the display, and associated
  380. * gubbins */
  381. SockAddr addr;
  382. int port;
  383. char *realhost;
  384. /* Our local auth details for talking to the real X display. */
  385. int localauthproto;
  386. unsigned char *localauthdata;
  387. int localauthdatalen;
  388. };
  389. struct X11FakeAuth {
  390. /* Auth details we invented for a virtual display on the SSH server. */
  391. int proto;
  392. unsigned char *data;
  393. int datalen;
  394. char *protoname;
  395. char *datastring;
  396. /* The encrypted form of the first block, in XDM-AUTHORIZATION-1.
  397. * Used as part of the key when these structures are organised
  398. * into a tree. See x11_invent_fake_auth for explanation. */
  399. unsigned char *xa1_firstblock;
  400. /*
  401. * Used inside x11fwd.c to remember recently seen
  402. * XDM-AUTHORIZATION-1 strings, to avoid replay attacks.
  403. */
  404. tree234 *xdmseen;
  405. /*
  406. * What to do with an X connection matching this auth data.
  407. */
  408. struct X11Display *disp;
  409. void *share_cs, *share_chan;
  410. };
  411. void *x11_make_greeting(int endian, int protomajor, int protominor,
  412. int auth_proto, const void *auth_data, int auth_len,
  413. const char *peer_ip, int peer_port,
  414. int *outlen);
  415. int x11_authcmp(void *av, void *bv); /* for putting X11FakeAuth in a tree234 */
  416. /*
  417. * x11_setup_display() parses the display variable and fills in an
  418. * X11Display structure. Some remote auth details are invented;
  419. * the supplied authtype parameter configures the preferred
  420. * authorisation protocol to use at the remote end. The local auth
  421. * details are looked up by calling platform_get_x11_auth.
  422. */
  423. extern struct X11Display *x11_setup_display(char *display, Conf *);
  424. void x11_free_display(struct X11Display *disp);
  425. struct X11FakeAuth *x11_invent_fake_auth(tree234 *t, int authtype);
  426. void x11_free_fake_auth(struct X11FakeAuth *auth);
  427. struct X11Connection; /* opaque outside x11fwd.c */
  428. struct X11Connection *x11_init(tree234 *authtree, void *, const char *, int);
  429. extern void x11_close(struct X11Connection *);
  430. extern int x11_send(struct X11Connection *, char *, int);
  431. extern void x11_send_eof(struct X11Connection *s);
  432. extern void x11_unthrottle(struct X11Connection *s);
  433. extern void x11_override_throttle(struct X11Connection *s, int enable);
  434. char *x11_display(const char *display);
  435. /* Platform-dependent X11 functions */
  436. extern void platform_get_x11_auth(struct X11Display *display, Conf *);
  437. /* examine a mostly-filled-in X11Display and fill in localauth* */
  438. extern const int platform_uses_x11_unix_by_default;
  439. /* choose default X transport in the absence of a specified one */
  440. SockAddr platform_get_x11_unix_address(const char *path, int displaynum);
  441. /* make up a SockAddr naming the address for displaynum */
  442. char *platform_get_x_display(void);
  443. /* allocated local X display string, if any */
  444. /* Callbacks in x11.c usable _by_ platform X11 functions */
  445. /*
  446. * This function does the job of platform_get_x11_auth, provided
  447. * it is told where to find a normally formatted .Xauthority file:
  448. * it opens that file, parses it to find an auth record which
  449. * matches the display details in "display", and fills in the
  450. * localauth fields.
  451. *
  452. * It is expected that most implementations of
  453. * platform_get_x11_auth() will work by finding their system's
  454. * .Xauthority file, adjusting the display details if necessary
  455. * for local oddities like Unix-domain socket transport, and
  456. * calling this function to do the rest of the work.
  457. */
  458. void x11_get_auth_from_authfile(struct X11Display *display,
  459. const char *authfilename);
  460. int x11_identify_auth_proto(const char *proto);
  461. void *x11_dehexify(const char *hex, int *outlen);
  462. Bignum copybn(Bignum b);
  463. Bignum bn_power_2(int n);
  464. void bn_restore_invariant(Bignum b);
  465. Bignum bignum_from_long(unsigned long n);
  466. void freebn(Bignum b);
  467. Bignum modpow(Bignum base, Bignum exp, Bignum mod);
  468. Bignum modmul(Bignum a, Bignum b, Bignum mod);
  469. void decbn(Bignum n);
  470. extern Bignum Zero, One;
  471. Bignum bignum_from_bytes(const unsigned char *data, int nbytes);
  472. int ssh1_read_bignum(const unsigned char *data, int len, Bignum * result);
  473. int bignum_bitcount(Bignum bn);
  474. int ssh1_bignum_length(Bignum bn);
  475. int ssh2_bignum_length(Bignum bn);
  476. int bignum_byte(Bignum bn, int i);
  477. int bignum_bit(Bignum bn, int i);
  478. void bignum_set_bit(Bignum bn, int i, int value);
  479. int ssh1_write_bignum(void *data, Bignum bn);
  480. Bignum biggcd(Bignum a, Bignum b);
  481. unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
  482. Bignum bignum_add_long(Bignum number, unsigned long addend);
  483. Bignum bigadd(Bignum a, Bignum b);
  484. Bignum bigsub(Bignum a, Bignum b);
  485. Bignum bigmul(Bignum a, Bignum b);
  486. Bignum bigmuladd(Bignum a, Bignum b, Bignum addend);
  487. Bignum bigdiv(Bignum a, Bignum b);
  488. Bignum bigmod(Bignum a, Bignum b);
  489. Bignum modinv(Bignum number, Bignum modulus);
  490. Bignum bignum_bitmask(Bignum number);
  491. Bignum bignum_rshift(Bignum number, int shift);
  492. int bignum_cmp(Bignum a, Bignum b);
  493. char *bignum_decimal(Bignum x);
  494. #ifdef DEBUG
  495. void diagbn(char *prefix, Bignum md);
  496. #endif
  497. void *dh_setup_group(const struct ssh_kex *kex);
  498. void *dh_setup_gex(Bignum pval, Bignum gval);
  499. void dh_cleanup(void *);
  500. Bignum dh_create_e(void *, int nbits);
  501. const char *dh_validate_f(void *handle, Bignum f);
  502. Bignum dh_find_K(void *, Bignum f);
  503. int loadrsakey(const Filename *filename, struct RSAKey *key,
  504. char *passphrase, const char **errorstr);
  505. int rsakey_encrypted(const Filename *filename, char **comment);
  506. int rsakey_pubblob(const Filename *filename, void **blob, int *bloblen,
  507. char **commentptr, const char **errorstr);
  508. int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase);
  509. extern int base64_decode_atom(char *atom, unsigned char *out);
  510. extern int base64_lines(int datalen);
  511. extern void base64_encode_atom(unsigned char *data, int n, char *out);
  512. extern void base64_encode(FILE *fp, unsigned char *data, int datalen, int cpl);
  513. /* ssh2_load_userkey can return this as an error */
  514. extern struct ssh2_userkey ssh2_wrong_passphrase;
  515. #define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
  516. int ssh2_userkey_encrypted(const Filename *filename, char **comment);
  517. struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
  518. char *passphrase, const char **errorstr);
  519. unsigned char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
  520. int *pub_blob_len, char **commentptr,
  521. const char **errorstr);
  522. int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
  523. char *passphrase);
  524. const struct ssh_signkey *find_pubkey_alg(const char *name);
  525. enum {
  526. SSH_KEYTYPE_UNOPENABLE,
  527. SSH_KEYTYPE_UNKNOWN,
  528. SSH_KEYTYPE_SSH1, SSH_KEYTYPE_SSH2,
  529. SSH_KEYTYPE_OPENSSH, SSH_KEYTYPE_SSHCOM
  530. };
  531. int key_type(const Filename *filename);
  532. char *key_type_to_str(int type);
  533. int import_possible(int type);
  534. int import_target_type(int type);
  535. int import_encrypted(const Filename *filename, int type, char **comment);
  536. int import_ssh1(const Filename *filename, int type,
  537. struct RSAKey *key, char *passphrase, const char **errmsg_p);
  538. struct ssh2_userkey *import_ssh2(const Filename *filename, int type,
  539. char *passphrase, const char **errmsg_p);
  540. int export_ssh1(const Filename *filename, int type,
  541. struct RSAKey *key, char *passphrase);
  542. int export_ssh2(const Filename *filename, int type,
  543. struct ssh2_userkey *key, char *passphrase);
  544. void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
  545. void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
  546. void des3_decrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
  547. unsigned char *blk, int len);
  548. void des3_encrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
  549. unsigned char *blk, int len);
  550. void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk,
  551. int len);
  552. void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk,
  553. int len);
  554. void des_encrypt_xdmauth(const unsigned char *key,
  555. unsigned char *blk, int len);
  556. void des_decrypt_xdmauth(const unsigned char *key,
  557. unsigned char *blk, int len);
  558. /*
  559. * For progress updates in the key generation utility.
  560. */
  561. #define PROGFN_INITIALISE 1
  562. #define PROGFN_LIN_PHASE 2
  563. #define PROGFN_EXP_PHASE 3
  564. #define PROGFN_PHASE_EXTENT 4
  565. #define PROGFN_READY 5
  566. #define PROGFN_PROGRESS 6
  567. typedef void (*progfn_t) (void *param, int action, int phase, int progress);
  568. int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
  569. void *pfnparam);
  570. int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
  571. void *pfnparam);
  572. Bignum primegen(int bits, int modulus, int residue, Bignum factor,
  573. int phase, progfn_t pfn, void *pfnparam, unsigned firstbits);
  574. void invent_firstbits(unsigned *one, unsigned *two);
  575. /*
  576. * zlib compression.
  577. */
  578. void *zlib_compress_init(void);
  579. void zlib_compress_cleanup(void *);
  580. void *zlib_decompress_init(void);
  581. void zlib_decompress_cleanup(void *);
  582. int zlib_compress_block(void *, unsigned char *block, int len,
  583. unsigned char **outblock, int *outlen);
  584. int zlib_decompress_block(void *, unsigned char *block, int len,
  585. unsigned char **outblock, int *outlen);
  586. /*
  587. * Connection-sharing API provided by platforms. This function must
  588. * either:
  589. * - return SHARE_NONE and do nothing
  590. * - return SHARE_DOWNSTREAM and set *sock to a Socket connected to
  591. * downplug
  592. * - return SHARE_UPSTREAM and set *sock to a Socket connected to
  593. * upplug.
  594. */
  595. enum { SHARE_NONE, SHARE_DOWNSTREAM, SHARE_UPSTREAM };
  596. int platform_ssh_share(const char *name, Conf *conf,
  597. Plug downplug, Plug upplug, Socket *sock,
  598. char **logtext, char **ds_err, char **us_err,
  599. int can_upstream, int can_downstream);
  600. void platform_ssh_share_cleanup(const char *name);
  601. /*
  602. * SSH-1 message type codes.
  603. */
  604. #define SSH1_MSG_DISCONNECT 1 /* 0x1 */
  605. #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */
  606. #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */
  607. #define SSH1_CMSG_USER 4 /* 0x4 */
  608. #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */
  609. #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */
  610. #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */
  611. #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */
  612. #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */
  613. #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */
  614. #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */
  615. #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */
  616. #define SSH1_SMSG_SUCCESS 14 /* 0xe */
  617. #define SSH1_SMSG_FAILURE 15 /* 0xf */
  618. #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */
  619. #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */
  620. #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */
  621. #define SSH1_CMSG_EOF 19 /* 0x13 */
  622. #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */
  623. #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */
  624. #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */
  625. #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */
  626. #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */
  627. #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */
  628. #define SSH1_SMSG_X11_OPEN 27 /* 0x1b */
  629. #define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */
  630. #define SSH1_MSG_PORT_OPEN 29 /* 0x1d */
  631. #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */
  632. #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */
  633. #define SSH1_MSG_IGNORE 32 /* 0x20 */
  634. #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */
  635. #define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */
  636. #define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */
  637. #define SSH1_MSG_DEBUG 36 /* 0x24 */
  638. #define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */
  639. #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */
  640. #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */
  641. #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */
  642. #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */
  643. #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */
  644. #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */
  645. #define SSH1_AUTH_RHOSTS 1 /* 0x1 */
  646. #define SSH1_AUTH_RSA 2 /* 0x2 */
  647. #define SSH1_AUTH_PASSWORD 3 /* 0x3 */
  648. #define SSH1_AUTH_RHOSTS_RSA 4 /* 0x4 */
  649. #define SSH1_AUTH_TIS 5 /* 0x5 */
  650. #define SSH1_AUTH_CCARD 16 /* 0x10 */
  651. #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */
  652. /* Mask for protoflags we will echo back to server if seen */
  653. #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */
  654. /*
  655. * SSH-2 message type codes.
  656. */
  657. #define SSH2_MSG_DISCONNECT 1 /* 0x1 */
  658. #define SSH2_MSG_IGNORE 2 /* 0x2 */
  659. #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */
  660. #define SSH2_MSG_DEBUG 4 /* 0x4 */
  661. #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */
  662. #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */
  663. #define SSH2_MSG_KEXINIT 20 /* 0x14 */
  664. #define SSH2_MSG_NEWKEYS 21 /* 0x15 */
  665. #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */
  666. #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */
  667. #define SSH2_MSG_KEX_DH_GEX_REQUEST_OLD 30 /* 0x1e */
  668. #define SSH2_MSG_KEX_DH_GEX_REQUEST 34 /* 0x22 */
  669. #define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */
  670. #define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */
  671. #define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */
  672. #define SSH2_MSG_KEXRSA_PUBKEY 30 /* 0x1e */
  673. #define SSH2_MSG_KEXRSA_SECRET 31 /* 0x1f */
  674. #define SSH2_MSG_KEXRSA_DONE 32 /* 0x20 */
  675. #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */
  676. #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */
  677. #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */
  678. #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */
  679. #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */
  680. #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */
  681. #define SSH2_MSG_USERAUTH_INFO_REQUEST 60 /* 0x3c */
  682. #define SSH2_MSG_USERAUTH_INFO_RESPONSE 61 /* 0x3d */
  683. #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */
  684. #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */
  685. #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */
  686. #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */
  687. #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */
  688. #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */
  689. #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */
  690. #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */
  691. #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */
  692. #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */
  693. #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */
  694. #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */
  695. #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */
  696. #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */
  697. #define SSH2_MSG_USERAUTH_GSSAPI_RESPONSE 60
  698. #define SSH2_MSG_USERAUTH_GSSAPI_TOKEN 61
  699. #define SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE 63
  700. #define SSH2_MSG_USERAUTH_GSSAPI_ERROR 64
  701. #define SSH2_MSG_USERAUTH_GSSAPI_ERRTOK 65
  702. #define SSH2_MSG_USERAUTH_GSSAPI_MIC 66
  703. /*
  704. * SSH-1 agent messages.
  705. */
  706. #define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
  707. #define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
  708. #define SSH1_AGENTC_RSA_CHALLENGE 3
  709. #define SSH1_AGENT_RSA_RESPONSE 4
  710. #define SSH1_AGENTC_ADD_RSA_IDENTITY 7
  711. #define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
  712. #define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
  713. /*
  714. * Messages common to SSH-1 and OpenSSH's SSH-2.
  715. */
  716. #define SSH_AGENT_FAILURE 5
  717. #define SSH_AGENT_SUCCESS 6
  718. /*
  719. * OpenSSH's SSH-2 agent messages.
  720. */
  721. #define SSH2_AGENTC_REQUEST_IDENTITIES 11
  722. #define SSH2_AGENT_IDENTITIES_ANSWER 12
  723. #define SSH2_AGENTC_SIGN_REQUEST 13
  724. #define SSH2_AGENT_SIGN_RESPONSE 14
  725. #define SSH2_AGENTC_ADD_IDENTITY 17
  726. #define SSH2_AGENTC_REMOVE_IDENTITY 18
  727. #define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
  728. /*
  729. * Assorted other SSH-related enumerations.
  730. */
  731. #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */
  732. #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */
  733. #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */
  734. #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */
  735. #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */
  736. #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */
  737. #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */
  738. #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */
  739. #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */
  740. #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */
  741. #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */
  742. #define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */
  743. #define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */
  744. #define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */
  745. #define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */
  746. #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */
  747. #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */
  748. #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */
  749. #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */
  750. #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */
  751. /*
  752. * Need this to warn about support for the original SSH-2 keyfile
  753. * format.
  754. */
  755. void old_keyfile_warning(void);
  756. #endif // WINSCP_VS