Turned the old `Telnet Command' System-submenu into a more general
[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, struct RSAKey *result,
59 unsigned char **keystr, int order);
60int makeprivate(unsigned char *data, struct RSAKey *result);
61void 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);
71void freersakey(struct RSAKey *key);
72
73typedef unsigned int word32;
74typedef unsigned int uint32;
75
76unsigned long crc32(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);
104
105typedef struct {
106 uint32 h[5];
107 unsigned char block[64];
108 int blkused;
109 uint32 lenhi, lenlo;
110} SHA_State;
111void SHA_Init(SHA_State * s);
112void SHA_Bytes(SHA_State * s, void *p, int len);
113void SHA_Final(SHA_State * s, unsigned char *output);
114void SHA_Simple(void *p, int len, unsigned char *output);
115
116void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
117 unsigned char *output);
118
119typedef struct {
120 uint64 h[8];
121 unsigned char block[128];
122 int blkused;
123 uint32 len[4];
124} SHA512_State;
125void SHA512_Init(SHA512_State * s);
126void SHA512_Bytes(SHA512_State * s, const void *p, int len);
127void SHA512_Final(SHA512_State * s, unsigned char *output);
128void SHA512_Simple(const void *p, int len, unsigned char *output);
129
130struct ssh_cipher {
131 void *(*make_context)(void);
132 void (*free_context)(void *);
133 void (*sesskey) (void *, unsigned char *key); /* for ssh 1 */
134 void (*encrypt) (void *, unsigned char *blk, int len);
135 void (*decrypt) (void *, unsigned char *blk, int len);
136 int blksize;
137 char *text_name;
138};
139
140struct ssh2_cipher {
141 void *(*make_context)(void);
142 void (*free_context)(void *);
143 void (*setiv) (void *, unsigned char *key); /* for ssh 2 */
144 void (*setkey) (void *, unsigned char *key);/* for ssh 2 */
145 void (*encrypt) (void *, unsigned char *blk, int len);
146 void (*decrypt) (void *, unsigned char *blk, int len);
147 char *name;
148 int blksize;
149 int keylen;
150 char *text_name;
151};
152
153struct ssh2_ciphers {
154 int nciphers;
155 const struct ssh2_cipher *const *list;
156};
157
158struct ssh_mac {
159 void *(*make_context)(void);
160 void (*free_context)(void *);
161 void (*setkey) (void *, unsigned char *key);
162 void (*generate) (void *, unsigned char *blk, int len, unsigned long seq);
163 int (*verify) (void *, unsigned char *blk, int len, unsigned long seq);
164 char *name;
165 int len;
166};
167
168struct ssh_kex {
169 /*
170 * Plugging in another KEX algorithm requires structural chaos,
171 * so it's hard to abstract them into nice little structures
172 * like this. Hence, for the moment, this is just a
173 * placeholder. I claim justification in the fact that OpenSSH
174 * does this too :-)
175 */
176 char *name;
177};
178
179struct ssh_signkey {
180 void *(*newkey) (char *data, int len);
181 void (*freekey) (void *key);
182 char *(*fmtkey) (void *key);
183 unsigned char *(*public_blob) (void *key, int *len);
184 unsigned char *(*private_blob) (void *key, int *len);
185 void *(*createkey) (unsigned char *pub_blob, int pub_len,
186 unsigned char *priv_blob, int priv_len);
187 void *(*openssh_createkey) (unsigned char **blob, int *len);
188 int (*openssh_fmtkey) (void *key, unsigned char *blob, int len);
189 char *(*fingerprint) (void *key);
190 int (*verifysig) (void *key, char *sig, int siglen,
191 char *data, int datalen);
192 unsigned char *(*sign) (void *key, char *data, int datalen,
193 int *siglen);
194 char *name;
195 char *keytype; /* for host key cache */
196};
197
198struct ssh_compress {
199 char *name;
200 void *(*compress_init) (void);
201 void (*compress_cleanup) (void *);
202 int (*compress) (void *, unsigned char *block, int len,
203 unsigned char **outblock, int *outlen);
204 void *(*decompress_init) (void);
205 void (*decompress_cleanup) (void *);
206 int (*decompress) (void *, unsigned char *block, int len,
207 unsigned char **outblock, int *outlen);
208 int (*disable_compression) (void *);
209 char *text_name;
210};
211
212struct ssh2_userkey {
213 const struct ssh_signkey *alg; /* the key algorithm */
214 void *data; /* the key data */
215 char *comment; /* the key comment */
216};
217
218extern const struct ssh_cipher ssh_3des;
219extern const struct ssh_cipher ssh_des;
220extern const struct ssh_cipher ssh_blowfish_ssh1;
221extern const struct ssh2_ciphers ssh2_3des;
222extern const struct ssh2_ciphers ssh2_des;
223extern const struct ssh2_ciphers ssh2_aes;
224extern const struct ssh2_ciphers ssh2_blowfish;
225extern const struct ssh_kex ssh_diffiehellman;
226extern const struct ssh_kex ssh_diffiehellman_gex;
227extern const struct ssh_signkey ssh_dss;
228extern const struct ssh_signkey ssh_rsa;
229extern const struct ssh_mac ssh_md5;
230extern const struct ssh_mac ssh_sha1;
231extern const struct ssh_mac ssh_sha1_buggy;
232
233/*
234 * PuTTY version number formatted as an SSH version string.
235 */
236extern char sshver[];
237
238/*
239 * Gross hack: pscp will try to start SFTP but fall back to scp1 if
240 * that fails. This variable is the means by which scp.c can reach
241 * into the SSH code and find out which one it got.
242 */
243extern int ssh_fallback_cmd(void *handle);
244
245#ifndef MSCRYPTOAPI
246void SHATransform(word32 * digest, word32 * data);
247#endif
248
249int random_byte(void);
250void random_add_noise(void *noise, int length);
251void random_add_heavynoise(void *noise, int length);
252
253void logevent(void *, char *);
254
255/* Allocate and register a new channel for port forwarding */
256void *new_sock_channel(void *handle, Socket s);
257void ssh_send_port_open(void *channel, char *hostname, int port, char *org);
258
259/* Exports from portfwd.c */
260extern char *pfd_newconnect(Socket * s, char *hostname, int port, void *c,
261 const Config *cfg);
262extern char *pfd_addforward(char *desthost, int destport, char *srcaddr,
263 int port, void *backhandle, const Config *cfg);
264extern void pfd_close(Socket s);
265extern int pfd_send(Socket s, char *data, int len);
266extern void pfd_confirm(Socket s);
267extern void pfd_unthrottle(Socket s);
268extern void pfd_override_throttle(Socket s, int enable);
269
270/* Exports from x11fwd.c */
271extern char *x11_init(Socket *, char *, void *, void *, const char *, int,
272 const Config *);
273extern void x11_close(Socket);
274extern int x11_send(Socket, char *, int);
275extern void *x11_invent_auth(char *, int, char *, int, int);
276extern void x11_free_auth(void *);
277extern void x11_unthrottle(Socket s);
278extern void x11_override_throttle(Socket s, int enable);
279extern int x11_get_screen_number(char *display);
280void x11_get_real_auth(void *authv, char *display);
281
282/* Platfdorm-dependent X11 function */
283extern void platform_get_x11_auth(char *display, int *proto,
284 unsigned char *data, int *datalen);
285
286Bignum copybn(Bignum b);
287Bignum bn_power_2(int n);
288void bn_restore_invariant(Bignum b);
289Bignum bignum_from_long(unsigned long n);
290void freebn(Bignum b);
291Bignum modpow(Bignum base, Bignum exp, Bignum mod);
292Bignum modmul(Bignum a, Bignum b, Bignum mod);
293void decbn(Bignum n);
294extern Bignum Zero, One;
295Bignum bignum_from_bytes(const unsigned char *data, int nbytes);
296int ssh1_read_bignum(const unsigned char *data, Bignum * result);
297int bignum_bitcount(Bignum bn);
298int ssh1_bignum_length(Bignum bn);
299int ssh2_bignum_length(Bignum bn);
300int bignum_byte(Bignum bn, int i);
301int bignum_bit(Bignum bn, int i);
302void bignum_set_bit(Bignum bn, int i, int value);
303int ssh1_write_bignum(void *data, Bignum bn);
304Bignum biggcd(Bignum a, Bignum b);
305unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
306Bignum bignum_add_long(Bignum number, unsigned long addend);
307Bignum bigmul(Bignum a, Bignum b);
308Bignum bigmuladd(Bignum a, Bignum b, Bignum addend);
309Bignum bigdiv(Bignum a, Bignum b);
310Bignum bigmod(Bignum a, Bignum b);
311Bignum modinv(Bignum number, Bignum modulus);
312Bignum bignum_bitmask(Bignum number);
313Bignum bignum_rshift(Bignum number, int shift);
314int bignum_cmp(Bignum a, Bignum b);
315char *bignum_decimal(Bignum x);
316
317void *dh_setup_group1(void);
318void *dh_setup_group(Bignum pval, Bignum gval);
319void dh_cleanup(void *);
320Bignum dh_create_e(void *, int nbits);
321Bignum dh_find_K(void *, Bignum f);
322
323int loadrsakey(const Filename *filename, struct RSAKey *key,
324 char *passphrase);
325int rsakey_encrypted(const Filename *filename, char **comment);
326int rsakey_pubblob(const Filename *filename, void **blob, int *bloblen);
327
328int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase);
329
330extern int base64_decode_atom(char *atom, unsigned char *out);
331extern int base64_lines(int datalen);
332extern void base64_encode_atom(unsigned char *data, int n, char *out);
333extern void base64_encode(FILE *fp, unsigned char *data, int datalen, int cpl);
334
335/* ssh2_load_userkey can return this as an error */
336extern struct ssh2_userkey ssh2_wrong_passphrase;
337#define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
338
339int ssh2_userkey_encrypted(const Filename *filename, char **comment);
340struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
341 char *passphrase);
342char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
343 int *pub_blob_len);
344int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
345 char *passphrase);
346
347enum {
348 SSH_KEYTYPE_UNOPENABLE,
349 SSH_KEYTYPE_UNKNOWN,
350 SSH_KEYTYPE_SSH1, SSH_KEYTYPE_SSH2,
351 SSH_KEYTYPE_OPENSSH, SSH_KEYTYPE_SSHCOM
352};
353int key_type(const Filename *filename);
354char *key_type_to_str(int type);
355
356int import_possible(int type);
357int import_target_type(int type);
358int import_encrypted(const Filename *filename, int type, char **comment);
359int import_ssh1(const Filename *filename, int type,
360 struct RSAKey *key, char *passphrase);
361struct ssh2_userkey *import_ssh2(const Filename *filename, int type,
362 char *passphrase);
363int export_ssh1(const Filename *filename, int type,
364 struct RSAKey *key, char *passphrase);
365int export_ssh2(const Filename *filename, int type,
366 struct ssh2_userkey *key, char *passphrase);
367
368void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
369void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
370void des3_decrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
371 unsigned char *blk, int len);
372void des3_encrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
373 unsigned char *blk, int len);
374void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk,
375 int len);
376void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk,
377 int len);
378
379void des_encrypt_xdmauth(unsigned char *key, unsigned char *blk, int len);
380void des_decrypt_xdmauth(unsigned char *key, unsigned char *blk, int len);
381
382/*
383 * For progress updates in the key generation utility.
384 */
385#define PROGFN_INITIALISE 1
386#define PROGFN_LIN_PHASE 2
387#define PROGFN_EXP_PHASE 3
388#define PROGFN_PHASE_EXTENT 4
389#define PROGFN_READY 5
390#define PROGFN_PROGRESS 6
391typedef void (*progfn_t) (void *param, int action, int phase, int progress);
392
393int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
394 void *pfnparam);
395int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
396 void *pfnparam);
397Bignum primegen(int bits, int modulus, int residue, Bignum factor,
398 int phase, progfn_t pfn, void *pfnparam);
399
400
401/*
402 * zlib compression.
403 */
404void *zlib_compress_init(void);
405void zlib_compress_cleanup(void *);
406void *zlib_decompress_init(void);
407void zlib_decompress_cleanup(void *);
408int zlib_compress_block(void *, unsigned char *block, int len,
409 unsigned char **outblock, int *outlen);
410int zlib_decompress_block(void *, unsigned char *block, int len,
411 unsigned char **outblock, int *outlen);
412
413/*
414 * SSH1 agent messages.
415 */
416#define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
417#define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
418#define SSH1_AGENTC_RSA_CHALLENGE 3
419#define SSH1_AGENT_RSA_RESPONSE 4
420#define SSH1_AGENTC_ADD_RSA_IDENTITY 7
421#define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
422#define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
423
424/*
425 * Messages common to SSH1 and OpenSSH's SSH2.
426 */
427#define SSH_AGENT_FAILURE 5
428#define SSH_AGENT_SUCCESS 6
429
430/*
431 * OpenSSH's SSH2 agent messages.
432 */
433#define SSH2_AGENTC_REQUEST_IDENTITIES 11
434#define SSH2_AGENT_IDENTITIES_ANSWER 12
435#define SSH2_AGENTC_SIGN_REQUEST 13
436#define SSH2_AGENT_SIGN_RESPONSE 14
437#define SSH2_AGENTC_ADD_IDENTITY 17
438#define SSH2_AGENTC_REMOVE_IDENTITY 18
439#define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
440
441/*
442 * Need this to warn about support for the original SSH2 keyfile
443 * format.
444 */
445void old_keyfile_warning(void);