ssh.h 52 KB

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