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