12 extern void sshfwd_close(struct ssh_channel
*c
);
13 extern int sshfwd_write(struct ssh_channel
*c
, char *, int);
14 extern void sshfwd_unthrottle(struct ssh_channel
*c
, int bufsize
);
20 #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
23 #define SSH_CIPHER_IDEA 1
24 #define SSH_CIPHER_DES 2
25 #define SSH_CIPHER_3DES 3
26 #define SSH_CIPHER_BLOWFISH 6
34 #ifndef BIGNUM_INTERNAL
42 unsigned long exponent
;
43 unsigned char *modulus
;
47 Bignum private_exponent
;
59 int makekey(unsigned char *data
, int len
, struct RSAKey
*result
,
60 unsigned char **keystr
, int order
);
61 int makeprivate(unsigned char *data
, int len
, struct RSAKey
*result
);
62 int rsaencrypt(unsigned char *data
, int length
, struct RSAKey
*key
);
63 Bignum
rsadecrypt(Bignum input
, struct RSAKey
*key
);
64 void rsasign(unsigned char *data
, int length
, struct RSAKey
*key
);
65 void rsasanitise(struct RSAKey
*key
);
66 int rsastr_len(struct RSAKey
*key
);
67 void rsastr_fmt(char *str
, struct RSAKey
*key
);
68 void rsa_fingerprint(char *str
, int len
, struct RSAKey
*key
);
69 int rsa_verify(struct RSAKey
*key
);
70 unsigned char *rsa_public_blob(struct RSAKey
*key
, int *len
);
71 int rsa_public_blob_len(void *data
, int maxlen
);
72 void freersakey(struct RSAKey
*key
);
74 typedef unsigned int word32
;
75 typedef unsigned int uint32
;
77 unsigned long crc32_compute(const void *s
, size_t len
);
78 unsigned long crc32_update(unsigned long crc_input
, const void *s
, size_t len
);
80 /* SSH CRC compensation attack detector */
81 void *crcda_make_context(void);
82 void crcda_free_context(void *handle
);
83 int detect_attack(void *handle
, unsigned char *buf
, uint32 len
,
87 * SSH2 RSA key exchange functions
90 void *ssh_rsakex_newkey(char *data
, int len
);
91 void ssh_rsakex_freekey(void *key
);
92 int ssh_rsakex_klen(void *key
);
93 void ssh_rsakex_encrypt(const struct ssh_hash
*h
, unsigned char *in
, int inlen
,
94 unsigned char *out
, int outlen
,
106 unsigned char block
[64];
112 void MD5Init(struct MD5Context
*context
);
113 void MD5Update(struct MD5Context
*context
, unsigned char const *buf
,
115 void MD5Final(unsigned char digest
[16], struct MD5Context
*context
);
116 void MD5Simple(void const *p
, unsigned len
, unsigned char output
[16]);
118 void *hmacmd5_make_context(void);
119 void hmacmd5_free_context(void *handle
);
120 void hmacmd5_key(void *handle
, void const *key
, int len
);
121 void hmacmd5_do_hmac(void *handle
, unsigned char const *blk
, int len
,
122 unsigned char *hmac
);
126 unsigned char block
[64];
130 void SHA_Init(SHA_State
* s
);
131 void SHA_Bytes(SHA_State
* s
, void *p
, int len
);
132 void SHA_Final(SHA_State
* s
, unsigned char *output
);
133 void SHA_Simple(void *p
, int len
, unsigned char *output
);
135 void hmac_sha1_simple(void *key
, int keylen
, void *data
, int datalen
,
136 unsigned char *output
);
139 unsigned char block
[64];
143 void SHA256_Init(SHA256_State
* s
);
144 void SHA256_Bytes(SHA256_State
* s
, const void *p
, int len
);
145 void SHA256_Final(SHA256_State
* s
, unsigned char *output
);
146 void SHA256_Simple(const void *p
, int len
, unsigned char *output
);
150 unsigned char block
[128];
154 void SHA512_Init(SHA512_State
* s
);
155 void SHA512_Bytes(SHA512_State
* s
, const void *p
, int len
);
156 void SHA512_Final(SHA512_State
* s
, unsigned char *output
);
157 void SHA512_Simple(const void *p
, int len
, unsigned char *output
);
160 void *(*make_context
)(void);
161 void (*free_context
)(void *);
162 void (*sesskey
) (void *, unsigned char *key
); /* for SSH-1 */
163 void (*encrypt
) (void *, unsigned char *blk
, int len
);
164 void (*decrypt
) (void *, unsigned char *blk
, int len
);
170 void *(*make_context
)(void);
171 void (*free_context
)(void *);
172 void (*setiv
) (void *, unsigned char *key
); /* for SSH-2 */
173 void (*setkey
) (void *, unsigned char *key
);/* for SSH-2 */
174 void (*encrypt
) (void *, unsigned char *blk
, int len
);
175 void (*decrypt
) (void *, unsigned char *blk
, int len
);
180 #define SSH_CIPHER_IS_CBC 1
184 struct ssh2_ciphers
{
186 const struct ssh2_cipher
*const *list
;
190 void *(*make_context
)(void);
191 void (*free_context
)(void *);
192 void (*setkey
) (void *, unsigned char *key
);
193 /* whole-packet operations */
194 void (*generate
) (void *, unsigned char *blk
, int len
, unsigned long seq
);
195 int (*verify
) (void *, unsigned char *blk
, int len
, unsigned long seq
);
196 /* partial-packet operations */
197 void (*start
) (void *);
198 void (*bytes
) (void *, unsigned char const *, int);
199 void (*genresult
) (void *, unsigned char *);
200 int (*verresult
) (void *, unsigned char const *);
207 void *(*init
)(void); /* also allocates context */
208 void (*bytes
)(void *, void *, int);
209 void (*final
)(void *, unsigned char *); /* also frees context */
210 int hlen
; /* output length in bytes */
215 char *name
, *groupname
;
216 enum { KEXTYPE_DH
, KEXTYPE_RSA
} main_type
;
218 const unsigned char *pdata
, *gdata
; /* NULL means group exchange */
220 const struct ssh_hash
*hash
;
225 const struct ssh_kex
*const *list
;
229 void *(*newkey
) (char *data
, int len
);
230 void (*freekey
) (void *key
);
231 char *(*fmtkey
) (void *key
);
232 unsigned char *(*public_blob
) (void *key
, int *len
);
233 unsigned char *(*private_blob
) (void *key
, int *len
);
234 void *(*createkey
) (unsigned char *pub_blob
, int pub_len
,
235 unsigned char *priv_blob
, int priv_len
);
236 void *(*openssh_createkey
) (unsigned char **blob
, int *len
);
237 int (*openssh_fmtkey
) (void *key
, unsigned char *blob
, int len
);
238 int (*pubkey_bits
) (void *blob
, int len
);
239 char *(*fingerprint
) (void *key
);
240 int (*verifysig
) (void *key
, char *sig
, int siglen
,
241 char *data
, int datalen
);
242 unsigned char *(*sign
) (void *key
, char *data
, int datalen
,
245 char *keytype
; /* for host key cache */
248 struct ssh_compress
{
250 void *(*compress_init
) (void);
251 void (*compress_cleanup
) (void *);
252 int (*compress
) (void *, unsigned char *block
, int len
,
253 unsigned char **outblock
, int *outlen
);
254 void *(*decompress_init
) (void);
255 void (*decompress_cleanup
) (void *);
256 int (*decompress
) (void *, unsigned char *block
, int len
,
257 unsigned char **outblock
, int *outlen
);
258 int (*disable_compression
) (void *);
262 struct ssh2_userkey
{
263 const struct ssh_signkey
*alg
; /* the key algorithm */
264 void *data
; /* the key data */
265 char *comment
; /* the key comment */
268 /* The maximum length of any hash algorithm used in kex. (bytes) */
269 #define SSH2_KEX_MAX_HASH_LEN (32) /* SHA-256 */
271 extern const struct ssh_cipher ssh_3des
;
272 extern const struct ssh_cipher ssh_des
;
273 extern const struct ssh_cipher ssh_blowfish_ssh1
;
274 extern const struct ssh2_ciphers ssh2_3des
;
275 extern const struct ssh2_ciphers ssh2_des
;
276 extern const struct ssh2_ciphers ssh2_aes
;
277 extern const struct ssh2_ciphers ssh2_blowfish
;
278 extern const struct ssh2_ciphers ssh2_arcfour
;
279 extern const struct ssh_hash ssh_sha1
;
280 extern const struct ssh_hash ssh_sha256
;
281 extern const struct ssh_kexes ssh_diffiehellman_group1
;
282 extern const struct ssh_kexes ssh_diffiehellman_group14
;
283 extern const struct ssh_kexes ssh_diffiehellman_gex
;
284 extern const struct ssh_kexes ssh_rsa_kex
;
285 extern const struct ssh_signkey ssh_dss
;
286 extern const struct ssh_signkey ssh_rsa
;
287 extern const struct ssh_mac ssh_hmac_md5
;
288 extern const struct ssh_mac ssh_hmac_sha1
;
289 extern const struct ssh_mac ssh_hmac_sha1_buggy
;
290 extern const struct ssh_mac ssh_hmac_sha1_96
;
291 extern const struct ssh_mac ssh_hmac_sha1_96_buggy
;
293 void *aes_make_context(void);
294 void aes_free_context(void *handle
);
295 void aes128_key(void *handle
, unsigned char *key
);
296 void aes192_key(void *handle
, unsigned char *key
);
297 void aes256_key(void *handle
, unsigned char *key
);
298 void aes_iv(void *handle
, unsigned char *iv
);
299 void aes_ssh2_encrypt_blk(void *handle
, unsigned char *blk
, int len
);
300 void aes_ssh2_decrypt_blk(void *handle
, unsigned char *blk
, int len
);
303 * PuTTY version number formatted as an SSH version string.
305 extern char sshver
[];
308 * Gross hack: pscp will try to start SFTP but fall back to scp1 if
309 * that fails. This variable is the means by which scp.c can reach
310 * into the SSH code and find out which one it got.
312 extern int ssh_fallback_cmd(void *handle
);
315 void SHATransform(word32
* digest
, word32
* data
);
318 int random_byte(void);
319 void random_add_noise(void *noise
, int length
);
320 void random_add_heavynoise(void *noise
, int length
);
322 void logevent(void *, const char *);
324 /* Allocate and register a new channel for port forwarding */
325 void *new_sock_channel(void *handle
, Socket s
);
326 void ssh_send_port_open(void *channel
, char *hostname
, int port
, char *org
);
328 /* Exports from portfwd.c */
329 extern const char *pfd_newconnect(Socket
* s
, char *hostname
, int port
,
330 void *c
, const Config
*cfg
,
332 /* desthost == NULL indicates dynamic (SOCKS) port forwarding */
333 extern const char *pfd_addforward(char *desthost
, int destport
, char *srcaddr
,
334 int port
, void *backhandle
,
335 const Config
*cfg
, void **sockdata
,
337 extern void pfd_close(Socket s
);
338 extern void pfd_terminate(void *sockdata
);
339 extern int pfd_send(Socket s
, char *data
, int len
);
340 extern void pfd_confirm(Socket s
);
341 extern void pfd_unthrottle(Socket s
);
342 extern void pfd_override_throttle(Socket s
, int enable
);
344 /* Exports from x11fwd.c */
346 X11_TRANS_IPV4
= 0, X11_TRANS_IPV6
= 6, X11_TRANS_UNIX
= 256
349 /* Broken-down components of the display name itself */
354 /* OSX sometimes replaces all the above with a full Unix-socket pathname */
355 char *unixsocketpath
;
357 /* PuTTY networking SockAddr to connect to the display, and associated
363 /* Auth details we invented for the virtual display on the SSH server. */
365 unsigned char *remoteauthdata
;
366 int remoteauthdatalen
;
367 char *remoteauthprotoname
;
368 char *remoteauthdatastring
;
370 /* Our local auth details for talking to the real X display. */
372 unsigned char *localauthdata
;
373 int localauthdatalen
;
376 * Used inside x11fwd.c to remember recently seen
377 * XDM-AUTHORIZATION-1 strings, to avoid replay attacks.
382 * x11_setup_display() parses the display variable and fills in an
383 * X11Display structure. Some remote auth details are invented;
384 * the supplied authtype parameter configures the preferred
385 * authorisation protocol to use at the remote end. The local auth
386 * details are looked up by calling platform_get_x11_auth.
388 extern struct X11Display
*x11_setup_display(char *display
, int authtype
,
390 void x11_free_display(struct X11Display
*disp
);
391 extern const char *x11_init(Socket
*, struct X11Display
*, void *,
392 const char *, int, const Config
*);
393 extern void x11_close(Socket
);
394 extern int x11_send(Socket
, char *, int);
395 extern void x11_unthrottle(Socket s
);
396 extern void x11_override_throttle(Socket s
, int enable
);
397 char *x11_display(const char *display
);
398 /* Platform-dependent X11 functions */
399 extern void platform_get_x11_auth(struct X11Display
*display
,
401 /* examine a mostly-filled-in X11Display and fill in localauth* */
402 extern const int platform_uses_x11_unix_by_default
;
403 /* choose default X transport in the absence of a specified one */
404 SockAddr
platform_get_x11_unix_address(const char *path
, int displaynum
);
405 /* make up a SockAddr naming the address for displaynum */
406 char *platform_get_x_display(void);
407 /* allocated local X display string, if any */
408 /* Callbacks in x11.c usable _by_ platform X11 functions */
410 * This function does the job of platform_get_x11_auth, provided
411 * it is told where to find a normally formatted .Xauthority file:
412 * it opens that file, parses it to find an auth record which
413 * matches the display details in "display", and fills in the
416 * It is expected that most implementations of
417 * platform_get_x11_auth() will work by finding their system's
418 * .Xauthority file, adjusting the display details if necessary
419 * for local oddities like Unix-domain socket transport, and
420 * calling this function to do the rest of the work.
422 void x11_get_auth_from_authfile(struct X11Display
*display
,
423 const char *authfilename
);
425 Bignum
copybn(Bignum b
);
426 Bignum
bn_power_2(int n
);
427 void bn_restore_invariant(Bignum b
);
428 Bignum
bignum_from_long(unsigned long n
);
429 void freebn(Bignum b
);
430 Bignum
modpow(Bignum base
, Bignum exp
, Bignum mod
);
431 Bignum
modmul(Bignum a
, Bignum b
, Bignum mod
);
432 void decbn(Bignum n
);
433 extern Bignum Zero
, One
;
434 Bignum
bignum_from_bytes(const unsigned char *data
, int nbytes
);
435 int ssh1_read_bignum(const unsigned char *data
, int len
, Bignum
* result
);
436 int bignum_bitcount(Bignum bn
);
437 int ssh1_bignum_length(Bignum bn
);
438 int ssh2_bignum_length(Bignum bn
);
439 int bignum_byte(Bignum bn
, int i
);
440 int bignum_bit(Bignum bn
, int i
);
441 void bignum_set_bit(Bignum bn
, int i
, int value
);
442 int ssh1_write_bignum(void *data
, Bignum bn
);
443 Bignum
biggcd(Bignum a
, Bignum b
);
444 unsigned short bignum_mod_short(Bignum number
, unsigned short modulus
);
445 Bignum
bignum_add_long(Bignum number
, unsigned long addend
);
446 Bignum
bigmul(Bignum a
, Bignum b
);
447 Bignum
bigmuladd(Bignum a
, Bignum b
, Bignum addend
);
448 Bignum
bigdiv(Bignum a
, Bignum b
);
449 Bignum
bigmod(Bignum a
, Bignum b
);
450 Bignum
modinv(Bignum number
, Bignum modulus
);
451 Bignum
bignum_bitmask(Bignum number
);
452 Bignum
bignum_rshift(Bignum number
, int shift
);
453 int bignum_cmp(Bignum a
, Bignum b
);
454 char *bignum_decimal(Bignum x
);
457 void diagbn(char *prefix
, Bignum md
);
460 void *dh_setup_group(const struct ssh_kex
*kex
);
461 void *dh_setup_gex(Bignum pval
, Bignum gval
);
462 void dh_cleanup(void *);
463 Bignum
dh_create_e(void *, int nbits
);
464 Bignum
dh_find_K(void *, Bignum f
);
466 int loadrsakey(const Filename
*filename
, struct RSAKey
*key
,
467 char *passphrase
, const char **errorstr
);
468 int rsakey_encrypted(const Filename
*filename
, char **comment
);
469 int rsakey_pubblob(const Filename
*filename
, void **blob
, int *bloblen
,
470 char **commentptr
, const char **errorstr
);
472 int saversakey(const Filename
*filename
, struct RSAKey
*key
, char *passphrase
);
474 extern int base64_decode_atom(char *atom
, unsigned char *out
);
475 extern int base64_lines(int datalen
);
476 extern void base64_encode_atom(unsigned char *data
, int n
, char *out
);
477 extern void base64_encode(FILE *fp
, unsigned char *data
, int datalen
, int cpl
);
479 /* ssh2_load_userkey can return this as an error */
480 extern struct ssh2_userkey ssh2_wrong_passphrase
;
481 #define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
483 int ssh2_userkey_encrypted(const Filename
*filename
, char **comment
);
484 struct ssh2_userkey
*ssh2_load_userkey(const Filename
*filename
,
485 char *passphrase
, const char **errorstr
);
486 unsigned char *ssh2_userkey_loadpub(const Filename
*filename
, char **algorithm
,
487 int *pub_blob_len
, char **commentptr
,
488 const char **errorstr
);
489 int ssh2_save_userkey(const Filename
*filename
, struct ssh2_userkey
*key
,
491 const struct ssh_signkey
*find_pubkey_alg(const char *name
);
494 SSH_KEYTYPE_UNOPENABLE
,
496 SSH_KEYTYPE_SSH1
, SSH_KEYTYPE_SSH2
,
497 SSH_KEYTYPE_OPENSSH
, SSH_KEYTYPE_SSHCOM
499 int key_type(const Filename
*filename
);
500 char *key_type_to_str(int type
);
502 int import_possible(int type
);
503 int import_target_type(int type
);
504 int import_encrypted(const Filename
*filename
, int type
, char **comment
);
505 int import_ssh1(const Filename
*filename
, int type
,
506 struct RSAKey
*key
, char *passphrase
, const char **errmsg_p
);
507 struct ssh2_userkey
*import_ssh2(const Filename
*filename
, int type
,
508 char *passphrase
, const char **errmsg_p
);
509 int export_ssh1(const Filename
*filename
, int type
,
510 struct RSAKey
*key
, char *passphrase
);
511 int export_ssh2(const Filename
*filename
, int type
,
512 struct ssh2_userkey
*key
, char *passphrase
);
514 void des3_decrypt_pubkey(unsigned char *key
, unsigned char *blk
, int len
);
515 void des3_encrypt_pubkey(unsigned char *key
, unsigned char *blk
, int len
);
516 void des3_decrypt_pubkey_ossh(unsigned char *key
, unsigned char *iv
,
517 unsigned char *blk
, int len
);
518 void des3_encrypt_pubkey_ossh(unsigned char *key
, unsigned char *iv
,
519 unsigned char *blk
, int len
);
520 void aes256_encrypt_pubkey(unsigned char *key
, unsigned char *blk
,
522 void aes256_decrypt_pubkey(unsigned char *key
, unsigned char *blk
,
525 void des_encrypt_xdmauth(unsigned char *key
, unsigned char *blk
, int len
);
526 void des_decrypt_xdmauth(unsigned char *key
, unsigned char *blk
, int len
);
529 * For progress updates in the key generation utility.
531 #define PROGFN_INITIALISE 1
532 #define PROGFN_LIN_PHASE 2
533 #define PROGFN_EXP_PHASE 3
534 #define PROGFN_PHASE_EXTENT 4
535 #define PROGFN_READY 5
536 #define PROGFN_PROGRESS 6
537 typedef void (*progfn_t
) (void *param
, int action
, int phase
, int progress
);
539 int rsa_generate(struct RSAKey
*key
, int bits
, progfn_t pfn
,
541 int dsa_generate(struct dss_key
*key
, int bits
, progfn_t pfn
,
543 Bignum
primegen(int bits
, int modulus
, int residue
, Bignum factor
,
544 int phase
, progfn_t pfn
, void *pfnparam
);
550 void *zlib_compress_init(void);
551 void zlib_compress_cleanup(void *);
552 void *zlib_decompress_init(void);
553 void zlib_decompress_cleanup(void *);
554 int zlib_compress_block(void *, unsigned char *block
, int len
,
555 unsigned char **outblock
, int *outlen
);
556 int zlib_decompress_block(void *, unsigned char *block
, int len
,
557 unsigned char **outblock
, int *outlen
);
560 * SSH-1 agent messages.
562 #define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
563 #define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
564 #define SSH1_AGENTC_RSA_CHALLENGE 3
565 #define SSH1_AGENT_RSA_RESPONSE 4
566 #define SSH1_AGENTC_ADD_RSA_IDENTITY 7
567 #define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
568 #define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
571 * Messages common to SSH-1 and OpenSSH's SSH-2.
573 #define SSH_AGENT_FAILURE 5
574 #define SSH_AGENT_SUCCESS 6
577 * OpenSSH's SSH-2 agent messages.
579 #define SSH2_AGENTC_REQUEST_IDENTITIES 11
580 #define SSH2_AGENT_IDENTITIES_ANSWER 12
581 #define SSH2_AGENTC_SIGN_REQUEST 13
582 #define SSH2_AGENT_SIGN_RESPONSE 14
583 #define SSH2_AGENTC_ADD_IDENTITY 17
584 #define SSH2_AGENTC_REMOVE_IDENTITY 18
585 #define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
588 * Need this to warn about support for the original SSH-2 keyfile
591 void old_keyfile_warning(void);