Remove SECURITY_WIN32 (knock-on from r8910, which validates it somewhat).
[u/mdw/putty] / ssh.h
... / ...
CommitLineData
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
10struct ssh_channel;
11
12extern void sshfwd_close(struct ssh_channel *c);
13extern int sshfwd_write(struct ssh_channel *c, char *, int);
14extern void sshfwd_unthrottle(struct ssh_channel *c, int bufsize);
15
16/*
17 * Useful thing.
18 */
19#ifndef lenof
20#define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
21#endif
22
23#define SSH_CIPHER_IDEA 1
24#define SSH_CIPHER_DES 2
25#define SSH_CIPHER_3DES 3
26#define SSH_CIPHER_BLOWFISH 6
27
28#ifdef MSCRYPTOAPI
29#define APIEXTRA 8
30#else
31#define APIEXTRA 0
32#endif
33
34#ifndef BIGNUM_INTERNAL
35typedef void *Bignum;
36#endif
37
38struct RSAKey {
39 int bits;
40 int bytes;
41#ifdef MSCRYPTOAPI
42 unsigned long exponent;
43 unsigned char *modulus;
44#else
45 Bignum modulus;
46 Bignum exponent;
47 Bignum private_exponent;
48 Bignum p;
49 Bignum q;
50 Bignum iqmp;
51#endif
52 char *comment;
53};
54
55struct dss_key {
56 Bignum p, q, g, y, x;
57};
58
59int makekey(unsigned char *data, int len, struct RSAKey *result,
60 unsigned char **keystr, int order);
61int makeprivate(unsigned char *data, int len, struct RSAKey *result);
62int rsaencrypt(unsigned char *data, int length, struct RSAKey *key);
63Bignum rsadecrypt(Bignum input, struct RSAKey *key);
64void rsasign(unsigned char *data, int length, struct RSAKey *key);
65void rsasanitise(struct RSAKey *key);
66int rsastr_len(struct RSAKey *key);
67void rsastr_fmt(char *str, struct RSAKey *key);
68void rsa_fingerprint(char *str, int len, struct RSAKey *key);
69int rsa_verify(struct RSAKey *key);
70unsigned char *rsa_public_blob(struct RSAKey *key, int *len);
71int rsa_public_blob_len(void *data, int maxlen);
72void freersakey(struct RSAKey *key);
73
74typedef unsigned int word32;
75typedef unsigned int uint32;
76
77unsigned long crc32_compute(const void *s, size_t len);
78unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len);
79
80/* SSH CRC compensation attack detector */
81void *crcda_make_context(void);
82void crcda_free_context(void *handle);
83int detect_attack(void *handle, unsigned char *buf, uint32 len,
84 unsigned char *IV);
85
86/*
87 * SSH2 RSA key exchange functions
88 */
89struct ssh_hash;
90void *ssh_rsakex_newkey(char *data, int len);
91void ssh_rsakex_freekey(void *key);
92int ssh_rsakex_klen(void *key);
93void ssh_rsakex_encrypt(const struct ssh_hash *h, unsigned char *in, int inlen,
94 unsigned char *out, int outlen,
95 void *key);
96
97typedef struct {
98 uint32 h[4];
99} MD5_Core_State;
100
101struct MD5Context {
102#ifdef MSCRYPTOAPI
103 unsigned long hHash;
104#else
105 MD5_Core_State core;
106 unsigned char block[64];
107 int blkused;
108 uint32 lenhi, lenlo;
109#endif
110};
111
112void MD5Init(struct MD5Context *context);
113void MD5Update(struct MD5Context *context, unsigned char const *buf,
114 unsigned len);
115void MD5Final(unsigned char digest[16], struct MD5Context *context);
116void MD5Simple(void const *p, unsigned len, unsigned char output[16]);
117
118void *hmacmd5_make_context(void);
119void hmacmd5_free_context(void *handle);
120void hmacmd5_key(void *handle, void const *key, int len);
121void hmacmd5_do_hmac(void *handle, unsigned char const *blk, int len,
122 unsigned char *hmac);
123
124typedef struct {
125 uint32 h[5];
126 unsigned char block[64];
127 int blkused;
128 uint32 lenhi, lenlo;
129} SHA_State;
130void SHA_Init(SHA_State * s);
131void SHA_Bytes(SHA_State * s, void *p, int len);
132void SHA_Final(SHA_State * s, unsigned char *output);
133void SHA_Simple(void *p, int len, unsigned char *output);
134
135void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
136 unsigned char *output);
137typedef struct {
138 uint32 h[8];
139 unsigned char block[64];
140 int blkused;
141 uint32 lenhi, lenlo;
142} SHA256_State;
143void SHA256_Init(SHA256_State * s);
144void SHA256_Bytes(SHA256_State * s, const void *p, int len);
145void SHA256_Final(SHA256_State * s, unsigned char *output);
146void SHA256_Simple(const void *p, int len, unsigned char *output);
147
148typedef struct {
149 uint64 h[8];
150 unsigned char block[128];
151 int blkused;
152 uint32 len[4];
153} SHA512_State;
154void SHA512_Init(SHA512_State * s);
155void SHA512_Bytes(SHA512_State * s, const void *p, int len);
156void SHA512_Final(SHA512_State * s, unsigned char *output);
157void SHA512_Simple(const void *p, int len, unsigned char *output);
158
159struct ssh_cipher {
160 void *(*make_context)(void);
161 void (*free_context)(void *);
162 void (*sesskey) (void *, unsigned char *key); /* for SSH-1 */
163 void (*encrypt) (void *, unsigned char *blk, int len);
164 void (*decrypt) (void *, unsigned char *blk, int len);
165 int blksize;
166 char *text_name;
167};
168
169struct ssh2_cipher {
170 void *(*make_context)(void);
171 void (*free_context)(void *);
172 void (*setiv) (void *, unsigned char *key); /* for SSH-2 */
173 void (*setkey) (void *, unsigned char *key);/* for SSH-2 */
174 void (*encrypt) (void *, unsigned char *blk, int len);
175 void (*decrypt) (void *, unsigned char *blk, int len);
176 char *name;
177 int blksize;
178 int keylen;
179 unsigned int flags;
180#define SSH_CIPHER_IS_CBC 1
181 char *text_name;
182};
183
184struct ssh2_ciphers {
185 int nciphers;
186 const struct ssh2_cipher *const *list;
187};
188
189struct ssh_mac {
190 void *(*make_context)(void);
191 void (*free_context)(void *);
192 void (*setkey) (void *, unsigned char *key);
193 /* whole-packet operations */
194 void (*generate) (void *, unsigned char *blk, int len, unsigned long seq);
195 int (*verify) (void *, unsigned char *blk, int len, unsigned long seq);
196 /* partial-packet operations */
197 void (*start) (void *);
198 void (*bytes) (void *, unsigned char const *, int);
199 void (*genresult) (void *, unsigned char *);
200 int (*verresult) (void *, unsigned char const *);
201 char *name;
202 int len;
203 char *text_name;
204};
205
206struct ssh_hash {
207 void *(*init)(void); /* also allocates context */
208 void (*bytes)(void *, void *, int);
209 void (*final)(void *, unsigned char *); /* also frees context */
210 int hlen; /* output length in bytes */
211 char *text_name;
212};
213
214struct ssh_kex {
215 char *name, *groupname;
216 enum { KEXTYPE_DH, KEXTYPE_RSA } main_type;
217 /* For DH */
218 const unsigned char *pdata, *gdata; /* NULL means group exchange */
219 int plen, glen;
220 const struct ssh_hash *hash;
221};
222
223struct ssh_kexes {
224 int nkexes;
225 const struct ssh_kex *const *list;
226};
227
228struct ssh_signkey {
229 void *(*newkey) (char *data, int len);
230 void (*freekey) (void *key);
231 char *(*fmtkey) (void *key);
232 unsigned char *(*public_blob) (void *key, int *len);
233 unsigned char *(*private_blob) (void *key, int *len);
234 void *(*createkey) (unsigned char *pub_blob, int pub_len,
235 unsigned char *priv_blob, int priv_len);
236 void *(*openssh_createkey) (unsigned char **blob, int *len);
237 int (*openssh_fmtkey) (void *key, unsigned char *blob, int len);
238 int (*pubkey_bits) (void *blob, int len);
239 char *(*fingerprint) (void *key);
240 int (*verifysig) (void *key, char *sig, int siglen,
241 char *data, int datalen);
242 unsigned char *(*sign) (void *key, char *data, int datalen,
243 int *siglen);
244 char *name;
245 char *keytype; /* for host key cache */
246};
247
248struct ssh_compress {
249 char *name;
250 void *(*compress_init) (void);
251 void (*compress_cleanup) (void *);
252 int (*compress) (void *, unsigned char *block, int len,
253 unsigned char **outblock, int *outlen);
254 void *(*decompress_init) (void);
255 void (*decompress_cleanup) (void *);
256 int (*decompress) (void *, unsigned char *block, int len,
257 unsigned char **outblock, int *outlen);
258 int (*disable_compression) (void *);
259 char *text_name;
260};
261
262struct ssh2_userkey {
263 const struct ssh_signkey *alg; /* the key algorithm */
264 void *data; /* the key data */
265 char *comment; /* the key comment */
266};
267
268/* The maximum length of any hash algorithm used in kex. (bytes) */
269#define SSH2_KEX_MAX_HASH_LEN (32) /* SHA-256 */
270
271extern const struct ssh_cipher ssh_3des;
272extern const struct ssh_cipher ssh_des;
273extern const struct ssh_cipher ssh_blowfish_ssh1;
274extern const struct ssh2_ciphers ssh2_3des;
275extern const struct ssh2_ciphers ssh2_des;
276extern const struct ssh2_ciphers ssh2_aes;
277extern const struct ssh2_ciphers ssh2_blowfish;
278extern const struct ssh2_ciphers ssh2_arcfour;
279extern const struct ssh_hash ssh_sha1;
280extern const struct ssh_hash ssh_sha256;
281extern const struct ssh_kexes ssh_diffiehellman_group1;
282extern const struct ssh_kexes ssh_diffiehellman_group14;
283extern const struct ssh_kexes ssh_diffiehellman_gex;
284extern const struct ssh_kexes ssh_rsa_kex;
285extern const struct ssh_signkey ssh_dss;
286extern const struct ssh_signkey ssh_rsa;
287extern const struct ssh_mac ssh_hmac_md5;
288extern const struct ssh_mac ssh_hmac_sha1;
289extern const struct ssh_mac ssh_hmac_sha1_buggy;
290extern const struct ssh_mac ssh_hmac_sha1_96;
291extern const struct ssh_mac ssh_hmac_sha1_96_buggy;
292
293
294/*
295 * PuTTY version number formatted as an SSH version string.
296 */
297extern char sshver[];
298
299/*
300 * Gross hack: pscp will try to start SFTP but fall back to scp1 if
301 * that fails. This variable is the means by which scp.c can reach
302 * into the SSH code and find out which one it got.
303 */
304extern int ssh_fallback_cmd(void *handle);
305
306#ifndef MSCRYPTOAPI
307void SHATransform(word32 * digest, word32 * data);
308#endif
309
310int random_byte(void);
311void random_add_noise(void *noise, int length);
312void random_add_heavynoise(void *noise, int length);
313
314void logevent(void *, const char *);
315
316/* Allocate and register a new channel for port forwarding */
317void *new_sock_channel(void *handle, Socket s);
318void ssh_send_port_open(void *channel, char *hostname, int port, char *org);
319
320/* Exports from portfwd.c */
321extern const char *pfd_newconnect(Socket * s, char *hostname, int port,
322 void *c, const Config *cfg,
323 int addressfamily);
324/* desthost == NULL indicates dynamic (SOCKS) port forwarding */
325extern const char *pfd_addforward(char *desthost, int destport, char *srcaddr,
326 int port, void *backhandle,
327 const Config *cfg, void **sockdata,
328 int address_family);
329extern void pfd_close(Socket s);
330extern void pfd_terminate(void *sockdata);
331extern int pfd_send(Socket s, char *data, int len);
332extern void pfd_confirm(Socket s);
333extern void pfd_unthrottle(Socket s);
334extern void pfd_override_throttle(Socket s, int enable);
335
336/* Exports from x11fwd.c */
337enum {
338 X11_TRANS_IPV4 = 0, X11_TRANS_IPV6 = 6, X11_TRANS_UNIX = 256
339};
340struct X11Display {
341 /* Broken-down components of the display name itself */
342 int unixdomain;
343 char *hostname;
344 int displaynum;
345 int screennum;
346 /* OSX sometimes replaces all the above with a full Unix-socket pathname */
347 char *unixsocketpath;
348
349 /* PuTTY networking SockAddr to connect to the display, and associated
350 * gubbins */
351 SockAddr addr;
352 int port;
353 char *realhost;
354
355 /* Auth details we invented for the virtual display on the SSH server. */
356 int remoteauthproto;
357 unsigned char *remoteauthdata;
358 int remoteauthdatalen;
359 char *remoteauthprotoname;
360 char *remoteauthdatastring;
361
362 /* Our local auth details for talking to the real X display. */
363 int localauthproto;
364 unsigned char *localauthdata;
365 int localauthdatalen;
366
367 /*
368 * Used inside x11fwd.c to remember recently seen
369 * XDM-AUTHORIZATION-1 strings, to avoid replay attacks.
370 */
371 tree234 *xdmseen;
372};
373/*
374 * x11_setup_display() parses the display variable and fills in an
375 * X11Display structure. Some remote auth details are invented;
376 * the supplied authtype parameter configures the preferred
377 * authorisation protocol to use at the remote end. The local auth
378 * details are looked up by calling platform_get_x11_auth.
379 */
380extern struct X11Display *x11_setup_display(char *display, int authtype,
381 const Config *);
382void x11_free_display(struct X11Display *disp);
383extern const char *x11_init(Socket *, struct X11Display *, void *,
384 const char *, int, const Config *);
385extern void x11_close(Socket);
386extern int x11_send(Socket, char *, int);
387extern void x11_unthrottle(Socket s);
388extern void x11_override_throttle(Socket s, int enable);
389char *x11_display(const char *display);
390/* Platform-dependent X11 functions */
391extern void platform_get_x11_auth(struct X11Display *display,
392 const Config *);
393 /* examine a mostly-filled-in X11Display and fill in localauth* */
394extern const int platform_uses_x11_unix_by_default;
395 /* choose default X transport in the absence of a specified one */
396SockAddr platform_get_x11_unix_address(const char *path, int displaynum);
397 /* make up a SockAddr naming the address for displaynum */
398char *platform_get_x_display(void);
399 /* allocated local X display string, if any */
400/* Callbacks in x11.c usable _by_ platform X11 functions */
401/*
402 * This function does the job of platform_get_x11_auth, provided
403 * it is told where to find a normally formatted .Xauthority file:
404 * it opens that file, parses it to find an auth record which
405 * matches the display details in "display", and fills in the
406 * localauth fields.
407 *
408 * It is expected that most implementations of
409 * platform_get_x11_auth() will work by finding their system's
410 * .Xauthority file, adjusting the display details if necessary
411 * for local oddities like Unix-domain socket transport, and
412 * calling this function to do the rest of the work.
413 */
414void x11_get_auth_from_authfile(struct X11Display *display,
415 const char *authfilename);
416
417Bignum copybn(Bignum b);
418Bignum bn_power_2(int n);
419void bn_restore_invariant(Bignum b);
420Bignum bignum_from_long(unsigned long n);
421void freebn(Bignum b);
422Bignum modpow(Bignum base, Bignum exp, Bignum mod);
423Bignum modmul(Bignum a, Bignum b, Bignum mod);
424void decbn(Bignum n);
425extern Bignum Zero, One;
426Bignum bignum_from_bytes(const unsigned char *data, int nbytes);
427int ssh1_read_bignum(const unsigned char *data, int len, Bignum * result);
428int bignum_bitcount(Bignum bn);
429int ssh1_bignum_length(Bignum bn);
430int ssh2_bignum_length(Bignum bn);
431int bignum_byte(Bignum bn, int i);
432int bignum_bit(Bignum bn, int i);
433void bignum_set_bit(Bignum bn, int i, int value);
434int ssh1_write_bignum(void *data, Bignum bn);
435Bignum biggcd(Bignum a, Bignum b);
436unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
437Bignum bignum_add_long(Bignum number, unsigned long addend);
438Bignum bigmul(Bignum a, Bignum b);
439Bignum bigmuladd(Bignum a, Bignum b, Bignum addend);
440Bignum bigdiv(Bignum a, Bignum b);
441Bignum bigmod(Bignum a, Bignum b);
442Bignum modinv(Bignum number, Bignum modulus);
443Bignum bignum_bitmask(Bignum number);
444Bignum bignum_rshift(Bignum number, int shift);
445int bignum_cmp(Bignum a, Bignum b);
446char *bignum_decimal(Bignum x);
447
448#ifdef DEBUG
449void diagbn(char *prefix, Bignum md);
450#endif
451
452void *dh_setup_group(const struct ssh_kex *kex);
453void *dh_setup_gex(Bignum pval, Bignum gval);
454void dh_cleanup(void *);
455Bignum dh_create_e(void *, int nbits);
456Bignum dh_find_K(void *, Bignum f);
457
458int loadrsakey(const Filename *filename, struct RSAKey *key,
459 char *passphrase, const char **errorstr);
460int rsakey_encrypted(const Filename *filename, char **comment);
461int rsakey_pubblob(const Filename *filename, void **blob, int *bloblen,
462 char **commentptr, const char **errorstr);
463
464int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase);
465
466extern int base64_decode_atom(char *atom, unsigned char *out);
467extern int base64_lines(int datalen);
468extern void base64_encode_atom(unsigned char *data, int n, char *out);
469extern void base64_encode(FILE *fp, unsigned char *data, int datalen, int cpl);
470
471/* ssh2_load_userkey can return this as an error */
472extern struct ssh2_userkey ssh2_wrong_passphrase;
473#define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
474
475int ssh2_userkey_encrypted(const Filename *filename, char **comment);
476struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
477 char *passphrase, const char **errorstr);
478unsigned char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
479 int *pub_blob_len, char **commentptr,
480 const char **errorstr);
481int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
482 char *passphrase);
483const struct ssh_signkey *find_pubkey_alg(const char *name);
484
485enum {
486 SSH_KEYTYPE_UNOPENABLE,
487 SSH_KEYTYPE_UNKNOWN,
488 SSH_KEYTYPE_SSH1, SSH_KEYTYPE_SSH2,
489 SSH_KEYTYPE_OPENSSH, SSH_KEYTYPE_SSHCOM
490};
491int key_type(const Filename *filename);
492char *key_type_to_str(int type);
493
494int import_possible(int type);
495int import_target_type(int type);
496int import_encrypted(const Filename *filename, int type, char **comment);
497int import_ssh1(const Filename *filename, int type,
498 struct RSAKey *key, char *passphrase, const char **errmsg_p);
499struct ssh2_userkey *import_ssh2(const Filename *filename, int type,
500 char *passphrase, const char **errmsg_p);
501int export_ssh1(const Filename *filename, int type,
502 struct RSAKey *key, char *passphrase);
503int export_ssh2(const Filename *filename, int type,
504 struct ssh2_userkey *key, char *passphrase);
505
506void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
507void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
508void des3_decrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
509 unsigned char *blk, int len);
510void des3_encrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
511 unsigned char *blk, int len);
512void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk,
513 int len);
514void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk,
515 int len);
516
517void des_encrypt_xdmauth(unsigned char *key, unsigned char *blk, int len);
518void des_decrypt_xdmauth(unsigned char *key, unsigned char *blk, int len);
519
520/*
521 * For progress updates in the key generation utility.
522 */
523#define PROGFN_INITIALISE 1
524#define PROGFN_LIN_PHASE 2
525#define PROGFN_EXP_PHASE 3
526#define PROGFN_PHASE_EXTENT 4
527#define PROGFN_READY 5
528#define PROGFN_PROGRESS 6
529typedef void (*progfn_t) (void *param, int action, int phase, int progress);
530
531int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
532 void *pfnparam);
533int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
534 void *pfnparam);
535Bignum primegen(int bits, int modulus, int residue, Bignum factor,
536 int phase, progfn_t pfn, void *pfnparam);
537
538
539/*
540 * zlib compression.
541 */
542void *zlib_compress_init(void);
543void zlib_compress_cleanup(void *);
544void *zlib_decompress_init(void);
545void zlib_decompress_cleanup(void *);
546int zlib_compress_block(void *, unsigned char *block, int len,
547 unsigned char **outblock, int *outlen);
548int zlib_decompress_block(void *, unsigned char *block, int len,
549 unsigned char **outblock, int *outlen);
550
551/*
552 * SSH-1 agent messages.
553 */
554#define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
555#define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
556#define SSH1_AGENTC_RSA_CHALLENGE 3
557#define SSH1_AGENT_RSA_RESPONSE 4
558#define SSH1_AGENTC_ADD_RSA_IDENTITY 7
559#define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
560#define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
561
562/*
563 * Messages common to SSH-1 and OpenSSH's SSH-2.
564 */
565#define SSH_AGENT_FAILURE 5
566#define SSH_AGENT_SUCCESS 6
567
568/*
569 * OpenSSH's SSH-2 agent messages.
570 */
571#define SSH2_AGENTC_REQUEST_IDENTITIES 11
572#define SSH2_AGENT_IDENTITIES_ANSWER 12
573#define SSH2_AGENTC_SIGN_REQUEST 13
574#define SSH2_AGENT_SIGN_RESPONSE 14
575#define SSH2_AGENTC_ADD_IDENTITY 17
576#define SSH2_AGENTC_REMOVE_IDENTITY 18
577#define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
578
579/*
580 * Need this to warn about support for the original SSH-2 keyfile
581 * format.
582 */
583void old_keyfile_warning(void);