mirror of
https://github.com/libssh2/libssh2.git
synced 2025-12-03 13:31:12 +03:00
fix return codes, shortened a very long function name
This commit is contained in:
114
src/kex.c
114
src/kex.c
@@ -69,22 +69,20 @@
|
|||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
/* kex_method_diffie_hellman_groupGP_sha1_key_exchange
|
/*
|
||||||
|
* diffie_hellman_sha1
|
||||||
|
*
|
||||||
* Diffie Hellman Key Exchange, Group Agnostic
|
* Diffie Hellman Key Exchange, Group Agnostic
|
||||||
*/
|
*/
|
||||||
static int
|
static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
|
||||||
kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|
||||||
_libssh2_bn *g,
|
_libssh2_bn *g,
|
||||||
_libssh2_bn *p,
|
_libssh2_bn *p,
|
||||||
int group_order,
|
int group_order,
|
||||||
unsigned char
|
unsigned char packet_type_init,
|
||||||
packet_type_init,
|
unsigned char packet_type_reply,
|
||||||
unsigned char
|
|
||||||
packet_type_reply,
|
|
||||||
unsigned char *midhash,
|
unsigned char *midhash,
|
||||||
unsigned long midhash_len,
|
unsigned long midhash_len,
|
||||||
kmdhgGPsha1kex_state_t
|
kmdhgGPsha1kex_state_t *exchange_state)
|
||||||
* exchange_state)
|
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int rc;
|
int rc;
|
||||||
@@ -122,7 +120,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
if (!exchange_state->e_packet) {
|
if (!exchange_state->e_packet) {
|
||||||
libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Out of memory error",
|
libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Out of memory error",
|
||||||
0);
|
0);
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_ALLOC;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
exchange_state->e_packet[0] = packet_type_init;
|
exchange_state->e_packet[0] = packet_type_init;
|
||||||
@@ -171,7 +169,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
return PACKET_EAGAIN;
|
return PACKET_EAGAIN;
|
||||||
} else if (burn_type <= 0) {
|
} else if (burn_type <= 0) {
|
||||||
/* Failed to receive a packet */
|
/* Failed to receive a packet */
|
||||||
ret = -1;
|
ret = burn_type;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
session->burn_optimistic_kexinit = 0;
|
session->burn_optimistic_kexinit = 0;
|
||||||
@@ -196,7 +194,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
if (rc) {
|
if (rc) {
|
||||||
libssh2_error(session, LIBSSH2_ERROR_TIMEOUT,
|
libssh2_error(session, LIBSSH2_ERROR_TIMEOUT,
|
||||||
"Timed out waiting for KEX reply", 0);
|
"Timed out waiting for KEX reply", 0);
|
||||||
ret = -1;
|
ret = rc;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,7 +209,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||||
"Unable to allocate memory for a copy of the host key",
|
"Unable to allocate memory for a copy of the host key",
|
||||||
0);
|
0);
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_ALLOC;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
memcpy(session->server_hostkey, exchange_state->s,
|
memcpy(session->server_hostkey, exchange_state->s,
|
||||||
@@ -263,12 +261,12 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
}
|
}
|
||||||
#endif /* LIBSSH2DEBUG */
|
#endif /* LIBSSH2DEBUG */
|
||||||
|
|
||||||
if (session->hostkey->
|
if (session->hostkey->init(session, session->server_hostkey,
|
||||||
init(session, session->server_hostkey, session->server_hostkey_len,
|
session->server_hostkey_len,
|
||||||
&session->server_hostkey_abstract)) {
|
&session->server_hostkey_abstract)) {
|
||||||
libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT,
|
libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT,
|
||||||
"Unable to initialize hostkey importer", 0);
|
"Unable to initialize hostkey importer", 0);
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_HOSTKEY_INIT;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,7 +294,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
if (!exchange_state->k_value) {
|
if (!exchange_state->k_value) {
|
||||||
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||||
"Unable to allocate buffer for K", 0);
|
"Unable to allocate buffer for K", 0);
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_ALLOC;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
_libssh2_htonu32(exchange_state->k_value,
|
_libssh2_htonu32(exchange_state->k_value,
|
||||||
@@ -423,9 +421,8 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
if (rc == PACKET_EAGAIN) {
|
if (rc == PACKET_EAGAIN) {
|
||||||
return PACKET_EAGAIN;
|
return PACKET_EAGAIN;
|
||||||
} else if (rc) {
|
} else if (rc) {
|
||||||
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
libssh2_error(session, rc, "Unable to send NEWKEYS message", 0);
|
||||||
"Unable to send NEWKEYS message", 0);
|
ret = rc;
|
||||||
ret = -1;
|
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -440,9 +437,8 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
if (rc == PACKET_EAGAIN) {
|
if (rc == PACKET_EAGAIN) {
|
||||||
return PACKET_EAGAIN;
|
return PACKET_EAGAIN;
|
||||||
} else if (rc) {
|
} else if (rc) {
|
||||||
libssh2_error(session, LIBSSH2_ERROR_TIMEOUT,
|
libssh2_error(session, rc, "Timed out waiting for NEWKEYS", 0);
|
||||||
"Timed out waiting for NEWKEYS", 0);
|
ret = rc;
|
||||||
ret = -1;
|
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
/* The first key exchange has been performed,
|
/* The first key exchange has been performed,
|
||||||
@@ -457,7 +453,9 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
if (!session->session_id) {
|
if (!session->session_id) {
|
||||||
session->session_id = LIBSSH2_ALLOC(session, SHA_DIGEST_LENGTH);
|
session->session_id = LIBSSH2_ALLOC(session, SHA_DIGEST_LENGTH);
|
||||||
if (!session->session_id) {
|
if (!session->session_id) {
|
||||||
ret = -1;
|
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||||
|
"Unable to allocate buffer for SHA digest", 0);
|
||||||
|
ret = LIBSSH2_ERROR_ALLOC;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
memcpy(session->session_id, exchange_state->h_sig_comp,
|
memcpy(session->session_id, exchange_state->h_sig_comp,
|
||||||
@@ -489,7 +487,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
secret_len, "C");
|
secret_len, "C");
|
||||||
if (!secret) {
|
if (!secret) {
|
||||||
LIBSSH2_FREE(session, iv);
|
LIBSSH2_FREE(session, iv);
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_KEX_FAILURE;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
if (session->local.crypt->
|
if (session->local.crypt->
|
||||||
@@ -497,7 +495,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
&free_secret, 1, &session->local.crypt_abstract)) {
|
&free_secret, 1, &session->local.crypt_abstract)) {
|
||||||
LIBSSH2_FREE(session, iv);
|
LIBSSH2_FREE(session, iv);
|
||||||
LIBSSH2_FREE(session, secret);
|
LIBSSH2_FREE(session, secret);
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_KEX_FAILURE;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -528,7 +526,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
session->remote.crypt->
|
session->remote.crypt->
|
||||||
iv_len, "B");
|
iv_len, "B");
|
||||||
if (!iv) {
|
if (!iv) {
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_KEX_FAILURE;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
LIBSSH2_KEX_METHOD_DIFFIE_HELLMAN_SHA1_HASH(secret,
|
LIBSSH2_KEX_METHOD_DIFFIE_HELLMAN_SHA1_HASH(secret,
|
||||||
@@ -536,7 +534,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
secret_len, "D");
|
secret_len, "D");
|
||||||
if (!secret) {
|
if (!secret) {
|
||||||
LIBSSH2_FREE(session, iv);
|
LIBSSH2_FREE(session, iv);
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_KEX_FAILURE;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
if (session->remote.crypt->
|
if (session->remote.crypt->
|
||||||
@@ -544,7 +542,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
&free_secret, 0, &session->remote.crypt_abstract)) {
|
&free_secret, 0, &session->remote.crypt_abstract)) {
|
||||||
LIBSSH2_FREE(session, iv);
|
LIBSSH2_FREE(session, iv);
|
||||||
LIBSSH2_FREE(session, secret);
|
LIBSSH2_FREE(session, secret);
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_KEX_FAILURE;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -573,7 +571,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
session->local.mac->
|
session->local.mac->
|
||||||
key_len, "E");
|
key_len, "E");
|
||||||
if (!key) {
|
if (!key) {
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_KEX_FAILURE;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
session->local.mac->init(session, key, &free_key,
|
session->local.mac->init(session, key, &free_key,
|
||||||
@@ -599,7 +597,7 @@ kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
session->remote.mac->
|
session->remote.mac->
|
||||||
key_len, "F");
|
key_len, "F");
|
||||||
if (!key) {
|
if (!key) {
|
||||||
ret = -1;
|
ret = LIBSSH2_ERROR_KEX_FAILURE;
|
||||||
goto clean_exit;
|
goto clean_exit;
|
||||||
}
|
}
|
||||||
session->remote.mac->init(session, key, &free_key,
|
session->remote.mac->init(session, key, &free_key,
|
||||||
@@ -691,16 +689,9 @@ kex_method_diffie_hellman_group1_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
|
|
||||||
key_state->state = libssh2_NB_state_created;
|
key_state->state = libssh2_NB_state_created;
|
||||||
}
|
}
|
||||||
|
ret = diffie_hellman_sha1(session, key_state->g, key_state->p, 128,
|
||||||
ret =
|
SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY,
|
||||||
kex_method_diffie_hellman_groupGP_sha1_key_exchange(session,
|
NULL, 0, &key_state->exchange_state);
|
||||||
key_state->g,
|
|
||||||
key_state->p, 128,
|
|
||||||
SSH_MSG_KEXDH_INIT,
|
|
||||||
SSH_MSG_KEXDH_REPLY,
|
|
||||||
NULL, 0,
|
|
||||||
&key_state->
|
|
||||||
exchange_state);
|
|
||||||
if (ret == PACKET_EAGAIN) {
|
if (ret == PACKET_EAGAIN) {
|
||||||
return PACKET_EAGAIN;
|
return PACKET_EAGAIN;
|
||||||
}
|
}
|
||||||
@@ -774,16 +765,9 @@ kex_method_diffie_hellman_group14_sha1_key_exchange(LIBSSH2_SESSION *session,
|
|||||||
|
|
||||||
key_state->state = libssh2_NB_state_created;
|
key_state->state = libssh2_NB_state_created;
|
||||||
}
|
}
|
||||||
ret =
|
ret = diffie_hellman_sha1(session, key_state->g, key_state->p,
|
||||||
kex_method_diffie_hellman_groupGP_sha1_key_exchange(session,
|
256, SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY,
|
||||||
key_state->g,
|
NULL, 0, &key_state->exchange_state);
|
||||||
key_state->p,
|
|
||||||
256,
|
|
||||||
SSH_MSG_KEXDH_INIT,
|
|
||||||
SSH_MSG_KEXDH_REPLY,
|
|
||||||
NULL, 0,
|
|
||||||
&key_state->
|
|
||||||
exchange_state);
|
|
||||||
if (ret == PACKET_EAGAIN) {
|
if (ret == PACKET_EAGAIN) {
|
||||||
return PACKET_EAGAIN;
|
return PACKET_EAGAIN;
|
||||||
}
|
}
|
||||||
@@ -878,11 +862,11 @@ kex_method_diffie_hellman_group_exchange_sha1_key_exchange
|
|||||||
_libssh2_bn_from_bin(key_state->g, g_len, s);
|
_libssh2_bn_from_bin(key_state->g, g_len, s);
|
||||||
s += g_len;
|
s += g_len;
|
||||||
|
|
||||||
ret =
|
ret = diffie_hellman_sha1(session, key_state->g, key_state->p, p_len,
|
||||||
kex_method_diffie_hellman_groupGP_sha1_key_exchange
|
SSH_MSG_KEX_DH_GEX_INIT,
|
||||||
(session, key_state->g, key_state->p, p_len,
|
SSH_MSG_KEX_DH_GEX_REPLY,
|
||||||
SSH_MSG_KEX_DH_GEX_INIT, SSH_MSG_KEX_DH_GEX_REPLY,
|
key_state->data + 1,
|
||||||
key_state->data + 1, key_state->data_len - 1,
|
key_state->data_len - 1,
|
||||||
&key_state->exchange_state);
|
&key_state->exchange_state);
|
||||||
if (ret == PACKET_EAGAIN) {
|
if (ret == PACKET_EAGAIN) {
|
||||||
return PACKET_EAGAIN;
|
return PACKET_EAGAIN;
|
||||||
@@ -1056,7 +1040,7 @@ static int kexinit(LIBSSH2_SESSION * session)
|
|||||||
if (!data) {
|
if (!data) {
|
||||||
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||||
"Unable to allocate memory", 0);
|
"Unable to allocate memory", 0);
|
||||||
return -1;
|
return LIBSSH2_ERROR_ALLOC;
|
||||||
}
|
}
|
||||||
|
|
||||||
*(s++) = SSH_MSG_KEXINIT;
|
*(s++) = SSH_MSG_KEXINIT;
|
||||||
@@ -1143,10 +1127,10 @@ static int kexinit(LIBSSH2_SESSION * session)
|
|||||||
}
|
}
|
||||||
else if (rc) {
|
else if (rc) {
|
||||||
LIBSSH2_FREE(session, data);
|
LIBSSH2_FREE(session, data);
|
||||||
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
libssh2_error(session, rc,
|
||||||
"Unable to send KEXINIT packet to remote host", 0);
|
"Unable to send KEXINIT packet to remote host", 0);
|
||||||
session->kexinit_state = libssh2_NB_state_idle;
|
session->kexinit_state = libssh2_NB_state_idle;
|
||||||
return -1;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session->local.kexinit) {
|
if (session->local.kexinit) {
|
||||||
@@ -1662,6 +1646,8 @@ static int kex_agree_methods(LIBSSH2_SESSION * session, unsigned char *data,
|
|||||||
/* libssh2_kex_exchange
|
/* libssh2_kex_exchange
|
||||||
* Exchange keys
|
* Exchange keys
|
||||||
* Returns 0 on success, non-zero on failure
|
* Returns 0 on success, non-zero on failure
|
||||||
|
*
|
||||||
|
* Returns some errors without libssh2_error()
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
|
libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
|
||||||
@@ -1746,9 +1732,8 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
|
|||||||
session->remote.kexinit_len = key_state->data_len;
|
session->remote.kexinit_len = key_state->data_len;
|
||||||
|
|
||||||
if (kex_agree_methods(session, key_state->data,
|
if (kex_agree_methods(session, key_state->data,
|
||||||
key_state->data_len)) {
|
key_state->data_len))
|
||||||
rc = -1;
|
rc = LIBSSH2_ERROR_KEX_FAILURE;
|
||||||
}
|
|
||||||
|
|
||||||
key_state->state = libssh2_NB_state_sent2;
|
key_state->state = libssh2_NB_state_sent2;
|
||||||
}
|
}
|
||||||
@@ -1758,8 +1743,7 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
|
|||||||
|
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
if (key_state->state == libssh2_NB_state_sent2) {
|
if (key_state->state == libssh2_NB_state_sent2) {
|
||||||
retcode =
|
retcode = session->kex->exchange_keys(session,
|
||||||
session->kex->exchange_keys(session,
|
|
||||||
&key_state->key_state_low);
|
&key_state->key_state_low);
|
||||||
if (retcode == PACKET_EAGAIN) {
|
if (retcode == PACKET_EAGAIN) {
|
||||||
session->state &= ~LIBSSH2_STATE_KEX_ACTIVE;
|
session->state &= ~LIBSSH2_STATE_KEX_ACTIVE;
|
||||||
@@ -1767,7 +1751,7 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
|
|||||||
} else if (retcode) {
|
} else if (retcode) {
|
||||||
libssh2_error(session, LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE,
|
libssh2_error(session, LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE,
|
||||||
"Unrecoverable error exchanging keys", 0);
|
"Unrecoverable error exchanging keys", 0);
|
||||||
rc = -1;
|
rc = retcode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user