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