mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-08-11 17:22:40 +03:00
Reformat ssh_packet_socket_callback
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
(cherry picked from commit 19c4de7350
)
This commit is contained in:
62
src/packet.c
62
src/packet.c
@@ -1052,9 +1052,11 @@ static bool ssh_packet_need_rekey(ssh_session session,
|
|||||||
* @param user pointer to current ssh_session
|
* @param user pointer to current ssh_session
|
||||||
* @param data pointer to the data received
|
* @param data pointer to the data received
|
||||||
* @len length of data received. It might not be enough for a complete packet
|
* @len length of data received. It might not be enough for a complete packet
|
||||||
* @returns number of bytes read and processed.
|
* @returns number of bytes read and processed. Zero means only partial packet
|
||||||
|
* received and negative value means error.
|
||||||
*/
|
*/
|
||||||
size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *user)
|
size_t
|
||||||
|
ssh_packet_socket_callback(const void *data, size_t receivedlen, void *user)
|
||||||
{
|
{
|
||||||
ssh_session session = (ssh_session)user;
|
ssh_session session = (ssh_session)user;
|
||||||
uint32_t blocksize = 8;
|
uint32_t blocksize = 8;
|
||||||
@@ -1066,7 +1068,7 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
uint8_t *cleartext_packet = NULL;
|
uint8_t *cleartext_packet = NULL;
|
||||||
uint8_t *packet_second_block = NULL;
|
uint8_t *packet_second_block = NULL;
|
||||||
uint8_t *mac = NULL;
|
uint8_t *mac = NULL;
|
||||||
size_t packet_remaining;
|
size_t packet_remaining, packet_offset;
|
||||||
uint32_t packet_len, compsize, payloadsize;
|
uint32_t packet_len, compsize, payloadsize;
|
||||||
uint8_t padding;
|
uint8_t padding;
|
||||||
size_t processed = 0; /* number of bytes processed from the callback */
|
size_t processed = 0; /* number of bytes processed from the callback */
|
||||||
@@ -1112,7 +1114,7 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
session->packet_state == PACKET_STATE_PROCESSING ?
|
session->packet_state == PACKET_STATE_PROCESSING ?
|
||||||
"PROCESSING" : "unknown");
|
"PROCESSING" : "unknown");
|
||||||
#endif
|
#endif
|
||||||
switch(session->packet_state) {
|
switch (session->packet_state) {
|
||||||
case PACKET_STATE_INIT:
|
case PACKET_STATE_INIT:
|
||||||
if (receivedlen < lenfield_blocksize + etm_packet_offset) {
|
if (receivedlen < lenfield_blocksize + etm_packet_offset) {
|
||||||
/*
|
/*
|
||||||
@@ -1145,11 +1147,13 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!etm) {
|
if (!etm) {
|
||||||
ptr = ssh_buffer_allocate(session->in_buffer, lenfield_blocksize);
|
ptr = ssh_buffer_allocate(session->in_buffer,
|
||||||
|
lenfield_blocksize);
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
packet_len = ssh_packet_decrypt_len(session, ptr, (uint8_t *)data);
|
packet_len = ssh_packet_decrypt_len(session, ptr,
|
||||||
|
(uint8_t *)data);
|
||||||
to_be_read = packet_len - lenfield_blocksize + sizeof(uint32_t);
|
to_be_read = packet_len - lenfield_blocksize + sizeof(uint32_t);
|
||||||
} else {
|
} else {
|
||||||
/* Length is unencrypted in case of Encrypt-then-MAC */
|
/* Length is unencrypted in case of Encrypt-then-MAC */
|
||||||
@@ -1179,11 +1183,11 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
FALL_THROUGH;
|
FALL_THROUGH;
|
||||||
case PACKET_STATE_SIZEREAD:
|
case PACKET_STATE_SIZEREAD:
|
||||||
packet_len = session->in_packet.len;
|
packet_len = session->in_packet.len;
|
||||||
processed = lenfield_blocksize + etm_packet_offset;
|
packet_offset = processed = lenfield_blocksize + etm_packet_offset;
|
||||||
to_be_read = packet_len + sizeof(uint32_t) + current_macsize;
|
to_be_read = packet_len + sizeof(uint32_t) + current_macsize;
|
||||||
/* if to_be_read is zero, the whole packet was blocksize bytes. */
|
/* if to_be_read is zero, the whole packet was blocksize bytes. */
|
||||||
if (to_be_read != 0) {
|
if (to_be_read != 0) {
|
||||||
if (receivedlen < (unsigned long)to_be_read) {
|
if (receivedlen < (unsigned long)to_be_read) {
|
||||||
/* give up, not enough data in buffer */
|
/* give up, not enough data in buffer */
|
||||||
SSH_LOG(SSH_LOG_PACKET,
|
SSH_LOG(SSH_LOG_PACKET,
|
||||||
"packet: partial packet (read len) "
|
"packet: partial packet (read len) "
|
||||||
@@ -1194,13 +1198,12 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
packet_second_block = (uint8_t*)data + lenfield_blocksize + etm_packet_offset;
|
packet_second_block = (uint8_t*)data + packet_offset;
|
||||||
processed = to_be_read - current_macsize;
|
processed = to_be_read - current_macsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* remaining encrypted bytes from the packet, MAC not included */
|
/* remaining encrypted bytes from the packet, MAC not included */
|
||||||
packet_remaining =
|
packet_remaining = packet_len - (packet_offset - sizeof(uint32_t));
|
||||||
packet_len - (lenfield_blocksize - sizeof(uint32_t) + etm_packet_offset);
|
|
||||||
cleartext_packet = ssh_buffer_allocate(session->in_buffer,
|
cleartext_packet = ssh_buffer_allocate(session->in_buffer,
|
||||||
packet_remaining);
|
packet_remaining);
|
||||||
if (cleartext_packet == NULL) {
|
if (cleartext_packet == NULL) {
|
||||||
@@ -1223,16 +1226,16 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* Decrypt the packet. In case of EtM mode, the length is already
|
* Decrypt the packet. In case of EtM mode, the length is
|
||||||
* known as it's unencrypted. In the other case, lenfield_blocksize bytes
|
* already known as it's unencrypted. In the other case,
|
||||||
* already have been decrypted.
|
* lenfield_blocksize bytes already have been decrypted.
|
||||||
*/
|
*/
|
||||||
if (packet_remaining > 0) {
|
if (packet_remaining > 0) {
|
||||||
rc = ssh_packet_decrypt(session,
|
rc = ssh_packet_decrypt(session,
|
||||||
cleartext_packet,
|
cleartext_packet,
|
||||||
(uint8_t *)data,
|
(uint8_t *)data,
|
||||||
lenfield_blocksize + etm_packet_offset,
|
packet_offset,
|
||||||
processed - (lenfield_blocksize + etm_packet_offset));
|
processed - packet_offset);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
ssh_set_error(session,
|
ssh_set_error(session,
|
||||||
SSH_FATAL,
|
SSH_FATAL,
|
||||||
@@ -1242,9 +1245,10 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (crypto->in_hmac != SSH_HMAC_NONE && !etm) {
|
if (crypto->in_hmac != SSH_HMAC_NONE && !etm) {
|
||||||
|
ssh_buffer in = session->in_buffer;
|
||||||
rc = ssh_packet_hmac_verify(session,
|
rc = ssh_packet_hmac_verify(session,
|
||||||
ssh_buffer_get(session->in_buffer),
|
ssh_buffer_get(in),
|
||||||
ssh_buffer_get_len(session->in_buffer),
|
ssh_buffer_get_len(in),
|
||||||
mac,
|
mac,
|
||||||
crypto->in_hmac);
|
crypto->in_hmac);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
@@ -1295,9 +1299,10 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
compsize = ssh_buffer_get_len(session->in_buffer);
|
compsize = ssh_buffer_get_len(session->in_buffer);
|
||||||
|
|
||||||
#ifdef WITH_ZLIB
|
#ifdef WITH_ZLIB
|
||||||
if (crypto && crypto->do_compress_in
|
if (crypto && crypto->do_compress_in &&
|
||||||
&& ssh_buffer_get_len(session->in_buffer) > 0) {
|
ssh_buffer_get_len(session->in_buffer) > 0) {
|
||||||
rc = decompress_buffer(session, session->in_buffer,MAX_PACKET_LEN);
|
rc = decompress_buffer(session, session->in_buffer,
|
||||||
|
MAX_PACKET_LEN);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
@@ -1324,13 +1329,15 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
session->packet_state = PACKET_STATE_PROCESSING;
|
session->packet_state = PACKET_STATE_PROCESSING;
|
||||||
ssh_packet_parse_type(session);
|
ssh_packet_parse_type(session);
|
||||||
SSH_LOG(SSH_LOG_PACKET,
|
SSH_LOG(SSH_LOG_PACKET,
|
||||||
"packet: read type %hhd [len=%d,padding=%hhd,comp=%d,payload=%d]",
|
"packet: read type %hhd [len=%" PRIu32 ",padding=%hhd,"
|
||||||
session->in_packet.type, packet_len, padding, compsize, payloadsize);
|
"comp=%" PRIu32 ",payload=%" PRIu32 "]",
|
||||||
|
session->in_packet.type, packet_len, padding, compsize,
|
||||||
|
payloadsize);
|
||||||
|
|
||||||
/* Check if the packet is expected */
|
/* Check if the packet is expected */
|
||||||
filter_result = ssh_packet_incoming_filter(session);
|
filter_result = ssh_packet_incoming_filter(session);
|
||||||
|
|
||||||
switch(filter_result) {
|
switch (filter_result) {
|
||||||
case SSH_PACKET_ALLOWED:
|
case SSH_PACKET_ALLOWED:
|
||||||
/* Execute callbacks */
|
/* Execute callbacks */
|
||||||
ssh_packet_process(session, session->in_packet.type);
|
ssh_packet_process(session, session->in_packet.type);
|
||||||
@@ -1355,7 +1362,8 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
|
|
||||||
ptr = ((uint8_t*)data) + processed;
|
ptr = ((uint8_t*)data) + processed;
|
||||||
|
|
||||||
rc = ssh_packet_socket_callback(ptr, receivedlen - processed,user);
|
rc = ssh_packet_socket_callback(ptr, receivedlen - processed,
|
||||||
|
user);
|
||||||
processed += rc;
|
processed += rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1381,8 +1389,8 @@ size_t ssh_packet_socket_callback(const void *data, size_t receivedlen, void *us
|
|||||||
session->packet_state);
|
session->packet_state);
|
||||||
|
|
||||||
error:
|
error:
|
||||||
session->session_state= SSH_SESSION_STATE_ERROR;
|
session->session_state = SSH_SESSION_STATE_ERROR;
|
||||||
SSH_LOG(SSH_LOG_PACKET,"Packet: processed %zu bytes", processed);
|
SSH_LOG(SSH_LOG_PACKET, "Packet: processed %zu bytes", processed);
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user