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