| 29 |
#include "ttxssh.h" |
#include "ttxssh.h" |
| 30 |
#include "kex.h" |
#include "kex.h" |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
char *myproposal[PROPOSAL_MAX] = { |
| 34 |
|
KEX_DEFAULT_KEX, |
| 35 |
|
KEX_DEFAULT_PK_ALG, |
| 36 |
|
KEX_DEFAULT_ENCRYPT, |
| 37 |
|
KEX_DEFAULT_ENCRYPT, |
| 38 |
|
KEX_DEFAULT_MAC, |
| 39 |
|
KEX_DEFAULT_MAC, |
| 40 |
|
KEX_DEFAULT_COMP, |
| 41 |
|
KEX_DEFAULT_COMP, |
| 42 |
|
KEX_DEFAULT_LANG, |
| 43 |
|
KEX_DEFAULT_LANG, |
| 44 |
|
}; |
| 45 |
|
|
| 46 |
|
struct ssh2_kex_algorithm_t { |
| 47 |
|
kex_algorithm kextype; |
| 48 |
|
char *name; |
| 49 |
|
const EVP_MD *(*evp_md)(void); |
| 50 |
|
}; |
| 51 |
|
|
| 52 |
|
static const struct ssh2_kex_algorithm_t ssh2_kex_algorithms[] = { |
| 53 |
|
{KEX_DH_GRP1_SHA1, "diffie-hellman-group1-sha1", EVP_sha1}, // RFC4253 |
| 54 |
|
{KEX_DH_GRP14_SHA1, "diffie-hellman-group14-sha1", EVP_sha1}, // RFC4253 |
| 55 |
|
{KEX_DH_GEX_SHA1, "diffie-hellman-group-exchange-sha1", EVP_sha1}, // RFC4419 |
| 56 |
|
{KEX_DH_GEX_SHA256, "diffie-hellman-group-exchange-sha256", EVP_sha256}, // RFC4419 |
| 57 |
|
{KEX_ECDH_SHA2_256, "ecdh-sha2-nistp256", EVP_sha256}, // RFC5656 |
| 58 |
|
{KEX_ECDH_SHA2_384, "ecdh-sha2-nistp384", EVP_sha384}, // RFC5656 |
| 59 |
|
{KEX_ECDH_SHA2_521, "ecdh-sha2-nistp521", EVP_sha512}, // RFC5656 |
| 60 |
|
{KEX_DH_GRP14_SHA256, "diffie-hellman-group14-sha256", EVP_sha256}, // RFC8268 |
| 61 |
|
{KEX_DH_GRP16_SHA512, "diffie-hellman-group16-sha512", EVP_sha512}, // RFC8268 |
| 62 |
|
{KEX_DH_GRP18_SHA512, "diffie-hellman-group18-sha512", EVP_sha512}, // RFC8268 |
| 63 |
|
{KEX_DH_NONE , NULL, NULL}, |
| 64 |
|
}; |
| 65 |
|
|
| 66 |
|
|
| 67 |
extern SSHKeys current_keys[MODE_MAX]; |
extern SSHKeys current_keys[MODE_MAX]; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
char* get_kex_algorithm_name(kex_algorithm kextype) |
| 71 |
|
{ |
| 72 |
|
const struct ssh2_kex_algorithm_t *ptr = ssh2_kex_algorithms; |
| 73 |
|
|
| 74 |
|
while (ptr->name != NULL) { |
| 75 |
|
if (kextype == ptr->kextype) { |
| 76 |
|
return ptr->name; |
| 77 |
|
} |
| 78 |
|
ptr++; |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
// not found. |
| 82 |
|
return "unknown"; |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
const EVP_MD* get_kex_algorithm_EVP_MD(kex_algorithm kextype) |
| 86 |
|
{ |
| 87 |
|
const struct ssh2_kex_algorithm_t *ptr = ssh2_kex_algorithms; |
| 88 |
|
|
| 89 |
|
while (ptr->name != NULL) { |
| 90 |
|
if (kextype == ptr->kextype) { |
| 91 |
|
return ptr->evp_md(); |
| 92 |
|
} |
| 93 |
|
ptr++; |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
// not found. |
| 97 |
|
return EVP_md_null(); |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
void normalize_kex_order(char *buf) |
| 101 |
|
{ |
| 102 |
|
static char default_strings[] = { |
| 103 |
|
KEX_ECDH_SHA2_256, |
| 104 |
|
KEX_ECDH_SHA2_384, |
| 105 |
|
KEX_ECDH_SHA2_521, |
| 106 |
|
KEX_DH_GRP18_SHA512, |
| 107 |
|
KEX_DH_GRP16_SHA512, |
| 108 |
|
KEX_DH_GRP14_SHA256, |
| 109 |
|
KEX_DH_GEX_SHA256, |
| 110 |
|
KEX_DH_GEX_SHA1, |
| 111 |
|
KEX_DH_GRP14_SHA1, |
| 112 |
|
KEX_DH_GRP1_SHA1, |
| 113 |
|
KEX_DH_NONE, |
| 114 |
|
}; |
| 115 |
|
|
| 116 |
|
normalize_generic_order(buf, default_strings, NUM_ELEM(default_strings)); |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
kex_algorithm choose_SSH2_kex_algorithm(char *server_proposal, char *my_proposal) |
| 120 |
|
{ |
| 121 |
|
kex_algorithm type = KEX_DH_UNKNOWN; |
| 122 |
|
char str_kextype[40]; |
| 123 |
|
const struct ssh2_kex_algorithm_t *ptr = ssh2_kex_algorithms; |
| 124 |
|
|
| 125 |
|
choose_SSH2_proposal(server_proposal, my_proposal, str_kextype, sizeof(str_kextype)); |
| 126 |
|
|
| 127 |
|
while (ptr->name != NULL) { |
| 128 |
|
if (strcmp(ptr->name, str_kextype) == 0) { |
| 129 |
|
type = ptr->kextype; |
| 130 |
|
break; |
| 131 |
|
} |
| 132 |
|
ptr++; |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
return (type); |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
// KEXアルゴリズム優先順位に応じて、myproposal[]を書き換える。 |
| 139 |
|
// (2011.2.28 yutaka) |
| 140 |
|
void SSH2_update_kex_myproposal(PTInstVar pvar) |
| 141 |
|
{ |
| 142 |
|
static char buf[512]; // TODO: malloc()にすべき |
| 143 |
|
int index; |
| 144 |
|
int len, i; |
| 145 |
|
|
| 146 |
|
// 通信中には呼ばれないはずだが、念のため。(2006.6.26 maya) |
| 147 |
|
if (pvar->socket != INVALID_SOCKET) { |
| 148 |
|
return; |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
buf[0] = '\0'; |
| 152 |
|
for (i = 0 ; pvar->settings.KexOrder[i] != 0 ; i++) { |
| 153 |
|
index = pvar->settings.KexOrder[i] - '0'; |
| 154 |
|
if (index == KEX_DH_NONE) // disabled line |
| 155 |
|
break; |
| 156 |
|
strncat_s(buf, sizeof(buf), get_kex_algorithm_name(index), _TRUNCATE); |
| 157 |
|
strncat_s(buf, sizeof(buf), ",", _TRUNCATE); |
| 158 |
|
} |
| 159 |
|
len = strlen(buf); |
| 160 |
|
if (len > 0) |
| 161 |
|
buf[len - 1] = '\0'; // get rid of comma |
| 162 |
|
myproposal[PROPOSAL_KEX_ALGS] = buf; |
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
|
| 166 |
static DH *dh_new_group_asc(const char *gen, const char *modulus) |
static DH *dh_new_group_asc(const char *gen, const char *modulus) |
| 167 |
{ |
{ |
| 168 |
DH *dh = NULL; |
DH *dh = NULL; |