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