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