1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-11-20 02:42:09 +03:00

misc.c : String buffer API improvements (#332)

Files : misc.c, hostkey.c, kex.c, misc.h, openssl.c, sftp.c

Notes : 
* updated _libssh2_get_bignum_bytes and _libssh2_get_string. Now pass in length as an argument instead of returning it to keep signedness correct. Now returns -1 for failure, 0 for success.

_libssh2_check_length now returns 0 on success and -1 on failure to match the other string_buf functions. Added comment to _libssh2_check_length.

Credit : Will Cosgrove
This commit is contained in:
Will Cosgrove
2019-04-23 10:28:01 -07:00
committed by GitHub
parent a19d85319d
commit 8ab5c36a32
7 changed files with 121 additions and 131 deletions

View File

@@ -1705,7 +1705,7 @@ kex_method_diffie_hellman_group_exchange_sha1_key_exchange
}
if(key_state->state == libssh2_NB_state_sent1) {
unsigned int p_len, g_len;
size_t p_len, g_len;
unsigned char *p, *g;
struct string_buf buf;
@@ -1721,15 +1721,13 @@ kex_method_diffie_hellman_group_exchange_sha1_key_exchange
buf.dataptr++; /* increment to big num */
p_len = _libssh2_get_bignum_bytes(&buf, &p);
if(p_len <= 0) {
if(_libssh2_get_bignum_bytes(&buf, &p, &p_len)) {
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Unexpected value");
goto dh_gex_clean_exit;
}
g_len = _libssh2_get_bignum_bytes(&buf, &g);
if(g_len <= 0) {
if(_libssh2_get_bignum_bytes(&buf, &g, &g_len)) {
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Unexpected value");
goto dh_gex_clean_exit;
@@ -1833,7 +1831,7 @@ kex_method_diffie_hellman_group_exchange_sha256_key_exchange
if(key_state->state == libssh2_NB_state_sent1) {
unsigned char *p, *g;
unsigned long p_len, g_len;
size_t p_len, g_len;
struct string_buf buf;
if(key_state->data_len < 9) {
@@ -1848,15 +1846,13 @@ kex_method_diffie_hellman_group_exchange_sha256_key_exchange
buf.dataptr++; /* increment to big num */
p_len = _libssh2_get_bignum_bytes(&buf, &p);
if(p_len <= 0) {
if(_libssh2_get_bignum_bytes(&buf, &p, &p_len)) {
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Unexpected value");
goto dh_gex_clean_exit;
}
g_len = _libssh2_get_bignum_bytes(&buf, &g);
if(g_len <= 0) {
if(_libssh2_get_bignum_bytes(&buf, &g, &g_len)) {
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Unexpected value");
goto dh_gex_clean_exit;
@@ -2675,7 +2671,7 @@ curve25519_sha256(LIBSSH2_SESSION *session, unsigned char *data,
if(exchange_state->state == libssh2_NB_state_created) {
/* parse INIT reply data */
unsigned char *server_public_key, *server_host_key;
unsigned int server_public_key_len;
size_t server_public_key_len, hostkey_len;
struct string_buf buf;
if(data_len < 5) {
@@ -2689,14 +2685,13 @@ curve25519_sha256(LIBSSH2_SESSION *session, unsigned char *data,
buf.dataptr = buf.data;
buf.dataptr++; /* advance past packet type */
session->server_hostkey_len =
_libssh2_get_c_string(&buf, &server_host_key);
if(session->server_hostkey_len <= 0) {
if(_libssh2_get_string(&buf, &server_host_key, &hostkey_len)) {
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Unexpected key length");
goto clean_exit;
}
session->server_hostkey_len = (u_int32_t)hostkey_len;
session->server_hostkey = LIBSSH2_ALLOC(session,
session->server_hostkey_len);
if(!session->server_hostkey) {
@@ -2806,8 +2801,8 @@ curve25519_sha256(LIBSSH2_SESSION *session, unsigned char *data,
}
/* server public key Q_S */
if((server_public_key_len =
_libssh2_get_c_string(&buf, &server_public_key)) <= 0) {
if(_libssh2_get_string(&buf, &server_public_key,
&server_public_key_len)) {
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Unexpected key length");
goto clean_exit;
@@ -2821,12 +2816,11 @@ curve25519_sha256(LIBSSH2_SESSION *session, unsigned char *data,
}
/* server signature */
if((exchange_state->h_sig_len =
_libssh2_get_c_string(&buf, &exchange_state->h_sig)) <= 0) {
if(_libssh2_get_string(&buf, &exchange_state->h_sig,
&(exchange_state->h_sig_len))) {
ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT,
"Unexpected curve25519 server sig length");
goto clean_exit;
}
/* Compute the shared secret K */