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