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