ssh.h 53 KB

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