Add more error checks to packet_send1().

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@460 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
Andreas Schneider
2009-04-14 09:14:09 +00:00
parent 02ebbfdeca
commit 13dcfa6bfc

View File

@@ -501,61 +501,92 @@ error:
} }
#ifdef HAVE_SSH1 #ifdef HAVE_SSH1
static int packet_send1(SSH_SESSION *session){ static int packet_send1(SSH_SESSION *session) {
char padstring[32]; unsigned int blocksize = (session->current_crypto ?
u32 finallen; session->current_crypto->out_cipher->blocksize : 8);
u8 padding; u32 currentlen = buffer_get_len(session->out_buffer) + sizeof(u32);
u32 crc; char padstring[32] = {0};
u32 currentlen=buffer_get_len(session->out_buffer)+sizeof(u32); int rc = SSH_ERROR;
int ret=0; u32 finallen;
unsigned int blocksize=(session->current_crypto?session->current_crypto->out_cipher->blocksize:8); u32 crc;
enter_function(); u8 padding;
ssh_log(session,SSH_LOG_PACKET,"Sending a %d bytes long packet",currentlen);
/* enter_function();
ssh_log(session,SSH_LOG_PACKET,"Sending a %d bytes long packet",currentlen);
/* TODO FIXME
#if defined(HAVE_LIBZ) && defined(WITH_LIBZ) #if defined(HAVE_LIBZ) && defined(WITH_LIBZ)
if(session->current_crypto && session->current_crypto->do_compress_out){ if (session->current_crypto && session->current_crypto->do_compress_out) {
compress_buffer(session,session->out_buffer); if (compress_buffer(session, session->out_buffer) < 0) {
currentlen=buffer_get_len(session->out_buffer); goto error;
} }
currentlen = buffer_get_len(session->out_buffer);
}
#endif #endif
*/ */
padding=blocksize-(currentlen % blocksize); padding = blocksize - (currentlen % blocksize);
if(session->current_crypto) if (session->current_crypto) {
ssh_get_random(padstring,padding,0); ssh_get_random(padstring, padding, 0);
else } else {
memset(padstring,0,padding); memset(padstring, 0, padding);
finallen=htonl(currentlen); }
ssh_log(session,SSH_LOG_PACKET,"%d bytes after comp + %d padding bytes = %d bytes packet",currentlen,padding,(ntohl(finallen)));
buffer_add_data_begin(session->out_buffer,&padstring,padding); finallen = htonl(currentlen);
buffer_add_data_begin(session->out_buffer,&finallen,sizeof(u32)); ssh_log(session, SSH_LOG_PACKET,
crc=ssh_crc32(buffer_get(session->out_buffer)+sizeof(u32),buffer_get_len(session->out_buffer)-sizeof(u32)); "%d bytes after comp + %d padding bytes = %d bytes packet",
buffer_add_u32(session->out_buffer,ntohl(crc)); currentlen, padding, ntohl(finallen));
if (buffer_add_data_begin(session->out_buffer,i &padstring, padding) < 0) {
goto error;
}
if (buffer_add_data_begin(session->out_buffer, &finallen, sizeof(u32)) < 0) {
goto error;
}
crc = ssh_crc32(buffer_get(session->out_buffer) + sizeof(u32),
buffer_get_len(session->out_buffer) - sizeof(u32));
if (buffer_add_u32(session->out_buffer, ntohl(crc)) < 0) {
goto error;
}
#ifdef DEBUG_CRYPTO #ifdef DEBUG_CRYPTO
ssh_print_hexa("clear packet",buffer_get(session->out_buffer), ssh_print_hexa("Clear packet", buffer_get(session->out_buffer),
buffer_get_len(session->out_buffer)); buffer_get_len(session->out_buffer));
#endif #endif
packet_encrypt(session,buffer_get(session->out_buffer)+sizeof(u32),buffer_get_len(session->out_buffer)-sizeof(u32));
packet_encrypt(session, buffer_get(session->out_buffer) + sizeof(u32),
buffer_get_len(session->out_buffer) - sizeof(u32));
#ifdef DEBUG_CRYPTO #ifdef DEBUG_CRYPTO
ssh_print_hexa("encrypted packet",buffer_get(session->out_buffer), ssh_print_hexa("encrypted packet",buffer_get(session->out_buffer),
buffer_get_len(session->out_buffer)); buffer_get_len(session->out_buffer));
#endif #endif
ssh_socket_write(session->socket,buffer_get(session->out_buffer),buffer_get_len(session->out_buffer)); if (ssh_socket_write(session->socket, buffer_get(session->out_buffer),
ret=packet_flush(session,0); buffer_get_len(session->out_buffer)) == SSH_ERROR) {
session->send_seq++; goto error;
buffer_reinit(session->out_buffer); }
leave_function();
return ret; /* SSH_OK, AGAIN or ERROR */ rc = packet_flush(session, 0);
session->send_seq++;
if (buffer_reinit(session->out_buffer) < 0) {
rc = SSH_ERROR;
}
error:
leave_function();
return rc; /* SSH_OK, AGAIN or ERROR */
} }
#endif /* HAVE_SSH1 */ #endif /* HAVE_SSH1 */
int packet_send(SSH_SESSION *session){ int packet_send(SSH_SESSION *session) {
#ifdef HAVE_SSH1 #ifdef HAVE_SSH1
if (session->version==1) if (session->version == 1) {
return packet_send1(session); return packet_send1(session);
else }
#endif #endif
return packet_send2(session); return packet_send2(session);
} }
void packet_parse(SSH_SESSION *session){ void packet_parse(SSH_SESSION *session){