4 #include "puttymem.h"
\r
12 extern void sshfwd_close(struct ssh_channel *c);
\r
13 extern int sshfwd_write(struct ssh_channel *c, char *, int);
\r
14 extern void sshfwd_unthrottle(struct ssh_channel *c, int bufsize);
\r
20 #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
\r
23 #define SSH_CIPHER_IDEA 1
\r
24 #define SSH_CIPHER_DES 2
\r
25 #define SSH_CIPHER_3DES 3
\r
26 #define SSH_CIPHER_BLOWFISH 6
\r
34 #ifndef BIGNUM_INTERNAL
\r
35 typedef void *Bignum;
\r
42 unsigned long exponent;
\r
43 unsigned char *modulus;
\r
47 Bignum private_exponent;
\r
56 Bignum p, q, g, y, x;
\r
59 int makekey(unsigned char *data, int len, struct RSAKey *result,
\r
60 unsigned char **keystr, int order);
\r
61 int makeprivate(unsigned char *data, int len, struct RSAKey *result);
\r
62 int rsaencrypt(unsigned char *data, int length, struct RSAKey *key);
\r
63 Bignum rsadecrypt(Bignum input, struct RSAKey *key);
\r
64 void rsasign(unsigned char *data, int length, struct RSAKey *key);
\r
65 void rsasanitise(struct RSAKey *key);
\r
66 int rsastr_len(struct RSAKey *key);
\r
67 void rsastr_fmt(char *str, struct RSAKey *key);
\r
68 void rsa_fingerprint(char *str, int len, struct RSAKey *key);
\r
69 int rsa_verify(struct RSAKey *key);
\r
70 unsigned char *rsa_public_blob(struct RSAKey *key, int *len);
\r
71 int rsa_public_blob_len(void *data, int maxlen);
\r
72 void freersakey(struct RSAKey *key);
\r
74 #ifndef PUTTY_UINT32_DEFINED
\r
75 /* This makes assumptions about the int type. */
\r
76 typedef unsigned int uint32;
\r
77 #define PUTTY_UINT32_DEFINED
\r
79 typedef uint32 word32;
\r
81 unsigned long crc32_compute(const void *s, size_t len);
\r
82 unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len);
\r
84 /* SSH CRC compensation attack detector */
\r
85 void *crcda_make_context(void);
\r
86 void crcda_free_context(void *handle);
\r
87 int detect_attack(void *handle, unsigned char *buf, uint32 len,
\r
91 * SSH2 RSA key exchange functions
\r
94 void *ssh_rsakex_newkey(char *data, int len);
\r
95 void ssh_rsakex_freekey(void *key);
\r
96 int ssh_rsakex_klen(void *key);
\r
97 void ssh_rsakex_encrypt(const struct ssh_hash *h, unsigned char *in, int inlen,
\r
98 unsigned char *out, int outlen,
\r
105 struct MD5Context {
\r
107 unsigned long hHash;
\r
109 MD5_Core_State core;
\r
110 unsigned char block[64];
\r
112 uint32 lenhi, lenlo;
\r
116 void MD5Init(struct MD5Context *context);
\r
117 void MD5Update(struct MD5Context *context, unsigned char const *buf,
\r
119 void MD5Final(unsigned char digest[16], struct MD5Context *context);
\r
120 void MD5Simple(void const *p, unsigned len, unsigned char output[16]);
\r
122 void *hmacmd5_make_context(void);
\r
123 void hmacmd5_free_context(void *handle);
\r
124 void hmacmd5_key(void *handle, void const *key, int len);
\r
125 void hmacmd5_do_hmac(void *handle, unsigned char const *blk, int len,
\r
126 unsigned char *hmac);
\r
130 unsigned char block[64];
\r
132 uint32 lenhi, lenlo;
\r
134 void SHA_Init(SHA_State * s);
\r
135 void SHA_Bytes(SHA_State * s, void *p, int len);
\r
136 void SHA_Final(SHA_State * s, unsigned char *output);
\r
137 void SHA_Simple(void *p, int len, unsigned char *output);
\r
139 void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
\r
140 unsigned char *output);
\r
143 unsigned char block[64];
\r
145 uint32 lenhi, lenlo;
\r
147 void SHA256_Init(SHA256_State * s);
\r
148 void SHA256_Bytes(SHA256_State * s, const void *p, int len);
\r
149 void SHA256_Final(SHA256_State * s, unsigned char *output);
\r
150 void SHA256_Simple(const void *p, int len, unsigned char *output);
\r
154 unsigned char block[128];
\r
158 void SHA512_Init(SHA512_State * s);
\r
159 void SHA512_Bytes(SHA512_State * s, const void *p, int len);
\r
160 void SHA512_Final(SHA512_State * s, unsigned char *output);
\r
161 void SHA512_Simple(const void *p, int len, unsigned char *output);
\r
163 struct ssh_cipher {
\r
164 void *(*make_context)(void);
\r
165 void (*free_context)(void *);
\r
166 void (*sesskey) (void *, unsigned char *key); /* for SSH-1 */
\r
167 void (*encrypt) (void *, unsigned char *blk, int len);
\r
168 void (*decrypt) (void *, unsigned char *blk, int len);
\r
173 struct ssh2_cipher {
\r
174 void *(*make_context)(void);
\r
175 void (*free_context)(void *);
\r
176 void (*setiv) (void *, unsigned char *key); /* for SSH-2 */
\r
177 void (*setkey) (void *, unsigned char *key);/* for SSH-2 */
\r
178 void (*encrypt) (void *, unsigned char *blk, int len);
\r
179 void (*decrypt) (void *, unsigned char *blk, int len);
\r
183 unsigned int flags;
\r
184 #define SSH_CIPHER_IS_CBC 1
\r
188 struct ssh2_ciphers {
\r
190 const struct ssh2_cipher *const *list;
\r
194 void *(*make_context)(void);
\r
195 void (*free_context)(void *);
\r
196 void (*setkey) (void *, unsigned char *key);
\r
197 /* whole-packet operations */
\r
198 void (*generate) (void *, unsigned char *blk, int len, unsigned long seq);
\r
199 int (*verify) (void *, unsigned char *blk, int len, unsigned long seq);
\r
200 /* partial-packet operations */
\r
201 void (*start) (void *);
\r
202 void (*bytes) (void *, unsigned char const *, int);
\r
203 void (*genresult) (void *, unsigned char *);
\r
204 int (*verresult) (void *, unsigned char const *);
\r
211 void *(*init)(void); /* also allocates context */
\r
212 void (*bytes)(void *, void *, int);
\r
213 void (*final)(void *, unsigned char *); /* also frees context */
\r
214 int hlen; /* output length in bytes */
\r
219 char *name, *groupname;
\r
220 enum { KEXTYPE_DH, KEXTYPE_RSA } main_type;
\r
222 const unsigned char *pdata, *gdata; /* NULL means group exchange */
\r
224 const struct ssh_hash *hash;
\r
229 const struct ssh_kex *const *list;
\r
232 struct ssh_signkey {
\r
233 void *(*newkey) (char *data, int len);
\r
234 void (*freekey) (void *key);
\r
235 char *(*fmtkey) (void *key);
\r
236 unsigned char *(*public_blob) (void *key, int *len);
\r
237 unsigned char *(*private_blob) (void *key, int *len);
\r
238 void *(*createkey) (unsigned char *pub_blob, int pub_len,
\r
239 unsigned char *priv_blob, int priv_len);
\r
240 void *(*openssh_createkey) (unsigned char **blob, int *len);
\r
241 int (*openssh_fmtkey) (void *key, unsigned char *blob, int len);
\r
242 int (*pubkey_bits) (void *blob, int len);
\r
243 char *(*fingerprint) (void *key);
\r
244 int (*verifysig) (void *key, char *sig, int siglen,
\r
245 char *data, int datalen);
\r
246 unsigned char *(*sign) (void *key, char *data, int datalen,
\r
249 char *keytype; /* for host key cache */
\r
252 struct ssh_compress {
\r
254 /* For zlib@openssh.com: if non-NULL, this name will be considered once
\r
255 * userauth has completed successfully. */
\r
256 char *delayed_name;
\r
257 void *(*compress_init) (void);
\r
258 void (*compress_cleanup) (void *);
\r
259 int (*compress) (void *, unsigned char *block, int len,
\r
260 unsigned char **outblock, int *outlen);
\r
261 void *(*decompress_init) (void);
\r
262 void (*decompress_cleanup) (void *);
\r
263 int (*decompress) (void *, unsigned char *block, int len,
\r
264 unsigned char **outblock, int *outlen);
\r
265 int (*disable_compression) (void *);
\r
269 struct ssh2_userkey {
\r
270 const struct ssh_signkey *alg; /* the key algorithm */
\r
271 void *data; /* the key data */
\r
272 char *comment; /* the key comment */
\r
275 /* The maximum length of any hash algorithm used in kex. (bytes) */
\r
276 #define SSH2_KEX_MAX_HASH_LEN (32) /* SHA-256 */
\r
278 extern const struct ssh_cipher ssh_3des;
\r
279 extern const struct ssh_cipher ssh_des;
\r
280 extern const struct ssh_cipher ssh_blowfish_ssh1;
\r
281 extern const struct ssh2_ciphers ssh2_3des;
\r
282 extern const struct ssh2_ciphers ssh2_des;
\r
283 extern const struct ssh2_ciphers ssh2_aes;
\r
284 extern const struct ssh2_ciphers ssh2_blowfish;
\r
285 extern const struct ssh2_ciphers ssh2_arcfour;
\r
286 extern const struct ssh_hash ssh_sha1;
\r
287 extern const struct ssh_hash ssh_sha256;
\r
288 extern const struct ssh_kexes ssh_diffiehellman_group1;
\r
289 extern const struct ssh_kexes ssh_diffiehellman_group14;
\r
290 extern const struct ssh_kexes ssh_diffiehellman_gex;
\r
291 extern const struct ssh_kexes ssh_rsa_kex;
\r
292 extern const struct ssh_signkey ssh_dss;
\r
293 extern const struct ssh_signkey ssh_rsa;
\r
294 extern const struct ssh_mac ssh_hmac_md5;
\r
295 extern const struct ssh_mac ssh_hmac_sha1;
\r
296 extern const struct ssh_mac ssh_hmac_sha1_buggy;
\r
297 extern const struct ssh_mac ssh_hmac_sha1_96;
\r
298 extern const struct ssh_mac ssh_hmac_sha1_96_buggy;
\r
300 void *aes_make_context(void);
\r
301 void aes_free_context(void *handle);
\r
302 void aes128_key(void *handle, unsigned char *key);
\r
303 void aes192_key(void *handle, unsigned char *key);
\r
304 void aes256_key(void *handle, unsigned char *key);
\r
305 void aes_iv(void *handle, unsigned char *iv);
\r
306 void aes_ssh2_encrypt_blk(void *handle, unsigned char *blk, int len);
\r
307 void aes_ssh2_decrypt_blk(void *handle, unsigned char *blk, int len);
\r
310 * PuTTY version number formatted as an SSH version string.
\r
312 extern char sshver[];
\r
315 * Gross hack: pscp will try to start SFTP but fall back to scp1 if
\r
316 * that fails. This variable is the means by which scp.c can reach
\r
317 * into the SSH code and find out which one it got.
\r
319 extern int ssh_fallback_cmd(void *handle);
\r
321 #ifndef MSCRYPTOAPI
\r
322 void SHATransform(word32 * digest, word32 * data);
\r
325 int random_byte(void);
\r
326 void random_add_noise(void *noise, int length);
\r
327 void random_add_heavynoise(void *noise, int length);
\r
329 void logevent(void *, const char *);
\r
331 /* Allocate and register a new channel for port forwarding */
\r
332 void *new_sock_channel(void *handle, Socket s);
\r
333 void ssh_send_port_open(void *channel, char *hostname, int port, char *org);
\r
335 /* Exports from portfwd.c */
\r
336 extern const char *pfd_newconnect(Socket * s, char *hostname, int port,
\r
337 void *c, const Config *cfg,
\r
338 int addressfamily);
\r
339 /* desthost == NULL indicates dynamic (SOCKS) port forwarding */
\r
340 extern const char *pfd_addforward(char *desthost, int destport, char *srcaddr,
\r
341 int port, void *backhandle,
\r
342 const Config *cfg, void **sockdata,
\r
343 int address_family);
\r
344 extern void pfd_close(Socket s);
\r
345 extern void pfd_terminate(void *sockdata);
\r
346 extern int pfd_send(Socket s, char *data, int len);
\r
347 extern void pfd_confirm(Socket s);
\r
348 extern void pfd_unthrottle(Socket s);
\r
349 extern void pfd_override_throttle(Socket s, int enable);
\r
351 /* Exports from x11fwd.c */
\r
353 X11_TRANS_IPV4 = 0, X11_TRANS_IPV6 = 6, X11_TRANS_UNIX = 256
\r
355 struct X11Display {
\r
356 /* Broken-down components of the display name itself */
\r
361 /* OSX sometimes replaces all the above with a full Unix-socket pathname */
\r
362 char *unixsocketpath;
\r
364 /* PuTTY networking SockAddr to connect to the display, and associated
\r
370 /* Auth details we invented for the virtual display on the SSH server. */
\r
371 int remoteauthproto;
\r
372 unsigned char *remoteauthdata;
\r
373 int remoteauthdatalen;
\r
374 char *remoteauthprotoname;
\r
375 char *remoteauthdatastring;
\r
377 /* Our local auth details for talking to the real X display. */
\r
378 int localauthproto;
\r
379 unsigned char *localauthdata;
\r
380 int localauthdatalen;
\r
383 * Used inside x11fwd.c to remember recently seen
\r
384 * XDM-AUTHORIZATION-1 strings, to avoid replay attacks.
\r
389 * x11_setup_display() parses the display variable and fills in an
\r
390 * X11Display structure. Some remote auth details are invented;
\r
391 * the supplied authtype parameter configures the preferred
\r
392 * authorisation protocol to use at the remote end. The local auth
\r
393 * details are looked up by calling platform_get_x11_auth.
\r
395 extern struct X11Display *x11_setup_display(char *display, int authtype,
\r
397 void x11_free_display(struct X11Display *disp);
\r
398 extern const char *x11_init(Socket *, struct X11Display *, void *,
\r
399 const char *, int, const Config *);
\r
400 extern void x11_close(Socket);
\r
401 extern int x11_send(Socket, char *, int);
\r
402 extern void x11_unthrottle(Socket s);
\r
403 extern void x11_override_throttle(Socket s, int enable);
\r
404 char *x11_display(const char *display);
\r
405 /* Platform-dependent X11 functions */
\r
406 extern void platform_get_x11_auth(struct X11Display *display,
\r
408 /* examine a mostly-filled-in X11Display and fill in localauth* */
\r
409 extern const int platform_uses_x11_unix_by_default;
\r
410 /* choose default X transport in the absence of a specified one */
\r
411 SockAddr platform_get_x11_unix_address(const char *path, int displaynum);
\r
412 /* make up a SockAddr naming the address for displaynum */
\r
413 char *platform_get_x_display(void);
\r
414 /* allocated local X display string, if any */
\r
415 /* Callbacks in x11.c usable _by_ platform X11 functions */
\r
417 * This function does the job of platform_get_x11_auth, provided
\r
418 * it is told where to find a normally formatted .Xauthority file:
\r
419 * it opens that file, parses it to find an auth record which
\r
420 * matches the display details in "display", and fills in the
\r
421 * localauth fields.
\r
423 * It is expected that most implementations of
\r
424 * platform_get_x11_auth() will work by finding their system's
\r
425 * .Xauthority file, adjusting the display details if necessary
\r
426 * for local oddities like Unix-domain socket transport, and
\r
427 * calling this function to do the rest of the work.
\r
429 void x11_get_auth_from_authfile(struct X11Display *display,
\r
430 const char *authfilename);
\r
432 Bignum copybn(Bignum b);
\r
433 Bignum bn_power_2(int n);
\r
434 void bn_restore_invariant(Bignum b);
\r
435 Bignum bignum_from_long(unsigned long n);
\r
436 void freebn(Bignum b);
\r
437 Bignum modpow(Bignum base, Bignum exp, Bignum mod);
\r
438 Bignum modmul(Bignum a, Bignum b, Bignum mod);
\r
439 void decbn(Bignum n);
\r
440 extern Bignum Zero, One;
\r
441 Bignum bignum_from_bytes(const unsigned char *data, int nbytes);
\r
442 int ssh1_read_bignum(const unsigned char *data, int len, Bignum * result);
\r
443 int bignum_bitcount(Bignum bn);
\r
444 int ssh1_bignum_length(Bignum bn);
\r
445 int ssh2_bignum_length(Bignum bn);
\r
446 int bignum_byte(Bignum bn, int i);
\r
447 int bignum_bit(Bignum bn, int i);
\r
448 void bignum_set_bit(Bignum bn, int i, int value);
\r
449 int ssh1_write_bignum(void *data, Bignum bn);
\r
450 Bignum biggcd(Bignum a, Bignum b);
\r
451 unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
\r
452 Bignum bignum_add_long(Bignum number, unsigned long addend);
\r
453 Bignum bigadd(Bignum a, Bignum b);
\r
454 Bignum bigsub(Bignum a, Bignum b);
\r
455 Bignum bigmul(Bignum a, Bignum b);
\r
456 Bignum bigmuladd(Bignum a, Bignum b, Bignum addend);
\r
457 Bignum bigdiv(Bignum a, Bignum b);
\r
458 Bignum bigmod(Bignum a, Bignum b);
\r
459 Bignum modinv(Bignum number, Bignum modulus);
\r
460 Bignum bignum_bitmask(Bignum number);
\r
461 Bignum bignum_rshift(Bignum number, int shift);
\r
462 int bignum_cmp(Bignum a, Bignum b);
\r
463 char *bignum_decimal(Bignum x);
\r
466 void diagbn(char *prefix, Bignum md);
\r
469 void *dh_setup_group(const struct ssh_kex *kex);
\r
470 void *dh_setup_gex(Bignum pval, Bignum gval);
\r
471 void dh_cleanup(void *);
\r
472 Bignum dh_create_e(void *, int nbits);
\r
473 Bignum dh_find_K(void *, Bignum f);
\r
475 int loadrsakey(const Filename *filename, struct RSAKey *key,
\r
476 char *passphrase, const char **errorstr);
\r
477 int rsakey_encrypted(const Filename *filename, char **comment);
\r
478 int rsakey_pubblob(const Filename *filename, void **blob, int *bloblen,
\r
479 char **commentptr, const char **errorstr);
\r
481 int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase);
\r
483 extern int base64_decode_atom(char *atom, unsigned char *out);
\r
484 extern int base64_lines(int datalen);
\r
485 extern void base64_encode_atom(unsigned char *data, int n, char *out);
\r
486 extern void base64_encode(FILE *fp, unsigned char *data, int datalen, int cpl);
\r
488 /* ssh2_load_userkey can return this as an error */
\r
489 extern struct ssh2_userkey ssh2_wrong_passphrase;
\r
490 #define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
\r
492 int ssh2_userkey_encrypted(const Filename *filename, char **comment);
\r
493 struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
\r
494 char *passphrase, const char **errorstr);
\r
495 unsigned char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
\r
496 int *pub_blob_len, char **commentptr,
\r
497 const char **errorstr);
\r
498 int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
\r
500 const struct ssh_signkey *find_pubkey_alg(const char *name);
\r
503 SSH_KEYTYPE_UNOPENABLE,
\r
504 SSH_KEYTYPE_UNKNOWN,
\r
505 SSH_KEYTYPE_SSH1, SSH_KEYTYPE_SSH2,
\r
506 SSH_KEYTYPE_OPENSSH, SSH_KEYTYPE_SSHCOM
\r
508 int key_type(const Filename *filename);
\r
509 char *key_type_to_str(int type);
\r
511 int import_possible(int type);
\r
512 int import_target_type(int type);
\r
513 int import_encrypted(const Filename *filename, int type, char **comment);
\r
514 int import_ssh1(const Filename *filename, int type,
\r
515 struct RSAKey *key, char *passphrase, const char **errmsg_p);
\r
516 struct ssh2_userkey *import_ssh2(const Filename *filename, int type,
\r
517 char *passphrase, const char **errmsg_p);
\r
518 int export_ssh1(const Filename *filename, int type,
\r
519 struct RSAKey *key, char *passphrase);
\r
520 int export_ssh2(const Filename *filename, int type,
\r
521 struct ssh2_userkey *key, char *passphrase);
\r
523 void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
\r
524 void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
\r
525 void des3_decrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
\r
526 unsigned char *blk, int len);
\r
527 void des3_encrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
\r
528 unsigned char *blk, int len);
\r
529 void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk,
\r
531 void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk,
\r
534 void des_encrypt_xdmauth(unsigned char *key, unsigned char *blk, int len);
\r
535 void des_decrypt_xdmauth(unsigned char *key, unsigned char *blk, int len);
\r
538 * For progress updates in the key generation utility.
\r
540 #define PROGFN_INITIALISE 1
\r
541 #define PROGFN_LIN_PHASE 2
\r
542 #define PROGFN_EXP_PHASE 3
\r
543 #define PROGFN_PHASE_EXTENT 4
\r
544 #define PROGFN_READY 5
\r
545 #define PROGFN_PROGRESS 6
\r
546 typedef void (*progfn_t) (void *param, int action, int phase, int progress);
\r
548 int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
\r
550 int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
\r
552 Bignum primegen(int bits, int modulus, int residue, Bignum factor,
\r
553 int phase, progfn_t pfn, void *pfnparam);
\r
557 * zlib compression.
\r
559 void *zlib_compress_init(void);
\r
560 void zlib_compress_cleanup(void *);
\r
561 void *zlib_decompress_init(void);
\r
562 void zlib_decompress_cleanup(void *);
\r
563 int zlib_compress_block(void *, unsigned char *block, int len,
\r
564 unsigned char **outblock, int *outlen);
\r
565 int zlib_decompress_block(void *, unsigned char *block, int len,
\r
566 unsigned char **outblock, int *outlen);
\r
569 * SSH-1 agent messages.
\r
571 #define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
\r
572 #define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
\r
573 #define SSH1_AGENTC_RSA_CHALLENGE 3
\r
574 #define SSH1_AGENT_RSA_RESPONSE 4
\r
575 #define SSH1_AGENTC_ADD_RSA_IDENTITY 7
\r
576 #define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
\r
577 #define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
\r
580 * Messages common to SSH-1 and OpenSSH's SSH-2.
\r
582 #define SSH_AGENT_FAILURE 5
\r
583 #define SSH_AGENT_SUCCESS 6
\r
586 * OpenSSH's SSH-2 agent messages.
\r
588 #define SSH2_AGENTC_REQUEST_IDENTITIES 11
\r
589 #define SSH2_AGENT_IDENTITIES_ANSWER 12
\r
590 #define SSH2_AGENTC_SIGN_REQUEST 13
\r
591 #define SSH2_AGENT_SIGN_RESPONSE 14
\r
592 #define SSH2_AGENTC_ADD_IDENTITY 17
\r
593 #define SSH2_AGENTC_REMOVE_IDENTITY 18
\r
594 #define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
\r
597 * Need this to warn about support for the original SSH-2 keyfile
\r
600 void old_keyfile_warning(void);
\r