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