Oops - the help for the new Telnet NL option wasn't quite there.
[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);
5c58ad2d 67void freersakey(struct RSAKey *key);
374330e2 68
69typedef unsigned int word32;
70typedef unsigned int uint32;
71
8160fbfc 72unsigned long crc32(const void *s, size_t len);
374330e2 73
48672163 74typedef struct {
75 uint32 h[4];
76} MD5_Core_State;
77
374330e2 78struct MD5Context {
8f203108 79#ifdef MSCRYPTOAPI
80 unsigned long hHash;
81#else
48672163 82 MD5_Core_State core;
bb093ca7 83 unsigned char block[64];
48672163 84 int blkused;
85 uint32 lenhi, lenlo;
8f203108 86#endif
374330e2 87};
88
89void MD5Init(struct MD5Context *context);
90void MD5Update(struct MD5Context *context, unsigned char const *buf,
32874aea 91 unsigned len);
374330e2 92void MD5Final(unsigned char digest[16], struct MD5Context *context);
93
e5574168 94typedef struct {
95 uint32 h[5];
96 unsigned char block[64];
97 int blkused;
98 uint32 lenhi, lenlo;
99} SHA_State;
32874aea 100void SHA_Init(SHA_State * s);
101void SHA_Bytes(SHA_State * s, void *p, int len);
102void SHA_Final(SHA_State * s, unsigned char *output);
7cca0d81 103void SHA_Simple(void *p, int len, unsigned char *output);
e5574168 104
5c72ca61 105void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
106 unsigned char *output);
107
108typedef struct {
109 uint64 h[8];
110 unsigned char block[128];
111 int blkused;
112 uint32 len[4];
113} SHA512_State;
114void SHA512_Init(SHA512_State * s);
115void SHA512_Bytes(SHA512_State * s, const void *p, int len);
116void SHA512_Final(SHA512_State * s, unsigned char *output);
117void SHA512_Simple(const void *p, int len, unsigned char *output);
118
374330e2 119struct ssh_cipher {
32874aea 120 void (*sesskey) (unsigned char *key); /* for ssh 1 */
121 void (*encrypt) (unsigned char *blk, int len);
122 void (*decrypt) (unsigned char *blk, int len);
0a3f1d48 123 int blksize;
124};
125
126struct ssh2_cipher {
32874aea 127 void (*setcsiv) (unsigned char *key); /* for ssh 2 */
128 void (*setcskey) (unsigned char *key); /* for ssh 2 */
129 void (*setsciv) (unsigned char *key); /* for ssh 2 */
130 void (*setsckey) (unsigned char *key); /* for ssh 2 */
131 void (*encrypt) (unsigned char *blk, int len);
132 void (*decrypt) (unsigned char *blk, int len);
e5574168 133 char *name;
134 int blksize;
d2a0e0be 135 int keylen;
e5574168 136};
137
0a3f1d48 138struct ssh2_ciphers {
139 int nciphers;
65a22376 140 const struct ssh2_cipher *const *list;
0a3f1d48 141};
142
e5574168 143struct ssh_mac {
32874aea 144 void (*setcskey) (unsigned char *key);
145 void (*setsckey) (unsigned char *key);
146 void (*generate) (unsigned char *blk, int len, unsigned long seq);
147 int (*verify) (unsigned char *blk, int len, unsigned long seq);
e5574168 148 char *name;
149 int len;
150};
151
152struct ssh_kex {
7cca0d81 153 /*
154 * Plugging in another KEX algorithm requires structural chaos,
155 * so it's hard to abstract them into nice little structures
156 * like this. Hence, for the moment, this is just a
157 * placeholder. I claim justification in the fact that OpenSSH
158 * does this too :-)
159 */
e5574168 160 char *name;
161};
162
e055a386 163struct ssh_signkey {
32874aea 164 void *(*newkey) (char *data, int len);
165 void (*freekey) (void *key);
166 char *(*fmtkey) (void *key);
167 unsigned char *(*public_blob) (void *key, int *len);
168 unsigned char *(*private_blob) (void *key, int *len);
169 void *(*createkey) (unsigned char *pub_blob, int pub_len,
170 unsigned char *priv_blob, int priv_len);
171 void *(*openssh_createkey) (unsigned char **blob, int *len);
172 int (*openssh_fmtkey) (void *key, unsigned char *blob, int len);
173 char *(*fingerprint) (void *key);
174 int (*verifysig) (void *key, char *sig, int siglen,
175 char *data, int datalen);
176 unsigned char *(*sign) (void *key, char *data, int datalen,
177 int *siglen);
e5574168 178 char *name;
32874aea 179 char *keytype; /* for host key cache */
e5574168 180};
181
182struct ssh_compress {
183 char *name;
32874aea 184 void (*compress_init) (void);
185 int (*compress) (unsigned char *block, int len,
186 unsigned char **outblock, int *outlen);
187 void (*decompress_init) (void);
188 int (*decompress) (unsigned char *block, int len,
189 unsigned char **outblock, int *outlen);
190 int (*disable_compression) (void);
374330e2 191};
192
65a22376 193struct ssh2_userkey {
194 const struct ssh_signkey *alg; /* the key algorithm */
195 void *data; /* the key data */
196 char *comment; /* the key comment */
197};
198
199extern const struct ssh_cipher ssh_3des;
200extern const struct ssh_cipher ssh_des;
201extern const struct ssh_cipher ssh_blowfish_ssh1;
202extern const struct ssh2_ciphers ssh2_3des;
0d7c43a6 203extern const struct ssh2_ciphers ssh2_des;
65a22376 204extern const struct ssh2_ciphers ssh2_aes;
205extern const struct ssh2_ciphers ssh2_blowfish;
206extern const struct ssh_kex ssh_diffiehellman;
207extern const struct ssh_kex ssh_diffiehellman_gex;
208extern const struct ssh_signkey ssh_dss;
209extern const struct ssh_signkey ssh_rsa;
210extern const struct ssh_mac ssh_md5;
211extern const struct ssh_mac ssh_sha1;
212extern const struct ssh_mac ssh_sha1_buggy;
213
900a4ee6 214/*
215 * PuTTY version number formatted as an SSH version string.
216 */
217extern char sshver[];
218
fd5e5847 219/*
220 * Gross hack: pscp will try to start SFTP but fall back to scp1 if
221 * that fails. This variable is the means by which scp.c can reach
222 * into the SSH code and find out which one it got.
223 */
224extern int ssh_fallback_cmd;
225
8f203108 226#ifndef MSCRYPTOAPI
32874aea 227void SHATransform(word32 * digest, word32 * data);
8f203108 228#endif
374330e2 229
230int random_byte(void);
231void random_add_noise(void *noise, int length);
6e522441 232void random_add_heavynoise(void *noise, int length);
c5e9c988 233
32874aea 234void logevent(char *);
d74d141c 235void *new_sock_channel(Socket s); // allocates and register a new channel for port forwarding
236void ssh_send_port_open(void *channel, char *hostname, int port, char *org);
e5574168 237
7cca0d81 238Bignum copybn(Bignum b);
3709bfe9 239Bignum bn_power_2(int n);
240void bn_restore_invariant(Bignum b);
5c72ca61 241Bignum bignum_from_long(unsigned long n);
e5574168 242void freebn(Bignum b);
59600f67 243Bignum modpow(Bignum base, Bignum exp, Bignum mod);
244Bignum modmul(Bignum a, Bignum b, Bignum mod);
7cca0d81 245void decbn(Bignum n);
246extern Bignum Zero, One;
3709bfe9 247Bignum bignum_from_bytes(unsigned char *data, int nbytes);
32874aea 248int ssh1_read_bignum(unsigned char *data, Bignum * result);
ddecd643 249int bignum_bitcount(Bignum bn);
5c58ad2d 250int ssh1_bignum_length(Bignum bn);
260f3dec 251int ssh2_bignum_length(Bignum bn);
5c58ad2d 252int bignum_byte(Bignum bn, int i);
9400cf6f 253int bignum_bit(Bignum bn, int i);
254void bignum_set_bit(Bignum bn, int i, int value);
5c58ad2d 255int ssh1_write_bignum(void *data, Bignum bn);
9400cf6f 256Bignum biggcd(Bignum a, Bignum b);
257unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
258Bignum bignum_add_long(Bignum number, unsigned long addend);
259Bignum bigmul(Bignum a, Bignum b);
5c72ca61 260Bignum bigmuladd(Bignum a, Bignum b, Bignum addend);
261Bignum bigdiv(Bignum a, Bignum b);
262Bignum bigmod(Bignum a, Bignum b);
9400cf6f 263Bignum modinv(Bignum number, Bignum modulus);
3709bfe9 264Bignum bignum_bitmask(Bignum number);
9400cf6f 265Bignum bignum_rshift(Bignum number, int shift);
8c3cd914 266int bignum_cmp(Bignum a, Bignum b);
6e522441 267char *bignum_decimal(Bignum x);
e5574168 268
3709bfe9 269void dh_setup_group1(void);
a92dd380 270void dh_setup_group(Bignum pval, Bignum gval);
3709bfe9 271void dh_cleanup(void);
7bd5a860 272Bignum dh_create_e(int nbits);
e5574168 273Bignum dh_find_K(Bignum f);
7cca0d81 274
65a22376 275int loadrsakey(char *filename, struct RSAKey *key, char *passphrase);
a52f067e 276int rsakey_encrypted(char *filename, char **comment);
7cca0d81 277
65a22376 278int saversakey(char *filename, struct RSAKey *key, char *passphrase);
279
280void base64_encode_atom(unsigned char *data, int n, char *out);
281
282/* ssh2_load_userkey can return this as an error */
283extern struct ssh2_userkey ssh2_wrong_passphrase;
284#define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
285
286int ssh2_userkey_encrypted(char *filename, char **comment);
287struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase);
32874aea 288char *ssh2_userkey_loadpub(char *filename, char **algorithm,
289 int *pub_blob_len);
290int ssh2_save_userkey(char *filename, struct ssh2_userkey *key,
291 char *passphrase);
65a22376 292
293int keyfile_version(char *filename);
6e522441 294
65a22376 295void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
296void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
32874aea 297void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk,
298 int len);
299void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk,
300 int len);
9400cf6f 301
302/*
303 * For progress updates in the key generation utility.
304 */
7c7f6893 305#define PROGFN_INITIALISE 1
306#define PROGFN_LIN_PHASE 2
307#define PROGFN_EXP_PHASE 3
308#define PROGFN_PHASE_EXTENT 4
309#define PROGFN_READY 5
310#define PROGFN_PROGRESS 6
5c72ca61 311typedef void (*progfn_t) (void *param, int action, int phase, int progress);
9400cf6f 312
32874aea 313int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
314 void *pfnparam);
5c72ca61 315int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
316 void *pfnparam);
317Bignum primegen(int bits, int modulus, int residue, Bignum factor,
318 int phase, progfn_t pfn, void *pfnparam);
4ba9b64b 319
d74d141c 320
4ba9b64b 321/*
322 * zlib compression.
323 */
324void zlib_compress_init(void);
325void zlib_decompress_init(void);
326int zlib_compress_block(unsigned char *block, int len,
327 unsigned char **outblock, int *outlen);
328int zlib_decompress_block(unsigned char *block, int len,
329 unsigned char **outblock, int *outlen);
1983e559 330
331/*
332 * SSH1 agent messages.
333 */
334#define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
335#define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
336#define SSH1_AGENTC_RSA_CHALLENGE 3
337#define SSH1_AGENT_RSA_RESPONSE 4
338#define SSH1_AGENTC_ADD_RSA_IDENTITY 7
339#define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
32874aea 340#define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
1983e559 341
342/*
343 * Messages common to SSH1 and OpenSSH's SSH2.
344 */
345#define SSH_AGENT_FAILURE 5
346#define SSH_AGENT_SUCCESS 6
347
348/*
349 * OpenSSH's SSH2 agent messages.
350 */
351#define SSH2_AGENTC_REQUEST_IDENTITIES 11
352#define SSH2_AGENT_IDENTITIES_ANSWER 12
353#define SSH2_AGENTC_SIGN_REQUEST 13
354#define SSH2_AGENT_SIGN_RESPONSE 14
355#define SSH2_AGENTC_ADD_IDENTITY 17
356#define SSH2_AGENTC_REMOVE_IDENTITY 18
357#define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
7bedb13c 358
359/*
360 * Need this to warn about support for the original SSH2 keyfile
361 * format.
362 */
363void old_keyfile_warning(void);