Fix the nasty flashing-light-grey-on-resize problem, after MCV
[u/mdw/putty] / ssh.h
CommitLineData
37508af4 1#include <string.h>
2
dcbde236 3#include "puttymem.h"
d74d141c 4#include "network.h"
5c72ca61 5#include "int64.h"
dcbde236 6
2871113a 7struct ssh_channel;
8
5471d09a 9extern void sshfwd_close(struct ssh_channel *c);
10extern int sshfwd_write(struct ssh_channel *c, char *, int);
11extern void sshfwd_unthrottle(struct ssh_channel *c, int bufsize);
12
e5574168 13/*
14 * Useful thing.
15 */
16#ifndef lenof
17#define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
18#endif
19
bea1ef5f 20#define SSH_CIPHER_IDEA 1
9697bfd2 21#define SSH_CIPHER_DES 2
bea1ef5f 22#define SSH_CIPHER_3DES 3
23#define SSH_CIPHER_BLOWFISH 6
24
e5574168 25#ifdef MSCRYPTOAPI
26#define APIEXTRA 8
27#else
28#define APIEXTRA 0
29#endif
30
3709bfe9 31#ifndef BIGNUM_INTERNAL
32typedef void *Bignum;
33#endif
7cca0d81 34
374330e2 35struct RSAKey {
36 int bits;
37 int bytes;
8f203108 38#ifdef MSCRYPTOAPI
39 unsigned long exponent;
40 unsigned char *modulus;
41#else
7cca0d81 42 Bignum modulus;
43 Bignum exponent;
44 Bignum private_exponent;
9400cf6f 45 Bignum p;
46 Bignum q;
47 Bignum iqmp;
65a22376 48#endif
49 char *comment;
9400cf6f 50};
51
5c72ca61 52struct dss_key {
53 Bignum p, q, g, y, x;
54};
55
374330e2 56int makekey(unsigned char *data, struct RSAKey *result,
7cca0d81 57 unsigned char **keystr, int order);
58int makeprivate(unsigned char *data, struct RSAKey *result);
374330e2 59void rsaencrypt(unsigned char *data, int length, struct RSAKey *key);
7cca0d81 60Bignum rsadecrypt(Bignum input, struct RSAKey *key);
61void rsasign(unsigned char *data, int length, struct RSAKey *key);
62void rsasanitise(struct RSAKey *key);
374330e2 63int rsastr_len(struct RSAKey *key);
64void rsastr_fmt(char *str, struct RSAKey *key);
1c2a93c4 65void rsa_fingerprint(char *str, int len, struct RSAKey *key);
014970c8 66int rsa_verify(struct RSAKey *key);
3f2d010c 67unsigned char *rsa_public_blob(struct RSAKey *key, int *len);
68int rsa_public_blob_len(void *data);
5c58ad2d 69void freersakey(struct RSAKey *key);
374330e2 70
71typedef unsigned int word32;
72typedef unsigned int uint32;
73
8160fbfc 74unsigned long crc32(const void *s, size_t len);
9a3a93a5 75unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len);
76
77/* SSH CRC compensation attack detector */
78int detect_attack(unsigned char *buf, uint32 len, unsigned char *IV);
374330e2 79
48672163 80typedef struct {
81 uint32 h[4];
82} MD5_Core_State;
83
374330e2 84struct MD5Context {
8f203108 85#ifdef MSCRYPTOAPI
86 unsigned long hHash;
87#else
48672163 88 MD5_Core_State core;
bb093ca7 89 unsigned char block[64];
48672163 90 int blkused;
91 uint32 lenhi, lenlo;
8f203108 92#endif
374330e2 93};
94
95void MD5Init(struct MD5Context *context);
96void MD5Update(struct MD5Context *context, unsigned char const *buf,
32874aea 97 unsigned len);
374330e2 98void MD5Final(unsigned char digest[16], struct MD5Context *context);
99
e5574168 100typedef struct {
101 uint32 h[5];
102 unsigned char block[64];
103 int blkused;
104 uint32 lenhi, lenlo;
105} SHA_State;
32874aea 106void SHA_Init(SHA_State * s);
107void SHA_Bytes(SHA_State * s, void *p, int len);
108void SHA_Final(SHA_State * s, unsigned char *output);
7cca0d81 109void SHA_Simple(void *p, int len, unsigned char *output);
e5574168 110
5c72ca61 111void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
112 unsigned char *output);
113
114typedef struct {
115 uint64 h[8];
116 unsigned char block[128];
117 int blkused;
118 uint32 len[4];
119} SHA512_State;
120void SHA512_Init(SHA512_State * s);
121void SHA512_Bytes(SHA512_State * s, const void *p, int len);
122void SHA512_Final(SHA512_State * s, unsigned char *output);
123void SHA512_Simple(const void *p, int len, unsigned char *output);
124
374330e2 125struct ssh_cipher {
32874aea 126 void (*sesskey) (unsigned char *key); /* for ssh 1 */
127 void (*encrypt) (unsigned char *blk, int len);
128 void (*decrypt) (unsigned char *blk, int len);
0a3f1d48 129 int blksize;
130};
131
132struct ssh2_cipher {
32874aea 133 void (*setcsiv) (unsigned char *key); /* for ssh 2 */
134 void (*setcskey) (unsigned char *key); /* for ssh 2 */
135 void (*setsciv) (unsigned char *key); /* for ssh 2 */
136 void (*setsckey) (unsigned char *key); /* for ssh 2 */
137 void (*encrypt) (unsigned char *blk, int len);
138 void (*decrypt) (unsigned char *blk, int len);
e5574168 139 char *name;
140 int blksize;
d2a0e0be 141 int keylen;
e5574168 142};
143
0a3f1d48 144struct ssh2_ciphers {
145 int nciphers;
65a22376 146 const struct ssh2_cipher *const *list;
0a3f1d48 147};
148
e5574168 149struct ssh_mac {
32874aea 150 void (*setcskey) (unsigned char *key);
151 void (*setsckey) (unsigned char *key);
152 void (*generate) (unsigned char *blk, int len, unsigned long seq);
153 int (*verify) (unsigned char *blk, int len, unsigned long seq);
e5574168 154 char *name;
155 int len;
156};
157
158struct ssh_kex {
7cca0d81 159 /*
160 * Plugging in another KEX algorithm requires structural chaos,
161 * so it's hard to abstract them into nice little structures
162 * like this. Hence, for the moment, this is just a
163 * placeholder. I claim justification in the fact that OpenSSH
164 * does this too :-)
165 */
e5574168 166 char *name;
167};
168
e055a386 169struct ssh_signkey {
32874aea 170 void *(*newkey) (char *data, int len);
171 void (*freekey) (void *key);
172 char *(*fmtkey) (void *key);
173 unsigned char *(*public_blob) (void *key, int *len);
174 unsigned char *(*private_blob) (void *key, int *len);
175 void *(*createkey) (unsigned char *pub_blob, int pub_len,
176 unsigned char *priv_blob, int priv_len);
177 void *(*openssh_createkey) (unsigned char **blob, int *len);
178 int (*openssh_fmtkey) (void *key, unsigned char *blob, int len);
179 char *(*fingerprint) (void *key);
180 int (*verifysig) (void *key, char *sig, int siglen,
181 char *data, int datalen);
182 unsigned char *(*sign) (void *key, char *data, int datalen,
183 int *siglen);
e5574168 184 char *name;
32874aea 185 char *keytype; /* for host key cache */
e5574168 186};
187
188struct ssh_compress {
189 char *name;
32874aea 190 void (*compress_init) (void);
191 int (*compress) (unsigned char *block, int len,
192 unsigned char **outblock, int *outlen);
193 void (*decompress_init) (void);
194 int (*decompress) (unsigned char *block, int len,
195 unsigned char **outblock, int *outlen);
196 int (*disable_compression) (void);
374330e2 197};
198
65a22376 199struct ssh2_userkey {
200 const struct ssh_signkey *alg; /* the key algorithm */
201 void *data; /* the key data */
202 char *comment; /* the key comment */
203};
204
205extern const struct ssh_cipher ssh_3des;
206extern const struct ssh_cipher ssh_des;
207extern const struct ssh_cipher ssh_blowfish_ssh1;
208extern const struct ssh2_ciphers ssh2_3des;
0d7c43a6 209extern const struct ssh2_ciphers ssh2_des;
65a22376 210extern const struct ssh2_ciphers ssh2_aes;
211extern const struct ssh2_ciphers ssh2_blowfish;
212extern const struct ssh_kex ssh_diffiehellman;
213extern const struct ssh_kex ssh_diffiehellman_gex;
214extern const struct ssh_signkey ssh_dss;
215extern const struct ssh_signkey ssh_rsa;
216extern const struct ssh_mac ssh_md5;
217extern const struct ssh_mac ssh_sha1;
218extern const struct ssh_mac ssh_sha1_buggy;
219
900a4ee6 220/*
221 * PuTTY version number formatted as an SSH version string.
222 */
223extern char sshver[];
224
fd5e5847 225/*
226 * Gross hack: pscp will try to start SFTP but fall back to scp1 if
227 * that fails. This variable is the means by which scp.c can reach
228 * into the SSH code and find out which one it got.
229 */
51470298 230extern int ssh_fallback_cmd(void *handle);
fd5e5847 231
8f203108 232#ifndef MSCRYPTOAPI
32874aea 233void SHATransform(word32 * digest, word32 * data);
8f203108 234#endif
374330e2 235
236int random_byte(void);
237void random_add_noise(void *noise, int length);
6e522441 238void random_add_heavynoise(void *noise, int length);
c5e9c988 239
32874aea 240void logevent(char *);
51470298 241
242/* Allocate and register a new channel for port forwarding */
243void *new_sock_channel(void *handle, Socket s);
244void ssh_send_port_open(void *handle, void *channel,
245 char *hostname, int port, char *org);
e5574168 246
7cca0d81 247Bignum copybn(Bignum b);
3709bfe9 248Bignum bn_power_2(int n);
249void bn_restore_invariant(Bignum b);
5c72ca61 250Bignum bignum_from_long(unsigned long n);
e5574168 251void freebn(Bignum b);
59600f67 252Bignum modpow(Bignum base, Bignum exp, Bignum mod);
253Bignum modmul(Bignum a, Bignum b, Bignum mod);
7cca0d81 254void decbn(Bignum n);
255extern Bignum Zero, One;
3709bfe9 256Bignum bignum_from_bytes(unsigned char *data, int nbytes);
32874aea 257int ssh1_read_bignum(unsigned char *data, Bignum * result);
ddecd643 258int bignum_bitcount(Bignum bn);
5c58ad2d 259int ssh1_bignum_length(Bignum bn);
260f3dec 260int ssh2_bignum_length(Bignum bn);
5c58ad2d 261int bignum_byte(Bignum bn, int i);
9400cf6f 262int bignum_bit(Bignum bn, int i);
263void bignum_set_bit(Bignum bn, int i, int value);
5c58ad2d 264int ssh1_write_bignum(void *data, Bignum bn);
9400cf6f 265Bignum biggcd(Bignum a, Bignum b);
266unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
267Bignum bignum_add_long(Bignum number, unsigned long addend);
268Bignum bigmul(Bignum a, Bignum b);
5c72ca61 269Bignum bigmuladd(Bignum a, Bignum b, Bignum addend);
270Bignum bigdiv(Bignum a, Bignum b);
271Bignum bigmod(Bignum a, Bignum b);
9400cf6f 272Bignum modinv(Bignum number, Bignum modulus);
3709bfe9 273Bignum bignum_bitmask(Bignum number);
9400cf6f 274Bignum bignum_rshift(Bignum number, int shift);
8c3cd914 275int bignum_cmp(Bignum a, Bignum b);
6e522441 276char *bignum_decimal(Bignum x);
e5574168 277
3709bfe9 278void dh_setup_group1(void);
a92dd380 279void dh_setup_group(Bignum pval, Bignum gval);
3709bfe9 280void dh_cleanup(void);
7bd5a860 281Bignum dh_create_e(int nbits);
e5574168 282Bignum dh_find_K(Bignum f);
7cca0d81 283
65a22376 284int loadrsakey(char *filename, struct RSAKey *key, char *passphrase);
a52f067e 285int rsakey_encrypted(char *filename, char **comment);
3f2d010c 286int rsakey_pubblob(char *filename, void **blob, int *bloblen);
7cca0d81 287
65a22376 288int saversakey(char *filename, struct RSAKey *key, char *passphrase);
289
290void base64_encode_atom(unsigned char *data, int n, char *out);
291
292/* ssh2_load_userkey can return this as an error */
293extern struct ssh2_userkey ssh2_wrong_passphrase;
294#define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
295
296int ssh2_userkey_encrypted(char *filename, char **comment);
297struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase);
32874aea 298char *ssh2_userkey_loadpub(char *filename, char **algorithm,
299 int *pub_blob_len);
300int ssh2_save_userkey(char *filename, struct ssh2_userkey *key,
301 char *passphrase);
65a22376 302
231ee168 303enum {
304 SSH_KEYTYPE_UNOPENABLE,
305 SSH_KEYTYPE_UNKNOWN,
306 SSH_KEYTYPE_SSH1, SSH_KEYTYPE_SSH2,
307 SSH_KEYTYPE_OPENSSH, SSH_KEYTYPE_SSHCOM
308};
309int key_type(char *filename);
310char *key_type_to_str(int type);
6e522441 311
9dda6459 312int import_possible(int type);
313int import_target_type(int type);
314int import_encrypted(char *filename, int type, char **comment);
315int import_ssh1(char *filename, int type, struct RSAKey *key,char *passphrase);
316struct ssh2_userkey *import_ssh2(char *filename, int type, char *passphrase);
4801c01c 317int export_ssh1(char *filename, int type, struct RSAKey *key,char *passphrase);
318int export_ssh2(char *filename, int type,
319 struct ssh2_userkey *key, char *passphrase);
9dda6459 320
65a22376 321void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
322void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
9dda6459 323void des3_decrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
324 unsigned char *blk, int len);
325void des3_encrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
326 unsigned char *blk, int len);
32874aea 327void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk,
328 int len);
329void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk,
330 int len);
9400cf6f 331
332/*
333 * For progress updates in the key generation utility.
334 */
7c7f6893 335#define PROGFN_INITIALISE 1
336#define PROGFN_LIN_PHASE 2
337#define PROGFN_EXP_PHASE 3
338#define PROGFN_PHASE_EXTENT 4
339#define PROGFN_READY 5
340#define PROGFN_PROGRESS 6
5c72ca61 341typedef void (*progfn_t) (void *param, int action, int phase, int progress);
9400cf6f 342
32874aea 343int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
344 void *pfnparam);
5c72ca61 345int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
346 void *pfnparam);
347Bignum primegen(int bits, int modulus, int residue, Bignum factor,
348 int phase, progfn_t pfn, void *pfnparam);
4ba9b64b 349
d74d141c 350
4ba9b64b 351/*
352 * zlib compression.
353 */
354void zlib_compress_init(void);
355void zlib_decompress_init(void);
356int zlib_compress_block(unsigned char *block, int len,
357 unsigned char **outblock, int *outlen);
358int zlib_decompress_block(unsigned char *block, int len,
359 unsigned char **outblock, int *outlen);
1983e559 360
361/*
362 * SSH1 agent messages.
363 */
364#define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
365#define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
366#define SSH1_AGENTC_RSA_CHALLENGE 3
367#define SSH1_AGENT_RSA_RESPONSE 4
368#define SSH1_AGENTC_ADD_RSA_IDENTITY 7
369#define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
32874aea 370#define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
1983e559 371
372/*
373 * Messages common to SSH1 and OpenSSH's SSH2.
374 */
375#define SSH_AGENT_FAILURE 5
376#define SSH_AGENT_SUCCESS 6
377
378/*
379 * OpenSSH's SSH2 agent messages.
380 */
381#define SSH2_AGENTC_REQUEST_IDENTITIES 11
382#define SSH2_AGENT_IDENTITIES_ANSWER 12
383#define SSH2_AGENTC_SIGN_REQUEST 13
384#define SSH2_AGENT_SIGN_RESPONSE 14
385#define SSH2_AGENTC_ADD_IDENTITY 17
386#define SSH2_AGENTC_REMOVE_IDENTITY 18
387#define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
7bedb13c 388
389/*
390 * Need this to warn about support for the original SSH2 keyfile
391 * format.
392 */
393void old_keyfile_warning(void);