code style: unify code style
Indent-level: 4 Max columns: 79 No spaces after if/for/while Unified brace positions Unified white spaces
Этот коммит содержится в:
родитель
76f1e8735b
Коммит
12bddb0d45
@ -147,3 +147,6 @@ $(VCPROJ): win32/vc8proj.head win32/vc8proj.foot Makefile.am
|
||||
done; \
|
||||
cat $(srcdir)/vc8proj.foot) | \
|
||||
awk '{printf("%s\r\n", gensub("\r", "", "g"))}' > $@ )
|
||||
|
||||
checksrc:
|
||||
perl src/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE -Wsrc/libssh2_config.h src/*.[ch]
|
||||
|
@ -27,7 +27,7 @@
|
||||
|
||||
#include "blf.h"
|
||||
|
||||
#define MINIMUM(a,b) (((a) < (b)) ? (a) : (b))
|
||||
#define MINIMUM(a,b) (((a) < (b)) ? (a) : (b))
|
||||
|
||||
/*
|
||||
* pkcs #5 pbkdf2 implementation using the "bcrypt" hash
|
||||
@ -59,120 +59,122 @@
|
||||
static void
|
||||
bcrypt_hash(uint8_t *sha2pass, uint8_t *sha2salt, uint8_t *out)
|
||||
{
|
||||
blf_ctx state;
|
||||
uint8_t ciphertext[BCRYPT_HASHSIZE] =
|
||||
"OxychromaticBlowfishSwatDynamite";
|
||||
uint32_t cdata[BCRYPT_BLOCKS];
|
||||
int i;
|
||||
uint16_t j;
|
||||
size_t shalen = SHA512_DIGEST_LENGTH;
|
||||
blf_ctx state;
|
||||
uint8_t ciphertext[BCRYPT_HASHSIZE] =
|
||||
"OxychromaticBlowfishSwatDynamite";
|
||||
uint32_t cdata[BCRYPT_BLOCKS];
|
||||
int i;
|
||||
uint16_t j;
|
||||
size_t shalen = SHA512_DIGEST_LENGTH;
|
||||
|
||||
/* key expansion */
|
||||
Blowfish_initstate(&state);
|
||||
Blowfish_expandstate(&state, sha2salt, shalen, sha2pass, shalen);
|
||||
for (i = 0; i < 64; i++) {
|
||||
Blowfish_expand0state(&state, sha2salt, shalen);
|
||||
Blowfish_expand0state(&state, sha2pass, shalen);
|
||||
}
|
||||
/* key expansion */
|
||||
Blowfish_initstate(&state);
|
||||
Blowfish_expandstate(&state, sha2salt, shalen, sha2pass, shalen);
|
||||
for(i = 0; i < 64; i++) {
|
||||
Blowfish_expand0state(&state, sha2salt, shalen);
|
||||
Blowfish_expand0state(&state, sha2pass, shalen);
|
||||
}
|
||||
|
||||
/* encryption */
|
||||
j = 0;
|
||||
for (i = 0; i < BCRYPT_BLOCKS; i++)
|
||||
cdata[i] = Blowfish_stream2word(ciphertext, sizeof(ciphertext),
|
||||
&j);
|
||||
for (i = 0; i < 64; i++)
|
||||
blf_enc(&state, cdata, sizeof(cdata) / sizeof(uint64_t));
|
||||
/* encryption */
|
||||
j = 0;
|
||||
for(i = 0; i < BCRYPT_BLOCKS; i++)
|
||||
cdata[i] = Blowfish_stream2word(ciphertext, sizeof(ciphertext),
|
||||
&j);
|
||||
for(i = 0; i < 64; i++)
|
||||
blf_enc(&state, cdata, sizeof(cdata) / sizeof(uint64_t));
|
||||
|
||||
/* copy out */
|
||||
for (i = 0; i < BCRYPT_BLOCKS; i++) {
|
||||
out[4 * i + 3] = (cdata[i] >> 24) & 0xff;
|
||||
out[4 * i + 2] = (cdata[i] >> 16) & 0xff;
|
||||
out[4 * i + 1] = (cdata[i] >> 8) & 0xff;
|
||||
out[4 * i + 0] = cdata[i] & 0xff;
|
||||
}
|
||||
/* copy out */
|
||||
for(i = 0; i < BCRYPT_BLOCKS; i++) {
|
||||
out[4 * i + 3] = (cdata[i] >> 24) & 0xff;
|
||||
out[4 * i + 2] = (cdata[i] >> 16) & 0xff;
|
||||
out[4 * i + 1] = (cdata[i] >> 8) & 0xff;
|
||||
out[4 * i + 0] = cdata[i] & 0xff;
|
||||
}
|
||||
|
||||
/* zap */
|
||||
_libssh2_explicit_zero(ciphertext, sizeof(ciphertext));
|
||||
_libssh2_explicit_zero(cdata, sizeof(cdata));
|
||||
_libssh2_explicit_zero(&state, sizeof(state));
|
||||
/* zap */
|
||||
_libssh2_explicit_zero(ciphertext, sizeof(ciphertext));
|
||||
_libssh2_explicit_zero(cdata, sizeof(cdata));
|
||||
_libssh2_explicit_zero(&state, sizeof(state));
|
||||
}
|
||||
|
||||
int
|
||||
bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt, size_t saltlen,
|
||||
uint8_t *key, size_t keylen, unsigned int rounds)
|
||||
bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt,
|
||||
size_t saltlen,
|
||||
uint8_t *key, size_t keylen, unsigned int rounds)
|
||||
{
|
||||
uint8_t sha2pass[SHA512_DIGEST_LENGTH];
|
||||
uint8_t sha2salt[SHA512_DIGEST_LENGTH];
|
||||
uint8_t out[BCRYPT_HASHSIZE];
|
||||
uint8_t tmpout[BCRYPT_HASHSIZE];
|
||||
uint8_t *countsalt;
|
||||
size_t i, j, amt, stride;
|
||||
uint32_t count;
|
||||
size_t origkeylen = keylen;
|
||||
libssh2_sha512_ctx ctx;
|
||||
uint8_t sha2pass[SHA512_DIGEST_LENGTH];
|
||||
uint8_t sha2salt[SHA512_DIGEST_LENGTH];
|
||||
uint8_t out[BCRYPT_HASHSIZE];
|
||||
uint8_t tmpout[BCRYPT_HASHSIZE];
|
||||
uint8_t *countsalt;
|
||||
size_t i, j, amt, stride;
|
||||
uint32_t count;
|
||||
size_t origkeylen = keylen;
|
||||
libssh2_sha512_ctx ctx;
|
||||
|
||||
/* nothing crazy */
|
||||
if (rounds < 1)
|
||||
return -1;
|
||||
if (passlen == 0 || saltlen == 0 || keylen == 0 ||
|
||||
keylen > sizeof(out) * sizeof(out) || saltlen > 1<<20)
|
||||
return -1;
|
||||
if ((countsalt = calloc(1, saltlen + 4)) == NULL)
|
||||
return -1;
|
||||
stride = (keylen + sizeof(out) - 1) / sizeof(out);
|
||||
amt = (keylen + stride - 1) / stride;
|
||||
/* nothing crazy */
|
||||
if(rounds < 1)
|
||||
return -1;
|
||||
if(passlen == 0 || saltlen == 0 || keylen == 0 ||
|
||||
keylen > sizeof(out) * sizeof(out) || saltlen > 1<<20)
|
||||
return -1;
|
||||
countsalt = calloc(1, saltlen + 4);
|
||||
if(countsalt == NULL)
|
||||
return -1;
|
||||
stride = (keylen + sizeof(out) - 1) / sizeof(out);
|
||||
amt = (keylen + stride - 1) / stride;
|
||||
|
||||
memcpy(countsalt, salt, saltlen);
|
||||
memcpy(countsalt, salt, saltlen);
|
||||
|
||||
/* collapse password */
|
||||
libssh2_sha512_init(&ctx);
|
||||
libssh2_sha512_update(ctx, pass, passlen);
|
||||
libssh2_sha512_final(ctx, sha2pass);
|
||||
/* collapse password */
|
||||
libssh2_sha512_init(&ctx);
|
||||
libssh2_sha512_update(ctx, pass, passlen);
|
||||
libssh2_sha512_final(ctx, sha2pass);
|
||||
|
||||
/* generate key, sizeof(out) at a time */
|
||||
for (count = 1; keylen > 0; count++) {
|
||||
countsalt[saltlen + 0] = (count >> 24) & 0xff;
|
||||
countsalt[saltlen + 1] = (count >> 16) & 0xff;
|
||||
countsalt[saltlen + 2] = (count >> 8) & 0xff;
|
||||
countsalt[saltlen + 3] = count & 0xff;
|
||||
/* generate key, sizeof(out) at a time */
|
||||
for(count = 1; keylen > 0; count++) {
|
||||
countsalt[saltlen + 0] = (count >> 24) & 0xff;
|
||||
countsalt[saltlen + 1] = (count >> 16) & 0xff;
|
||||
countsalt[saltlen + 2] = (count >> 8) & 0xff;
|
||||
countsalt[saltlen + 3] = count & 0xff;
|
||||
|
||||
/* first round, salt is salt */
|
||||
libssh2_sha512_init(&ctx);
|
||||
libssh2_sha512_update(ctx, countsalt, saltlen + 4);
|
||||
libssh2_sha512_final(ctx, sha2salt);
|
||||
/* first round, salt is salt */
|
||||
libssh2_sha512_init(&ctx);
|
||||
libssh2_sha512_update(ctx, countsalt, saltlen + 4);
|
||||
libssh2_sha512_final(ctx, sha2salt);
|
||||
|
||||
bcrypt_hash(sha2pass, sha2salt, tmpout);
|
||||
memcpy(out, tmpout, sizeof(out));
|
||||
bcrypt_hash(sha2pass, sha2salt, tmpout);
|
||||
memcpy(out, tmpout, sizeof(out));
|
||||
|
||||
for (i = 1; i < rounds; i++) {
|
||||
/* subsequent rounds, salt is previous output */
|
||||
libssh2_sha512_init(&ctx);
|
||||
libssh2_sha512_update(ctx, tmpout, sizeof(tmpout));
|
||||
libssh2_sha512_final(ctx, sha2salt);
|
||||
for(i = 1; i < rounds; i++) {
|
||||
/* subsequent rounds, salt is previous output */
|
||||
libssh2_sha512_init(&ctx);
|
||||
libssh2_sha512_update(ctx, tmpout, sizeof(tmpout));
|
||||
libssh2_sha512_final(ctx, sha2salt);
|
||||
|
||||
bcrypt_hash(sha2pass, sha2salt, tmpout);
|
||||
for (j = 0; j < sizeof(out); j++)
|
||||
out[j] ^= tmpout[j];
|
||||
}
|
||||
bcrypt_hash(sha2pass, sha2salt, tmpout);
|
||||
for(j = 0; j < sizeof(out); j++)
|
||||
out[j] ^= tmpout[j];
|
||||
}
|
||||
|
||||
/*
|
||||
* pbkdf2 deviation: ouput the key material non-linearly.
|
||||
*/
|
||||
amt = MINIMUM(amt, keylen);
|
||||
for (i = 0; i < amt; i++) {
|
||||
size_t dest = i * stride + (count - 1);
|
||||
if (dest >= origkeylen) {
|
||||
break;
|
||||
}
|
||||
key[dest] = out[i];
|
||||
}
|
||||
keylen -= i;
|
||||
}
|
||||
/*
|
||||
* pbkdf2 deviation: ouput the key material non-linearly.
|
||||
*/
|
||||
amt = MINIMUM(amt, keylen);
|
||||
for(i = 0; i < amt; i++) {
|
||||
size_t dest = i * stride + (count - 1);
|
||||
if(dest >= origkeylen) {
|
||||
break;
|
||||
}
|
||||
key[dest] = out[i];
|
||||
}
|
||||
keylen -= i;
|
||||
}
|
||||
|
||||
/* zap */
|
||||
_libssh2_explicit_zero(out, sizeof(out));
|
||||
free(countsalt);
|
||||
/* zap */
|
||||
_libssh2_explicit_zero(out, sizeof(out));
|
||||
free(countsalt);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
#endif /* HAVE_BCRYPT_PBKDF */
|
||||
|
23
src/blf.h
23
src/blf.h
@ -43,20 +43,20 @@
|
||||
* of the key affect all cipherbits.
|
||||
*/
|
||||
|
||||
#define BLF_N 16 /* Number of Subkeys */
|
||||
#define BLF_MAXKEYLEN ((BLF_N-2)*4) /* 448 bits */
|
||||
#define BLF_MAXUTILIZED ((BLF_N+2)*4) /* 576 bits */
|
||||
#define BLF_N 16 /* Number of Subkeys */
|
||||
#define BLF_MAXKEYLEN ((BLF_N-2)*4) /* 448 bits */
|
||||
#define BLF_MAXUTILIZED ((BLF_N + 2)*4) /* 576 bits */
|
||||
|
||||
/* Blowfish context */
|
||||
typedef struct BlowfishContext {
|
||||
uint32_t S[4][256]; /* S-Boxes */
|
||||
uint32_t P[BLF_N + 2]; /* Subkeys */
|
||||
uint32_t S[4][256]; /* S-Boxes */
|
||||
uint32_t P[BLF_N + 2]; /* Subkeys */
|
||||
} blf_ctx;
|
||||
|
||||
/* Raw access to customized Blowfish
|
||||
* blf_key is just:
|
||||
* Blowfish_initstate( state )
|
||||
* Blowfish_expand0state( state, key, keylen )
|
||||
* blf_key is just:
|
||||
* Blowfish_initstate( state )
|
||||
* Blowfish_expand0state( state, key, keylen )
|
||||
*/
|
||||
|
||||
void Blowfish_encipher(blf_ctx *, uint32_t *, uint32_t *);
|
||||
@ -79,11 +79,12 @@ void blf_cbc_encrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t);
|
||||
void blf_cbc_decrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t);
|
||||
|
||||
/* Converts uint8_t to uint32_t */
|
||||
uint32_t Blowfish_stream2word(const uint8_t *, uint16_t , uint16_t *);
|
||||
uint32_t Blowfish_stream2word(const uint8_t *, uint16_t, uint16_t *);
|
||||
|
||||
/* bcrypt with pbkd */
|
||||
int bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt, size_t saltlen,
|
||||
uint8_t *key, size_t keylen, unsigned int rounds);
|
||||
int bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt,
|
||||
size_t saltlen,
|
||||
uint8_t *key, size_t keylen, unsigned int rounds);
|
||||
|
||||
#endif /* !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H) */
|
||||
#endif /* _BLF_H */
|
||||
|
1041
src/blowfish.c
1041
src/blowfish.c
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -280,11 +280,13 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
|
||||
}
|
||||
|
||||
if(session->open_data[0] == SSH_MSG_CHANNEL_OPEN_FAILURE) {
|
||||
unsigned int reason_code = _libssh2_ntohu32(session->open_data + 5);
|
||||
unsigned int reason_code =
|
||||
_libssh2_ntohu32(session->open_data + 5);
|
||||
switch(reason_code) {
|
||||
case SSH_OPEN_ADMINISTRATIVELY_PROHIBITED:
|
||||
_libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
|
||||
"Channel open failure (administratively prohibited)");
|
||||
"Channel open failure "
|
||||
"(administratively prohibited)");
|
||||
break;
|
||||
case SSH_OPEN_CONNECT_FAILED:
|
||||
_libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
|
||||
@ -388,14 +390,15 @@ channel_direct_tcpip(LIBSSH2_SESSION * session, const char *host,
|
||||
session->direct_host_len + session->direct_shost_len + 16;
|
||||
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_CONN,
|
||||
"Requesting direct-tcpip session to from %s:%d to %s:%d",
|
||||
"Requesting direct-tcpip session from %s:%d to %s:%d",
|
||||
shost, sport, host, port);
|
||||
|
||||
s = session->direct_message =
|
||||
LIBSSH2_ALLOC(session, session->direct_message_len);
|
||||
if(!session->direct_message) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for direct-tcpip connection");
|
||||
"Unable to allocate memory for "
|
||||
"direct-tcpip connection");
|
||||
return NULL;
|
||||
}
|
||||
_libssh2_store_str(&s, host, session->direct_host_len);
|
||||
@ -443,7 +446,8 @@ libssh2_channel_direct_tcpip_ex(LIBSSH2_SESSION *session, const char *host,
|
||||
return NULL;
|
||||
|
||||
BLOCK_ADJUST_ERRNO(ptr, session,
|
||||
channel_direct_tcpip(session, host, port, shost, sport));
|
||||
channel_direct_tcpip(session, host, port,
|
||||
shost, sport));
|
||||
return ptr;
|
||||
}
|
||||
|
||||
@ -551,7 +555,8 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
|
||||
LIBSSH2_ALLOC(session, session->fwdLstn_host_len + 1);
|
||||
if(!listener->host) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for listener queue");
|
||||
"Unable to allocate memory "
|
||||
"for listener queue");
|
||||
LIBSSH2_FREE(session, listener);
|
||||
listener = NULL;
|
||||
}
|
||||
@ -562,7 +567,8 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
|
||||
if(data_len >= 5 && !port) {
|
||||
listener->port = _libssh2_ntohu32(data + 1);
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_CONN,
|
||||
"Dynamic tcpip-forward port allocated: %d",
|
||||
"Dynamic tcpip-forward port "
|
||||
"allocated: %d",
|
||||
listener->port);
|
||||
}
|
||||
else
|
||||
@ -871,7 +877,7 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
|
||||
if(rc) {
|
||||
channel->setenv_state = libssh2_NB_state_idle;
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
else if(data_len < 1) {
|
||||
channel->setenv_state = libssh2_NB_state_idle;
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
@ -1011,7 +1017,8 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
|
||||
}
|
||||
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
|
||||
"Unable to complete request for channel request-pty");
|
||||
"Unable to complete request for "
|
||||
"channel request-pty");
|
||||
}
|
||||
|
||||
/*
|
||||
@ -1424,23 +1431,24 @@ _libssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid)
|
||||
while(packet) {
|
||||
unsigned char packet_type;
|
||||
LIBSSH2_PACKET *next = _libssh2_list_next(&packet->node);
|
||||
|
||||
|
||||
if(packet->data_len < 1) {
|
||||
packet = next;
|
||||
_libssh2_debug(channel->session, LIBSSH2_TRACE_ERROR,
|
||||
"Unexpected packet length");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
packet_type = packet->data[0];
|
||||
|
||||
if(((packet_type == SSH_MSG_CHANNEL_DATA)
|
||||
|| (packet_type == SSH_MSG_CHANNEL_EXTENDED_DATA))
|
||||
&& ((packet->data_len >= 5)
|
||||
&& (_libssh2_ntohu32(packet->data + 1) == channel->local.id))) {
|
||||
&& ((packet->data_len >= 5)
|
||||
&& (_libssh2_ntohu32(packet->data + 1)
|
||||
== channel->local.id))) {
|
||||
/* It's our channel at least */
|
||||
int packet_stream_id;
|
||||
|
||||
|
||||
if(packet_type == SSH_MSG_CHANNEL_DATA) {
|
||||
packet_stream_id = 0;
|
||||
}
|
||||
@ -1460,7 +1468,8 @@ _libssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid)
|
||||
|| (streamid == packet_stream_id)))
|
||||
|| ((packet_type == SSH_MSG_CHANNEL_DATA)
|
||||
&& (streamid == 0))) {
|
||||
size_t bytes_to_flush = packet->data_len - packet->data_head;
|
||||
size_t bytes_to_flush = packet->data_len -
|
||||
packet->data_head;
|
||||
|
||||
_libssh2_debug(channel->session, LIBSSH2_TRACE_CONN,
|
||||
"Flushing %d bytes of data from stream "
|
||||
@ -1489,11 +1498,10 @@ _libssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid)
|
||||
channel->remote.window_size -= channel->flush_flush_bytes;
|
||||
|
||||
if(channel->flush_refund_bytes) {
|
||||
int rc;
|
||||
|
||||
rc = _libssh2_channel_receive_window_adjust(channel,
|
||||
channel->flush_refund_bytes,
|
||||
1, NULL);
|
||||
int rc =
|
||||
_libssh2_channel_receive_window_adjust(channel,
|
||||
channel->flush_refund_bytes,
|
||||
1, NULL);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
@ -1831,9 +1839,11 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
|
||||
|
||||
/* expand the receiving window first if it has become too narrow */
|
||||
if((channel->read_state == libssh2_NB_state_jump1) ||
|
||||
(channel->remote.window_size < channel->remote.window_size_initial / 4 * 3 + buflen) ) {
|
||||
(channel->remote.window_size <
|
||||
channel->remote.window_size_initial / 4 * 3 + buflen) ) {
|
||||
|
||||
uint32_t adjustment = channel->remote.window_size_initial + buflen - channel->remote.window_size;
|
||||
uint32_t adjustment = channel->remote.window_size_initial + buflen -
|
||||
channel->remote.window_size;
|
||||
if(adjustment < LIBSSH2_CHANNEL_MINADJUST)
|
||||
adjustment = LIBSSH2_CHANNEL_MINADJUST;
|
||||
|
||||
@ -2235,7 +2245,8 @@ static int channel_send_eof(LIBSSH2_CHANNEL *channel)
|
||||
unsigned char packet[5]; /* packet_type(1) + channelno(4) */
|
||||
int rc;
|
||||
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_CONN, "Sending EOF on channel %lu/%lu",
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_CONN,
|
||||
"Sending EOF on channel %lu/%lu",
|
||||
channel->local.id, channel->remote.id);
|
||||
packet[0] = SSH_MSG_CHANNEL_EOF;
|
||||
_libssh2_htonu32(packet + 1, channel->remote.id);
|
||||
@ -2343,7 +2354,8 @@ static int channel_wait_eof(LIBSSH2_CHANNEL *channel)
|
||||
if((channel->remote.window_size == channel->read_avail) &&
|
||||
session->api_block_mode)
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_WINDOW_FULL,
|
||||
"Receiving channel window has been exhausted");
|
||||
"Receiving channel window "
|
||||
"has been exhausted");
|
||||
|
||||
rc = _libssh2_transport_read(session);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
@ -2444,8 +2456,8 @@ int _libssh2_channel_close(LIBSSH2_CHANNEL * channel)
|
||||
}
|
||||
|
||||
if(rc != LIBSSH2_ERROR_EAGAIN) {
|
||||
/* set the local close state first when we're perfectly confirmed to not
|
||||
do any more EAGAINs */
|
||||
/* set the local close state first when we're perfectly confirmed to
|
||||
not do any more EAGAINs */
|
||||
channel->local.close = 1;
|
||||
|
||||
/* We call the callback last in this function to make it keep the local
|
||||
@ -2674,7 +2686,7 @@ libssh2_channel_window_read_ex(LIBSSH2_CHANNEL *channel,
|
||||
while(packet) {
|
||||
unsigned char packet_type;
|
||||
next_packet = _libssh2_list_next(&packet->node);
|
||||
|
||||
|
||||
if(packet->data_len < 1) {
|
||||
packet = next_packet;
|
||||
_libssh2_debug(channel->session, LIBSSH2_TRACE_ERROR,
|
||||
@ -2685,9 +2697,10 @@ libssh2_channel_window_read_ex(LIBSSH2_CHANNEL *channel,
|
||||
packet_type = packet->data[0];
|
||||
|
||||
if(((packet_type == SSH_MSG_CHANNEL_DATA)
|
||||
|| (packet_type == SSH_MSG_CHANNEL_EXTENDED_DATA))
|
||||
&& ((packet->data_len >= 5)
|
||||
&& (_libssh2_ntohu32(packet->data + 1) == channel->local.id))) {
|
||||
|| (packet_type == SSH_MSG_CHANNEL_EXTENDED_DATA))
|
||||
&& ((packet->data_len >= 5)
|
||||
&& (_libssh2_ntohu32(packet->data + 1) ==
|
||||
channel->local.id))) {
|
||||
bytes_queued += packet->data_len - packet->data_head;
|
||||
}
|
||||
|
||||
|
12
src/comp.c
12
src/comp.c
@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org>
|
||||
/* Copyright (c) 2004-2007, 2019, Sara Golemon <sarag@libssh2.org>
|
||||
* Copyright (c) 2010-2014, Daniel Stenberg <daniel@haxx.se>
|
||||
* All rights reserved.
|
||||
*
|
||||
@ -204,7 +204,8 @@ comp_method_zlib_comp(LIBSSH2_SESSION *session,
|
||||
}
|
||||
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_TRANS,
|
||||
"unhandled zlib compression error %d, avail_out", status, strm->avail_out);
|
||||
"unhandled zlib compression error %d, avail_out",
|
||||
status, strm->avail_out);
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_ZLIB, "compression failure");
|
||||
}
|
||||
|
||||
@ -226,8 +227,8 @@ comp_method_zlib_decomp(LIBSSH2_SESSION * session,
|
||||
reallocs */
|
||||
char *out;
|
||||
size_t out_maxlen = src_len;
|
||||
|
||||
if (src_len <= SIZE_MAX / 4)
|
||||
|
||||
if(src_len <= SIZE_MAX / 4)
|
||||
out_maxlen = src_len * 4;
|
||||
else
|
||||
out_maxlen = payload_limit;
|
||||
@ -263,7 +264,8 @@ comp_method_zlib_decomp(LIBSSH2_SESSION * session,
|
||||
|
||||
if(status == Z_OK) {
|
||||
if(strm->avail_out > 0)
|
||||
/* status is OK and the output buffer has not been exhausted so we're done */
|
||||
/* status is OK and the output buffer has not been exhausted
|
||||
so we're done */
|
||||
break;
|
||||
}
|
||||
else if(status == Z_BUF_ERROR) {
|
||||
|
@ -54,10 +54,10 @@ crypt_none_crypt(LIBSSH2_SESSION * session, unsigned char *buf,
|
||||
static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_none = {
|
||||
"none",
|
||||
"DEK-Info: NONE",
|
||||
8, /* blocksize (SSH2 defines minimum blocksize as 8) */
|
||||
0, /* iv_len */
|
||||
0, /* secret_len */
|
||||
0, /* flags */
|
||||
8, /* blocksize (SSH2 defines minimum blocksize as 8) */
|
||||
0, /* iv_len */
|
||||
0, /* secret_len */
|
||||
0, /* flags */
|
||||
NULL,
|
||||
crypt_none_crypt,
|
||||
NULL
|
||||
|
42
src/crypto.h
42
src/crypto.h
@ -95,7 +95,8 @@ int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session,
|
||||
size_t *signature_len);
|
||||
int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx ** rsa,
|
||||
LIBSSH2_SESSION * session,
|
||||
const char *filedata, size_t filedata_len,
|
||||
const char *filedata,
|
||||
size_t filedata_len,
|
||||
unsigned const char *passphrase);
|
||||
#endif
|
||||
|
||||
@ -122,7 +123,8 @@ int _libssh2_dsa_sha1_sign(libssh2_dsa_ctx * dsactx,
|
||||
unsigned long hash_len, unsigned char *sig);
|
||||
int _libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx ** dsa,
|
||||
LIBSSH2_SESSION * session,
|
||||
const char *filedata, size_t filedata_len,
|
||||
const char *filedata,
|
||||
size_t filedata_len,
|
||||
unsigned const char *passphrase);
|
||||
#endif
|
||||
|
||||
@ -130,11 +132,13 @@ int _libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx ** dsa,
|
||||
int
|
||||
_libssh2_ecdsa_curve_name_with_octal_new(libssh2_ecdsa_ctx ** ecdsactx,
|
||||
const unsigned char *k,
|
||||
size_t k_len, libssh2_curve_type type);
|
||||
size_t k_len,
|
||||
libssh2_curve_type type);
|
||||
int
|
||||
_libssh2_ecdsa_new_private(libssh2_ecdsa_ctx ** ec_ctx,
|
||||
LIBSSH2_SESSION * session,
|
||||
const char *filename, unsigned const char *passphrase);
|
||||
const char *filename,
|
||||
unsigned const char *passphrase);
|
||||
|
||||
int
|
||||
_libssh2_ecdsa_verify(libssh2_ecdsa_ctx * ctx,
|
||||
@ -143,13 +147,16 @@ _libssh2_ecdsa_verify(libssh2_ecdsa_ctx * ctx,
|
||||
const unsigned char *m, size_t m_len);
|
||||
|
||||
int
|
||||
_libssh2_ecdsa_create_key(LIBSSH2_SESSION *session, _libssh2_ec_key **out_private_key,
|
||||
_libssh2_ecdsa_create_key(LIBSSH2_SESSION *session,
|
||||
_libssh2_ec_key **out_private_key,
|
||||
unsigned char **out_public_key_octal,
|
||||
size_t *out_public_key_octal_len, libssh2_curve_type curve_type);
|
||||
size_t *out_public_key_octal_len,
|
||||
libssh2_curve_type curve_type);
|
||||
|
||||
int
|
||||
_libssh2_ecdh_gen_k(_libssh2_bn **k, _libssh2_ec_key *private_key,
|
||||
const unsigned char *server_public_key, size_t server_public_key_len);
|
||||
const unsigned char *server_public_key,
|
||||
size_t server_public_key_len);
|
||||
|
||||
int
|
||||
_libssh2_ecdsa_sign(LIBSSH2_SESSION *session, libssh2_ecdsa_ctx *ec_ctx,
|
||||
@ -157,26 +164,29 @@ _libssh2_ecdsa_sign(LIBSSH2_SESSION *session, libssh2_ecdsa_ctx *ec_ctx,
|
||||
unsigned char **signature, size_t *signature_len);
|
||||
|
||||
int _libssh2_ecdsa_new_private_frommemory(libssh2_ecdsa_ctx ** ec_ctx,
|
||||
LIBSSH2_SESSION * session,
|
||||
const char *filedata, size_t filedata_len,
|
||||
unsigned const char *passphrase);
|
||||
LIBSSH2_SESSION * session,
|
||||
const char *filedata,
|
||||
size_t filedata_len,
|
||||
unsigned const char *passphrase);
|
||||
|
||||
libssh2_curve_type
|
||||
_libssh2_ecdsa_key_get_curve_type(_libssh2_ec_key *key);
|
||||
|
||||
int
|
||||
_libssh2_ecdsa_curve_type_from_name(const char *name, libssh2_curve_type *out_type);
|
||||
_libssh2_ecdsa_curve_type_from_name(const char *name,
|
||||
libssh2_curve_type *out_type);
|
||||
|
||||
#endif /* LIBSSH2_ECDSA */
|
||||
|
||||
#if LIBSSH2_ED25519
|
||||
|
||||
int
|
||||
_libssh2_curve25519_new(LIBSSH2_SESSION *session, libssh2_ed25519_ctx **ctx,
|
||||
_libssh2_curve25519_new(LIBSSH2_SESSION *session, libssh2_ed25519_ctx **ctx,
|
||||
uint8_t **out_public_key, uint8_t **out_private_key);
|
||||
|
||||
int
|
||||
_libssh2_curve25519_gen_k(_libssh2_bn **k, uint8_t private_key[LIBSSH2_ED25519_KEY_LEN],
|
||||
_libssh2_curve25519_gen_k(_libssh2_bn **k,
|
||||
uint8_t private_key[LIBSSH2_ED25519_KEY_LEN],
|
||||
uint8_t server_public_key[LIBSSH2_ED25519_KEY_LEN]);
|
||||
|
||||
int
|
||||
@ -191,7 +201,8 @@ _libssh2_ed25519_new_private(libssh2_ed25519_ctx **ed_ctx,
|
||||
int
|
||||
_libssh2_ed25519_new_public(libssh2_ed25519_ctx **ed_ctx,
|
||||
LIBSSH2_SESSION *session,
|
||||
const unsigned char *raw_pub_key, const uint8_t key_len);
|
||||
const unsigned char *raw_pub_key,
|
||||
const uint8_t key_len);
|
||||
|
||||
int
|
||||
_libssh2_ed25519_sign(libssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session,
|
||||
@ -201,7 +212,8 @@ _libssh2_ed25519_sign(libssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session,
|
||||
int
|
||||
_libssh2_ed25519_new_private_frommemory(libssh2_ed25519_ctx **ed_ctx,
|
||||
LIBSSH2_SESSION *session,
|
||||
const char *filedata, size_t filedata_len,
|
||||
const char *filedata,
|
||||
size_t filedata_len,
|
||||
unsigned const char *passphrase);
|
||||
|
||||
#endif /* LIBSSH2_ED25519 */
|
||||
|
127
src/hostkey.c
127
src/hostkey.c
@ -74,23 +74,25 @@ hostkey_method_ssh_rsa_init(LIBSSH2_SESSION * session,
|
||||
}
|
||||
|
||||
if(hostkey_data_len < 19) {
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_ERROR,
|
||||
"host key length too short");
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_ERROR,
|
||||
"host key length too short");
|
||||
return -1;
|
||||
}
|
||||
|
||||
buf.offset = 0;
|
||||
buf.data = (unsigned char*)hostkey_data;
|
||||
buf.data = (unsigned char *)hostkey_data;
|
||||
buf.dataptr = buf.data;
|
||||
buf.len = hostkey_data_len;
|
||||
|
||||
if(_libssh2_match_string(&buf, "ssh-rsa") != 0)
|
||||
return -1;
|
||||
|
||||
if((e_len = _libssh2_get_c_string(&buf, &e)) <= 0)
|
||||
e_len = _libssh2_get_c_string(&buf, &e);
|
||||
if(e_len <= 0)
|
||||
return -1;
|
||||
|
||||
if((n_len = _libssh2_get_c_string(&buf, &n)) <= 0)
|
||||
n_len = _libssh2_get_c_string(&buf, &n);
|
||||
if(n_len <= 0)
|
||||
return -1;
|
||||
|
||||
if(_libssh2_rsa_new(&rsactx, e, e_len, n, n_len, NULL, 0,
|
||||
@ -299,27 +301,31 @@ hostkey_method_ssh_dss_init(LIBSSH2_SESSION * session,
|
||||
}
|
||||
|
||||
buf.offset = 0;
|
||||
buf.data = (unsigned char*)hostkey_data;
|
||||
buf.data = (unsigned char *)hostkey_data;
|
||||
buf.dataptr = buf.data;
|
||||
buf.len = hostkey_data_len;
|
||||
|
||||
if(_libssh2_match_string(&buf, "ssh-dss") != 0)
|
||||
return -1;
|
||||
|
||||
if((p_len = _libssh2_get_c_string(&buf, &p)) < 0)
|
||||
p_len = _libssh2_get_c_string(&buf, &p);
|
||||
if(p_len < 0)
|
||||
return -1;
|
||||
|
||||
if((q_len = _libssh2_get_c_string(&buf, &q)) < 0)
|
||||
q_len = _libssh2_get_c_string(&buf, &q);
|
||||
if(q_len < 0)
|
||||
return -1;
|
||||
|
||||
if((g_len = _libssh2_get_c_string(&buf, &g)) < 0)
|
||||
g_len = _libssh2_get_c_string(&buf, &g);
|
||||
if(g_len < 0)
|
||||
return -1;
|
||||
|
||||
if((y_len = _libssh2_get_c_string(&buf, &y)) < 0)
|
||||
y_len = _libssh2_get_c_string(&buf, &y);
|
||||
if(y_len < 0)
|
||||
return -1;
|
||||
|
||||
if(_libssh2_dsa_new(&dsactx, p, p_len, q, q_len,
|
||||
g, g_len, y, y_len, NULL, 0)) {
|
||||
g, g_len, y, y_len, NULL, 0)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -404,7 +410,7 @@ hostkey_method_ssh_dss_sig_verify(LIBSSH2_SESSION * session,
|
||||
libssh2_dsa_ctx *dsactx = (libssh2_dsa_ctx *) (*abstract);
|
||||
|
||||
/* Skip past keyname_len(4) + keyname(7){"ssh-dss"} + signature_len(4) */
|
||||
if(sig_len != 55) {
|
||||
if(sig_len != 55) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Invalid DSS signature length");
|
||||
}
|
||||
@ -524,39 +530,49 @@ hostkey_method_ssh_ecdsa_init(LIBSSH2_SESSION * session,
|
||||
}
|
||||
|
||||
buf.offset = 0;
|
||||
buf.data = (unsigned char*)hostkey_data;
|
||||
buf.data = (unsigned char *)hostkey_data;
|
||||
buf.dataptr = buf.data;
|
||||
buf.len = hostkey_data_len;
|
||||
|
||||
if(_libssh2_get_c_string(&buf, &type_str) != 19)
|
||||
return -1;
|
||||
|
||||
if (strncmp((char*) type_str, "ecdsa-sha2-nistp256", 19) == 0 ){
|
||||
if(strncmp((char *) type_str, "ecdsa-sha2-nistp256", 19) == 0) {
|
||||
type = LIBSSH2_EC_CURVE_NISTP256;
|
||||
}else if(strncmp((char*) type_str, "ecdsa-sha2-nistp384", 19) == 0 ){
|
||||
}
|
||||
else if(strncmp((char *) type_str, "ecdsa-sha2-nistp384", 19) == 0) {
|
||||
type = LIBSSH2_EC_CURVE_NISTP384;
|
||||
}else if(strncmp((char*) type_str, "ecdsa-sha2-nistp521", 19) == 0 ){
|
||||
}
|
||||
else if(strncmp((char *) type_str, "ecdsa-sha2-nistp521", 19) == 0) {
|
||||
type = LIBSSH2_EC_CURVE_NISTP521;
|
||||
}else{
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(_libssh2_get_c_string(&buf, &domain) != 8)
|
||||
return -1;
|
||||
|
||||
if ( type == LIBSSH2_EC_CURVE_NISTP256 && strncmp((char*)domain, "nistp256", 8) != 0){
|
||||
if(type == LIBSSH2_EC_CURVE_NISTP256 &&
|
||||
strncmp((char *)domain, "nistp256", 8) != 0) {
|
||||
return -1;
|
||||
}else if ( type == LIBSSH2_EC_CURVE_NISTP384 && strncmp((char*)domain, "nistp384", 8) != 0){
|
||||
}
|
||||
else if(type == LIBSSH2_EC_CURVE_NISTP384 &&
|
||||
strncmp((char *)domain, "nistp384", 8) != 0) {
|
||||
return -1;
|
||||
}else if ( type == LIBSSH2_EC_CURVE_NISTP521 && strncmp((char*)domain, "nistp521", 8) != 0){
|
||||
}
|
||||
else if(type == LIBSSH2_EC_CURVE_NISTP521 &&
|
||||
strncmp((char *)domain, "nistp521", 8) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* public key */
|
||||
if((key_len = _libssh2_get_c_string(&buf, &public_key)) <= 0)
|
||||
key_len = _libssh2_get_c_string(&buf, &public_key);
|
||||
if(key_len <= 0)
|
||||
return -1;
|
||||
|
||||
if(_libssh2_ecdsa_curve_name_with_octal_new(&ecdsactx, public_key, key_len, type) )
|
||||
if(_libssh2_ecdsa_curve_name_with_octal_new(&ecdsactx, public_key,
|
||||
key_len, type))
|
||||
return -1;
|
||||
|
||||
if(abstract != NULL)
|
||||
@ -584,7 +600,8 @@ hostkey_method_ssh_ecdsa_initPEM(LIBSSH2_SESSION * session,
|
||||
*abstract = NULL;
|
||||
}
|
||||
|
||||
ret = _libssh2_ecdsa_new_private(&ec_ctx, session, privkeyfile, passphrase);
|
||||
ret = _libssh2_ecdsa_new_private(&ec_ctx, session,
|
||||
privkeyfile, passphrase);
|
||||
|
||||
if(abstract != NULL)
|
||||
*abstract = ec_ctx;
|
||||
@ -614,7 +631,8 @@ hostkey_method_ssh_ecdsa_initPEMFromMemory(LIBSSH2_SESSION * session,
|
||||
|
||||
ret = _libssh2_ecdsa_new_private_frommemory(&ec_ctx, session,
|
||||
privkeyfiledata,
|
||||
privkeyfiledata_len, passphrase);
|
||||
privkeyfiledata_len,
|
||||
passphrase);
|
||||
if(ret) {
|
||||
return -1;
|
||||
}
|
||||
@ -647,9 +665,10 @@ hostkey_method_ssh_ecdsa_sig_verify(LIBSSH2_SESSION * session,
|
||||
if(sig_len < 35)
|
||||
return -1;
|
||||
|
||||
/* keyname_len(4) + keyname(19){"ecdsa-sha2-nistp256"} + signature_len(4) */
|
||||
/* keyname_len(4) + keyname(19){"ecdsa-sha2-nistp256"} +
|
||||
signature_len(4) */
|
||||
buf.offset = 0;
|
||||
buf.data = (unsigned char*)sig;
|
||||
buf.data = (unsigned char *)sig;
|
||||
buf.dataptr = buf.data;
|
||||
buf.len = sig_len;
|
||||
|
||||
@ -659,29 +678,33 @@ hostkey_method_ssh_ecdsa_sig_verify(LIBSSH2_SESSION * session,
|
||||
if(_libssh2_get_u32(&buf, &len) != 0 || len < 8)
|
||||
return -1;
|
||||
|
||||
if((r_len = _libssh2_get_c_string(&buf, &r)) <= 0)
|
||||
r_len = _libssh2_get_c_string(&buf, &r);
|
||||
if(r_len <= 0)
|
||||
return -1;
|
||||
|
||||
if((s_len = _libssh2_get_c_string(&buf, &s)) <= 0)
|
||||
s_len = _libssh2_get_c_string(&buf, &s);
|
||||
if(s_len <= 0)
|
||||
return -1;
|
||||
|
||||
return _libssh2_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len);
|
||||
}
|
||||
|
||||
|
||||
#define LIBSSH2_HOSTKEY_METHOD_EC_SIGNV_HASH(digest_type) \
|
||||
{ \
|
||||
unsigned char hash[SHA##digest_type##_DIGEST_LENGTH]; \
|
||||
libssh2_sha##digest_type##_ctx ctx; \
|
||||
int i; \
|
||||
libssh2_sha##digest_type##_init(&ctx); \
|
||||
for(i = 0; i < veccount; i++) { \
|
||||
libssh2_sha##digest_type##_update(ctx, datavec[i].iov_base, datavec[i].iov_len); \
|
||||
} \
|
||||
libssh2_sha##digest_type##_final(ctx, hash); \
|
||||
ret = _libssh2_ecdsa_sign(session, ec_ctx, hash, SHA##digest_type##_DIGEST_LENGTH, \
|
||||
signature, signature_len); \
|
||||
}
|
||||
#define LIBSSH2_HOSTKEY_METHOD_EC_SIGNV_HASH(digest_type) \
|
||||
{ \
|
||||
unsigned char hash[SHA##digest_type##_DIGEST_LENGTH]; \
|
||||
libssh2_sha##digest_type##_ctx ctx; \
|
||||
int i; \
|
||||
libssh2_sha##digest_type##_init(&ctx); \
|
||||
for(i = 0; i < veccount; i++) { \
|
||||
libssh2_sha##digest_type##_update(ctx, datavec[i].iov_base, \
|
||||
datavec[i].iov_len); \
|
||||
} \
|
||||
libssh2_sha##digest_type##_final(ctx, hash); \
|
||||
ret = _libssh2_ecdsa_sign(session, ec_ctx, hash, \
|
||||
SHA##digest_type##_DIGEST_LENGTH, \
|
||||
signature, signature_len); \
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
@ -851,7 +874,8 @@ hostkey_method_ssh_ed25519_initPEM(LIBSSH2_SESSION * session,
|
||||
*abstract = NULL;
|
||||
}
|
||||
|
||||
ret = _libssh2_ed25519_new_private(&ec_ctx, session, privkeyfile, passphrase);
|
||||
ret = _libssh2_ed25519_new_private(&ec_ctx, session,
|
||||
privkeyfile, passphrase);
|
||||
if(ret) {
|
||||
return -1;
|
||||
}
|
||||
@ -883,7 +907,8 @@ hostkey_method_ssh_ed25519_initPEMFromMemory(LIBSSH2_SESSION * session,
|
||||
|
||||
ret = _libssh2_ed25519_new_private_frommemory(&ed_ctx, session,
|
||||
privkeyfiledata,
|
||||
privkeyfiledata_len, passphrase);
|
||||
privkeyfiledata_len,
|
||||
passphrase);
|
||||
if(ret) {
|
||||
return -1;
|
||||
}
|
||||
@ -912,7 +937,8 @@ hostkey_method_ssh_ed25519_sig_verify(LIBSSH2_SESSION * session,
|
||||
if(sig_len < 19)
|
||||
return -1;
|
||||
|
||||
/* Skip past keyname_len(4) + keyname(11){"ssh-ed25519"} + signature_len(4) */
|
||||
/* Skip past keyname_len(4) + keyname(11){"ssh-ed25519"} +
|
||||
signature_len(4) */
|
||||
sig += 19;
|
||||
sig_len -= 19;
|
||||
|
||||
@ -937,7 +963,7 @@ hostkey_method_ssh_ed25519_signv(LIBSSH2_SESSION * session,
|
||||
{
|
||||
libssh2_ed25519_ctx *ctx = (libssh2_ed25519_ctx *) (*abstract);
|
||||
|
||||
if (veccount != 1) {
|
||||
if(veccount != 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -1047,13 +1073,16 @@ static int hostkey_type(const unsigned char *hostkey, size_t len)
|
||||
0, 0, 0, 0x07, 's', 's', 'h', '-', 'd', 's', 's'
|
||||
};
|
||||
static const unsigned char ecdsa_256[] = {
|
||||
0, 0, 0, 0x13, 'e', 'c', 'd', 's', 'a', '-', 's', 'h', 'a', '2', '-', 'n', 'i', 's', 't', 'p', '2', '5', '6'
|
||||
0, 0, 0, 0x13, 'e', 'c', 'd', 's', 'a', '-', 's', 'h', 'a', '2', '-',
|
||||
'n', 'i', 's', 't', 'p', '2', '5', '6'
|
||||
};
|
||||
static const unsigned char ecdsa_384[] = {
|
||||
0, 0, 0, 0x13, 'e', 'c', 'd', 's', 'a', '-', 's', 'h', 'a', '2', '-', 'n', 'i', 's', 't', 'p', '3', '8', '4'
|
||||
0, 0, 0, 0x13, 'e', 'c', 'd', 's', 'a', '-', 's', 'h', 'a', '2', '-',
|
||||
'n', 'i', 's', 't', 'p', '3', '8', '4'
|
||||
};
|
||||
static const unsigned char ecdsa_521[] = {
|
||||
0, 0, 0, 0x13, 'e', 'c', 'd', 's', 'a', '-', 's', 'h', 'a', '2', '-', 'n', 'i', 's', 't', 'p', '5', '2', '1'
|
||||
0, 0, 0, 0x13, 'e', 'c', 'd', 's', 'a', '-', 's', 'h', 'a', '2', '-',
|
||||
'n', 'i', 's', 't', 'p', '5', '2', '1'
|
||||
};
|
||||
static const unsigned char ed25519[] = {
|
||||
0, 0, 0, 0x0b, 's', 's', 'h', '-', 'e', 'd', '2', '5', '5', '1', '9'
|
||||
@ -1071,7 +1100,7 @@ static int hostkey_type(const unsigned char *hostkey, size_t len)
|
||||
if(len < 15)
|
||||
return LIBSSH2_HOSTKEY_TYPE_UNKNOWN;
|
||||
|
||||
if (!memcmp(ed25519, hostkey, 15))
|
||||
if(!memcmp(ed25519, hostkey, 15))
|
||||
return LIBSSH2_HOSTKEY_TYPE_ED25519;
|
||||
|
||||
if(len < 23)
|
||||
|
593
src/kex.c
593
src/kex.c
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -778,13 +778,13 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
|
||||
key_type = LIBSSH2_KNOWNHOST_KEY_SSHDSS;
|
||||
else if(!strncmp(key_type_name, "ssh-rsa", key_type_len))
|
||||
key_type = LIBSSH2_KNOWNHOST_KEY_SSHRSA;
|
||||
else if (!strncmp(key_type_name, "ecdsa-sha2-nistp256", key_type_len))
|
||||
else if(!strncmp(key_type_name, "ecdsa-sha2-nistp256", key_type_len))
|
||||
key_type = LIBSSH2_KNOWNHOST_KEY_ECDSA_256;
|
||||
else if (!strncmp(key_type_name, "ecdsa-sha2-nistp384", key_type_len))
|
||||
else if(!strncmp(key_type_name, "ecdsa-sha2-nistp384", key_type_len))
|
||||
key_type = LIBSSH2_KNOWNHOST_KEY_ECDSA_384;
|
||||
else if (!strncmp(key_type_name, "ecdsa-sha2-nistp521", key_type_len))
|
||||
else if(!strncmp(key_type_name, "ecdsa-sha2-nistp521", key_type_len))
|
||||
key_type = LIBSSH2_KNOWNHOST_KEY_ECDSA_521;
|
||||
else if (!strncmp(key_type_name, "ssh-ed25519", key_type_len))
|
||||
else if(!strncmp(key_type_name, "ssh-ed25519", key_type_len))
|
||||
key_type = LIBSSH2_KNOWNHOST_KEY_ED25519;
|
||||
else
|
||||
key_type = LIBSSH2_KNOWNHOST_KEY_UNKNOWN;
|
||||
@ -1037,10 +1037,10 @@ knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
|
||||
key_type_name = "ecdsa-sha2-nistp521";
|
||||
key_type_len = 19;
|
||||
break;
|
||||
case LIBSSH2_KNOWNHOST_KEY_ED25519:
|
||||
key_type_name = "ssh-ed25519";
|
||||
key_type_len = 11;
|
||||
break;
|
||||
case LIBSSH2_KNOWNHOST_KEY_ED25519:
|
||||
key_type_name = "ssh-ed25519";
|
||||
key_type_len = 11;
|
||||
break;
|
||||
case LIBSSH2_KNOWNHOST_KEY_UNKNOWN:
|
||||
key_type_name = node->key_type_name;
|
||||
if(key_type_name) {
|
||||
|
@ -437,7 +437,8 @@ _libssh2_dsa_sha1_sign(libssh2_dsa_ctx * dsactx,
|
||||
memcpy(zhash + 1, hash, hash_len);
|
||||
zhash[0] = 0;
|
||||
|
||||
if(gcry_sexp_build(&data, NULL, "(data (value %b))", hash_len + 1, zhash)) {
|
||||
if(gcry_sexp_build(&data, NULL, "(data (value %b))",
|
||||
hash_len + 1, zhash)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -603,8 +604,9 @@ _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session,
|
||||
const char *passphrase)
|
||||
{
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
||||
"Unable to extract public key from private key in memory: "
|
||||
"Method unimplemented in libgcrypt backend");
|
||||
"Unable to extract public key from private "
|
||||
"key in memory: "
|
||||
"Method unimplemented in libgcrypt backend");
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -204,11 +204,16 @@
|
||||
#define _libssh2_bn_ctx_new() 0
|
||||
#define _libssh2_bn_ctx_free(bnctx) ((void)0)
|
||||
#define _libssh2_bn_init() gcry_mpi_new(0)
|
||||
#define _libssh2_bn_init_from_bin() NULL /* because gcry_mpi_scan() creates a new bignum */
|
||||
#define _libssh2_bn_init_from_bin() NULL /* because gcry_mpi_scan() creates a
|
||||
new bignum */
|
||||
#define _libssh2_bn_set_word(bn, val) gcry_mpi_set_ui(bn, val)
|
||||
#define _libssh2_bn_from_bin(bn, len, val) gcry_mpi_scan(&((bn)), GCRYMPI_FMT_USG, val, len, NULL)
|
||||
#define _libssh2_bn_to_bin(bn, val) gcry_mpi_print (GCRYMPI_FMT_USG, val, _libssh2_bn_bytes(bn), NULL, bn)
|
||||
#define _libssh2_bn_bytes(bn) (gcry_mpi_get_nbits (bn) / 8 + ((gcry_mpi_get_nbits (bn) % 8 == 0) ? 0 : 1))
|
||||
#define _libssh2_bn_from_bin(bn, len, val) \
|
||||
gcry_mpi_scan(&((bn)), GCRYMPI_FMT_USG, val, len, NULL)
|
||||
#define _libssh2_bn_to_bin(bn, val) \
|
||||
gcry_mpi_print(GCRYMPI_FMT_USG, val, _libssh2_bn_bytes(bn), NULL, bn)
|
||||
#define _libssh2_bn_bytes(bn) \
|
||||
(gcry_mpi_get_nbits (bn) / 8 + \
|
||||
((gcry_mpi_get_nbits (bn) % 8 == 0) ? 0 : 1))
|
||||
#define _libssh2_bn_bits(bn) gcry_mpi_get_nbits (bn)
|
||||
#define _libssh2_bn_free(bn) gcry_mpi_release(bn)
|
||||
|
||||
|
@ -58,15 +58,12 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
/* The following CPP block should really only be in session.c and
|
||||
packet.c. However, AIX have #define's for 'events' and 'revents'
|
||||
and we are using those names in libssh2.h, so we need to include
|
||||
the AIX headers first, to make sure all code is compiled with
|
||||
consistent names of these fields. While arguable the best would to
|
||||
change libssh2.h to use other names, that would break backwards
|
||||
compatibility. For more information, see:
|
||||
https://www.mail-archive.com/libssh2-devel%40lists.sourceforge.net/msg00003.html
|
||||
https://www.mail-archive.com/libssh2-devel%40lists.sourceforge.net/msg00224.html
|
||||
/* The following CPP block should really only be in session.c and packet.c.
|
||||
However, AIX have #define's for 'events' and 'revents' and we are using
|
||||
those names in libssh2.h, so we need to include the AIX headers first, to
|
||||
make sure all code is compiled with consistent names of these fields.
|
||||
While arguable the best would to change libssh2.h to use other names, that
|
||||
would break backwards compatibility.
|
||||
*/
|
||||
#ifdef HAVE_POLL
|
||||
# include <sys/poll.h>
|
||||
@ -289,9 +286,12 @@ typedef struct key_exchange_state_low_t
|
||||
size_t data_len;
|
||||
_libssh2_ec_key *private_key; /* SSH2 ecdh private key */
|
||||
unsigned char *public_key_oct; /* SSH2 ecdh public key octal value */
|
||||
size_t public_key_oct_len; /* SSH2 ecdh public key octal value length */
|
||||
unsigned char *curve25519_public_key; /* curve25519 public key, 32 bytes */
|
||||
unsigned char *curve25519_private_key; /* curve25519 private key, 32 bytes */
|
||||
size_t public_key_oct_len; /* SSH2 ecdh public key octal value
|
||||
length */
|
||||
unsigned char *curve25519_public_key; /* curve25519 public key, 32
|
||||
bytes */
|
||||
unsigned char *curve25519_private_key; /* curve25519 private key, 32
|
||||
bytes */
|
||||
} key_exchange_state_low_t;
|
||||
|
||||
typedef struct key_exchange_state_t
|
||||
@ -423,7 +423,8 @@ struct _LIBSSH2_CHANNEL
|
||||
|
||||
/* State variables used in libssh2_channel_receive_window_adjust() */
|
||||
libssh2_nonblocking_states adjust_state;
|
||||
unsigned char adjust_adjust[9]; /* packet_type(1) + channel(4) + adjustment(4) */
|
||||
unsigned char adjust_adjust[9]; /* packet_type(1) + channel(4) +
|
||||
adjustment(4) */
|
||||
|
||||
/* State variables used in libssh2_channel_read_ex() */
|
||||
libssh2_nonblocking_states read_state;
|
||||
@ -662,7 +663,8 @@ struct _LIBSSH2_SESSION
|
||||
struct transportpacket packet;
|
||||
#ifdef LIBSSH2DEBUG
|
||||
int showmask; /* what debug/trace messages to display */
|
||||
libssh2_trace_handler_func tracehandler; /* callback to display trace messages */
|
||||
libssh2_trace_handler_func tracehandler; /* callback to display trace
|
||||
messages */
|
||||
void *tracehandler_context; /* context for the trace handler */
|
||||
#endif
|
||||
|
||||
@ -870,7 +872,8 @@ struct _LIBSSH2_KEX_METHOD
|
||||
{
|
||||
const char *name;
|
||||
|
||||
/* Key exchange, populates session->* and returns 0 on success, non-0 on error */
|
||||
/* Key exchange, populates session->* and returns 0 on success, non-0 on
|
||||
error */
|
||||
int (*exchange_keys) (LIBSSH2_SESSION * session,
|
||||
key_exchange_state_low_t * key_state);
|
||||
|
||||
@ -887,8 +890,10 @@ struct _LIBSSH2_HOSTKEY_METHOD
|
||||
int (*initPEM) (LIBSSH2_SESSION * session, const char *privkeyfile,
|
||||
unsigned const char *passphrase, void **abstract);
|
||||
int (*initPEMFromMemory) (LIBSSH2_SESSION * session,
|
||||
const char *privkeyfiledata, size_t privkeyfiledata_len,
|
||||
unsigned const char *passphrase, void **abstract);
|
||||
const char *privkeyfiledata,
|
||||
size_t privkeyfiledata_len,
|
||||
unsigned const char *passphrase,
|
||||
void **abstract);
|
||||
int (*sig_verify) (LIBSSH2_SESSION * session, const unsigned char *sig,
|
||||
size_t sig_len, const unsigned char *m,
|
||||
size_t m_len, void **abstract);
|
||||
@ -951,7 +956,8 @@ struct _LIBSSH2_COMP_METHOD
|
||||
void _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format,
|
||||
...);
|
||||
#else
|
||||
#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || defined(__GNUC__)
|
||||
#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
|
||||
defined(__GNUC__)
|
||||
/* C99 supported and also by older GCC */
|
||||
#define _libssh2_debug(x,y,z,...) do {} while (0)
|
||||
#else
|
||||
@ -972,7 +978,8 @@ _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
|
||||
|
||||
/* Initial packet state, prior to MAC check */
|
||||
#define LIBSSH2_MAC_UNCONFIRMED 1
|
||||
/* When MAC type is "none" (proto initiation phase) all packets are deemed "confirmed" */
|
||||
/* When MAC type is "none" (proto initiation phase) all packets are deemed
|
||||
"confirmed" */
|
||||
#define LIBSSH2_MAC_CONFIRMED 0
|
||||
/* Something very bad is going on */
|
||||
#define LIBSSH2_MAC_INVALID -1
|
||||
@ -997,7 +1004,8 @@ _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
|
||||
#define SSH_MSG_KEXDH_INIT 30
|
||||
#define SSH_MSG_KEXDH_REPLY 31
|
||||
|
||||
/* diffie-hellman-group-exchange-sha1 and diffie-hellman-group-exchange-sha256 */
|
||||
/* diffie-hellman-group-exchange-sha1 and
|
||||
diffie-hellman-group-exchange-sha256 */
|
||||
#define SSH_MSG_KEX_DH_GEX_REQUEST_OLD 30
|
||||
#define SSH_MSG_KEX_DH_GEX_REQUEST 34
|
||||
#define SSH_MSG_KEX_DH_GEX_GROUP 31
|
||||
|
@ -250,7 +250,7 @@ _libssh2_mbedtls_bignum_init(void)
|
||||
void
|
||||
_libssh2_mbedtls_bignum_free(_libssh2_bn *bn)
|
||||
{
|
||||
if (bn) {
|
||||
if(bn) {
|
||||
mbedtls_mpi_free(bn);
|
||||
mbedtls_free(bn);
|
||||
}
|
||||
@ -267,7 +267,8 @@ _libssh2_mbedtls_bignum_random(_libssh2_bn *bn, int bits, int top, int bottom)
|
||||
return -1;
|
||||
|
||||
len = (bits + 7) >> 3;
|
||||
err = mbedtls_mpi_fill_random(bn, len, mbedtls_ctr_drbg_random, &_libssh2_mbedtls_ctr_drbg);
|
||||
err = mbedtls_mpi_fill_random(bn, len, mbedtls_ctr_drbg_random,
|
||||
&_libssh2_mbedtls_ctr_drbg);
|
||||
if(err)
|
||||
return -1;
|
||||
|
||||
@ -278,10 +279,11 @@ _libssh2_mbedtls_bignum_random(_libssh2_bn *bn, int bits, int top, int bottom)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If `top` is -1, the most significant bit of the random number can be zero.
|
||||
If top is 0, the most significant bit of the random number is set to 1,
|
||||
and if top is 1, the two most significant bits of the number will be set
|
||||
to 1, so that the product of two such random numbers will always have 2*bits length.
|
||||
/* If `top` is -1, the most significant bit of the random number can be
|
||||
zero. If top is 0, the most significant bit of the random number is
|
||||
set to 1, and if top is 1, the two most significant bits of the number
|
||||
will be set to 1, so that the product of two such random numbers will
|
||||
always have 2*bits length.
|
||||
*/
|
||||
for(i = 0; i <= top; ++i) {
|
||||
err = mbedtls_mpi_set_bit(bn, bits-i-1, 1);
|
||||
@ -351,7 +353,8 @@ _libssh2_mbedtls_rsa_new(libssh2_rsa_ctx **rsa,
|
||||
(ret = mbedtls_mpi_read_binary(&(ctx->Q), qdata, qlen) ) != 0 ||
|
||||
(ret = mbedtls_mpi_read_binary(&(ctx->DP), e1data, e1len) ) != 0 ||
|
||||
(ret = mbedtls_mpi_read_binary(&(ctx->DQ), e2data, e2len) ) != 0 ||
|
||||
(ret = mbedtls_mpi_read_binary(&(ctx->QP), coeffdata, coefflen) ) != 0) {
|
||||
(ret = mbedtls_mpi_read_binary(&(ctx->QP), coeffdata, coefflen) )
|
||||
!= 0) {
|
||||
ret = -1;
|
||||
}
|
||||
ret = mbedtls_rsa_check_privkey(ctx);
|
||||
@ -450,7 +453,8 @@ _libssh2_mbedtls_rsa_sha1_verify(libssh2_rsa_ctx *rsa,
|
||||
return -1; /* failure */
|
||||
|
||||
ret = mbedtls_rsa_pkcs1_verify(rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
|
||||
MBEDTLS_MD_SHA1, SHA_DIGEST_LENGTH, hash, sig);
|
||||
MBEDTLS_MD_SHA1, SHA_DIGEST_LENGTH,
|
||||
hash, sig);
|
||||
|
||||
return (ret == 0) ? 0 : -1;
|
||||
}
|
||||
|
18
src/misc.c
18
src/misc.c
@ -62,7 +62,8 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
int _libssh2_error_flags(LIBSSH2_SESSION* session, int errcode, const char *errmsg, int errflags)
|
||||
int _libssh2_error_flags(LIBSSH2_SESSION* session, int errcode,
|
||||
const char *errmsg, int errflags)
|
||||
{
|
||||
if(session->err_flags & LIBSSH2_ERR_FLAG_DUP)
|
||||
LIBSSH2_FREE(session, (char *)session->err_msg);
|
||||
@ -342,7 +343,8 @@ size_t _libssh2_base64_encode(LIBSSH2_SESSION *session,
|
||||
char *base64data;
|
||||
const char *indata = inp;
|
||||
|
||||
*outptr = NULL; /* set to NULL in case of failure before we reach the end */
|
||||
*outptr = NULL; /* set to NULL in case of failure before we reach the
|
||||
end */
|
||||
|
||||
if(0 == insize)
|
||||
insize = strlen(indata);
|
||||
@ -688,7 +690,8 @@ void _libssh2_aes_ctr_increment(unsigned char *ctr,
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
static void * (__cdecl * const volatile memset_libssh)(void *, int, size_t) = memset;
|
||||
static void * (__cdecl * const volatile memset_libssh)(void *, int, size_t) =
|
||||
memset;
|
||||
#else
|
||||
static void * (* const volatile memset_libssh)(void *, int, size_t) = memset;
|
||||
#endif
|
||||
@ -712,7 +715,8 @@ struct string_buf* _libssh2_string_buf_new(LIBSSH2_SESSION *session)
|
||||
{
|
||||
struct string_buf *ret;
|
||||
|
||||
if((ret = _libssh2_calloc(session, sizeof(*ret))) == NULL)
|
||||
ret = _libssh2_calloc(session, sizeof(*ret));
|
||||
if(ret == NULL)
|
||||
return NULL;
|
||||
|
||||
return ret;
|
||||
@ -749,8 +753,8 @@ int _libssh2_get_u32(struct string_buf *buf, uint32_t *out)
|
||||
int _libssh2_get_u64(struct string_buf *buf, libssh2_uint64_t *out)
|
||||
{
|
||||
unsigned char *p = buf->dataptr;
|
||||
if (!_libssh2_check_length(buf, 8)) {
|
||||
return -1;
|
||||
if(!_libssh2_check_length(buf, 8)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = _libssh2_ntohu64(p);
|
||||
@ -763,7 +767,7 @@ int _libssh2_match_string(struct string_buf *buf, const char *match)
|
||||
{
|
||||
unsigned char *out;
|
||||
if((size_t)_libssh2_get_c_string(buf, &out) != strlen(match) ||
|
||||
strncmp((char*)out, match, strlen(match)) != 0) {
|
||||
strncmp((char *)out, match, strlen(match)) != 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
|
14
src/misc.h
14
src/misc.h
@ -50,13 +50,14 @@ struct list_node {
|
||||
};
|
||||
|
||||
struct string_buf {
|
||||
unsigned char *data;
|
||||
unsigned char *dataptr;
|
||||
size_t len;
|
||||
size_t offset;
|
||||
unsigned char *data;
|
||||
unsigned char *dataptr;
|
||||
size_t len;
|
||||
size_t offset;
|
||||
};
|
||||
|
||||
int _libssh2_error_flags(LIBSSH2_SESSION* session, int errcode, const char *errmsg, int errflags);
|
||||
int _libssh2_error_flags(LIBSSH2_SESSION* session, int errcode,
|
||||
const char *errmsg, int errflags);
|
||||
int _libssh2_error(LIBSSH2_SESSION* session, int errcode, const char *errmsg);
|
||||
|
||||
void _libssh2_list_init(struct list_head *head);
|
||||
@ -89,7 +90,8 @@ void *_libssh2_calloc(LIBSSH2_SESSION *session, size_t size);
|
||||
void _libssh2_explicit_zero(void *buf, size_t size);
|
||||
|
||||
struct string_buf* _libssh2_string_buf_new(LIBSSH2_SESSION *session);
|
||||
void _libssh2_string_buf_free(LIBSSH2_SESSION *session, struct string_buf *buf);
|
||||
void _libssh2_string_buf_free(LIBSSH2_SESSION *session,
|
||||
struct string_buf *buf);
|
||||
int _libssh2_get_u32(struct string_buf *buf, uint32_t *out);
|
||||
int _libssh2_get_u64(struct string_buf *buf, libssh2_uint64_t *out);
|
||||
int _libssh2_match_string(struct string_buf *buf, const char *match);
|
||||
|
328
src/openssl.c
328
src/openssl.c
@ -279,7 +279,8 @@ _libssh2_ecdsa_key_get_curve_type(_libssh2_ec_key *key)
|
||||
*/
|
||||
|
||||
int
|
||||
_libssh2_ecdsa_curve_type_from_name(const char *name, libssh2_curve_type *out_type)
|
||||
_libssh2_ecdsa_curve_type_from_name(const char *name,
|
||||
libssh2_curve_type *out_type)
|
||||
{
|
||||
int ret = 0;
|
||||
libssh2_curve_type type;
|
||||
@ -526,7 +527,8 @@ aes_ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
the ciphertext block C1. The counter X is then incremented
|
||||
*/
|
||||
|
||||
if(EVP_EncryptUpdate(c->aes_ctx, b1, &outlen, c->ctr, AES_BLOCK_SIZE) != 1) {
|
||||
if(EVP_EncryptUpdate(c->aes_ctx, b1, &outlen,
|
||||
c->ctr, AES_BLOCK_SIZE) != 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -768,7 +770,7 @@ _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx ** rsa,
|
||||
filedata, filedata_len, passphrase);
|
||||
|
||||
if(rc) {
|
||||
rc = read_openssh_private_key_from_memory((void**)rsa, session,
|
||||
rc = read_openssh_private_key_from_memory((void **)rsa, session,
|
||||
"ssh-rsa", filedata, filedata_len, passphrase);
|
||||
}
|
||||
|
||||
@ -891,20 +893,24 @@ static int _libssh2_rsa_new_additional_parameters(RSA *rsa)
|
||||
q = (*rsa).q;
|
||||
#endif
|
||||
|
||||
if((ctx = BN_CTX_new()) == NULL)
|
||||
ctx = BN_CTX_new();
|
||||
if(ctx == NULL)
|
||||
return -1;
|
||||
|
||||
if((aux = BN_new()) == NULL) {
|
||||
aux = BN_new();
|
||||
if(aux == NULL) {
|
||||
rc = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if((dmp1 = BN_new()) == NULL) {
|
||||
dmp1 = BN_new();
|
||||
if(dmp1 == NULL) {
|
||||
rc = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if((dmq1 = BN_new()) == NULL) {
|
||||
dmq1 = BN_new();
|
||||
if(dmq1 == NULL) {
|
||||
rc = -1;
|
||||
goto out;
|
||||
}
|
||||
@ -959,51 +965,59 @@ gen_publickey_from_rsa_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
"Computing RSA keys from private key data");
|
||||
|
||||
/* public key data */
|
||||
if((nlen = _libssh2_get_bignum_bytes(decrypted, &n)) <= 0) {
|
||||
nlen = _libssh2_get_bignum_bytes(decrypted, &n);
|
||||
if(nlen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"RSA no n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((elen = _libssh2_get_bignum_bytes(decrypted, &e)) <= 0) {
|
||||
elen = _libssh2_get_bignum_bytes(decrypted, &e);
|
||||
if(elen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"RSA no e");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* private key data */
|
||||
if((dlen = _libssh2_get_bignum_bytes(decrypted, &d)) <= 0) {
|
||||
dlen = _libssh2_get_bignum_bytes(decrypted, &d);
|
||||
if(dlen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"RSA no d");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((coefflen = _libssh2_get_bignum_bytes(decrypted, &coeff)) <= 0) {
|
||||
coefflen = _libssh2_get_bignum_bytes(decrypted, &coeff);
|
||||
if(coefflen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"RSA no coeff");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((plen = _libssh2_get_bignum_bytes(decrypted, &p)) <= 0) {
|
||||
plen = _libssh2_get_bignum_bytes(decrypted, &p);
|
||||
if(plen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"RSA no p");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((qlen = _libssh2_get_bignum_bytes(decrypted, &q)) <= 0) {
|
||||
qlen = _libssh2_get_bignum_bytes(decrypted, &q);
|
||||
if(qlen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"RSA no q");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((commentlen = _libssh2_get_c_string(decrypted, &comment)) < 0) {
|
||||
commentlen = _libssh2_get_c_string(decrypted, &comment);
|
||||
if(commentlen < 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"RSA no comment");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((rc = _libssh2_rsa_new(&rsa, e, elen, n, nlen, d, dlen, p, plen,
|
||||
q, qlen, NULL, 0, NULL, 0, coeff, coefflen)) != 0) {
|
||||
q, qlen, NULL, 0, NULL, 0,
|
||||
coeff, coefflen)) != 0) {
|
||||
_libssh2_debug(session,
|
||||
LIBSSH2_TRACE_AUTH,
|
||||
"Could not create RSA private key");
|
||||
@ -1013,7 +1027,7 @@ gen_publickey_from_rsa_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
if(rsa != NULL)
|
||||
rc = _libssh2_rsa_new_additional_parameters(rsa);
|
||||
|
||||
if(rsa != NULL && pubkeydata != NULL && method != NULL){
|
||||
if(rsa != NULL && pubkeydata != NULL && method != NULL) {
|
||||
EVP_PKEY *pk = EVP_PKEY_new();
|
||||
EVP_PKEY_set1_RSA(pk, rsa);
|
||||
|
||||
@ -1083,11 +1097,12 @@ _libssh2_rsa_new_openssh_private(libssh2_rsa_ctx ** rsa,
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(strcmp("ssh-rsa", (const char*)buf) == 0) {
|
||||
if(strcmp("ssh-rsa", (const char *)buf) == 0) {
|
||||
rc = gen_publickey_from_rsa_openssh_priv_data(session, decrypted,
|
||||
NULL, 0,
|
||||
NULL, 0, rsa);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
rc = -1;
|
||||
}
|
||||
|
||||
@ -1114,7 +1129,8 @@ _libssh2_rsa_new_private(libssh2_rsa_ctx ** rsa,
|
||||
filename, passphrase);
|
||||
|
||||
if(rc) {
|
||||
rc = _libssh2_rsa_new_openssh_private(rsa, session, filename, passphrase);
|
||||
rc = _libssh2_rsa_new_openssh_private(rsa, session,
|
||||
filename, passphrase);
|
||||
}
|
||||
|
||||
return rc;
|
||||
@ -1135,11 +1151,11 @@ _libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx ** dsa,
|
||||
|
||||
_libssh2_init_if_needed();
|
||||
|
||||
rc = read_private_key_from_memory((void**)dsa, read_dsa,
|
||||
rc = read_private_key_from_memory((void **)dsa, read_dsa,
|
||||
filedata, filedata_len, passphrase);
|
||||
|
||||
if(rc) {
|
||||
rc = read_openssh_private_key_from_memory((void**)dsa, session,
|
||||
rc = read_openssh_private_key_from_memory((void **)dsa, session,
|
||||
"ssh-dsa", filedata, filedata_len, passphrase);
|
||||
}
|
||||
|
||||
@ -1274,38 +1290,44 @@ gen_publickey_from_dsa_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
LIBSSH2_TRACE_AUTH,
|
||||
"Computing DSA keys from private key data");
|
||||
|
||||
if((plen = _libssh2_get_bignum_bytes(decrypted, &p)) <= 0) {
|
||||
plen = _libssh2_get_bignum_bytes(decrypted, &p);
|
||||
if(plen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"DSA no p");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((qlen = _libssh2_get_bignum_bytes(decrypted, &q)) <= 0) {
|
||||
qlen = _libssh2_get_bignum_bytes(decrypted, &q);
|
||||
if(qlen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"DSA no q");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((glen = _libssh2_get_bignum_bytes(decrypted, &g)) <= 0) {
|
||||
glen = _libssh2_get_bignum_bytes(decrypted, &g);
|
||||
if(glen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"DSA no g");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((pub_len = _libssh2_get_bignum_bytes(decrypted, &pub_key)) <= 0) {
|
||||
pub_len = _libssh2_get_bignum_bytes(decrypted, &pub_key);
|
||||
if(pub_len <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"DSA no public key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((priv_len = _libssh2_get_bignum_bytes(decrypted, &priv_key)) <= 0) {
|
||||
priv_len = _libssh2_get_bignum_bytes(decrypted, &priv_key);
|
||||
if(priv_len <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"DSA no private key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((rc = _libssh2_dsa_new(&dsa, p, plen, q, qlen, g, glen, pub_key, pub_len,
|
||||
priv_key, priv_len)) != 0 ) {
|
||||
rc = _libssh2_dsa_new(&dsa, p, plen, q, qlen, g, glen, pub_key, pub_len,
|
||||
priv_key, priv_len);
|
||||
if(rc != 0) {
|
||||
_libssh2_debug(session,
|
||||
LIBSSH2_ERROR_PROTO,
|
||||
"Could not create DSA private key");
|
||||
@ -1382,7 +1404,7 @@ _libssh2_dsa_new_openssh_private(libssh2_dsa_ctx ** dsa,
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(strcmp("ssh-dss", (const char*)buf) == 0) {
|
||||
if(strcmp("ssh-dss", (const char *)buf) == 0) {
|
||||
rc = gen_publickey_from_dsa_openssh_priv_data(session, decrypted,
|
||||
NULL, 0,
|
||||
NULL, 0, dsa);
|
||||
@ -1414,7 +1436,8 @@ _libssh2_dsa_new_private(libssh2_dsa_ctx ** dsa,
|
||||
filename, passphrase);
|
||||
|
||||
if(rc) {
|
||||
rc = _libssh2_dsa_new_openssh_private(dsa, session, filename, passphrase);
|
||||
rc = _libssh2_dsa_new_openssh_private(dsa, session,
|
||||
filename, passphrase);
|
||||
}
|
||||
|
||||
return rc;
|
||||
@ -1442,8 +1465,9 @@ _libssh2_ecdsa_new_private_frommemory(libssh2_ecdsa_ctx ** ec_ctx,
|
||||
filedata, filedata_len, passphrase);
|
||||
|
||||
if(rc) {
|
||||
rc = read_openssh_private_key_from_memory((void**)ec_ctx, session,
|
||||
"ssh-ecdsa", filedata, filedata_len, passphrase);
|
||||
rc = read_openssh_private_key_from_memory((void **)ec_ctx, session,
|
||||
"ssh-ecdsa", filedata,
|
||||
filedata_len, passphrase);
|
||||
}
|
||||
|
||||
return rc;
|
||||
@ -1455,8 +1479,9 @@ _libssh2_ecdsa_new_private_frommemory(libssh2_ecdsa_ctx ** ec_ctx,
|
||||
#if LIBSSH2_ED25519
|
||||
|
||||
int
|
||||
_libssh2_curve25519_new(LIBSSH2_SESSION *session, libssh2_x25519_ctx **out_ctx,
|
||||
unsigned char **out_public_key, unsigned char **out_private_key)
|
||||
_libssh2_curve25519_new(LIBSSH2_SESSION *session, libssh2_x25519_ctx **out_ctx,
|
||||
unsigned char **out_public_key,
|
||||
unsigned char **out_private_key)
|
||||
{
|
||||
EVP_PKEY *key = NULL;
|
||||
EVP_PKEY_CTX *pctx = NULL;
|
||||
@ -1487,7 +1512,7 @@ _libssh2_curve25519_new(LIBSSH2_SESSION *session, libssh2_x25519_ctx **out_ctx,
|
||||
priv = ASN1_STRING_get0_data(oct);
|
||||
privLen = ASN1_STRING_length(oct);
|
||||
|
||||
if (privLen != LIBSSH2_ED25519_KEY_LEN)
|
||||
if(privLen != LIBSSH2_ED25519_KEY_LEN)
|
||||
goto cleanExit;
|
||||
|
||||
pubkey = X509_PUBKEY_new();
|
||||
@ -1521,13 +1546,15 @@ _libssh2_curve25519_new(LIBSSH2_SESSION *session, libssh2_x25519_ctx **out_ctx,
|
||||
if(ctx == NULL)
|
||||
goto cleanExit;
|
||||
|
||||
ctx->private_key = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL,
|
||||
(const unsigned char*)priv,
|
||||
LIBSSH2_ED25519_KEY_LEN);
|
||||
ctx->private_key =
|
||||
EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL,
|
||||
(const unsigned char *)priv,
|
||||
LIBSSH2_ED25519_KEY_LEN);
|
||||
|
||||
ctx->public_key = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL,
|
||||
(const unsigned char*)pub,
|
||||
LIBSSH2_ED25519_KEY_LEN);
|
||||
ctx->public_key =
|
||||
EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL,
|
||||
(const unsigned char *)pub,
|
||||
LIBSSH2_ED25519_KEY_LEN);
|
||||
|
||||
if(ctx->public_key == NULL || ctx->private_key == NULL) {
|
||||
_libssh2_x25519_free(ctx);
|
||||
@ -1566,8 +1593,8 @@ gen_publickey_from_ed25519_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
libssh2_ed25519_ctx **out_ctx)
|
||||
{
|
||||
libssh2_ed25519_ctx *ctx = NULL;
|
||||
unsigned char* method_buf = NULL;
|
||||
unsigned char* key = NULL;
|
||||
unsigned char *method_buf = NULL;
|
||||
unsigned char *key = NULL;
|
||||
int i, rc, ret = 0;
|
||||
unsigned char *pub_key, *priv_key, *buf;
|
||||
size_t key_len = 0;
|
||||
@ -1583,7 +1610,8 @@ gen_publickey_from_ed25519_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(_libssh2_get_c_string(decrypted, &priv_key) != LIBSSH2_ED25519_PRIVATE_KEY_LEN) {
|
||||
if(_libssh2_get_c_string(decrypted, &priv_key) !=
|
||||
LIBSSH2_ED25519_PRIVATE_KEY_LEN) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Wrong private key length");
|
||||
ret = -1;
|
||||
@ -1591,22 +1619,28 @@ gen_publickey_from_ed25519_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
}
|
||||
|
||||
ctx = _libssh2_ed25519_new_ctx();
|
||||
if (ctx == NULL) {
|
||||
if(ctx == NULL) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for ed25519 key");
|
||||
ret = -1;
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
/* first 32 bytes of priv_key is the private key, the last 32 bytes are the public key */
|
||||
ctx->private_key = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL,
|
||||
(const unsigned char*)priv_key, LIBSSH2_ED25519_KEY_LEN);
|
||||
/* first 32 bytes of priv_key is the private key, the last 32 bytes are
|
||||
the public key */
|
||||
ctx->private_key =
|
||||
EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL,
|
||||
(const unsigned char *)priv_key,
|
||||
LIBSSH2_ED25519_KEY_LEN);
|
||||
|
||||
ctx->public_key = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL,
|
||||
(const unsigned char*)pub_key, LIBSSH2_ED25519_KEY_LEN);
|
||||
ctx->public_key =
|
||||
EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL,
|
||||
(const unsigned char *)pub_key,
|
||||
LIBSSH2_ED25519_KEY_LEN);
|
||||
|
||||
/* comment */
|
||||
if((rc = _libssh2_get_c_string(decrypted, &buf)) < 0) {
|
||||
rc = _libssh2_get_c_string(decrypted, &buf);
|
||||
if(rc < 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Unable to read comment");
|
||||
ret = -1;
|
||||
@ -1619,7 +1653,8 @@ gen_publickey_from_ed25519_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
memcpy(comment, buf, rc);
|
||||
memcpy(comment + rc, "\0", 1);
|
||||
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_AUTH, "Key comment: %s", comment);
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_AUTH, "Key comment: %s",
|
||||
comment);
|
||||
|
||||
LIBSSH2_FREE(session, comment);
|
||||
}
|
||||
@ -1641,14 +1676,16 @@ gen_publickey_from_ed25519_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
if(ret == 0) {
|
||||
_libssh2_debug(session,
|
||||
LIBSSH2_TRACE_AUTH,
|
||||
"Computing public key from ED25519 private key envelop");
|
||||
"Computing public key from ED25519 "
|
||||
"private key envelop");
|
||||
|
||||
method_buf = LIBSSH2_ALLOC(session, 11); /* ssh-ed25519. */
|
||||
if(method_buf == NULL) {
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
/* Key form is: type_len(4) + type(11) + pub_key_len(4) + pub_key(32). */
|
||||
/* Key form is: type_len(4) + type(11) + pub_key_len(4) +
|
||||
pub_key(32). */
|
||||
key_len = LIBSSH2_ED25519_KEY_LEN + 19;
|
||||
key = LIBSSH2_CALLOC(session, key_len);
|
||||
if(key == NULL) {
|
||||
@ -1658,7 +1695,7 @@ gen_publickey_from_ed25519_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
p = key;
|
||||
|
||||
_libssh2_store_str(&p, "ssh-ed25519", 11);
|
||||
_libssh2_store_str(&p, (const char*)pub_key, LIBSSH2_ED25519_KEY_LEN);
|
||||
_libssh2_store_str(&p, (const char *)pub_key, LIBSSH2_ED25519_KEY_LEN);
|
||||
|
||||
memcpy(method_buf, "ssh-ed25519", 11);
|
||||
|
||||
@ -1678,7 +1715,7 @@ gen_publickey_from_ed25519_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
if(pubkeydata_len != NULL)
|
||||
*pubkeydata_len = key_len;
|
||||
|
||||
if (out_ctx != NULL)
|
||||
if(out_ctx != NULL)
|
||||
*out_ctx = ctx;
|
||||
else if(ctx != NULL)
|
||||
_libssh2_ed25519_free(ctx);
|
||||
@ -1741,7 +1778,7 @@ _libssh2_ed25519_new_private(libssh2_ed25519_ctx ** ed_ctx,
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(strcmp("ssh-ed25519", (const char*)buf) == 0) {
|
||||
if(strcmp("ssh-ed25519", (const char *)buf) == 0) {
|
||||
rc = gen_publickey_from_ed25519_openssh_priv_data(session,
|
||||
decrypted,
|
||||
NULL,
|
||||
@ -1767,17 +1804,21 @@ _libssh2_ed25519_new_private(libssh2_ed25519_ctx ** ed_ctx,
|
||||
int
|
||||
_libssh2_ed25519_new_private_frommemory(libssh2_ed25519_ctx ** ed_ctx,
|
||||
LIBSSH2_SESSION * session,
|
||||
const char *filedata, size_t filedata_len,
|
||||
const char *filedata,
|
||||
size_t filedata_len,
|
||||
unsigned const char *passphrase)
|
||||
{
|
||||
return read_openssh_private_key_from_memory((void**)ed_ctx, session, "ssh-ed25519",
|
||||
filedata, filedata_len, passphrase);
|
||||
return read_openssh_private_key_from_memory((void **)ed_ctx, session,
|
||||
"ssh-ed25519",
|
||||
filedata, filedata_len,
|
||||
passphrase);
|
||||
}
|
||||
|
||||
int
|
||||
_libssh2_ed25519_new_public(libssh2_ed25519_ctx ** ed_ctx,
|
||||
LIBSSH2_SESSION * session,
|
||||
const unsigned char *raw_pub_key, const uint8_t key_len)
|
||||
const unsigned char *raw_pub_key,
|
||||
const uint8_t key_len)
|
||||
{
|
||||
libssh2_ed25519_ctx *ctx = NULL;
|
||||
EVP_PKEY *public_key = NULL;
|
||||
@ -1785,18 +1826,23 @@ _libssh2_ed25519_new_public(libssh2_ed25519_ctx ** ed_ctx,
|
||||
if(ed_ctx == NULL)
|
||||
return -1;
|
||||
|
||||
public_key = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, (const unsigned char*)raw_pub_key, key_len);
|
||||
public_key =
|
||||
EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL,
|
||||
(const unsigned char *)raw_pub_key,
|
||||
key_len);
|
||||
if(public_key == NULL) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_PROTO, "could not create ED25519 public key");
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"could not create ED25519 public key");
|
||||
}
|
||||
|
||||
|
||||
ctx = _libssh2_ed25519_new_ctx();
|
||||
if(ctx == NULL) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "could not alloc public/private key");
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"could not alloc public/private key");
|
||||
}
|
||||
|
||||
|
||||
ctx->public_key = public_key;
|
||||
|
||||
|
||||
if(ed_ctx != NULL)
|
||||
*ed_ctx = ctx;
|
||||
else if(ctx != NULL)
|
||||
@ -2238,7 +2284,9 @@ gen_publickey_from_ec_evp(LIBSSH2_SESSION *session,
|
||||
}
|
||||
|
||||
/* get length */
|
||||
octal_len = EC_POINT_point2oct(group, public_key, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, bn_ctx);
|
||||
octal_len = EC_POINT_point2oct(group, public_key,
|
||||
POINT_CONVERSION_UNCOMPRESSED,
|
||||
NULL, 0, bn_ctx);
|
||||
if(octal_len > EC_MAX_POINT_LEN) {
|
||||
rc = -1;
|
||||
goto clean_exit;
|
||||
@ -2257,7 +2305,8 @@ gen_publickey_from_ec_evp(LIBSSH2_SESSION *session,
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
/* Key form is: type_len(4) + type(19) + domain_len(4) + domain(8) + pub_key_len(4) + pub_key(~65). */
|
||||
/* Key form is: type_len(4) + type(19) + domain_len(4) + domain(8) +
|
||||
pub_key_len(4) + pub_key(~65). */
|
||||
key_len = 4 + 19 + 4 + 8 + 4 + octal_len;
|
||||
key = LIBSSH2_ALLOC(session, key_len);
|
||||
if(key == NULL) {
|
||||
@ -2323,32 +2372,37 @@ gen_publickey_from_ecdsa_openssh_priv_data(LIBSSH2_SESSION *session,
|
||||
LIBSSH2_TRACE_AUTH,
|
||||
"Computing ECDSA keys from private key data");
|
||||
|
||||
if ((curvelen = _libssh2_get_c_string(decrypted, &curve)) <= 0) {
|
||||
curvelen = _libssh2_get_c_string(decrypted, &curve);
|
||||
if(curvelen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"ECDSA no curve");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((pointlen = _libssh2_get_c_string(decrypted, &point_buf)) <= 0) {
|
||||
pointlen = _libssh2_get_c_string(decrypted, &point_buf);
|
||||
if(pointlen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"ECDSA no point");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((exponentlen = _libssh2_get_bignum_bytes(decrypted, &exponent)) <= 0) {
|
||||
exponentlen = _libssh2_get_bignum_bytes(decrypted, &exponent);
|
||||
if(exponentlen <= 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"ECDSA no exponent");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((rc = _libssh2_ecdsa_curve_name_with_octal_new(&ec_key, point_buf,
|
||||
pointlen, curve_type)) != 0) {
|
||||
rc = _libssh2_ecdsa_curve_name_with_octal_new(&ec_key, point_buf,
|
||||
pointlen, curve_type);
|
||||
if(rc != 0) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"ECDSA could not create key");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if ((bn_exponent = BN_new()) == NULL) {
|
||||
bn_exponent = BN_new();
|
||||
if(bn_exponent == NULL) {
|
||||
rc = -1;
|
||||
goto fail;
|
||||
}
|
||||
@ -2408,7 +2462,7 @@ _libssh2_ecdsa_new_openssh_private(libssh2_ecdsa_ctx ** ec_ctx,
|
||||
_libssh2_init_if_needed();
|
||||
|
||||
fp = fopen(filename, "r");
|
||||
if (!fp) {
|
||||
if(!fp) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_FILE,
|
||||
"Unable to open OpenSSH ECDSA private key file");
|
||||
return -1;
|
||||
@ -2429,7 +2483,7 @@ _libssh2_ecdsa_new_openssh_private(libssh2_ecdsa_ctx ** ec_ctx,
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = _libssh2_ecdsa_curve_type_from_name((const char*)buf, &type);
|
||||
rc = _libssh2_ecdsa_curve_type_from_name((const char *)buf, &type);
|
||||
|
||||
if(rc == 0) {
|
||||
rc = gen_publickey_from_ecdsa_openssh_priv_data(session, type,
|
||||
@ -2462,7 +2516,8 @@ _libssh2_ecdsa_new_private(libssh2_ecdsa_ctx ** ec_ctx,
|
||||
filename, passphrase);
|
||||
|
||||
if(rc) {
|
||||
return _libssh2_ecdsa_new_openssh_private(ec_ctx, session, filename, passphrase);
|
||||
return _libssh2_ecdsa_new_openssh_private(ec_ctx, session,
|
||||
filename, passphrase);
|
||||
}
|
||||
|
||||
return rc;
|
||||
@ -2477,9 +2532,11 @@ _libssh2_ecdsa_new_private(libssh2_ecdsa_ctx ** ec_ctx,
|
||||
*/
|
||||
|
||||
int
|
||||
_libssh2_ecdsa_create_key(LIBSSH2_SESSION *session, _libssh2_ec_key **out_private_key,
|
||||
_libssh2_ecdsa_create_key(LIBSSH2_SESSION *session,
|
||||
_libssh2_ec_key **out_private_key,
|
||||
unsigned char **out_public_key_octal,
|
||||
size_t *out_public_key_octal_len, libssh2_curve_type curve_type)
|
||||
size_t *out_public_key_octal_len,
|
||||
libssh2_curve_type curve_type)
|
||||
{
|
||||
int ret = 1;
|
||||
size_t octal_len = 0;
|
||||
@ -2500,7 +2557,9 @@ _libssh2_ecdsa_create_key(LIBSSH2_SESSION *session, _libssh2_ec_key **out_privat
|
||||
public_key = EC_KEY_get0_public_key(private_key);
|
||||
|
||||
/* get length */
|
||||
octal_len = EC_POINT_point2oct(group, public_key, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, bn_ctx);
|
||||
octal_len = EC_POINT_point2oct(group, public_key,
|
||||
POINT_CONVERSION_UNCOMPRESSED,
|
||||
NULL, 0, bn_ctx);
|
||||
if(octal_len > EC_MAX_POINT_LEN) {
|
||||
ret = -1;
|
||||
goto clean_exit;
|
||||
@ -2568,7 +2627,8 @@ _libssh2_ecdh_gen_k(_libssh2_bn **k, _libssh2_ec_key *private_key,
|
||||
if(server_public_key_point == NULL)
|
||||
return -1;
|
||||
|
||||
rc = EC_POINT_oct2point(private_key_group, server_public_key_point, server_public_key, server_public_key_len, bn_ctx);
|
||||
rc = EC_POINT_oct2point(private_key_group, server_public_key_point,
|
||||
server_public_key, server_public_key_len, bn_ctx);
|
||||
if(rc != 1) {
|
||||
ret = -1;
|
||||
goto clean_exit;
|
||||
@ -2581,7 +2641,8 @@ _libssh2_ecdh_gen_k(_libssh2_bn **k, _libssh2_ec_key *private_key,
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
secret_len = ECDH_compute_key(secret, secret_len, server_public_key_point, private_key, NULL);
|
||||
secret_len = ECDH_compute_key(secret, secret_len, server_public_key_point,
|
||||
private_key, NULL);
|
||||
|
||||
if(secret_len <= 0 || secret_len > EC_MAX_POINT_LEN) {
|
||||
ret = -1;
|
||||
@ -2620,9 +2681,9 @@ _libssh2_ed25519_sign(libssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session,
|
||||
unsigned char *sig = NULL;
|
||||
|
||||
if(md_ctx != NULL) {
|
||||
if (EVP_DigestSignInit(md_ctx, NULL, NULL, NULL, ctx->private_key) != 1)
|
||||
if(EVP_DigestSignInit(md_ctx, NULL, NULL, NULL, ctx->private_key) != 1)
|
||||
goto clean_exit;
|
||||
if (EVP_DigestSign(md_ctx, NULL, &sig_len, message, message_len) != 1)
|
||||
if(EVP_DigestSign(md_ctx, NULL, &sig_len, message, message_len) != 1)
|
||||
goto clean_exit;
|
||||
|
||||
if(sig_len != LIBSSH2_ED25519_SIG_LEN)
|
||||
@ -2638,7 +2699,8 @@ _libssh2_ed25519_sign(libssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session,
|
||||
if(rc == 1) {
|
||||
*out_sig = sig;
|
||||
*out_sig_len = sig_len;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
*out_sig_len = 0;
|
||||
*out_sig = NULL;
|
||||
LIBSSH2_FREE(session, sig);
|
||||
@ -2706,7 +2768,8 @@ _libssh2_curve25519_gen_k(_libssh2_bn **k,
|
||||
|
||||
if(rc == 1 && out_len == LIBSSH2_ED25519_KEY_LEN) {
|
||||
BN_bin2bn(out_shared_key, LIBSSH2_ED25519_KEY_LEN, *k);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
rc = -1;
|
||||
}
|
||||
|
||||
@ -2718,7 +2781,7 @@ cleanExit:
|
||||
EVP_PKEY_free(peer_key);
|
||||
if(server_key)
|
||||
EVP_PKEY_free(server_key);
|
||||
if (bn_ctx != NULL)
|
||||
if(bn_ctx != NULL)
|
||||
BN_CTX_free(bn_ctx);
|
||||
|
||||
return (rc == 1) ? 0 : -1;
|
||||
@ -2764,8 +2827,7 @@ _libssh2_pub_priv_openssh_keyfile(LIBSSH2_SESSION *session,
|
||||
struct string_buf *decrypted = NULL;
|
||||
int rc = 0;
|
||||
|
||||
if(session == NULL)
|
||||
{
|
||||
if(session == NULL) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Session is required");
|
||||
return -1;
|
||||
@ -2780,7 +2842,7 @@ _libssh2_pub_priv_openssh_keyfile(LIBSSH2_SESSION *session,
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = _libssh2_openssh_pem_parse(session, (const unsigned char*)passphrase,
|
||||
rc = _libssh2_openssh_pem_parse(session, (const unsigned char *)passphrase,
|
||||
fp, &decrypted);
|
||||
fclose(fp);
|
||||
if(rc) {
|
||||
@ -2792,7 +2854,7 @@ _libssh2_pub_priv_openssh_keyfile(LIBSSH2_SESSION *session,
|
||||
/* We have a new key file, now try and parse it using supported types */
|
||||
rc = _libssh2_get_c_string(decrypted, &buf);
|
||||
|
||||
if(rc < 1 || buf == NULL){
|
||||
if(rc < 1 || buf == NULL) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Public key type in decrypted key data not found");
|
||||
return -1;
|
||||
@ -2801,34 +2863,44 @@ _libssh2_pub_priv_openssh_keyfile(LIBSSH2_SESSION *session,
|
||||
rc = -1;
|
||||
|
||||
#if LIBSSH2_ED25519
|
||||
if(strcmp("ssh-ed25519", (const char*)buf) == 0) {
|
||||
if(strcmp("ssh-ed25519", (const char *)buf) == 0) {
|
||||
rc = gen_publickey_from_ed25519_openssh_priv_data(session, decrypted,
|
||||
method, method_len,
|
||||
pubkeydata, pubkeydata_len, NULL);
|
||||
pubkeydata,
|
||||
pubkeydata_len,
|
||||
NULL);
|
||||
}
|
||||
#endif
|
||||
#if LIBSSH2_RSA
|
||||
if(strcmp("ssh-rsa", (const char*)buf) == 0) {
|
||||
if(strcmp("ssh-rsa", (const char *)buf) == 0) {
|
||||
rc = gen_publickey_from_rsa_openssh_priv_data(session, decrypted,
|
||||
method, method_len,
|
||||
pubkeydata, pubkeydata_len, NULL);
|
||||
method, method_len,
|
||||
pubkeydata,
|
||||
pubkeydata_len,
|
||||
NULL);
|
||||
}
|
||||
#endif
|
||||
#if LIBSSH2_DSA
|
||||
if(strcmp("ssh-dss", (const char*)buf) == 0) {
|
||||
if(strcmp("ssh-dss", (const char *)buf) == 0) {
|
||||
rc = gen_publickey_from_dsa_openssh_priv_data(session, decrypted,
|
||||
method, method_len,
|
||||
pubkeydata, pubkeydata_len, NULL);
|
||||
pubkeydata,
|
||||
pubkeydata_len,
|
||||
NULL);
|
||||
}
|
||||
#endif
|
||||
#if LIBSSH2_ECDSA
|
||||
{
|
||||
libssh2_curve_type type;
|
||||
|
||||
if(_libssh2_ecdsa_curve_type_from_name((const char*)buf, &type) == 0) {
|
||||
rc = gen_publickey_from_ecdsa_openssh_priv_data(session, type, decrypted,
|
||||
if(_libssh2_ecdsa_curve_type_from_name((const char *)buf,
|
||||
&type) == 0) {
|
||||
rc = gen_publickey_from_ecdsa_openssh_priv_data(session, type,
|
||||
decrypted,
|
||||
method, method_len,
|
||||
pubkeydata, pubkeydata_len, NULL);
|
||||
pubkeydata,
|
||||
pubkeydata_len,
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -2955,9 +3027,9 @@ _libssh2_pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session,
|
||||
*key_ctx = NULL;
|
||||
|
||||
if(session == NULL) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Session is required");
|
||||
return -1;
|
||||
_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Session is required");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(key_type != NULL && (strlen(key_type) > 11 || strlen(key_type) < 7)) {
|
||||
@ -2973,7 +3045,7 @@ _libssh2_pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session,
|
||||
privatekeydata_len, &decrypted);
|
||||
|
||||
if(rc) {
|
||||
return rc;
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* We have a new key file, now try and parse it using supported types */
|
||||
@ -2986,32 +3058,37 @@ _libssh2_pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session,
|
||||
}
|
||||
|
||||
#if LIBSSH2_ED25519
|
||||
if(strcmp("ssh-ed25519", (const char*)buf) == 0) {
|
||||
if(strcmp("ssh-ed25519", (const char *)buf) == 0) {
|
||||
if(key_type == NULL || strcmp("ssh-ed25519", key_type) == 0) {
|
||||
rc = gen_publickey_from_ed25519_openssh_priv_data(session, decrypted,
|
||||
method, method_len,
|
||||
pubkeydata, pubkeydata_len,
|
||||
(libssh2_ed25519_ctx**)key_ctx);
|
||||
rc = gen_publickey_from_ed25519_openssh_priv_data(session,
|
||||
decrypted,
|
||||
method,
|
||||
method_len,
|
||||
pubkeydata,
|
||||
pubkeydata_len,
|
||||
(libssh2_ed25519_ctx**)key_ctx);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if LIBSSH2_RSA
|
||||
if(strcmp("ssh-rsa", (const char*)buf) == 0) {
|
||||
if(strcmp("ssh-rsa", (const char *)buf) == 0) {
|
||||
if(key_type == NULL || strcmp("ssh-rsa", key_type) == 0) {
|
||||
rc = gen_publickey_from_rsa_openssh_priv_data(session, decrypted,
|
||||
method, method_len,
|
||||
pubkeydata, pubkeydata_len,
|
||||
(libssh2_rsa_ctx**)key_ctx);
|
||||
method, method_len,
|
||||
pubkeydata,
|
||||
pubkeydata_len,
|
||||
(libssh2_rsa_ctx**)key_ctx);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if LIBSSH2_DSA
|
||||
if(strcmp("ssh-dss", (const char*)buf) == 0) {
|
||||
if(strcmp("ssh-dss", (const char *)buf) == 0) {
|
||||
if(key_type == NULL || strcmp("ssh-dss", key_type) == 0) {
|
||||
rc = gen_publickey_from_dsa_openssh_priv_data(session, decrypted,
|
||||
method, method_len,
|
||||
pubkeydata, pubkeydata_len,
|
||||
(libssh2_dsa_ctx**)key_ctx);
|
||||
pubkeydata,
|
||||
pubkeydata_len,
|
||||
(libssh2_dsa_ctx**)key_ctx);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -3019,12 +3096,14 @@ _libssh2_pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session,
|
||||
{
|
||||
libssh2_curve_type type;
|
||||
|
||||
if(_libssh2_ecdsa_curve_type_from_name((const char*)buf, &type) == 0) {
|
||||
if(_libssh2_ecdsa_curve_type_from_name((const char *)buf, &type) == 0) {
|
||||
if(key_type == NULL || strcmp("ssh-ecdsa", key_type) == 0) {
|
||||
rc = gen_publickey_from_ecdsa_openssh_priv_data(session, type, decrypted,
|
||||
rc = gen_publickey_from_ecdsa_openssh_priv_data(session, type,
|
||||
decrypted,
|
||||
method, method_len,
|
||||
pubkeydata, pubkeydata_len,
|
||||
(libssh2_ecdsa_ctx**)key_ctx);
|
||||
pubkeydata,
|
||||
pubkeydata_len,
|
||||
(libssh2_ecdsa_ctx**)key_ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3079,13 +3158,14 @@ _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session,
|
||||
|
||||
if(pk == NULL) {
|
||||
/* Try OpenSSH format */
|
||||
st = _libssh2_pub_priv_openssh_keyfilememory(session, NULL, NULL, method,
|
||||
st = _libssh2_pub_priv_openssh_keyfilememory(session, NULL, NULL,
|
||||
method,
|
||||
method_len,
|
||||
pubkeydata,
|
||||
pubkeydata_len,
|
||||
privatekeydata,
|
||||
privatekeydata_len,
|
||||
(unsigned const char*)passphrase);
|
||||
(unsigned const char *)passphrase);
|
||||
if(st != 0) {
|
||||
return _libssh2_error(session,
|
||||
LIBSSH2_ERROR_FILE,
|
||||
|
@ -178,7 +178,8 @@ int _libssh2_sha256_init(libssh2_sha256_ctx *ctx);
|
||||
EVP_MD_CTX_free(ctx); \
|
||||
} while(0)
|
||||
#else
|
||||
#define libssh2_sha256_update(ctx, data, len) EVP_DigestUpdate(&(ctx), data, len)
|
||||
#define libssh2_sha256_update(ctx, data, len) \
|
||||
EVP_DigestUpdate(&(ctx), data, len)
|
||||
#define libssh2_sha256_final(ctx, out) EVP_DigestFinal(&(ctx), out, NULL)
|
||||
#endif
|
||||
int _libssh2_sha256(const unsigned char *message, unsigned long len,
|
||||
@ -201,7 +202,8 @@ int _libssh2_sha384_init(libssh2_sha384_ctx *ctx);
|
||||
EVP_MD_CTX_free(ctx); \
|
||||
} while(0)
|
||||
#else
|
||||
#define libssh2_sha384_update(ctx, data, len) EVP_DigestUpdate(&(ctx), data, len)
|
||||
#define libssh2_sha384_update(ctx, data, len) \
|
||||
EVP_DigestUpdate(&(ctx), data, len)
|
||||
#define libssh2_sha384_final(ctx, out) EVP_DigestFinal(&(ctx), out, NULL)
|
||||
#endif
|
||||
int _libssh2_sha384(const unsigned char *message, unsigned long len,
|
||||
@ -224,7 +226,8 @@ int _libssh2_sha512_init(libssh2_sha512_ctx *ctx);
|
||||
EVP_MD_CTX_free(ctx); \
|
||||
} while(0)
|
||||
#else
|
||||
#define libssh2_sha512_update(ctx, data, len) EVP_DigestUpdate(&(ctx), data, len)
|
||||
#define libssh2_sha512_update(ctx, data, len) \
|
||||
EVP_DigestUpdate(&(ctx), data, len)
|
||||
#define libssh2_sha512_final(ctx, out) EVP_DigestFinal(&(ctx), out, NULL)
|
||||
#endif
|
||||
int _libssh2_sha512(const unsigned char *message, unsigned long len,
|
||||
@ -393,7 +396,8 @@ typedef struct {
|
||||
#define libssh2_dh_dtor(dhctx) _libssh2_dh_dtor(dhctx)
|
||||
extern void _libssh2_dh_init(_libssh2_dh_ctx *dhctx);
|
||||
extern int _libssh2_dh_key_pair(_libssh2_dh_ctx *dhctx, _libssh2_bn *public,
|
||||
_libssh2_bn *g, _libssh2_bn *p, int group_order,
|
||||
_libssh2_bn *g, _libssh2_bn *p,
|
||||
int group_order,
|
||||
_libssh2_bn_ctx *bnctx);
|
||||
extern int _libssh2_dh_secret(_libssh2_dh_ctx *dhctx, _libssh2_bn *secret,
|
||||
_libssh2_bn *f, _libssh2_bn *p,
|
||||
|
@ -924,7 +924,8 @@ _libssh2_os400qc3_crypto_dtor(_libssh2_os400qc3_crypto_ctx *x)
|
||||
if(!x)
|
||||
return;
|
||||
if(!null_token(x->hash.Alg_Context_Token)) {
|
||||
Qc3DestroyAlgorithmContext(x->hash.Alg_Context_Token, (char *) &ecnull);
|
||||
Qc3DestroyAlgorithmContext(x->hash.Alg_Context_Token,
|
||||
(char *) &ecnull);
|
||||
memset(x->hash.Alg_Context_Token, 0, sizeof x->hash.Alg_Context_Token);
|
||||
}
|
||||
if(!null_token(x->key.Key_Context_Token)) {
|
||||
@ -1315,7 +1316,8 @@ _libssh2_os400qc3_dh_secret(_libssh2_dh_ctx *dhctx, _libssh2_bn *secret,
|
||||
secretbuf = alloca(pubkeysize);
|
||||
set_EC_length(errcode, sizeof errcode);
|
||||
Qc3CalculateDHSecretKey(dhctx->token, pubkey, &pubkeysize,
|
||||
secretbuf, &secretbufsize, &secretbuflen, &errcode);
|
||||
secretbuf, &secretbufsize, &secretbuflen,
|
||||
&errcode);
|
||||
if(errcode.Bytes_Available)
|
||||
return -1;
|
||||
return _libssh2_bn_from_bin(secret,
|
||||
@ -1954,7 +1956,8 @@ pkcs1topkcs8(LIBSSH2_SESSION *session,
|
||||
}
|
||||
if(!pkcs8)
|
||||
return -1;
|
||||
data = (unsigned char *) LIBSSH2_ALLOC(session, pkcs8->end - pkcs8->header);
|
||||
data = (unsigned char *) LIBSSH2_ALLOC(session,
|
||||
pkcs8->end - pkcs8->header);
|
||||
if(!data) {
|
||||
asn1delete(pkcs8);
|
||||
return -1;
|
||||
@ -2111,7 +2114,8 @@ _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, LIBSSH2_SESSION *session,
|
||||
if(!ctx)
|
||||
return -1;
|
||||
ret = load_rsa_private_file(session, filename, passphrase,
|
||||
rsapkcs1privkey, rsapkcs8privkey, (void *) ctx);
|
||||
rsapkcs1privkey, rsapkcs8privkey,
|
||||
(void *) ctx);
|
||||
if(!ret) {
|
||||
/* Create the algorithm context. */
|
||||
algd.Public_Key_Alg = Qc3_RSA;
|
||||
|
@ -239,7 +239,7 @@ typedef struct { /* Diffie-Hellman context. */
|
||||
libssh2_os400qc3_hash_update(&(ctx), data, len)
|
||||
#define libssh2_sha256_final(ctx, out) \
|
||||
libssh2_os400qc3_hash_final(&(ctx), out)
|
||||
#define libssh2_sha256(message, len, out) \
|
||||
#define libssh2_sha256(message, len, out) \
|
||||
libssh2_os400qc3_hash(message, len, out, \
|
||||
Qc3_SHA256)
|
||||
#define libssh2_md5_init(x) libssh2_os400qc3_hash_init(x, Qc3_MD5)
|
||||
@ -250,29 +250,29 @@ typedef struct { /* Diffie-Hellman context. */
|
||||
#define libssh2_hmac_ctx_init(ctx) \
|
||||
memset((char *) &(ctx), 0, \
|
||||
sizeof(libssh2_hmac_ctx))
|
||||
#define libssh2_hmac_md5_init(ctx, key, keylen) \
|
||||
libssh2_os400qc3_hmac_init(ctx, Qc3_MD5, \
|
||||
MD5_DIGEST_LENGTH, \
|
||||
key, keylen)
|
||||
#define libssh2_hmac_sha1_init(ctx, key, keylen) \
|
||||
libssh2_os400qc3_hmac_init(ctx, Qc3_SHA1, \
|
||||
SHA_DIGEST_LENGTH, \
|
||||
key, keylen)
|
||||
#define libssh2_hmac_sha256_init(ctx, key, keylen) \
|
||||
libssh2_os400qc3_hmac_init(ctx, Qc3_SHA256, \
|
||||
SHA256_DIGEST_LENGTH, \
|
||||
key, keylen)
|
||||
#define libssh2_hmac_sha512_init(ctx, key, keylen) \
|
||||
libssh2_os400qc3_hmac_init(ctx, Qc3_SHA512, \
|
||||
SHA512_DIGEST_LENGTH, \
|
||||
key, keylen)
|
||||
#define libssh2_hmac_update(ctx, data, datalen) \
|
||||
libssh2_os400qc3_hmac_update(&(ctx), \
|
||||
data, datalen)
|
||||
#define libssh2_hmac_final(ctx, data) \
|
||||
libssh2_os400qc3_hmac_final(&(ctx), data)
|
||||
#define libssh2_hmac_cleanup(ctx) \
|
||||
_libssh2_os400qc3_crypto_dtor(ctx)
|
||||
#define libssh2_hmac_md5_init(ctx, key, keylen) \
|
||||
libssh2_os400qc3_hmac_init(ctx, Qc3_MD5, \
|
||||
MD5_DIGEST_LENGTH, \
|
||||
key, keylen)
|
||||
#define libssh2_hmac_sha1_init(ctx, key, keylen) \
|
||||
libssh2_os400qc3_hmac_init(ctx, Qc3_SHA1, \
|
||||
SHA_DIGEST_LENGTH, \
|
||||
key, keylen)
|
||||
#define libssh2_hmac_sha256_init(ctx, key, keylen) \
|
||||
libssh2_os400qc3_hmac_init(ctx, Qc3_SHA256, \
|
||||
SHA256_DIGEST_LENGTH, \
|
||||
key, keylen)
|
||||
#define libssh2_hmac_sha512_init(ctx, key, keylen) \
|
||||
libssh2_os400qc3_hmac_init(ctx, Qc3_SHA512, \
|
||||
SHA512_DIGEST_LENGTH, \
|
||||
key, keylen)
|
||||
#define libssh2_hmac_update(ctx, data, datalen) \
|
||||
libssh2_os400qc3_hmac_update(&(ctx), \
|
||||
data, datalen)
|
||||
#define libssh2_hmac_final(ctx, data) \
|
||||
libssh2_os400qc3_hmac_final(&(ctx), data)
|
||||
#define libssh2_hmac_cleanup(ctx) \
|
||||
_libssh2_os400qc3_crypto_dtor(ctx)
|
||||
|
||||
|
||||
#define _libssh2_bn_ctx int /* Not used. */
|
||||
|
16
src/packet.c
16
src/packet.c
@ -481,7 +481,8 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
|
||||
/* 9 = packet_type(1) + reason(4) + message_len(4) */
|
||||
message = (char *) data + 9;
|
||||
|
||||
language_len = _libssh2_ntohu32(data + 9 + message_len);
|
||||
language_len =
|
||||
_libssh2_ntohu32(data + 9 + message_len);
|
||||
language = (char *) data + 9 + message_len + 4;
|
||||
|
||||
if(language_len > (datalen-13-message_len)) {
|
||||
@ -543,7 +544,8 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
|
||||
if(message_len <= (datalen - 10)) {
|
||||
/* 6 = packet_type(1) + display(1) + message_len(4) */
|
||||
message = (char *) data + 6;
|
||||
language_len = _libssh2_ntohu32(data + 6 + message_len);
|
||||
language_len = _libssh2_ntohu32(data + 6 +
|
||||
message_len);
|
||||
|
||||
if(language_len <= (datalen - 10 - message_len))
|
||||
language = (char *) data + 10 + message_len;
|
||||
@ -665,7 +667,8 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
|
||||
|
||||
channelp->remote.window_size -= datalen - data_head;
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_CONN,
|
||||
"shrinking window size by %lu bytes to %lu, read_avail %lu",
|
||||
"shrinking window size by %lu bytes to %lu, "
|
||||
"read_avail %lu",
|
||||
datalen - data_head,
|
||||
channelp->remote.window_size,
|
||||
channelp->read_avail);
|
||||
@ -797,7 +800,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
|
||||
channelp =
|
||||
_libssh2_channel_locate(session, channel);
|
||||
|
||||
if (channelp && (sizeof("exit-status") + 13) <= datalen) {
|
||||
if(channelp && (sizeof("exit-status") + 13) <= datalen) {
|
||||
channelp->exit_status =
|
||||
_libssh2_ntohu32(data + 9 + sizeof("exit-status"));
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_CONN,
|
||||
@ -817,7 +820,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
|
||||
if(datalen >= 20)
|
||||
channelp = _libssh2_channel_locate(session, channel);
|
||||
|
||||
if (channelp && (sizeof("exit-signal") + 13) <= datalen) {
|
||||
if(channelp && (sizeof("exit-signal") + 13) <= datalen) {
|
||||
/* set signal name (without SIG prefix) */
|
||||
uint32_t namelen =
|
||||
_libssh2_ntohu32(data + 9 + sizeof("exit-signal"));
|
||||
@ -833,7 +836,8 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
|
||||
if(!channelp->exit_signal)
|
||||
rc = _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"memory for signal name");
|
||||
else if ((sizeof("exit-signal") + 13 + namelen <= datalen)) {
|
||||
else if((sizeof("exit-signal") + 13 + namelen <=
|
||||
datalen)) {
|
||||
memcpy(channelp->exit_signal,
|
||||
data + 13 + sizeof("exit-signal"), namelen);
|
||||
channelp->exit_signal[namelen] = '\0';
|
||||
|
104
src/pem.c
104
src/pem.c
@ -385,15 +385,15 @@ _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION * session,
|
||||
*decrypted_buf = NULL;
|
||||
|
||||
/* decode file */
|
||||
if(libssh2_base64_decode(session, (char**)&f, &f_len,
|
||||
if(libssh2_base64_decode(session, (char **)&f, &f_len,
|
||||
b64data, b64datalen)) {
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Parse the file */
|
||||
decoded.data = (unsigned char*)f;
|
||||
decoded.dataptr = (unsigned char*)f;
|
||||
decoded.data = (unsigned char *)f;
|
||||
decoded.dataptr = (unsigned char *)f;
|
||||
decoded.len = f_len;
|
||||
|
||||
if(decoded.len < strlen(AUTH_MAGIC)) {
|
||||
@ -401,7 +401,8 @@ _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION * session,
|
||||
goto out;
|
||||
}
|
||||
|
||||
if(strncmp((char*) decoded.dataptr, AUTH_MAGIC, strlen(AUTH_MAGIC)) != 0) {
|
||||
if(strncmp((char *) decoded.dataptr, AUTH_MAGIC,
|
||||
strlen(AUTH_MAGIC)) != 0) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"key auth magic mismatch");
|
||||
goto out;
|
||||
@ -409,48 +410,46 @@ _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION * session,
|
||||
|
||||
decoded.dataptr += strlen(AUTH_MAGIC) + 1;
|
||||
|
||||
if(_libssh2_get_c_string(&decoded, &ciphername) == 0)
|
||||
{
|
||||
if(_libssh2_get_c_string(&decoded, &ciphername) == 0) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"ciphername is missing");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if(_libssh2_get_c_string(&decoded, &kdfname) == 0)
|
||||
{
|
||||
if(_libssh2_get_c_string(&decoded, &kdfname) == 0) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"kdfname is missing");
|
||||
goto out;
|
||||
}
|
||||
|
||||
kdf_len = _libssh2_get_c_string(&decoded, &kdf);
|
||||
if(kdf == NULL )
|
||||
{
|
||||
if(kdf == NULL) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"kdf is missing");
|
||||
"kdf is missing");
|
||||
goto out;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
kdf_buf.data = kdf;
|
||||
kdf_buf.dataptr = kdf;
|
||||
kdf_buf.len = kdf_len;
|
||||
}
|
||||
|
||||
if((passphrase == NULL || strlen((const char*)passphrase) == 0) &&
|
||||
strcmp((const char*)ciphername, "none") != 0) {
|
||||
if((passphrase == NULL || strlen((const char *)passphrase) == 0) &&
|
||||
strcmp((const char *)ciphername, "none") != 0) {
|
||||
/* passphrase required */
|
||||
ret = LIBSSH2_ERROR_KEYFILE_AUTH_FAILED;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if(strcmp((const char*)kdfname, "none") != 0 && strcmp((const char*)kdfname, "bcrypt") != 0) {
|
||||
if(strcmp((const char *)kdfname, "none") != 0 &&
|
||||
strcmp((const char *)kdfname, "bcrypt") != 0) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"unknown cipher");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if(!strcmp((const char*)kdfname, "none") && strcmp((const char*)ciphername, "none") != 0) {
|
||||
if(!strcmp((const char *)kdfname, "none") &&
|
||||
strcmp((const char *)ciphername, "none") != 0) {
|
||||
ret =_libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"invalid format");
|
||||
goto out;
|
||||
@ -466,13 +465,13 @@ _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION * session,
|
||||
|
||||
if(_libssh2_get_c_string(&decoded, &buf) < 0) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Invalid private key; expect embedded public key");
|
||||
"Invalid private key; "
|
||||
"expect embedded public key");
|
||||
goto out;
|
||||
}
|
||||
|
||||
rc = _libssh2_get_c_string(&decoded, &buf);
|
||||
if(rc <= 0)
|
||||
{
|
||||
if(rc <= 0) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Private key data not found");
|
||||
goto out;
|
||||
@ -482,12 +481,11 @@ _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION * session,
|
||||
decrypted.data = decrypted.dataptr = buf;
|
||||
decrypted.len = rc;
|
||||
|
||||
if(ciphername && strcmp((const char*)ciphername, "none") != 0)
|
||||
{
|
||||
if(ciphername && strcmp((const char *)ciphername, "none") != 0) {
|
||||
const LIBSSH2_CRYPT_METHOD **all_methods, *cur_method;
|
||||
|
||||
all_methods = libssh2_crypt_methods();
|
||||
while ((cur_method = *all_methods++)) {
|
||||
while((cur_method = *all_methods++)) {
|
||||
if(*cur_method->name &&
|
||||
memcmp(ciphername, cur_method->name,
|
||||
strlen(cur_method->name)) == 0) {
|
||||
@ -513,23 +511,28 @@ _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION * session,
|
||||
ivlen = method->iv_len;
|
||||
total_len = keylen + ivlen;
|
||||
|
||||
if((key = LIBSSH2_CALLOC(session, total_len)) == NULL) {
|
||||
key = LIBSSH2_CALLOC(session, total_len);
|
||||
if(key == NULL) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Could not alloc key");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if(strcmp((const char*)kdfname, "bcrypt") == 0 && passphrase != NULL) {
|
||||
if(((salt_len = _libssh2_get_c_string(&kdf_buf, &salt)) <= 0) ||
|
||||
(_libssh2_get_u32(&kdf_buf, &rounds) != 0) ) {
|
||||
if(strcmp((const char *)kdfname, "bcrypt") == 0 &&
|
||||
passphrase != NULL) {
|
||||
salt_len = _libssh2_get_c_string(&kdf_buf, &salt);
|
||||
if((salt_len <= 0) ||
|
||||
(_libssh2_get_u32(&kdf_buf, &rounds) != 0) ) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"kdf contains unexpected values");
|
||||
LIBSSH2_FREE(session, key);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if(_libssh2_bcrypt_pbkdf((const char*)passphrase, strlen((const char*)passphrase),
|
||||
salt, salt_len, key, keylen + ivlen, rounds) < 0) {
|
||||
if(_libssh2_bcrypt_pbkdf((const char *)passphrase,
|
||||
strlen((const char *)passphrase),
|
||||
salt, salt_len, key,
|
||||
keylen + ivlen, rounds) < 0) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_DECRYPT,
|
||||
"invalid format");
|
||||
LIBSSH2_FREE(session, key);
|
||||
@ -546,13 +549,15 @@ _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION * session,
|
||||
/* Set up decryption */
|
||||
blocksize = method->blocksize;
|
||||
|
||||
if((key_part = LIBSSH2_CALLOC(session, keylen)) == NULL) {
|
||||
key_part = LIBSSH2_CALLOC(session, keylen);
|
||||
if(key_part == NULL) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Could not alloc key part");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if((iv_part = LIBSSH2_CALLOC(session, ivlen)) == NULL) {
|
||||
iv_part = LIBSSH2_CALLOC(session, ivlen);
|
||||
if(iv_part == NULL) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
|
||||
"Could not alloc iv part");
|
||||
goto out;
|
||||
@ -575,9 +580,10 @@ _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION * session,
|
||||
goto out;
|
||||
}
|
||||
|
||||
while ((size_t)len_decrypted <= decrypted.len - blocksize) {
|
||||
if (method->crypt(session, decrypted.data + len_decrypted, blocksize,
|
||||
&abstract)) {
|
||||
while((size_t)len_decrypted <= decrypted.len - blocksize) {
|
||||
if(method->crypt(session, decrypted.data + len_decrypted,
|
||||
blocksize,
|
||||
&abstract)) {
|
||||
ret = LIBSSH2_ERROR_DECRYPT;
|
||||
method->dtor(session, &abstract);
|
||||
goto out;
|
||||
@ -607,20 +613,22 @@ _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION * session,
|
||||
struct string_buf *out_buf = _libssh2_string_buf_new(session);
|
||||
if(!out_buf) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for decrypted struct");
|
||||
"Unable to allocate memory for "
|
||||
"decrypted struct");
|
||||
goto out;
|
||||
}
|
||||
|
||||
out_buf->data = LIBSSH2_CALLOC(session, decrypted.len);
|
||||
if(out_buf->data == NULL)
|
||||
{
|
||||
if(out_buf->data == NULL) {
|
||||
ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for decrypted struct");
|
||||
"Unable to allocate memory for "
|
||||
"decrypted struct");
|
||||
_libssh2_string_buf_free(session, out_buf);
|
||||
goto out;
|
||||
}
|
||||
memcpy(out_buf->data, decrypted.data, decrypted.len);
|
||||
out_buf->dataptr = out_buf->data + (decrypted.dataptr - decrypted.data);
|
||||
out_buf->dataptr = out_buf->data +
|
||||
(decrypted.dataptr - decrypted.data);
|
||||
out_buf->len = decrypted.len;
|
||||
|
||||
*decrypted_buf = out_buf;
|
||||
@ -638,8 +646,8 @@ out:
|
||||
LIBSSH2_FREE(session, key_part);
|
||||
}
|
||||
if(iv_part) {
|
||||
_libssh2_explicit_zero(iv_part, ivlen);
|
||||
LIBSSH2_FREE(session, iv_part);
|
||||
_libssh2_explicit_zero(iv_part, ivlen);
|
||||
LIBSSH2_FREE(session, iv_part);
|
||||
}
|
||||
if(f) {
|
||||
_libssh2_explicit_zero(f, f_len);
|
||||
@ -668,7 +676,7 @@ _libssh2_openssh_pem_parse(LIBSSH2_SESSION * session,
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
while (strcmp(line, OPENSSH_HEADER_BEGIN) != 0);
|
||||
while(strcmp(line, OPENSSH_HEADER_BEGIN) != 0);
|
||||
|
||||
if(readline(line, LINE_SIZE, fp)) {
|
||||
return -1;
|
||||
@ -696,7 +704,7 @@ _libssh2_openssh_pem_parse(LIBSSH2_SESSION * session,
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
} while (strcmp(line, OPENSSH_HEADER_END) != 0);
|
||||
} while(strcmp(line, OPENSSH_HEADER_END) != 0);
|
||||
|
||||
if(!b64data) {
|
||||
return -1;
|
||||
@ -704,7 +712,7 @@ _libssh2_openssh_pem_parse(LIBSSH2_SESSION * session,
|
||||
|
||||
ret = _libssh2_openssh_pem_parse_data(session,
|
||||
passphrase,
|
||||
(const char*)b64data,
|
||||
(const char *)b64data,
|
||||
(size_t)b64datalen,
|
||||
decrypted_buf);
|
||||
|
||||
@ -746,7 +754,7 @@ _libssh2_openssh_pem_parse_memory(LIBSSH2_SESSION * session,
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
while (strcmp(line, OPENSSH_HEADER_BEGIN) != 0);
|
||||
while(strcmp(line, OPENSSH_HEADER_BEGIN) != 0);
|
||||
|
||||
*line = '\0';
|
||||
|
||||
@ -757,7 +765,7 @@ _libssh2_openssh_pem_parse_memory(LIBSSH2_SESSION * session,
|
||||
|
||||
linelen = strlen(line);
|
||||
tmp = LIBSSH2_REALLOC(session, b64data, b64datalen + linelen);
|
||||
if (!tmp) {
|
||||
if(!tmp) {
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
@ -768,7 +776,7 @@ _libssh2_openssh_pem_parse_memory(LIBSSH2_SESSION * session,
|
||||
|
||||
*line = '\0';
|
||||
|
||||
if (off >= filedata_len) {
|
||||
if(off >= filedata_len) {
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
@ -777,7 +785,7 @@ _libssh2_openssh_pem_parse_memory(LIBSSH2_SESSION * session,
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
} while (strcmp(line, OPENSSH_HEADER_END) != 0);
|
||||
} while(strcmp(line, OPENSSH_HEADER_END) != 0);
|
||||
|
||||
if(!b64data) {
|
||||
return -1;
|
||||
|
111
src/publickey.c
111
src/publickey.c
@ -245,7 +245,7 @@ publickey_response_success(LIBSSH2_PUBLICKEY * pkey)
|
||||
"publickey subsystem");
|
||||
}
|
||||
|
||||
if (data_len < 4) {
|
||||
if(data_len < 4) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Publickey response too small");
|
||||
}
|
||||
@ -258,12 +258,12 @@ publickey_response_success(LIBSSH2_PUBLICKEY * pkey)
|
||||
/* Error, or processing complete */
|
||||
{
|
||||
unsigned long status = 0;
|
||||
|
||||
if (data_len < 8) {
|
||||
|
||||
if(data_len < 8) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Publickey response too small");
|
||||
}
|
||||
|
||||
|
||||
status = _libssh2_ntohu32(s);
|
||||
|
||||
LIBSSH2_FREE(session, data);
|
||||
@ -359,7 +359,7 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session)
|
||||
if(session->pkeyInit_state == libssh2_NB_state_sent1) {
|
||||
unsigned char *s;
|
||||
rc = _libssh2_channel_extended_data(session->pkeyInit_channel,
|
||||
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
|
||||
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block starting publickey subsystem");
|
||||
@ -445,7 +445,7 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session)
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (session->pkeyInit_data_len < 4) {
|
||||
if(session->pkeyInit_data_len < 4) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Public key init data too small");
|
||||
goto err_exit;
|
||||
@ -457,7 +457,7 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session)
|
||||
{
|
||||
unsigned long status, descr_len, lang_len;
|
||||
|
||||
if (session->pkeyInit_data_len >= 8) {
|
||||
if(session->pkeyInit_data_len >= 8) {
|
||||
status = _libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
descr_len = _libssh2_ntohu32(s);
|
||||
@ -469,12 +469,12 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session)
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (s + descr_len + 4 <=
|
||||
session->pkeyInit_data + session->pkeyInit_data_len) {
|
||||
/* description starts here */
|
||||
s += descr_len;
|
||||
lang_len = _libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
if(s + descr_len + 4 <=
|
||||
session->pkeyInit_data + session->pkeyInit_data_len) {
|
||||
/* description starts here */
|
||||
s += descr_len;
|
||||
lang_len = _libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
@ -482,10 +482,10 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session)
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (s + lang_len <=
|
||||
session->pkeyInit_data + session->pkeyInit_data_len) {
|
||||
/* lang starts here */
|
||||
s += lang_len;
|
||||
if(s + lang_len <=
|
||||
session->pkeyInit_data + session->pkeyInit_data_len) {
|
||||
/* lang starts here */
|
||||
s += lang_len;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
@ -512,7 +512,8 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session)
|
||||
if(session->pkeyInit_pkey->version >
|
||||
LIBSSH2_PUBLICKEY_VERSION) {
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_PUBLICKEY,
|
||||
"Truncate remote publickey version from %lu",
|
||||
"Truncate remote publickey version "
|
||||
"from %lu",
|
||||
session->pkeyInit_pkey->version);
|
||||
session->pkeyInit_pkey->version =
|
||||
LIBSSH2_PUBLICKEY_VERSION;
|
||||
@ -892,8 +893,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
{
|
||||
unsigned long status, descr_len, lang_len;
|
||||
|
||||
if (pkey->listFetch_s + 8 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + 8 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
status = _libssh2_ntohu32(pkey->listFetch_s);
|
||||
pkey->listFetch_s += 4;
|
||||
descr_len = _libssh2_ntohu32(pkey->listFetch_s);
|
||||
@ -905,8 +906,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + descr_len + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + descr_len + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
/* description starts at pkey->listFetch_s */
|
||||
pkey->listFetch_s += descr_len;
|
||||
lang_len = _libssh2_ntohu32(pkey->listFetch_s);
|
||||
@ -918,8 +919,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + lang_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + lang_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
/* lang starts at pkey->listFetch_s */
|
||||
pkey->listFetch_s += lang_len;
|
||||
}
|
||||
@ -969,8 +970,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
if(pkey->version == 1) {
|
||||
unsigned long comment_len;
|
||||
|
||||
if (pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
comment_len = _libssh2_ntohu32(pkey->listFetch_s);
|
||||
pkey->listFetch_s += 4;
|
||||
}
|
||||
@ -1004,7 +1005,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
list[keys].attrs = NULL;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + 4 <=
|
||||
if(pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].name_len = _libssh2_ntohu32(pkey->listFetch_s);
|
||||
pkey->listFetch_s += 4;
|
||||
@ -1015,8 +1016,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + list[keys].name_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + list[keys].name_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].name = pkey->listFetch_s;
|
||||
pkey->listFetch_s += list[keys].name_len;
|
||||
}
|
||||
@ -1026,8 +1027,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].blob_len = _libssh2_ntohu32(pkey->listFetch_s);
|
||||
pkey->listFetch_s += 4;
|
||||
}
|
||||
@ -1037,8 +1038,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + list[keys].blob_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + list[keys].blob_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].blob = pkey->listFetch_s;
|
||||
pkey->listFetch_s += list[keys].blob_len;
|
||||
}
|
||||
@ -1051,8 +1052,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
else {
|
||||
/* Version == 2 */
|
||||
|
||||
if (pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].name_len = _libssh2_ntohu32(pkey->listFetch_s);
|
||||
pkey->listFetch_s += 4;
|
||||
}
|
||||
@ -1062,8 +1063,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + list[keys].name_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + list[keys].name_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].name = pkey->listFetch_s;
|
||||
pkey->listFetch_s += list[keys].name_len;
|
||||
}
|
||||
@ -1073,8 +1074,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].blob_len = _libssh2_ntohu32(pkey->listFetch_s);
|
||||
pkey->listFetch_s += 4;
|
||||
}
|
||||
@ -1084,8 +1085,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + list[keys].blob_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + list[keys].blob_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].blob = pkey->listFetch_s;
|
||||
pkey->listFetch_s += list[keys].blob_len;
|
||||
}
|
||||
@ -1095,8 +1096,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].num_attrs = _libssh2_ntohu32(pkey->listFetch_s);
|
||||
pkey->listFetch_s += 4;
|
||||
}
|
||||
@ -1118,8 +1119,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
for(i = 0; i < list[keys].num_attrs; i++) {
|
||||
if (pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].attrs[i].name_len =
|
||||
_libssh2_ntohu32(pkey->listFetch_s);
|
||||
pkey->listFetch_s += 4;
|
||||
@ -1131,11 +1132,11 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + list[keys].attrs[i].name_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].attrs[i].name =
|
||||
(char *) pkey->listFetch_s;
|
||||
pkey->listFetch_s += list[keys].attrs[i].name_len;
|
||||
if(pkey->listFetch_s + list[keys].attrs[i].name_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].attrs[i].name =
|
||||
(char *) pkey->listFetch_s;
|
||||
pkey->listFetch_s += list[keys].attrs[i].name_len;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session,
|
||||
@ -1144,8 +1145,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s + 4 <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].attrs[i].value_len =
|
||||
_libssh2_ntohu32(pkey->listFetch_s);
|
||||
pkey->listFetch_s += 4;
|
||||
@ -1157,9 +1158,9 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
if (pkey->listFetch_s +
|
||||
list[keys].attrs[i].value_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
if(pkey->listFetch_s +
|
||||
list[keys].attrs[i].value_len <=
|
||||
pkey->listFetch_data + pkey->listFetch_data_len) {
|
||||
list[keys].attrs[i].value =
|
||||
(char *) pkey->listFetch_s;
|
||||
pkey->listFetch_s += list[keys].attrs[i].value_len;
|
||||
|
43
src/scp.c
43
src/scp.c
@ -341,7 +341,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
/* Request SCP for the desired file */
|
||||
rc = _libssh2_channel_process_startup(session->scpRecv_channel, "exec",
|
||||
sizeof("exec") - 1,
|
||||
(char *) session->scpRecv_command,
|
||||
(char *)session->scpRecv_command,
|
||||
session->scpRecv_command_len);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
@ -475,7 +475,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
LIBSSH2_SCP_RESPONSE_BUFLEN) {
|
||||
/* You had your chance */
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
|
||||
"Unterminated response from SCP server");
|
||||
"Unterminated response from "
|
||||
"SCP server");
|
||||
goto scp_recv_error;
|
||||
}
|
||||
/* Way too short to be an SCP response, or not done yet,
|
||||
@ -522,7 +523,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
if(!s || ((s - p) <= 0)) {
|
||||
/* No spaces or space in the wrong spot */
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
|
||||
"Invalid response from SCP server, malformed mtime.usec");
|
||||
"Invalid response from SCP server, "
|
||||
"malformed mtime.usec");
|
||||
goto scp_recv_error;
|
||||
}
|
||||
|
||||
@ -532,7 +534,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
if(!p || ((p - s) <= 0)) {
|
||||
/* No spaces or space in the wrong spot */
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
|
||||
"Invalid response from SCP server, too short or malformed");
|
||||
"Invalid response from SCP server, "
|
||||
"too short or malformed");
|
||||
goto scp_recv_error;
|
||||
}
|
||||
|
||||
@ -632,7 +635,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
LIBSSH2_SCP_RESPONSE_BUFLEN) {
|
||||
/* You had your chance */
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
|
||||
"Unterminated response from SCP server");
|
||||
"Unterminated response "
|
||||
"from SCP server");
|
||||
goto scp_recv_error;
|
||||
}
|
||||
/* Way too short to be an SCP response, or not done yet,
|
||||
@ -656,7 +660,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
if(session->scpRecv_response_len < 6) {
|
||||
/* EOL came too soon */
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
|
||||
"Invalid response from SCP server, too short");
|
||||
"Invalid response from SCP server, "
|
||||
"too short");
|
||||
goto scp_recv_error;
|
||||
}
|
||||
|
||||
@ -666,7 +671,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
if(!p || ((p - s) <= 0)) {
|
||||
/* No spaces or space in the wrong spot */
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
|
||||
"Invalid response from SCP server, malformed mode");
|
||||
"Invalid response from SCP server, "
|
||||
"malformed mode");
|
||||
goto scp_recv_error;
|
||||
}
|
||||
|
||||
@ -676,7 +682,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
session->scpRecv_mode = strtol(s, &e, 8);
|
||||
if(e && *e) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
|
||||
"Invalid response from SCP server, invalid mode");
|
||||
"Invalid response from SCP server, "
|
||||
"invalid mode");
|
||||
goto scp_recv_error;
|
||||
}
|
||||
|
||||
@ -684,7 +691,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
if(!s || ((s - p) <= 0)) {
|
||||
/* No spaces or space in the wrong spot */
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
|
||||
"Invalid response from SCP server, too short or malformed");
|
||||
"Invalid response from SCP server, "
|
||||
"too short or malformed");
|
||||
goto scp_recv_error;
|
||||
}
|
||||
|
||||
@ -693,7 +701,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
session->scpRecv_size = scpsize_strtol(p, &e, 10);
|
||||
if(e && *e) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
|
||||
"Invalid response from SCP server, invalid size");
|
||||
"Invalid response from SCP server, "
|
||||
"invalid size");
|
||||
goto scp_recv_error;
|
||||
}
|
||||
|
||||
@ -765,9 +774,9 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
|
||||
*
|
||||
* DEPRECATED
|
||||
*
|
||||
* Open a channel and request a remote file via SCP. This receives files larger
|
||||
* than 2 GB, but is unable to report the proper size on platforms where the
|
||||
* st_size member of struct stat is limited to 2 GB (e.g. windows).
|
||||
* Open a channel and request a remote file via SCP. This receives files
|
||||
* larger than 2 GB, but is unable to report the proper size on platforms
|
||||
* where the st_size member of struct stat is limited to 2 GB (e.g. windows).
|
||||
*
|
||||
*/
|
||||
LIBSSH2_API LIBSSH2_CHANNEL *
|
||||
@ -775,7 +784,8 @@ libssh2_scp_recv(LIBSSH2_SESSION *session, const char *path, struct stat * sb)
|
||||
{
|
||||
LIBSSH2_CHANNEL *ptr;
|
||||
|
||||
/* scp_recv uses libssh2_struct_stat, so pass one if the caller gave us a struct to populate... */
|
||||
/* scp_recv uses libssh2_struct_stat, so pass one if the caller gave us a
|
||||
struct to populate... */
|
||||
libssh2_struct_stat sb_intl;
|
||||
libssh2_struct_stat *sb_ptr;
|
||||
memset(&sb_intl, 0, sizeof(sb_intl));
|
||||
@ -804,7 +814,8 @@ libssh2_scp_recv(LIBSSH2_SESSION *session, const char *path, struct stat * sb)
|
||||
*
|
||||
*/
|
||||
LIBSSH2_API LIBSSH2_CHANNEL *
|
||||
libssh2_scp_recv2(LIBSSH2_SESSION *session, const char *path, libssh2_struct_stat * sb)
|
||||
libssh2_scp_recv2(LIBSSH2_SESSION *session, const char *path,
|
||||
libssh2_struct_stat *sb)
|
||||
{
|
||||
LIBSSH2_CHANNEL *ptr;
|
||||
BLOCK_ADJUST_ERRNO(ptr, session, scp_recv(session, path, sb));
|
||||
@ -887,7 +898,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
|
||||
/* Request SCP for the desired file */
|
||||
rc = _libssh2_channel_process_startup(session->scpSend_channel, "exec",
|
||||
sizeof("exec") - 1,
|
||||
(char *) session->scpSend_command,
|
||||
(char *)session->scpSend_command,
|
||||
session->scpSend_command_len);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
|
@ -557,7 +557,8 @@ libssh2_session_callback_set(LIBSSH2_SESSION * session,
|
||||
session->recv = callback;
|
||||
return oldcb;
|
||||
}
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_TRANS, "Setting Callback %d", cbtype);
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_TRANS, "Setting Callback %d",
|
||||
cbtype);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@ -858,7 +859,8 @@ session_free(LIBSSH2_SESSION *session)
|
||||
int packets_left = 0;
|
||||
|
||||
if(session->free_state == libssh2_NB_state_idle) {
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_TRANS, "Freeing session resource",
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_TRANS,
|
||||
"Freeing session resource",
|
||||
session->remote.banner);
|
||||
|
||||
session->free_state = libssh2_NB_state_created;
|
||||
@ -1081,7 +1083,8 @@ session_free(LIBSSH2_SESSION *session)
|
||||
}
|
||||
|
||||
/* error string */
|
||||
if(session->err_msg && ((session->err_flags & LIBSSH2_ERR_FLAG_DUP) != 0)) {
|
||||
if(session->err_msg &&
|
||||
((session->err_flags & LIBSSH2_ERR_FLAG_DUP) != 0)) {
|
||||
LIBSSH2_FREE(session, (char *)session->err_msg);
|
||||
}
|
||||
|
||||
@ -1433,7 +1436,7 @@ libssh2_poll_channel_read(LIBSSH2_CHANNEL *channel, int extended)
|
||||
packet = _libssh2_list_first(&session->packets);
|
||||
|
||||
while(packet) {
|
||||
if (packet->data_len < 5) {
|
||||
if(packet->data_len < 5) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Packet too small");
|
||||
}
|
||||
@ -1692,7 +1695,8 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
|
||||
switch(fds[i].type) {
|
||||
case LIBSSH2_POLLFD_SOCKET:
|
||||
fds[i].revents = sockets[i].revents;
|
||||
sockets[i].revents = 0; /* In case we loop again, be nice */
|
||||
sockets[i].revents = 0; /* In case we loop again, be
|
||||
nice */
|
||||
if(fds[i].revents) {
|
||||
active_fds++;
|
||||
}
|
||||
@ -1700,8 +1704,9 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
|
||||
case LIBSSH2_POLLFD_CHANNEL:
|
||||
if(sockets[i].events & POLLIN) {
|
||||
/* Spin session until no data available */
|
||||
while(_libssh2_transport_read(fds[i].fd.channel->session)
|
||||
> 0);
|
||||
while(_libssh2_transport_read(fds[i].fd.
|
||||
channel->session)
|
||||
> 0);
|
||||
}
|
||||
if(sockets[i].revents & POLLHUP) {
|
||||
fds[i].revents |=
|
||||
@ -1713,8 +1718,9 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
|
||||
case LIBSSH2_POLLFD_LISTENER:
|
||||
if(sockets[i].events & POLLIN) {
|
||||
/* Spin session until no data available */
|
||||
while(_libssh2_transport_read(fds[i].fd.listener->session)
|
||||
> 0);
|
||||
while(_libssh2_transport_read(fds[i].fd.
|
||||
listener->session)
|
||||
> 0);
|
||||
}
|
||||
if(sockets[i].revents & POLLHUP) {
|
||||
fds[i].revents |=
|
||||
@ -1764,10 +1770,12 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
|
||||
break;
|
||||
|
||||
case LIBSSH2_POLLFD_CHANNEL:
|
||||
if(FD_ISSET(fds[i].fd.channel->session->socket_fd, &rfds)) {
|
||||
if(FD_ISSET(fds[i].fd.channel->session->socket_fd,
|
||||
&rfds)) {
|
||||
/* Spin session until no data available */
|
||||
while(_libssh2_transport_read(fds[i].fd.channel->session)
|
||||
> 0);
|
||||
while(_libssh2_transport_read(fds[i].fd.
|
||||
channel->session)
|
||||
> 0);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -1775,8 +1783,9 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
|
||||
if(FD_ISSET
|
||||
(fds[i].fd.listener->session->socket_fd, &rfds)) {
|
||||
/* Spin session until no data available */
|
||||
while(_libssh2_transport_read(fds[i].fd.listener->session)
|
||||
> 0);
|
||||
while(_libssh2_transport_read(fds[i].fd.
|
||||
listener->session)
|
||||
> 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
186
src/sftp.c
186
src/sftp.c
@ -161,7 +161,8 @@ remove_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id)
|
||||
request_id);
|
||||
if(zombie) {
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
||||
"Removing request ID %ld from the list of zombie requests",
|
||||
"Removing request ID %ld from the list of "
|
||||
"zombie requests",
|
||||
request_id);
|
||||
|
||||
_libssh2_list_remove(&zombie->node);
|
||||
@ -204,7 +205,7 @@ sftp_packet_add(LIBSSH2_SFTP *sftp, unsigned char *data,
|
||||
LIBSSH2_SFTP_PACKET *packet;
|
||||
uint32_t request_id;
|
||||
|
||||
if (data_len < 5) {
|
||||
if(data_len < 5) {
|
||||
return LIBSSH2_ERROR_OUT_OF_BOUNDARY;
|
||||
}
|
||||
|
||||
@ -349,7 +350,7 @@ sftp_packet_read(LIBSSH2_SFTP *sftp)
|
||||
return _libssh2_error(session,
|
||||
LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED,
|
||||
"SFTP packet too large");
|
||||
if (sftp->partial_len == 0)
|
||||
if(sftp->partial_len == 0)
|
||||
return _libssh2_error(session,
|
||||
LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate empty SFTP packet");
|
||||
@ -372,7 +373,8 @@ sftp_packet_read(LIBSSH2_SFTP *sftp)
|
||||
if(sftp->partial_len > recv_window) {
|
||||
/* ask for twice the data amount we need at once */
|
||||
rc = _libssh2_channel_receive_window_adjust(channel,
|
||||
sftp->partial_len*2,
|
||||
sftp->partial_len
|
||||
* 2,
|
||||
1, NULL);
|
||||
/* store the state so that we continue with the correct
|
||||
operation at next invoke */
|
||||
@ -517,7 +519,7 @@ sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type,
|
||||
LIBSSH2_SESSION *session = sftp->channel->session;
|
||||
int rc;
|
||||
|
||||
if (data == NULL || data_len == NULL || required_size == 0) {
|
||||
if(data == NULL || data_len == NULL || required_size == 0) {
|
||||
return LIBSSH2_ERROR_BAD_USE;
|
||||
}
|
||||
|
||||
@ -571,7 +573,7 @@ sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses,
|
||||
int i;
|
||||
int rc;
|
||||
|
||||
if (data == NULL || data_len == NULL || required_size == 0) {
|
||||
if(data == NULL || data_len == NULL || required_size == 0) {
|
||||
return LIBSSH2_ERROR_BAD_USE;
|
||||
}
|
||||
|
||||
@ -605,7 +607,8 @@ sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses,
|
||||
else if(rc <= 0) {
|
||||
/* prevent busy-looping */
|
||||
long left =
|
||||
LIBSSH2_READ_TIMEOUT - (long)(time(NULL) - sftp->requirev_start);
|
||||
LIBSSH2_READ_TIMEOUT -
|
||||
(long)(time(NULL) - sftp->requirev_start);
|
||||
|
||||
if(left <= 0) {
|
||||
sftp->requirev_start = 0;
|
||||
@ -668,7 +671,8 @@ sftp_attr2bin(unsigned char *p, const LIBSSH2_SFTP_ATTRIBUTES * attrs)
|
||||
/* sftp_bin2attr
|
||||
*/
|
||||
static int
|
||||
sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES * attrs, const unsigned char *p, size_t data_len)
|
||||
sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES * attrs, const unsigned char *p,
|
||||
size_t data_len)
|
||||
{
|
||||
struct string_buf buf;
|
||||
buf.data = (unsigned char *)p;
|
||||
@ -676,32 +680,32 @@ sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES * attrs, const unsigned char *p, size_t da
|
||||
buf.len = data_len;
|
||||
buf.offset = 0;
|
||||
|
||||
if (_libssh2_get_u32(&buf, &(attrs->flags)) != 0) {
|
||||
if(_libssh2_get_u32(&buf, &(attrs->flags)) != 0) {
|
||||
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
if (attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) {
|
||||
if (_libssh2_get_u64(&buf, &(attrs->filesize)) != 0) {
|
||||
if(attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) {
|
||||
if(_libssh2_get_u64(&buf, &(attrs->filesize)) != 0) {
|
||||
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
}
|
||||
|
||||
if (attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) {
|
||||
if (_libssh2_get_u32(&buf, &(attrs->uid)) != 0 ||
|
||||
_libssh2_get_u32(&buf, &(attrs->gid)) != 0) {
|
||||
if(attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) {
|
||||
if(_libssh2_get_u32(&buf, &(attrs->uid)) != 0 ||
|
||||
_libssh2_get_u32(&buf, &(attrs->gid)) != 0) {
|
||||
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
}
|
||||
|
||||
if (attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
|
||||
if (_libssh2_get_u32(&buf, &(attrs->permissions)) != 0) {
|
||||
if(attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
|
||||
if(_libssh2_get_u32(&buf, &(attrs->permissions)) != 0) {
|
||||
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
}
|
||||
|
||||
if (attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
|
||||
if (_libssh2_get_u32(&buf, &(attrs->atime)) != 0 ||
|
||||
_libssh2_get_u32(&buf, &(attrs->mtime)) != 0) {
|
||||
if(attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
|
||||
if(_libssh2_get_u32(&buf, &(attrs->atime)) != 0 ||
|
||||
_libssh2_get_u32(&buf, &(attrs->mtime)) != 0) {
|
||||
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
}
|
||||
@ -798,7 +802,8 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
|
||||
if(session->sftpInit_state == libssh2_NB_state_sent) {
|
||||
int ret = _libssh2_channel_process_startup(session->sftpInit_channel,
|
||||
"subsystem",
|
||||
sizeof("subsystem") - 1, "sftp",
|
||||
sizeof("subsystem") - 1,
|
||||
"sftp",
|
||||
strlen("sftp"));
|
||||
if(ret == LIBSSH2_ERROR_EAGAIN) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
@ -816,7 +821,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
|
||||
|
||||
if(session->sftpInit_state == libssh2_NB_state_sent1) {
|
||||
rc = _libssh2_channel_extended_data(session->sftpInit_channel,
|
||||
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
|
||||
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block requesting handle extended data");
|
||||
@ -840,7 +845,8 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
|
||||
session->sftpInit_sent = 0; /* nothing's sent yet */
|
||||
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
||||
"Sending FXP_INIT packet advertising version %d support",
|
||||
"Sending FXP_INIT packet advertising "
|
||||
"version %d support",
|
||||
(int) LIBSSH2_SFTP_VERSION);
|
||||
|
||||
session->sftpInit_state = libssh2_NB_state_sent2;
|
||||
@ -881,8 +887,8 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
|
||||
"Would block receiving SSH_FXP_VERSION");
|
||||
return NULL;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -899,8 +905,8 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
|
||||
buf.dataptr = buf.data + 1;
|
||||
buf.len = data_len;
|
||||
buf.offset = 1;
|
||||
|
||||
if (_libssh2_get_u32(&buf, &(sftp_handle->version)) != 0) {
|
||||
|
||||
if(_libssh2_get_u32(&buf, &(sftp_handle->version)) != 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
rc = LIBSSH2_ERROR_BUFFER_TOO_SMALL;
|
||||
goto sftp_init_error;
|
||||
@ -915,17 +921,17 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
||||
"Enabling SFTP version %lu compatibility",
|
||||
sftp_handle->version);
|
||||
while (buf.offset < buf.len) {
|
||||
while(buf.offset < buf.len) {
|
||||
unsigned char *extname, *extdata;
|
||||
|
||||
if (_libssh2_get_c_string(&buf, &extname) < 0) {
|
||||
if(_libssh2_get_c_string(&buf, &extname) < 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short when extracting extname");
|
||||
goto sftp_init_error;
|
||||
}
|
||||
|
||||
if (_libssh2_get_c_string(&buf, &extdata) < 0) {
|
||||
if(_libssh2_get_c_string(&buf, &extdata) < 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short when extracting extdata");
|
||||
@ -1097,7 +1103,8 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
|
||||
/* packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) +
|
||||
flags(4) */
|
||||
sftp->open_packet_len = filename_len + 13 +
|
||||
(open_file? (4 + sftp_attrsize(LIBSSH2_SFTP_ATTR_PERMISSIONS)) : 0);
|
||||
(open_file? (4 +
|
||||
sftp_attrsize(LIBSSH2_SFTP_ATTR_PERMISSIONS)) : 0);
|
||||
|
||||
/* surprise! this starts out with nothing sent */
|
||||
sftp->open_packet_sent = 0;
|
||||
@ -1137,7 +1144,8 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
|
||||
sftp->open_packet_sent);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block sending FXP_OPEN or FXP_OPENDIR command");
|
||||
"Would block sending FXP_OPEN or "
|
||||
"FXP_OPENDIR command");
|
||||
return NULL;
|
||||
}
|
||||
else if(rc < 0) {
|
||||
@ -1173,8 +1181,8 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
|
||||
"Would block waiting for status message");
|
||||
return NULL;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -1204,7 +1212,8 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
|
||||
sftp->last_errno = _libssh2_ntohu32(data + 5);
|
||||
|
||||
if(LIBSSH2_FX_OK == sftp->last_errno) {
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "got HANDLE FXOK!");
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
||||
"got HANDLE FXOK!");
|
||||
|
||||
LIBSSH2_FREE(session, data);
|
||||
|
||||
@ -1217,8 +1226,8 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
|
||||
sftp->open_state = libssh2_NB_state_sent;
|
||||
return NULL;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -1233,7 +1242,8 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
|
||||
if(badness) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
"Failed opening remote file");
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "got FXP_STATUS %d",
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
||||
"got FXP_STATUS %d",
|
||||
sftp->last_errno);
|
||||
LIBSSH2_FREE(session, data);
|
||||
return NULL;
|
||||
@ -1562,8 +1572,8 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
|
||||
return bytes_in_buffer;
|
||||
}
|
||||
|
||||
if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -1765,7 +1775,8 @@ static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(buffer_maxlen >= filename_len && names_packet_len >= filename_len) {
|
||||
if(buffer_maxlen >= filename_len && names_packet_len >=
|
||||
filename_len) {
|
||||
memcpy(buffer, s, filename_len);
|
||||
buffer[filename_len] = '\0'; /* zero terminate */
|
||||
s += real_filename_len;
|
||||
@ -1789,7 +1800,8 @@ static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
|
||||
if(longentry && (longentry_maxlen>1)) {
|
||||
longentry_len = real_longentry_len;
|
||||
|
||||
if(longentry_len >= longentry_maxlen || longentry_len > names_packet_len) {
|
||||
if(longentry_len >= longentry_maxlen ||
|
||||
longentry_len > names_packet_len) {
|
||||
filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
|
||||
goto end;
|
||||
}
|
||||
@ -1810,9 +1822,10 @@ static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
|
||||
if(attrs)
|
||||
memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
|
||||
|
||||
attr_len = sftp_bin2attr(attrs ? attrs : &attrs_dummy, s, names_packet_len);
|
||||
attr_len = sftp_bin2attr(attrs ? attrs : &attrs_dummy, s,
|
||||
names_packet_len);
|
||||
|
||||
if (attr_len >= 0) {
|
||||
if(attr_len >= 0) {
|
||||
s += attr_len;
|
||||
names_packet_len -= attr_len;
|
||||
}
|
||||
@ -1878,12 +1891,12 @@ static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
|
||||
&data_len, 9);
|
||||
if(retcode == LIBSSH2_ERROR_EAGAIN)
|
||||
return retcode;
|
||||
else if (retcode == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(retcode == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
"Status message too short");
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
"Status message too short");
|
||||
}
|
||||
else if(retcode) {
|
||||
sftp->readdir_state = libssh2_NB_state_idle;
|
||||
@ -2010,7 +2023,8 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
|
||||
acked but we haven't been able to return as such yet, so we will
|
||||
get that data as well passed in here again.
|
||||
*/
|
||||
already = (size_t) (handle->u.file.offset_sent - handle->u.file.offset)+
|
||||
already = (size_t) (handle->u.file.offset_sent -
|
||||
handle->u.file.offset)+
|
||||
handle->u.file.acked;
|
||||
|
||||
if(count >= already) {
|
||||
@ -2063,8 +2077,8 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
|
||||
to create more packets */
|
||||
}
|
||||
|
||||
/* move through the WRITE packets that haven't been sent and send as many
|
||||
as possible - remember that we don't block */
|
||||
/* move through the WRITE packets that haven't been sent and send as
|
||||
many as possible - remember that we don't block */
|
||||
chunk = _libssh2_list_first(&handle->packet_list);
|
||||
|
||||
while(chunk) {
|
||||
@ -2112,8 +2126,8 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
|
||||
/* we check the packets in order */
|
||||
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
|
||||
chunk->request_id, &data, &data_len, 9);
|
||||
if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -2149,7 +2163,8 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
|
||||
|
||||
/* since we return error now, the application will not get any
|
||||
outstanding data acked, so we need to rewind the offset to
|
||||
where the application knows it has reached with acked data */
|
||||
where the application knows it has reached with acked
|
||||
data */
|
||||
handle->u.file.offset -= handle->u.file.acked;
|
||||
|
||||
/* then reset the offset_sent to be the same as the offset */
|
||||
@ -2159,8 +2174,8 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
|
||||
ack after an error */
|
||||
handle->u.file.acked = 0;
|
||||
|
||||
/* the server returned an error for that written chunk, propagate
|
||||
this back to our parent function */
|
||||
/* the server returned an error for that written chunk,
|
||||
propagate this back to our parent function */
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
"FXP write failed");
|
||||
}
|
||||
@ -2266,8 +2281,8 @@ static int sftp_fsync(LIBSSH2_SFTP_HANDLE *handle)
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return rc;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -2376,8 +2391,8 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
|
||||
&data_len, 9);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -2406,7 +2421,7 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
|
||||
}
|
||||
}
|
||||
|
||||
if (sftp_bin2attr(attrs, data + 5, data_len - 5) < 0) {
|
||||
if(sftp_bin2attr(attrs, data + 5, data_len - 5) < 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
"Attributes too short in SFTP fstat");
|
||||
@ -2595,8 +2610,8 @@ sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return rc;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
data = NULL;
|
||||
@ -2632,11 +2647,11 @@ sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
|
||||
/* remove this handle from the parent's list */
|
||||
_libssh2_list_remove(&handle->node);
|
||||
|
||||
if (handle->handle_type == LIBSSH2_SFTP_HANDLE_DIR) {
|
||||
if (handle->u.dir.names_left)
|
||||
if(handle->handle_type == LIBSSH2_SFTP_HANDLE_DIR) {
|
||||
if(handle->u.dir.names_left)
|
||||
LIBSSH2_FREE(session, handle->u.dir.names_packet);
|
||||
}
|
||||
else if (handle->handle_type == LIBSSH2_SFTP_HANDLE_FILE) {
|
||||
else if(handle->handle_type == LIBSSH2_SFTP_HANDLE_FILE) {
|
||||
if(handle->u.file.data)
|
||||
LIBSSH2_FREE(session, handle->u.file.data);
|
||||
}
|
||||
@ -2723,8 +2738,8 @@ static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename,
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return rc;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -2843,8 +2858,8 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return rc;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -2977,8 +2992,8 @@ static int sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st)
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return rc;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -3039,7 +3054,8 @@ libssh2_sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st)
|
||||
int rc;
|
||||
if(!handle || !st)
|
||||
return LIBSSH2_ERROR_BAD_USE;
|
||||
BLOCK_ADJUST(rc, handle->sftp->channel->session, sftp_fstatvfs(handle, st));
|
||||
BLOCK_ADJUST(rc, handle->sftp->channel->session,
|
||||
sftp_fstatvfs(handle, st));
|
||||
return rc;
|
||||
}
|
||||
|
||||
@ -3111,8 +3127,8 @@ static int sftp_statvfs(LIBSSH2_SFTP *sftp, const char *path,
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return rc;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -3254,8 +3270,8 @@ static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return rc;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -3358,8 +3374,8 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return rc;
|
||||
}
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -3480,8 +3496,8 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
|
||||
sftp->stat_request_id, &data, &data_len, 9);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
else if (rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -3512,7 +3528,7 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
|
||||
}
|
||||
|
||||
memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
|
||||
if (sftp_bin2attr(attrs, data + 5, data_len - 5) < 0) {
|
||||
if(sftp_bin2attr(attrs, data + 5, data_len - 5) < 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
"Attributes too short in SFTP fstat");
|
||||
@ -3625,8 +3641,8 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
|
||||
&data_len, 9);
|
||||
if(retcode == LIBSSH2_ERROR_EAGAIN)
|
||||
return retcode;
|
||||
else if (retcode == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if (data_len > 0) {
|
||||
else if(retcode == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
@ -3659,8 +3675,8 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
|
||||
"no name entries");
|
||||
}
|
||||
|
||||
if (data_len < 13) {
|
||||
if (data_len > 0) {
|
||||
if(data_len < 13) {
|
||||
if(data_len > 0) {
|
||||
LIBSSH2_FREE(session, data);
|
||||
}
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
||||
|
@ -445,7 +445,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
|
||||
}
|
||||
|
||||
p->padding_length = block[4];
|
||||
if ( p->padding_length > p->packet_length - 1 ) {
|
||||
if(p->padding_length > p->packet_length - 1) {
|
||||
return LIBSSH2_ERROR_DECRYPT;
|
||||
}
|
||||
|
||||
@ -482,10 +482,11 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
|
||||
/* copy the data from index 5 to the end of
|
||||
the blocksize from the temporary buffer to
|
||||
the start of the decrypted buffer */
|
||||
if (blocksize - 5 <= (int) total_num) {
|
||||
if(blocksize - 5 <= (int) total_num) {
|
||||
memcpy(p->wptr, &block[5], blocksize - 5);
|
||||
p->wptr += blocksize - 5; /* advance write pointer */
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return LIBSSH2_ERROR_OUT_OF_BOUNDARY;
|
||||
}
|
||||
}
|
||||
@ -563,8 +564,8 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
|
||||
/* if there are bytes to copy that aren't decrypted, simply
|
||||
copy them as-is to the target buffer */
|
||||
if(numbytes > 0) {
|
||||
|
||||
if (numbytes <= (int)(total_num - (p->wptr - p->payload))) {
|
||||
|
||||
if(numbytes <= (int)(total_num - (p->wptr - p->payload))) {
|
||||
memcpy(p->wptr, &p->buf[p->readidx], numbytes);
|
||||
}
|
||||
else {
|
||||
@ -591,12 +592,13 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
|
||||
|
||||
if(session->packAdd_state != libssh2_NB_state_idle) {
|
||||
/* fullpacket only returns LIBSSH2_ERROR_EAGAIN if
|
||||
* libssh2_packet_add returns LIBSSH2_ERROR_EAGAIN. If that
|
||||
* returns LIBSSH2_ERROR_EAGAIN but the packAdd_state is idle,
|
||||
* then the packet has been added to the brigade, but some
|
||||
* immediate action that was taken based on the packet
|
||||
* type (such as key re-exchange) is not yet complete.
|
||||
* Clear the way for a new packet to be read in.
|
||||
* libssh2_packet_add returns LIBSSH2_ERROR_EAGAIN. If
|
||||
* that returns LIBSSH2_ERROR_EAGAIN but the packAdd_state
|
||||
* is idle, then the packet has been added to the brigade,
|
||||
* but some immediate action that was taken based on the
|
||||
* packet type (such as key re-exchange) is not yet
|
||||
* complete. Clear the way for a new packet to be read
|
||||
* in.
|
||||
*/
|
||||
session->readPack_encrypted = encrypted;
|
||||
session->readPack_state = libssh2_NB_state_jump1;
|
||||
@ -783,7 +785,8 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session,
|
||||
dest2_len -= dest_len;
|
||||
|
||||
rc = session->local.comp->comp(session,
|
||||
&p->outbuf[5 + dest_len], &dest2_len,
|
||||
&p->outbuf[5 + dest_len],
|
||||
&dest2_len,
|
||||
data2, data2_len,
|
||||
&session->local.comp_abstract);
|
||||
}
|
||||
|
245
src/userauth.c
245
src/userauth.c
@ -122,7 +122,7 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
|
||||
&session->userauth_list_data,
|
||||
&session->userauth_list_data_len, 0,
|
||||
NULL, 0,
|
||||
&session->userauth_list_packet_requirev_state);
|
||||
&session->userauth_list_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block requesting userauth list");
|
||||
@ -158,7 +158,7 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
|
||||
"Unexpected userauth list size");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* Do note that the memory areas overlap! */
|
||||
memmove(session->userauth_list_data, session->userauth_list_data + 5,
|
||||
methods_len);
|
||||
@ -314,7 +314,8 @@ userauth_password(LIBSSH2_SESSION *session,
|
||||
session->userauth_pswd_state = libssh2_NB_state_idle;
|
||||
return 0;
|
||||
}
|
||||
else if(session->userauth_pswd_data[0] == SSH_MSG_USERAUTH_FAILURE) {
|
||||
else if(session->userauth_pswd_data[0] ==
|
||||
SSH_MSG_USERAUTH_FAILURE) {
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_AUTH,
|
||||
"Password authentication failed");
|
||||
LIBSSH2_FREE(session, session->userauth_pswd_data);
|
||||
@ -353,14 +354,15 @@ userauth_password(LIBSSH2_SESSION *session,
|
||||
session->userauth_pswd_data = NULL;
|
||||
}
|
||||
if(passwd_change_cb) {
|
||||
if(session->userauth_pswd_state == libssh2_NB_state_sent1) {
|
||||
if(session->userauth_pswd_state ==
|
||||
libssh2_NB_state_sent1) {
|
||||
passwd_change_cb(session,
|
||||
&session->userauth_pswd_newpw,
|
||||
&session->userauth_pswd_newpw_len,
|
||||
&session->abstract);
|
||||
if(!session->userauth_pswd_newpw) {
|
||||
return _libssh2_error(session,
|
||||
LIBSSH2_ERROR_PASSWORD_EXPIRED,
|
||||
LIBSSH2_ERROR_PASSWORD_EXPIRED,
|
||||
"Password expired, and "
|
||||
"callback failed");
|
||||
}
|
||||
@ -369,10 +371,10 @@ userauth_password(LIBSSH2_SESSION *session,
|
||||
if(username_len + password_len + 44 <= UINT_MAX) {
|
||||
session->userauth_pswd_data_len =
|
||||
username_len + password_len + 44;
|
||||
s = session->userauth_pswd_data =
|
||||
LIBSSH2_ALLOC(session,
|
||||
s = session->userauth_pswd_data =
|
||||
LIBSSH2_ALLOC(session,
|
||||
session->userauth_pswd_data_len);
|
||||
}
|
||||
}
|
||||
else {
|
||||
s = session->userauth_pswd_data = NULL;
|
||||
session->userauth_pswd_data_len = 0;
|
||||
@ -403,15 +405,17 @@ userauth_password(LIBSSH2_SESSION *session,
|
||||
session->userauth_pswd_state = libssh2_NB_state_sent2;
|
||||
}
|
||||
|
||||
if(session->userauth_pswd_state == libssh2_NB_state_sent2) {
|
||||
if(session->userauth_pswd_state ==
|
||||
libssh2_NB_state_sent2) {
|
||||
rc = _libssh2_transport_send(session,
|
||||
session->userauth_pswd_data,
|
||||
session->userauth_pswd_data_len,
|
||||
(unsigned char *)
|
||||
session->userauth_pswd_newpw,
|
||||
session->userauth_pswd_newpw_len);
|
||||
session->userauth_pswd_data,
|
||||
session->userauth_pswd_data_len,
|
||||
(unsigned char *)
|
||||
session->userauth_pswd_newpw,
|
||||
session->userauth_pswd_newpw_len);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
return _libssh2_error(session,
|
||||
LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block waiting");
|
||||
}
|
||||
|
||||
@ -465,7 +469,8 @@ LIBSSH2_API int
|
||||
libssh2_userauth_password_ex(LIBSSH2_SESSION *session, const char *username,
|
||||
unsigned int username_len, const char *password,
|
||||
unsigned int password_len,
|
||||
LIBSSH2_PASSWD_CHANGEREQ_FUNC((*passwd_change_cb)))
|
||||
LIBSSH2_PASSWD_CHANGEREQ_FUNC
|
||||
((*passwd_change_cb)))
|
||||
{
|
||||
int rc;
|
||||
BLOCK_ADJUST(rc, session,
|
||||
@ -967,7 +972,8 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
|
||||
session->userauth_host_packet + session->userauth_host_packet_len;
|
||||
|
||||
_libssh2_store_u32(&session->userauth_host_s,
|
||||
4 + session->userauth_host_method_len + 4 + sig_len);
|
||||
4 + session->userauth_host_method_len +
|
||||
4 + sig_len);
|
||||
_libssh2_store_str(&session->userauth_host_s,
|
||||
(const char *)session->userauth_host_method,
|
||||
session->userauth_host_method_len);
|
||||
@ -990,7 +996,8 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
|
||||
session->userauth_host_packet,
|
||||
NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block");
|
||||
}
|
||||
else if(rc) {
|
||||
LIBSSH2_FREE(session, session->userauth_host_packet);
|
||||
@ -1015,7 +1022,8 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
|
||||
&session->
|
||||
userauth_host_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block");
|
||||
}
|
||||
|
||||
session->userauth_host_state = libssh2_NB_state_idle;
|
||||
@ -1075,7 +1083,8 @@ _libssh2_userauth_publickey(LIBSSH2_SESSION *session,
|
||||
unsigned int username_len,
|
||||
const unsigned char *pubkeydata,
|
||||
unsigned long pubkeydata_len,
|
||||
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*sign_callback)),
|
||||
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC
|
||||
((*sign_callback)),
|
||||
void *abstract)
|
||||
{
|
||||
unsigned char reply_codes[4] =
|
||||
@ -1120,8 +1129,8 @@ _libssh2_userauth_publickey(LIBSSH2_SESSION *session,
|
||||
LIBSSH2_ALLOC(session, session->userauth_pblc_method_len);
|
||||
if(!session->userauth_pblc_method) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for public key "
|
||||
"data");
|
||||
"Unable to allocate memory "
|
||||
"for public key data");
|
||||
}
|
||||
memcpy(session->userauth_pblc_method, pubkeydata + 4,
|
||||
session->userauth_pblc_method_len);
|
||||
@ -1191,7 +1200,8 @@ _libssh2_userauth_publickey(LIBSSH2_SESSION *session,
|
||||
session->userauth_pblc_packet_len,
|
||||
NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block");
|
||||
else if(rc) {
|
||||
LIBSSH2_FREE(session, session->userauth_pblc_packet);
|
||||
session->userauth_pblc_packet = NULL;
|
||||
@ -1213,7 +1223,8 @@ _libssh2_userauth_publickey(LIBSSH2_SESSION *session,
|
||||
&session->
|
||||
userauth_pblc_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block");
|
||||
}
|
||||
else if(rc || (session->userauth_pblc_data_len < 1)) {
|
||||
LIBSSH2_FREE(session, session->userauth_pblc_packet);
|
||||
@ -1287,7 +1298,8 @@ _libssh2_userauth_publickey(LIBSSH2_SESSION *session,
|
||||
rc = sign_callback(session, &sig, &sig_len, buf, s - buf, abstract);
|
||||
LIBSSH2_FREE(session, buf);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block");
|
||||
}
|
||||
else if(rc) {
|
||||
LIBSSH2_FREE(session, session->userauth_pblc_method);
|
||||
@ -1329,7 +1341,8 @@ _libssh2_userauth_publickey(LIBSSH2_SESSION *session,
|
||||
session->userauth_pblc_b = NULL;
|
||||
|
||||
_libssh2_store_u32(&s,
|
||||
4 + session->userauth_pblc_method_len + 4 + sig_len);
|
||||
4 + session->userauth_pblc_method_len + 4 +
|
||||
sig_len);
|
||||
_libssh2_store_str(&s, (const char *)session->userauth_pblc_method,
|
||||
session->userauth_pblc_method_len);
|
||||
|
||||
@ -1352,7 +1365,8 @@ _libssh2_userauth_publickey(LIBSSH2_SESSION *session,
|
||||
session->userauth_pblc_packet,
|
||||
NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block");
|
||||
}
|
||||
else if(rc) {
|
||||
LIBSSH2_FREE(session, session->userauth_pblc_packet);
|
||||
@ -1371,9 +1385,9 @@ _libssh2_userauth_publickey(LIBSSH2_SESSION *session,
|
||||
reply_codes[2] = 0;
|
||||
|
||||
rc = _libssh2_packet_requirev(session, reply_codes,
|
||||
&session->userauth_pblc_data,
|
||||
&session->userauth_pblc_data_len, 0, NULL, 0,
|
||||
&session->userauth_pblc_packet_requirev_state);
|
||||
&session->userauth_pblc_data,
|
||||
&session->userauth_pblc_data_len, 0, NULL, 0,
|
||||
&session->userauth_pblc_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block requesting userauth list");
|
||||
@ -1444,11 +1458,11 @@ userauth_publickey_frommemory(LIBSSH2_SESSION *session,
|
||||
else if(privatekeydata_len && privatekeydata) {
|
||||
/* Compute public key from private key. */
|
||||
if(_libssh2_pub_priv_keyfilememory(session,
|
||||
&session->userauth_pblc_method,
|
||||
&session->userauth_pblc_method_len,
|
||||
&pubkeydata, &pubkeydata_len,
|
||||
privatekeydata, privatekeydata_len,
|
||||
passphrase))
|
||||
&session->userauth_pblc_method,
|
||||
&session->userauth_pblc_method_len,
|
||||
&pubkeydata, &pubkeydata_len,
|
||||
privatekeydata, privatekeydata_len,
|
||||
passphrase))
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_FILE,
|
||||
"Unable to extract public key "
|
||||
"from private key.");
|
||||
@ -1588,7 +1602,8 @@ libssh2_userauth_publickey(LIBSSH2_SESSION *session,
|
||||
const char *user,
|
||||
const unsigned char *pubkeydata,
|
||||
size_t pubkeydata_len,
|
||||
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*sign_callback)),
|
||||
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC
|
||||
((*sign_callback)),
|
||||
void **abstract)
|
||||
{
|
||||
int rc;
|
||||
@ -1614,7 +1629,8 @@ static int
|
||||
userauth_keyboard_interactive(LIBSSH2_SESSION * session,
|
||||
const char *username,
|
||||
unsigned int username_len,
|
||||
LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC((*response_callback)))
|
||||
LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC
|
||||
((*response_callback)))
|
||||
{
|
||||
unsigned char *s;
|
||||
int rc;
|
||||
@ -1685,14 +1701,16 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
|
||||
session->userauth_kybd_packet_len,
|
||||
NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block");
|
||||
}
|
||||
else if(rc) {
|
||||
LIBSSH2_FREE(session, session->userauth_kybd_data);
|
||||
session->userauth_kybd_data = NULL;
|
||||
session->userauth_kybd_state = libssh2_NB_state_idle;
|
||||
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
||||
"Unable to send keyboard-interactive request");
|
||||
"Unable to send keyboard-interactive"
|
||||
" request");
|
||||
}
|
||||
LIBSSH2_FREE(session, session->userauth_kybd_data);
|
||||
session->userauth_kybd_data = NULL;
|
||||
@ -1716,12 +1734,14 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
|
||||
session->userauth_kybd_state = libssh2_NB_state_idle;
|
||||
return _libssh2_error(session,
|
||||
LIBSSH2_ERROR_AUTHENTICATION_FAILED,
|
||||
"Waiting for keyboard USERAUTH response");
|
||||
"Waiting for keyboard "
|
||||
"USERAUTH response");
|
||||
}
|
||||
|
||||
if(session->userauth_kybd_data[0] == SSH_MSG_USERAUTH_SUCCESS) {
|
||||
_libssh2_debug(session, LIBSSH2_TRACE_AUTH,
|
||||
"Keyboard-interactive authentication successful");
|
||||
"Keyboard-interactive "
|
||||
"authentication successful");
|
||||
LIBSSH2_FREE(session, session->userauth_kybd_data);
|
||||
session->userauth_kybd_data = NULL;
|
||||
session->state |= LIBSSH2_STATE_AUTHENTICATED;
|
||||
@ -1755,24 +1775,24 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
|
||||
"to get length");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if(session->userauth_kybd_auth_name_len) {
|
||||
session->userauth_kybd_auth_name =
|
||||
LIBSSH2_ALLOC(session,
|
||||
session->userauth_kybd_auth_name_len);
|
||||
if(!session->userauth_kybd_auth_name) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for "
|
||||
"keyboard-interactive 'name' "
|
||||
"request field");
|
||||
goto cleanup;
|
||||
}
|
||||
if (s + session->userauth_list_data_len <=
|
||||
session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
memcpy(session->userauth_kybd_auth_name, s,
|
||||
session->userauth_kybd_auth_name_len);
|
||||
s += session->userauth_kybd_auth_name_len;
|
||||
|
||||
if(session->userauth_kybd_auth_name_len) {
|
||||
session->userauth_kybd_auth_name =
|
||||
LIBSSH2_ALLOC(session,
|
||||
session->userauth_kybd_auth_name_len);
|
||||
if(!session->userauth_kybd_auth_name) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for "
|
||||
"keyboard-interactive 'name' "
|
||||
"request field");
|
||||
goto cleanup;
|
||||
}
|
||||
if(s + session->userauth_list_data_len <=
|
||||
session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
memcpy(session->userauth_kybd_auth_name, s,
|
||||
session->userauth_kybd_auth_name_len);
|
||||
s += session->userauth_kybd_auth_name_len;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
@ -1780,14 +1800,14 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
|
||||
"for auth name");
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
if (s + 4 <= session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
/* string instruction (ISO-10646 UTF-8) */
|
||||
session->userauth_kybd_auth_instruction_len =
|
||||
_libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
}
|
||||
|
||||
if(s + 4 <= session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
/* string instruction (ISO-10646 UTF-8) */
|
||||
session->userauth_kybd_auth_instruction_len =
|
||||
_libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
@ -1808,25 +1828,25 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
|
||||
goto cleanup;
|
||||
}
|
||||
if(s + session->userauth_kybd_auth_instruction_len <=
|
||||
session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
memcpy(session->userauth_kybd_auth_instruction, s,
|
||||
session->userauth_kybd_auth_instruction_len);
|
||||
s += session->userauth_kybd_auth_instruction_len;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"userauth keyboard data buffer too small"
|
||||
"for auth instruction");
|
||||
session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
memcpy(session->userauth_kybd_auth_instruction, s,
|
||||
session->userauth_kybd_auth_instruction_len);
|
||||
s += session->userauth_kybd_auth_instruction_len;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"userauth keyboard data buffer too small"
|
||||
"for auth instruction");
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
if(s + 4 <= session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
/* string language tag (as defined in [RFC-3066]) */
|
||||
language_tag_len = _libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
session->userauth_kybd_data_len) {
|
||||
/* string language tag (as defined in [RFC-3066]) */
|
||||
language_tag_len = _libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
@ -1836,9 +1856,9 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
|
||||
}
|
||||
|
||||
if(s + language_tag_len <= session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
/* ignoring this field as deprecated */
|
||||
s += language_tag_len;
|
||||
session->userauth_kybd_data_len) {
|
||||
/* ignoring this field as deprecated */
|
||||
s += language_tag_len;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
@ -1848,16 +1868,16 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
|
||||
}
|
||||
|
||||
if(s + 4 <= session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
/* int num-prompts */
|
||||
session->userauth_kybd_num_prompts = _libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
session->userauth_kybd_data_len) {
|
||||
/* int num-prompts */
|
||||
session->userauth_kybd_num_prompts = _libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"userauth keyboard data buffer too small"
|
||||
"for auth num keyboard prompts");
|
||||
goto cleanup;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if(session->userauth_kybd_num_prompts > 100) {
|
||||
@ -1892,51 +1912,53 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
|
||||
|
||||
for(i = 0; i < session->userauth_kybd_num_prompts; i++) {
|
||||
if(s + 4 <= session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
/* string prompt[1] (ISO-10646 UTF-8) */
|
||||
session->userauth_kybd_prompts[i].length =
|
||||
_libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
session->userauth_kybd_data_len) {
|
||||
/* string prompt[1] (ISO-10646 UTF-8) */
|
||||
session->userauth_kybd_prompts[i].length =
|
||||
_libssh2_ntohu32(s);
|
||||
s += 4;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"userauth keyboard data buffer too small"
|
||||
"for auth keyboard prompt length");
|
||||
"userauth keyboard data buffer too "
|
||||
"small for auth keyboard "
|
||||
"prompt length");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
session->userauth_kybd_prompts[i].text =
|
||||
LIBSSH2_CALLOC(session,
|
||||
session->userauth_kybd_prompts[i].length);
|
||||
session->userauth_kybd_prompts[i].
|
||||
length);
|
||||
if(!session->userauth_kybd_prompts[i].text) {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for "
|
||||
"keyboard-interactive prompt message");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
|
||||
if(s + session->userauth_kybd_prompts[i].length <=
|
||||
session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
memcpy(session->userauth_kybd_prompts[i].text, s,
|
||||
session->userauth_kybd_prompts[i].length);
|
||||
s += session->userauth_kybd_prompts[i].length;
|
||||
session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
memcpy(session->userauth_kybd_prompts[i].text, s,
|
||||
session->userauth_kybd_prompts[i].length);
|
||||
s += session->userauth_kybd_prompts[i].length;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"userauth keyboard data buffer too small"
|
||||
"for auth keyboard prompt");
|
||||
"userauth keyboard data buffer too "
|
||||
"small for auth keyboard prompt");
|
||||
goto cleanup;
|
||||
}
|
||||
if(s < session->userauth_kybd_data +
|
||||
session->userauth_kybd_data_len) {
|
||||
/* boolean echo[1] */
|
||||
session->userauth_kybd_prompts[i].echo = *s++;
|
||||
session->userauth_kybd_data_len) {
|
||||
/* boolean echo[1] */
|
||||
session->userauth_kybd_prompts[i].echo = *s++;
|
||||
}
|
||||
else {
|
||||
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"userauth keyboard data buffer too small"
|
||||
"for auth keyboard prompt echo");
|
||||
"userauth keyboard data buffer too "
|
||||
"small for auth keyboard prompt echo");
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
@ -2077,7 +2099,8 @@ LIBSSH2_API int
|
||||
libssh2_userauth_keyboard_interactive_ex(LIBSSH2_SESSION *session,
|
||||
const char *user,
|
||||
unsigned int user_len,
|
||||
LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC((*response_callback)))
|
||||
LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC
|
||||
((*response_callback)))
|
||||
{
|
||||
int rc;
|
||||
BLOCK_ADJUST(rc, session,
|
||||
|
@ -44,7 +44,8 @@ _libssh2_userauth_publickey(LIBSSH2_SESSION *session,
|
||||
unsigned int username_len,
|
||||
const unsigned char *pubkeydata,
|
||||
unsigned long pubkeydata_len,
|
||||
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*sign_callback)),
|
||||
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC
|
||||
((*sign_callback)),
|
||||
void *abstract);
|
||||
|
||||
#endif /* LIBSSH2_USERAUTH_H */
|
||||
|
@ -287,7 +287,8 @@ _libssh2_wincng_init(void)
|
||||
(PBYTE)BCRYPT_CHAIN_MODE_CBC,
|
||||
sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
|
||||
if(!BCRYPT_SUCCESS(ret)) {
|
||||
(void)BCryptCloseAlgorithmProvider(_libssh2_wincng.hAlg3DES_CBC, 0);
|
||||
(void)BCryptCloseAlgorithmProvider(_libssh2_wincng.hAlg3DES_CBC,
|
||||
0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -310,7 +310,8 @@ struct _libssh2_wincng_cipher_ctx {
|
||||
struct _libssh2_wincng_cipher_type {
|
||||
BCRYPT_ALG_HANDLE *phAlg;
|
||||
unsigned long dwKeyLength;
|
||||
int useIV; /* TODO: Convert to bool when a C89 compatible bool type is defined */
|
||||
int useIV; /* TODO: Convert to bool when a C89 compatible bool type
|
||||
is defined */
|
||||
int ctrMode;
|
||||
};
|
||||
|
||||
|
Загрузка…
x
Ссылка в новой задаче
Block a user