| 1 | #include <string.h> |
| 2 | |
| 3 | #include "puttymem.h" |
| 4 | #include "network.h" |
| 5 | #include "int64.h" |
| 6 | |
| 7 | struct ssh_channel; |
| 8 | |
| 9 | extern void sshfwd_close(struct ssh_channel *c); |
| 10 | extern int sshfwd_write(struct ssh_channel *c, char *, int); |
| 11 | extern void sshfwd_unthrottle(struct ssh_channel *c, int bufsize); |
| 12 | |
| 13 | /* |
| 14 | * Useful thing. |
| 15 | */ |
| 16 | #ifndef lenof |
| 17 | #define lenof(x) ( (sizeof((x))) / (sizeof(*(x)))) |
| 18 | #endif |
| 19 | |
| 20 | #define SSH_CIPHER_IDEA 1 |
| 21 | #define SSH_CIPHER_DES 2 |
| 22 | #define SSH_CIPHER_3DES 3 |
| 23 | #define SSH_CIPHER_BLOWFISH 6 |
| 24 | |
| 25 | #ifdef MSCRYPTOAPI |
| 26 | #define APIEXTRA 8 |
| 27 | #else |
| 28 | #define APIEXTRA 0 |
| 29 | #endif |
| 30 | |
| 31 | #ifndef BIGNUM_INTERNAL |
| 32 | typedef void *Bignum; |
| 33 | #endif |
| 34 | |
| 35 | struct RSAKey { |
| 36 | int bits; |
| 37 | int bytes; |
| 38 | #ifdef MSCRYPTOAPI |
| 39 | unsigned long exponent; |
| 40 | unsigned char *modulus; |
| 41 | #else |
| 42 | Bignum modulus; |
| 43 | Bignum exponent; |
| 44 | Bignum private_exponent; |
| 45 | Bignum p; |
| 46 | Bignum q; |
| 47 | Bignum iqmp; |
| 48 | #endif |
| 49 | char *comment; |
| 50 | }; |
| 51 | |
| 52 | struct dss_key { |
| 53 | Bignum p, q, g, y, x; |
| 54 | }; |
| 55 | |
| 56 | int makekey(unsigned char *data, struct RSAKey *result, |
| 57 | unsigned char **keystr, int order); |
| 58 | int makeprivate(unsigned char *data, struct RSAKey *result); |
| 59 | void rsaencrypt(unsigned char *data, int length, struct RSAKey *key); |
| 60 | Bignum rsadecrypt(Bignum input, struct RSAKey *key); |
| 61 | void rsasign(unsigned char *data, int length, struct RSAKey *key); |
| 62 | void rsasanitise(struct RSAKey *key); |
| 63 | int rsastr_len(struct RSAKey *key); |
| 64 | void rsastr_fmt(char *str, struct RSAKey *key); |
| 65 | void rsa_fingerprint(char *str, int len, struct RSAKey *key); |
| 66 | int rsa_verify(struct RSAKey *key); |
| 67 | unsigned char *rsa_public_blob(struct RSAKey *key, int *len); |
| 68 | int rsa_public_blob_len(void *data); |
| 69 | void freersakey(struct RSAKey *key); |
| 70 | |
| 71 | typedef unsigned int word32; |
| 72 | typedef unsigned int uint32; |
| 73 | |
| 74 | unsigned long crc32(const void *s, size_t len); |
| 75 | unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len); |
| 76 | |
| 77 | /* SSH CRC compensation attack detector */ |
| 78 | int detect_attack(unsigned char *buf, uint32 len, unsigned char *IV); |
| 79 | |
| 80 | typedef struct { |
| 81 | uint32 h[4]; |
| 82 | } MD5_Core_State; |
| 83 | |
| 84 | struct MD5Context { |
| 85 | #ifdef MSCRYPTOAPI |
| 86 | unsigned long hHash; |
| 87 | #else |
| 88 | MD5_Core_State core; |
| 89 | unsigned char block[64]; |
| 90 | int blkused; |
| 91 | uint32 lenhi, lenlo; |
| 92 | #endif |
| 93 | }; |
| 94 | |
| 95 | void MD5Init(struct MD5Context *context); |
| 96 | void MD5Update(struct MD5Context *context, unsigned char const *buf, |
| 97 | unsigned len); |
| 98 | void MD5Final(unsigned char digest[16], struct MD5Context *context); |
| 99 | |
| 100 | typedef struct { |
| 101 | uint32 h[5]; |
| 102 | unsigned char block[64]; |
| 103 | int blkused; |
| 104 | uint32 lenhi, lenlo; |
| 105 | } SHA_State; |
| 106 | void SHA_Init(SHA_State * s); |
| 107 | void SHA_Bytes(SHA_State * s, void *p, int len); |
| 108 | void SHA_Final(SHA_State * s, unsigned char *output); |
| 109 | void SHA_Simple(void *p, int len, unsigned char *output); |
| 110 | |
| 111 | void hmac_sha1_simple(void *key, int keylen, void *data, int datalen, |
| 112 | unsigned char *output); |
| 113 | |
| 114 | typedef struct { |
| 115 | uint64 h[8]; |
| 116 | unsigned char block[128]; |
| 117 | int blkused; |
| 118 | uint32 len[4]; |
| 119 | } SHA512_State; |
| 120 | void SHA512_Init(SHA512_State * s); |
| 121 | void SHA512_Bytes(SHA512_State * s, const void *p, int len); |
| 122 | void SHA512_Final(SHA512_State * s, unsigned char *output); |
| 123 | void SHA512_Simple(const void *p, int len, unsigned char *output); |
| 124 | |
| 125 | struct ssh_cipher { |
| 126 | void (*sesskey) (unsigned char *key); /* for ssh 1 */ |
| 127 | void (*encrypt) (unsigned char *blk, int len); |
| 128 | void (*decrypt) (unsigned char *blk, int len); |
| 129 | int blksize; |
| 130 | }; |
| 131 | |
| 132 | struct ssh2_cipher { |
| 133 | void (*setcsiv) (unsigned char *key); /* for ssh 2 */ |
| 134 | void (*setcskey) (unsigned char *key); /* for ssh 2 */ |
| 135 | void (*setsciv) (unsigned char *key); /* for ssh 2 */ |
| 136 | void (*setsckey) (unsigned char *key); /* for ssh 2 */ |
| 137 | void (*encrypt) (unsigned char *blk, int len); |
| 138 | void (*decrypt) (unsigned char *blk, int len); |
| 139 | char *name; |
| 140 | int blksize; |
| 141 | int keylen; |
| 142 | }; |
| 143 | |
| 144 | struct ssh2_ciphers { |
| 145 | int nciphers; |
| 146 | const struct ssh2_cipher *const *list; |
| 147 | }; |
| 148 | |
| 149 | struct ssh_mac { |
| 150 | void (*setcskey) (unsigned char *key); |
| 151 | void (*setsckey) (unsigned char *key); |
| 152 | void (*generate) (unsigned char *blk, int len, unsigned long seq); |
| 153 | int (*verify) (unsigned char *blk, int len, unsigned long seq); |
| 154 | char *name; |
| 155 | int len; |
| 156 | }; |
| 157 | |
| 158 | struct ssh_kex { |
| 159 | /* |
| 160 | * Plugging in another KEX algorithm requires structural chaos, |
| 161 | * so it's hard to abstract them into nice little structures |
| 162 | * like this. Hence, for the moment, this is just a |
| 163 | * placeholder. I claim justification in the fact that OpenSSH |
| 164 | * does this too :-) |
| 165 | */ |
| 166 | char *name; |
| 167 | }; |
| 168 | |
| 169 | struct ssh_signkey { |
| 170 | void *(*newkey) (char *data, int len); |
| 171 | void (*freekey) (void *key); |
| 172 | char *(*fmtkey) (void *key); |
| 173 | unsigned char *(*public_blob) (void *key, int *len); |
| 174 | unsigned char *(*private_blob) (void *key, int *len); |
| 175 | void *(*createkey) (unsigned char *pub_blob, int pub_len, |
| 176 | unsigned char *priv_blob, int priv_len); |
| 177 | void *(*openssh_createkey) (unsigned char **blob, int *len); |
| 178 | int (*openssh_fmtkey) (void *key, unsigned char *blob, int len); |
| 179 | char *(*fingerprint) (void *key); |
| 180 | int (*verifysig) (void *key, char *sig, int siglen, |
| 181 | char *data, int datalen); |
| 182 | unsigned char *(*sign) (void *key, char *data, int datalen, |
| 183 | int *siglen); |
| 184 | char *name; |
| 185 | char *keytype; /* for host key cache */ |
| 186 | }; |
| 187 | |
| 188 | struct ssh_compress { |
| 189 | char *name; |
| 190 | void (*compress_init) (void); |
| 191 | int (*compress) (unsigned char *block, int len, |
| 192 | unsigned char **outblock, int *outlen); |
| 193 | void (*decompress_init) (void); |
| 194 | int (*decompress) (unsigned char *block, int len, |
| 195 | unsigned char **outblock, int *outlen); |
| 196 | int (*disable_compression) (void); |
| 197 | }; |
| 198 | |
| 199 | struct ssh2_userkey { |
| 200 | const struct ssh_signkey *alg; /* the key algorithm */ |
| 201 | void *data; /* the key data */ |
| 202 | char *comment; /* the key comment */ |
| 203 | }; |
| 204 | |
| 205 | extern const struct ssh_cipher ssh_3des; |
| 206 | extern const struct ssh_cipher ssh_des; |
| 207 | extern const struct ssh_cipher ssh_blowfish_ssh1; |
| 208 | extern const struct ssh2_ciphers ssh2_3des; |
| 209 | extern const struct ssh2_ciphers ssh2_des; |
| 210 | extern const struct ssh2_ciphers ssh2_aes; |
| 211 | extern const struct ssh2_ciphers ssh2_blowfish; |
| 212 | extern const struct ssh_kex ssh_diffiehellman; |
| 213 | extern const struct ssh_kex ssh_diffiehellman_gex; |
| 214 | extern const struct ssh_signkey ssh_dss; |
| 215 | extern const struct ssh_signkey ssh_rsa; |
| 216 | extern const struct ssh_mac ssh_md5; |
| 217 | extern const struct ssh_mac ssh_sha1; |
| 218 | extern const struct ssh_mac ssh_sha1_buggy; |
| 219 | |
| 220 | /* |
| 221 | * PuTTY version number formatted as an SSH version string. |
| 222 | */ |
| 223 | extern char sshver[]; |
| 224 | |
| 225 | /* |
| 226 | * Gross hack: pscp will try to start SFTP but fall back to scp1 if |
| 227 | * that fails. This variable is the means by which scp.c can reach |
| 228 | * into the SSH code and find out which one it got. |
| 229 | */ |
| 230 | extern int ssh_fallback_cmd; |
| 231 | |
| 232 | #ifndef MSCRYPTOAPI |
| 233 | void SHATransform(word32 * digest, word32 * data); |
| 234 | #endif |
| 235 | |
| 236 | int random_byte(void); |
| 237 | void random_add_noise(void *noise, int length); |
| 238 | void random_add_heavynoise(void *noise, int length); |
| 239 | |
| 240 | void logevent(char *); |
| 241 | void *new_sock_channel(Socket s); // allocates and register a new channel for port forwarding |
| 242 | void ssh_send_port_open(void *channel, char *hostname, int port, char *org); |
| 243 | |
| 244 | Bignum copybn(Bignum b); |
| 245 | Bignum bn_power_2(int n); |
| 246 | void bn_restore_invariant(Bignum b); |
| 247 | Bignum bignum_from_long(unsigned long n); |
| 248 | void freebn(Bignum b); |
| 249 | Bignum modpow(Bignum base, Bignum exp, Bignum mod); |
| 250 | Bignum modmul(Bignum a, Bignum b, Bignum mod); |
| 251 | void decbn(Bignum n); |
| 252 | extern Bignum Zero, One; |
| 253 | Bignum bignum_from_bytes(unsigned char *data, int nbytes); |
| 254 | int ssh1_read_bignum(unsigned char *data, Bignum * result); |
| 255 | int bignum_bitcount(Bignum bn); |
| 256 | int ssh1_bignum_length(Bignum bn); |
| 257 | int ssh2_bignum_length(Bignum bn); |
| 258 | int bignum_byte(Bignum bn, int i); |
| 259 | int bignum_bit(Bignum bn, int i); |
| 260 | void bignum_set_bit(Bignum bn, int i, int value); |
| 261 | int ssh1_write_bignum(void *data, Bignum bn); |
| 262 | Bignum biggcd(Bignum a, Bignum b); |
| 263 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus); |
| 264 | Bignum bignum_add_long(Bignum number, unsigned long addend); |
| 265 | Bignum bigmul(Bignum a, Bignum b); |
| 266 | Bignum bigmuladd(Bignum a, Bignum b, Bignum addend); |
| 267 | Bignum bigdiv(Bignum a, Bignum b); |
| 268 | Bignum bigmod(Bignum a, Bignum b); |
| 269 | Bignum modinv(Bignum number, Bignum modulus); |
| 270 | Bignum bignum_bitmask(Bignum number); |
| 271 | Bignum bignum_rshift(Bignum number, int shift); |
| 272 | int bignum_cmp(Bignum a, Bignum b); |
| 273 | char *bignum_decimal(Bignum x); |
| 274 | |
| 275 | void dh_setup_group1(void); |
| 276 | void dh_setup_group(Bignum pval, Bignum gval); |
| 277 | void dh_cleanup(void); |
| 278 | Bignum dh_create_e(int nbits); |
| 279 | Bignum dh_find_K(Bignum f); |
| 280 | |
| 281 | int loadrsakey(char *filename, struct RSAKey *key, char *passphrase); |
| 282 | int rsakey_encrypted(char *filename, char **comment); |
| 283 | int rsakey_pubblob(char *filename, void **blob, int *bloblen); |
| 284 | |
| 285 | int saversakey(char *filename, struct RSAKey *key, char *passphrase); |
| 286 | |
| 287 | void base64_encode_atom(unsigned char *data, int n, char *out); |
| 288 | |
| 289 | /* ssh2_load_userkey can return this as an error */ |
| 290 | extern struct ssh2_userkey ssh2_wrong_passphrase; |
| 291 | #define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase) |
| 292 | |
| 293 | int ssh2_userkey_encrypted(char *filename, char **comment); |
| 294 | struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase); |
| 295 | char *ssh2_userkey_loadpub(char *filename, char **algorithm, |
| 296 | int *pub_blob_len); |
| 297 | int ssh2_save_userkey(char *filename, struct ssh2_userkey *key, |
| 298 | char *passphrase); |
| 299 | |
| 300 | enum { |
| 301 | SSH_KEYTYPE_UNOPENABLE, |
| 302 | SSH_KEYTYPE_UNKNOWN, |
| 303 | SSH_KEYTYPE_SSH1, SSH_KEYTYPE_SSH2, |
| 304 | SSH_KEYTYPE_OPENSSH, SSH_KEYTYPE_SSHCOM |
| 305 | }; |
| 306 | int key_type(char *filename); |
| 307 | char *key_type_to_str(int type); |
| 308 | |
| 309 | int import_possible(int type); |
| 310 | int import_target_type(int type); |
| 311 | int import_encrypted(char *filename, int type, char **comment); |
| 312 | int import_ssh1(char *filename, int type, struct RSAKey *key,char *passphrase); |
| 313 | struct ssh2_userkey *import_ssh2(char *filename, int type, char *passphrase); |
| 314 | int export_ssh1(char *filename, int type, struct RSAKey *key,char *passphrase); |
| 315 | int export_ssh2(char *filename, int type, |
| 316 | struct ssh2_userkey *key, char *passphrase); |
| 317 | |
| 318 | void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len); |
| 319 | void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len); |
| 320 | void des3_decrypt_pubkey_ossh(unsigned char *key, unsigned char *iv, |
| 321 | unsigned char *blk, int len); |
| 322 | void des3_encrypt_pubkey_ossh(unsigned char *key, unsigned char *iv, |
| 323 | unsigned char *blk, int len); |
| 324 | void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk, |
| 325 | int len); |
| 326 | void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk, |
| 327 | int len); |
| 328 | |
| 329 | /* |
| 330 | * For progress updates in the key generation utility. |
| 331 | */ |
| 332 | #define PROGFN_INITIALISE 1 |
| 333 | #define PROGFN_LIN_PHASE 2 |
| 334 | #define PROGFN_EXP_PHASE 3 |
| 335 | #define PROGFN_PHASE_EXTENT 4 |
| 336 | #define PROGFN_READY 5 |
| 337 | #define PROGFN_PROGRESS 6 |
| 338 | typedef void (*progfn_t) (void *param, int action, int phase, int progress); |
| 339 | |
| 340 | int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn, |
| 341 | void *pfnparam); |
| 342 | int dsa_generate(struct dss_key *key, int bits, progfn_t pfn, |
| 343 | void *pfnparam); |
| 344 | Bignum primegen(int bits, int modulus, int residue, Bignum factor, |
| 345 | int phase, progfn_t pfn, void *pfnparam); |
| 346 | |
| 347 | |
| 348 | /* |
| 349 | * zlib compression. |
| 350 | */ |
| 351 | void zlib_compress_init(void); |
| 352 | void zlib_decompress_init(void); |
| 353 | int zlib_compress_block(unsigned char *block, int len, |
| 354 | unsigned char **outblock, int *outlen); |
| 355 | int zlib_decompress_block(unsigned char *block, int len, |
| 356 | unsigned char **outblock, int *outlen); |
| 357 | |
| 358 | /* |
| 359 | * SSH1 agent messages. |
| 360 | */ |
| 361 | #define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1 |
| 362 | #define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2 |
| 363 | #define SSH1_AGENTC_RSA_CHALLENGE 3 |
| 364 | #define SSH1_AGENT_RSA_RESPONSE 4 |
| 365 | #define SSH1_AGENTC_ADD_RSA_IDENTITY 7 |
| 366 | #define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8 |
| 367 | #define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */ |
| 368 | |
| 369 | /* |
| 370 | * Messages common to SSH1 and OpenSSH's SSH2. |
| 371 | */ |
| 372 | #define SSH_AGENT_FAILURE 5 |
| 373 | #define SSH_AGENT_SUCCESS 6 |
| 374 | |
| 375 | /* |
| 376 | * OpenSSH's SSH2 agent messages. |
| 377 | */ |
| 378 | #define SSH2_AGENTC_REQUEST_IDENTITIES 11 |
| 379 | #define SSH2_AGENT_IDENTITIES_ANSWER 12 |
| 380 | #define SSH2_AGENTC_SIGN_REQUEST 13 |
| 381 | #define SSH2_AGENT_SIGN_RESPONSE 14 |
| 382 | #define SSH2_AGENTC_ADD_IDENTITY 17 |
| 383 | #define SSH2_AGENTC_REMOVE_IDENTITY 18 |
| 384 | #define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19 |
| 385 | |
| 386 | /* |
| 387 | * Need this to warn about support for the original SSH2 keyfile |
| 388 | * format. |
| 389 | */ |
| 390 | void old_keyfile_warning(void); |