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