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