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