2017-01-31 12:29:48 +00:00
|
|
|
/* $OpenBSD: packet.c,v 1.234 2016/07/18 11:35:33 markus Exp $ */
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
|
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
|
|
|
* All rights reserved
|
|
|
|
* This file contains code implementing the packet protocol and communication
|
|
|
|
* with the other side. This same code is used both on client and server side.
|
2000-05-15 04:37:24 +00:00
|
|
|
*
|
2000-09-10 08:31:17 +00:00
|
|
|
* As far as I am concerned, the code I have written for this software
|
|
|
|
* can be used freely for any purpose. Any derived versions of this
|
|
|
|
* software must be clearly marked as such, and if the derived work is
|
|
|
|
* incompatible with the protocol description in the RFC file, it must be
|
|
|
|
* called by a name other than "ssh" or "Secure Shell".
|
|
|
|
*
|
|
|
|
*
|
2000-05-15 04:37:24 +00:00
|
|
|
* SSH2 packet format added by Markus Friedl.
|
2002-03-18 09:55:03 +00:00
|
|
|
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
2000-05-15 04:37:24 +00:00
|
|
|
*
|
2000-09-10 08:31:17 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2000-02-24 14:29:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
#include <sys/param.h> /* MIN roundup */
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <sys/types.h>
|
2004-01-07 11:10:17 +00:00
|
|
|
#include "openbsd-compat/sys-queue.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2017-01-31 12:29:48 +00:00
|
|
|
#include <netdb.h>
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2015-07-02 13:15:34 +00:00
|
|
|
#include <limits.h>
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <signal.h>
|
2013-09-18 17:27:38 +00:00
|
|
|
#include <time.h>
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
#include "buffer.h" /* typedefs XXX */
|
|
|
|
#include "key.h" /* typedefs XXX */
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "crc32.h"
|
|
|
|
#include "deattack.h"
|
2000-05-15 04:37:24 +00:00
|
|
|
#include "compat.h"
|
2001-05-04 03:57:05 +00:00
|
|
|
#include "ssh1.h"
|
2000-05-15 04:37:24 +00:00
|
|
|
#include "ssh2.h"
|
2000-12-05 02:20:19 +00:00
|
|
|
#include "cipher.h"
|
2015-07-02 13:15:34 +00:00
|
|
|
#include "sshkey.h"
|
2000-05-15 04:37:24 +00:00
|
|
|
#include "kex.h"
|
2015-07-02 13:15:34 +00:00
|
|
|
#include "digest.h"
|
2001-05-04 03:57:05 +00:00
|
|
|
#include "mac.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "canohost.h"
|
2002-03-18 09:55:03 +00:00
|
|
|
#include "misc.h"
|
2015-01-05 16:09:55 +00:00
|
|
|
#include "channels.h"
|
2002-06-23 14:01:54 +00:00
|
|
|
#include "ssh.h"
|
2015-07-02 13:15:34 +00:00
|
|
|
#include "packet.h"
|
|
|
|
#include "ssherr.h"
|
|
|
|
#include "sshbuf.h"
|
2000-05-15 04:37:24 +00:00
|
|
|
|
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
#define DBG(x) x
|
|
|
|
#else
|
|
|
|
#define DBG(x)
|
|
|
|
#endif
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-02-24 18:49:27 +00:00
|
|
|
#define PACKET_MAX_SIZE (256 * 1024)
|
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
struct packet_state {
|
|
|
|
u_int32_t seqnr;
|
|
|
|
u_int32_t packets;
|
|
|
|
u_int64_t blocks;
|
|
|
|
u_int64_t bytes;
|
|
|
|
};
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
struct packet {
|
|
|
|
TAILQ_ENTRY(packet) next;
|
|
|
|
u_char type;
|
2015-07-02 13:15:34 +00:00
|
|
|
struct sshbuf *payload;
|
2009-10-01 15:19:37 +00:00
|
|
|
};
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
struct session_state {
|
|
|
|
/*
|
|
|
|
* This variable contains the file descriptors used for
|
|
|
|
* communicating with the other side. connection_in is used for
|
|
|
|
* reading; connection_out for writing. These can be the same
|
|
|
|
* descriptor, in which case it is assumed to be a socket.
|
|
|
|
*/
|
|
|
|
int connection_in;
|
|
|
|
int connection_out;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Protocol flags for the remote side. */
|
|
|
|
u_int remote_protocol_flags;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Encryption context for receiving data. Only used for decryption. */
|
2015-07-02 13:15:34 +00:00
|
|
|
struct sshcipher_ctx receive_context;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Encryption context for sending data. Only used for encryption. */
|
2015-07-02 13:15:34 +00:00
|
|
|
struct sshcipher_ctx send_context;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Buffer for raw input data from the socket. */
|
2015-07-02 13:15:34 +00:00
|
|
|
struct sshbuf *input;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Buffer for raw output data going to the socket. */
|
2015-07-02 13:15:34 +00:00
|
|
|
struct sshbuf *output;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Buffer for the partial outgoing packet being constructed. */
|
2015-07-02 13:15:34 +00:00
|
|
|
struct sshbuf *outgoing_packet;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Buffer for the incoming packet currently being processed. */
|
2015-07-02 13:15:34 +00:00
|
|
|
struct sshbuf *incoming_packet;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Scratch buffer for packet compression/decompression. */
|
2015-07-02 13:15:34 +00:00
|
|
|
struct sshbuf *compression_buffer;
|
|
|
|
|
|
|
|
/* Incoming/outgoing compression dictionaries */
|
|
|
|
z_stream compression_in_stream;
|
|
|
|
z_stream compression_out_stream;
|
|
|
|
int compression_in_started;
|
|
|
|
int compression_out_started;
|
|
|
|
int compression_in_failures;
|
|
|
|
int compression_out_failures;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/*
|
|
|
|
* Flag indicating whether packet compression/decompression is
|
|
|
|
* enabled.
|
|
|
|
*/
|
|
|
|
int packet_compression;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* default maximum packet size */
|
|
|
|
u_int max_packet_size;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Flag indicating whether this module has been initialized. */
|
|
|
|
int initialized;
|
2005-09-03 06:59:33 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Set to true if the connection is interactive. */
|
|
|
|
int interactive_mode;
|
2005-09-03 06:59:33 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Set to true if we are the server side. */
|
|
|
|
int server_side;
|
2008-07-23 09:28:49 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Set to true if we are authenticated. */
|
|
|
|
int after_authentication;
|
2008-07-23 09:33:08 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
int keep_alive_timeouts;
|
2004-01-07 11:10:17 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* The maximum time that we will wait to send or receive a packet */
|
|
|
|
int packet_timeout_ms;
|
2002-06-23 14:01:54 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Session key information for Encryption and MAC */
|
2015-07-02 13:15:34 +00:00
|
|
|
struct newkeys *newkeys[MODE_MAX];
|
2009-10-01 15:19:37 +00:00
|
|
|
struct packet_state p_read, p_send;
|
2000-05-15 04:37:24 +00:00
|
|
|
|
2013-09-18 17:27:38 +00:00
|
|
|
/* Volume-based rekeying */
|
2016-03-10 20:10:25 +00:00
|
|
|
u_int64_t max_blocks_in, max_blocks_out, rekey_limit;
|
2000-05-15 04:37:24 +00:00
|
|
|
|
2013-09-18 17:27:38 +00:00
|
|
|
/* Time-based rekeying */
|
2015-07-02 13:15:34 +00:00
|
|
|
u_int32_t rekey_interval; /* how often in seconds */
|
2013-09-18 17:27:38 +00:00
|
|
|
time_t rekey_time; /* time of last rekeying */
|
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* Session key for protocol v1 */
|
|
|
|
u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
|
|
|
|
u_int ssh1_keylen;
|
2009-02-24 18:49:27 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
/* roundup current message to extra_pad bytes */
|
|
|
|
u_char extra_pad;
|
|
|
|
|
|
|
|
/* XXX discard incoming data after MAC error */
|
|
|
|
u_int packet_discard;
|
2017-01-31 12:29:48 +00:00
|
|
|
size_t packet_discard_mac_already;
|
2015-07-02 13:15:34 +00:00
|
|
|
struct sshmac *packet_discard_mac;
|
2009-10-01 15:19:37 +00:00
|
|
|
|
|
|
|
/* Used in packet_read_poll2() */
|
|
|
|
u_int packlen;
|
|
|
|
|
|
|
|
/* Used in packet_send2 */
|
|
|
|
int rekeying;
|
|
|
|
|
|
|
|
/* Used in packet_set_interactive */
|
|
|
|
int set_interactive_called;
|
|
|
|
|
|
|
|
/* Used in packet_set_maxsize */
|
|
|
|
int set_maxsize_called;
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
/* One-off warning about weak ciphers */
|
|
|
|
int cipher_warning_done;
|
2009-10-01 15:19:37 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
/* SSH1 CRC compensation attack detector */
|
|
|
|
struct deattack_ctx deattack;
|
2009-10-01 15:19:37 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
TAILQ_HEAD(, packet) outgoing;
|
|
|
|
};
|
2009-10-01 15:19:37 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
struct ssh *
|
|
|
|
ssh_alloc_session_state(void)
|
|
|
|
{
|
|
|
|
struct ssh *ssh = NULL;
|
|
|
|
struct session_state *state = NULL;
|
|
|
|
|
|
|
|
if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
|
|
|
|
(state = calloc(1, sizeof(*state))) == NULL ||
|
|
|
|
(state->input = sshbuf_new()) == NULL ||
|
|
|
|
(state->output = sshbuf_new()) == NULL ||
|
|
|
|
(state->outgoing_packet = sshbuf_new()) == NULL ||
|
|
|
|
(state->incoming_packet = sshbuf_new()) == NULL)
|
|
|
|
goto fail;
|
|
|
|
TAILQ_INIT(&state->outgoing);
|
|
|
|
TAILQ_INIT(&ssh->private_keys);
|
|
|
|
TAILQ_INIT(&ssh->public_keys);
|
|
|
|
state->connection_in = -1;
|
|
|
|
state->connection_out = -1;
|
|
|
|
state->max_packet_size = 32768;
|
|
|
|
state->packet_timeout_ms = -1;
|
|
|
|
state->p_send.packets = state->p_read.packets = 0;
|
|
|
|
state->initialized = 1;
|
|
|
|
/*
|
|
|
|
* ssh_packet_send2() needs to queue packets until
|
|
|
|
* we've done the initial key exchange.
|
|
|
|
*/
|
|
|
|
state->rekeying = 1;
|
|
|
|
ssh->state = state;
|
|
|
|
return ssh;
|
|
|
|
fail:
|
|
|
|
if (state) {
|
|
|
|
sshbuf_free(state->input);
|
|
|
|
sshbuf_free(state->output);
|
|
|
|
sshbuf_free(state->incoming_packet);
|
|
|
|
sshbuf_free(state->outgoing_packet);
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
free(ssh);
|
|
|
|
return NULL;
|
2009-10-01 15:19:37 +00:00
|
|
|
}
|
2004-01-07 11:10:17 +00:00
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
/* Returns nonzero if rekeying is in progress */
|
|
|
|
int
|
|
|
|
ssh_packet_is_rekeying(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
return compat20 &&
|
|
|
|
(ssh->state->rekeying || (ssh->kex != NULL && ssh->kex->done == 0));
|
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Sets the descriptors used for communication. Disables encryption until
|
|
|
|
* packet_set_encryption_key is called.
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
struct ssh *
|
|
|
|
ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state;
|
|
|
|
const struct sshcipher *none = cipher_by_name("none");
|
2015-01-05 16:09:55 +00:00
|
|
|
int r;
|
2002-10-29 09:43:00 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (none == NULL) {
|
|
|
|
error("%s: cannot load cipher 'none'", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (ssh == NULL)
|
|
|
|
ssh = ssh_alloc_session_state();
|
|
|
|
if (ssh == NULL) {
|
|
|
|
error("%s: cound not allocate state", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
state = ssh->state;
|
|
|
|
state->connection_in = fd_in;
|
|
|
|
state->connection_out = fd_out;
|
|
|
|
if ((r = cipher_init(&state->send_context, none,
|
2015-01-05 16:09:55 +00:00
|
|
|
(const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
|
2015-07-02 13:15:34 +00:00
|
|
|
(r = cipher_init(&state->receive_context, none,
|
|
|
|
(const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
|
|
|
|
error("%s: cipher_init failed: %s", __func__, ssh_err(r));
|
2017-01-31 12:29:48 +00:00
|
|
|
free(ssh); /* XXX need ssh_free_session_state? */
|
2015-07-02 13:15:34 +00:00
|
|
|
return NULL;
|
2008-07-23 09:33:08 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
|
|
|
|
deattack_init(&state->deattack);
|
|
|
|
/*
|
|
|
|
* Cache the IP address of the remote connection for use in error
|
|
|
|
* messages that might be generated after the connection has closed.
|
|
|
|
*/
|
|
|
|
(void)ssh_remote_ipaddr(ssh);
|
|
|
|
return ssh;
|
2008-07-23 09:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
|
2008-07-23 09:33:08 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
|
2012-08-29 15:46:01 +00:00
|
|
|
if (timeout <= 0 || count <= 0) {
|
2015-07-02 13:15:34 +00:00
|
|
|
state->packet_timeout_ms = -1;
|
2008-07-23 09:33:08 +00:00
|
|
|
return;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2008-07-23 09:33:08 +00:00
|
|
|
if ((INT_MAX / 1000) / count < timeout)
|
2015-07-02 13:15:34 +00:00
|
|
|
state->packet_timeout_ms = INT_MAX;
|
2008-07-23 09:33:08 +00:00
|
|
|
else
|
2015-07-02 13:15:34 +00:00
|
|
|
state->packet_timeout_ms = timeout * count * 1000;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_packet_stop_discard(struct ssh *ssh)
|
2009-02-24 18:49:27 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (state->packet_discard_mac) {
|
2009-02-24 18:49:27 +00:00
|
|
|
char buf[1024];
|
2017-01-31 12:29:48 +00:00
|
|
|
size_t dlen = PACKET_MAX_SIZE;
|
2015-07-02 13:15:34 +00:00
|
|
|
|
2017-01-31 12:29:48 +00:00
|
|
|
if (dlen > state->packet_discard_mac_already)
|
|
|
|
dlen -= state->packet_discard_mac_already;
|
2009-02-24 18:49:27 +00:00
|
|
|
memset(buf, 'a', sizeof(buf));
|
2017-01-31 12:29:48 +00:00
|
|
|
while (sshbuf_len(state->incoming_packet) < dlen)
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_put(state->incoming_packet, buf,
|
|
|
|
sizeof(buf))) != 0)
|
|
|
|
return r;
|
|
|
|
(void) mac_compute(state->packet_discard_mac,
|
|
|
|
state->p_read.seqnr,
|
2017-01-31 12:29:48 +00:00
|
|
|
sshbuf_ptr(state->incoming_packet), dlen,
|
2015-07-02 13:15:34 +00:00
|
|
|
NULL, 0);
|
|
|
|
}
|
2016-03-10 20:10:25 +00:00
|
|
|
logit("Finished discarding for %.200s port %d",
|
|
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
|
2015-07-02 13:15:34 +00:00
|
|
|
return SSH_ERR_MAC_INVALID;
|
2009-02-24 18:49:27 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
static int
|
|
|
|
ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
|
2017-01-31 12:29:48 +00:00
|
|
|
struct sshmac *mac, size_t mac_already, u_int discard)
|
2009-02-24 18:49:27 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
|
|
|
|
if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
|
|
|
|
return r;
|
|
|
|
return SSH_ERR_MAC_INVALID;
|
|
|
|
}
|
2017-01-31 12:29:48 +00:00
|
|
|
/*
|
|
|
|
* Record number of bytes over which the mac has already
|
|
|
|
* been computed in order to minimize timing attacks.
|
|
|
|
*/
|
|
|
|
if (mac && mac->enabled) {
|
2015-07-02 13:15:34 +00:00
|
|
|
state->packet_discard_mac = mac;
|
2017-01-31 12:29:48 +00:00
|
|
|
state->packet_discard_mac_already = mac_already;
|
|
|
|
}
|
|
|
|
if (sshbuf_len(state->input) >= discard)
|
|
|
|
return ssh_packet_stop_discard(ssh);
|
2015-07-02 13:15:34 +00:00
|
|
|
state->packet_discard = discard - sshbuf_len(state->input);
|
|
|
|
return 0;
|
2009-02-24 18:49:27 +00:00
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Returns 1 if remote host is connected via socket, 0 if not. */
|
|
|
|
|
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_connection_is_on_socket(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
2000-02-24 14:29:47 +00:00
|
|
|
struct sockaddr_storage from, to;
|
|
|
|
socklen_t fromlen, tolen;
|
|
|
|
|
2017-01-31 12:29:48 +00:00
|
|
|
if (state->connection_in == -1 || state->connection_out == -1)
|
|
|
|
return 0;
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/* filedescriptors in and out are the same, so it's a socket */
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->connection_in == state->connection_out)
|
2000-02-24 14:29:47 +00:00
|
|
|
return 1;
|
|
|
|
fromlen = sizeof(from);
|
|
|
|
memset(&from, 0, sizeof(from));
|
2015-07-02 13:15:34 +00:00
|
|
|
if (getpeername(state->connection_in, (struct sockaddr *)&from,
|
2009-10-01 15:19:37 +00:00
|
|
|
&fromlen) < 0)
|
2000-02-24 14:29:47 +00:00
|
|
|
return 0;
|
|
|
|
tolen = sizeof(to);
|
|
|
|
memset(&to, 0, sizeof(to));
|
2015-07-02 13:15:34 +00:00
|
|
|
if (getpeername(state->connection_out, (struct sockaddr *)&to,
|
2009-10-01 15:19:37 +00:00
|
|
|
&tolen) < 0)
|
2000-02-24 14:29:47 +00:00
|
|
|
return 0;
|
|
|
|
if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
|
|
|
|
return 0;
|
|
|
|
if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-06-23 14:01:54 +00:00
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
|
2002-06-23 14:01:54 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
if (ibytes)
|
|
|
|
*ibytes = ssh->state->p_read.bytes;
|
|
|
|
if (obytes)
|
|
|
|
*obytes = ssh->state->p_send.bytes;
|
2002-06-23 14:01:54 +00:00
|
|
|
}
|
2006-09-30 13:29:51 +00:00
|
|
|
|
2002-06-23 14:01:54 +00:00
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_connection_af(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_storage to;
|
|
|
|
socklen_t tolen = sizeof(to);
|
|
|
|
|
|
|
|
memset(&to, 0, sizeof(to));
|
2015-07-02 13:15:34 +00:00
|
|
|
if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
|
2009-10-01 15:19:37 +00:00
|
|
|
&tolen) < 0)
|
2000-02-24 14:29:47 +00:00
|
|
|
return 0;
|
2002-06-27 22:31:32 +00:00
|
|
|
#ifdef IPV4_IN_IPV6
|
2004-02-26 10:38:49 +00:00
|
|
|
if (to.ss_family == AF_INET6 &&
|
2002-06-27 22:31:32 +00:00
|
|
|
IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
|
2011-09-28 08:14:41 +00:00
|
|
|
return AF_INET;
|
2002-06-27 22:31:32 +00:00
|
|
|
#endif
|
2011-09-28 08:14:41 +00:00
|
|
|
return to.ss_family;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets the connection into non-blocking mode. */
|
|
|
|
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_set_nonblocking(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
|
|
|
/* Set the socket into non-blocking mode. */
|
2015-07-02 13:15:34 +00:00
|
|
|
set_nonblock(ssh->state->connection_in);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (ssh->state->connection_out != ssh->state->connection_in)
|
|
|
|
set_nonblock(ssh->state->connection_out);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the socket used for reading. */
|
|
|
|
|
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_get_connection_in(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
return ssh->state->connection_in;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the descriptor used for writing. */
|
|
|
|
|
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_get_connection_out(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
return ssh->state->connection_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the IP-address of the remote host as a string. The returned
|
|
|
|
* string must not be freed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ssh_remote_ipaddr(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2016-03-10 20:10:25 +00:00
|
|
|
const int sock = ssh->state->connection_in;
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
/* Check whether we have cached the ipaddr. */
|
2016-03-10 20:10:25 +00:00
|
|
|
if (ssh->remote_ipaddr == NULL) {
|
|
|
|
if (ssh_packet_connection_is_on_socket(ssh)) {
|
|
|
|
ssh->remote_ipaddr = get_peer_ipaddr(sock);
|
2017-01-31 12:29:48 +00:00
|
|
|
ssh->remote_port = get_peer_port(sock);
|
|
|
|
ssh->local_ipaddr = get_local_ipaddr(sock);
|
|
|
|
ssh->local_port = get_local_port(sock);
|
2016-03-10 20:10:25 +00:00
|
|
|
} else {
|
|
|
|
ssh->remote_ipaddr = strdup("UNKNOWN");
|
2017-01-31 12:29:48 +00:00
|
|
|
ssh->remote_port = 65535;
|
|
|
|
ssh->local_ipaddr = strdup("UNKNOWN");
|
|
|
|
ssh->local_port = 65535;
|
2016-03-10 20:10:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
return ssh->remote_ipaddr;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
/* Returns the port number of the remote host. */
|
|
|
|
|
|
|
|
int
|
|
|
|
ssh_remote_port(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
(void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
|
|
|
|
return ssh->remote_port;
|
|
|
|
}
|
|
|
|
|
2017-01-31 12:29:48 +00:00
|
|
|
/*
|
|
|
|
* Returns the IP-address of the local host as a string. The returned
|
|
|
|
* string must not be freed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ssh_local_ipaddr(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
(void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
|
|
|
|
return ssh->local_ipaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the port number of the local host. */
|
|
|
|
|
|
|
|
int
|
|
|
|
ssh_local_port(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
(void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
|
|
|
|
return ssh->local_port;
|
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Closes the connection and clears and frees internal data structures. */
|
|
|
|
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_close(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
int r;
|
|
|
|
u_int mode;
|
|
|
|
|
|
|
|
if (!state->initialized)
|
2000-02-24 14:29:47 +00:00
|
|
|
return;
|
2015-07-02 13:15:34 +00:00
|
|
|
state->initialized = 0;
|
|
|
|
if (state->connection_in == state->connection_out) {
|
|
|
|
shutdown(state->connection_out, SHUT_RDWR);
|
|
|
|
close(state->connection_out);
|
2000-02-24 14:29:47 +00:00
|
|
|
} else {
|
2015-07-02 13:15:34 +00:00
|
|
|
close(state->connection_in);
|
|
|
|
close(state->connection_out);
|
|
|
|
}
|
|
|
|
sshbuf_free(state->input);
|
|
|
|
sshbuf_free(state->output);
|
|
|
|
sshbuf_free(state->outgoing_packet);
|
|
|
|
sshbuf_free(state->incoming_packet);
|
|
|
|
for (mode = 0; mode < MODE_MAX; mode++)
|
|
|
|
kex_free_newkeys(state->newkeys[mode]);
|
|
|
|
if (state->compression_buffer) {
|
|
|
|
sshbuf_free(state->compression_buffer);
|
|
|
|
if (state->compression_out_started) {
|
|
|
|
z_streamp stream = &state->compression_out_stream;
|
|
|
|
debug("compress outgoing: "
|
|
|
|
"raw data %llu, compressed %llu, factor %.2f",
|
|
|
|
(unsigned long long)stream->total_in,
|
|
|
|
(unsigned long long)stream->total_out,
|
|
|
|
stream->total_in == 0 ? 0.0 :
|
|
|
|
(double) stream->total_out / stream->total_in);
|
|
|
|
if (state->compression_out_failures == 0)
|
|
|
|
deflateEnd(stream);
|
|
|
|
}
|
|
|
|
if (state->compression_in_started) {
|
|
|
|
z_streamp stream = &state->compression_out_stream;
|
|
|
|
debug("compress incoming: "
|
|
|
|
"raw data %llu, compressed %llu, factor %.2f",
|
|
|
|
(unsigned long long)stream->total_out,
|
|
|
|
(unsigned long long)stream->total_in,
|
|
|
|
stream->total_out == 0 ? 0.0 :
|
|
|
|
(double) stream->total_in / stream->total_out);
|
|
|
|
if (state->compression_in_failures == 0)
|
|
|
|
inflateEnd(stream);
|
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = cipher_cleanup(&state->send_context)) != 0)
|
|
|
|
error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
|
|
|
|
if ((r = cipher_cleanup(&state->receive_context)) != 0)
|
|
|
|
error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
|
2016-03-10 20:10:25 +00:00
|
|
|
free(ssh->remote_ipaddr);
|
|
|
|
ssh->remote_ipaddr = NULL;
|
2015-07-02 13:15:34 +00:00
|
|
|
free(ssh->state);
|
|
|
|
ssh->state = NULL;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets remote side protocol flags. */
|
|
|
|
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh->state->remote_protocol_flags = protocol_flags;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the remote protocol flags set earlier by the above function. */
|
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
u_int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_get_protocol_flags(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
return ssh->state->remote_protocol_flags;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Starts packet compression from the next packet on in both directions.
|
|
|
|
* Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
|
|
|
|
*/
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
static int
|
|
|
|
ssh_packet_init_compression(struct ssh *ssh)
|
2001-05-04 03:57:05 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
if (!ssh->state->compression_buffer &&
|
|
|
|
((ssh->state->compression_buffer = sshbuf_new()) == NULL))
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
return 0;
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
static int
|
|
|
|
start_compression_out(struct ssh *ssh, int level)
|
|
|
|
{
|
|
|
|
if (level < 1 || level > 9)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
debug("Enabling compression at level %d.", level);
|
|
|
|
if (ssh->state->compression_out_started == 1)
|
|
|
|
deflateEnd(&ssh->state->compression_out_stream);
|
|
|
|
switch (deflateInit(&ssh->state->compression_out_stream, level)) {
|
|
|
|
case Z_OK:
|
|
|
|
ssh->state->compression_out_started = 1;
|
|
|
|
break;
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
default:
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
return 0;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
static int
|
|
|
|
start_compression_in(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
if (ssh->state->compression_in_started == 1)
|
|
|
|
inflateEnd(&ssh->state->compression_in_stream);
|
|
|
|
switch (inflateInit(&ssh->state->compression_in_stream)) {
|
|
|
|
case Z_OK:
|
|
|
|
ssh->state->compression_in_started = 1;
|
|
|
|
break;
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
default:
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2002-06-23 14:01:54 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_packet_start_compression(struct ssh *ssh, int level)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-01-05 16:09:55 +00:00
|
|
|
int r;
|
2002-10-29 09:43:00 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (ssh->state->packet_compression && !compat20)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
ssh->state->packet_compression = 1;
|
|
|
|
if ((r = ssh_packet_init_compression(ssh)) != 0 ||
|
|
|
|
(r = start_compression_in(ssh)) != 0 ||
|
|
|
|
(r = start_compression_out(ssh, level)) != 0)
|
|
|
|
return r;
|
|
|
|
return 0;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
/* XXX remove need for separate compression buffer */
|
|
|
|
static int
|
|
|
|
compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
|
2002-06-23 14:01:54 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
u_char buf[4096];
|
|
|
|
int r, status;
|
2002-06-23 14:01:54 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (ssh->state->compression_out_started != 1)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2000-05-15 04:37:24 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
/* This case is not handled below. */
|
|
|
|
if (sshbuf_len(in) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Input is the contents of the input buffer. */
|
|
|
|
if ((ssh->state->compression_out_stream.next_in =
|
|
|
|
sshbuf_mutable_ptr(in)) == NULL)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
|
|
|
|
|
|
|
|
/* Loop compressing until deflate() returns with avail_out != 0. */
|
|
|
|
do {
|
|
|
|
/* Set up fixed-size output buffer. */
|
|
|
|
ssh->state->compression_out_stream.next_out = buf;
|
|
|
|
ssh->state->compression_out_stream.avail_out = sizeof(buf);
|
|
|
|
|
|
|
|
/* Compress as much data into the buffer as possible. */
|
|
|
|
status = deflate(&ssh->state->compression_out_stream,
|
|
|
|
Z_PARTIAL_FLUSH);
|
|
|
|
switch (status) {
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
case Z_OK:
|
|
|
|
/* Append compressed data to output_buffer. */
|
|
|
|
if ((r = sshbuf_put(out, buf, sizeof(buf) -
|
|
|
|
ssh->state->compression_out_stream.avail_out)) != 0)
|
|
|
|
return r;
|
|
|
|
break;
|
|
|
|
case Z_STREAM_ERROR:
|
|
|
|
default:
|
|
|
|
ssh->state->compression_out_failures++;
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
}
|
|
|
|
} while (ssh->state->compression_out_stream.avail_out == 0);
|
|
|
|
return 0;
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
static int
|
|
|
|
uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
u_char buf[4096];
|
|
|
|
int r, status;
|
2002-10-29 09:43:00 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (ssh->state->compression_in_started != 1)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2006-09-30 13:29:51 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((ssh->state->compression_in_stream.next_in =
|
|
|
|
sshbuf_mutable_ptr(in)) == NULL)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
|
2009-10-01 15:19:37 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
for (;;) {
|
|
|
|
/* Set up fixed-size output buffer. */
|
|
|
|
ssh->state->compression_in_stream.next_out = buf;
|
|
|
|
ssh->state->compression_in_stream.avail_out = sizeof(buf);
|
|
|
|
|
|
|
|
status = inflate(&ssh->state->compression_in_stream,
|
|
|
|
Z_PARTIAL_FLUSH);
|
|
|
|
switch (status) {
|
|
|
|
case Z_OK:
|
|
|
|
if ((r = sshbuf_put(out, buf, sizeof(buf) -
|
|
|
|
ssh->state->compression_in_stream.avail_out)) != 0)
|
|
|
|
return r;
|
|
|
|
break;
|
|
|
|
case Z_BUF_ERROR:
|
|
|
|
/*
|
|
|
|
* Comments in zlib.h say that we should keep calling
|
|
|
|
* inflate() until we get an error. This appears to
|
|
|
|
* be the error that we get.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
case Z_DATA_ERROR:
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
case Z_STREAM_ERROR:
|
|
|
|
default:
|
|
|
|
ssh->state->compression_in_failures++;
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2006-09-30 13:29:51 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
/* Serialise compression state into a blob for privsep */
|
|
|
|
static int
|
|
|
|
ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
struct sshbuf *b;
|
|
|
|
int r;
|
2006-09-30 13:29:51 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((b = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if (state->compression_in_started) {
|
|
|
|
if ((r = sshbuf_put_string(b, &state->compression_in_stream,
|
|
|
|
sizeof(state->compression_in_stream))) != 0)
|
|
|
|
goto out;
|
|
|
|
} else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (state->compression_out_started) {
|
|
|
|
if ((r = sshbuf_put_string(b, &state->compression_out_stream,
|
|
|
|
sizeof(state->compression_out_stream))) != 0)
|
|
|
|
goto out;
|
|
|
|
} else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = sshbuf_put_stringb(m, b);
|
|
|
|
out:
|
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deserialise compression state from a blob for privsep */
|
|
|
|
static int
|
|
|
|
ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
|
2000-05-15 04:37:24 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
int r;
|
|
|
|
const u_char *inblob, *outblob;
|
|
|
|
size_t inl, outl;
|
|
|
|
|
|
|
|
if ((r = sshbuf_froms(m, &b)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
|
|
|
|
(r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (inl == 0)
|
|
|
|
state->compression_in_started = 0;
|
|
|
|
else if (inl != sizeof(state->compression_in_stream)) {
|
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
state->compression_in_started = 1;
|
|
|
|
memcpy(&state->compression_in_stream, inblob, inl);
|
|
|
|
}
|
|
|
|
if (outl == 0)
|
|
|
|
state->compression_out_started = 0;
|
|
|
|
else if (outl != sizeof(state->compression_out_stream)) {
|
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
state->compression_out_started = 1;
|
|
|
|
memcpy(&state->compression_out_stream, outblob, outl);
|
|
|
|
}
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
2006-09-30 13:29:51 +00:00
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
|
|
|
|
void *(*allocfunc)(void *, u_int, u_int),
|
|
|
|
void (*freefunc)(void *, void *))
|
2000-05-15 04:37:24 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
|
|
|
|
ssh->state->compression_out_stream.zfree = (free_func)freefunc;
|
|
|
|
ssh->state->compression_out_stream.opaque = ctx;
|
|
|
|
ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
|
|
|
|
ssh->state->compression_in_stream.zfree = (free_func)freefunc;
|
|
|
|
ssh->state->compression_in_stream.opaque = ctx;
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
2006-09-30 13:29:51 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
/*
|
|
|
|
* Causes any further packets to be encrypted using the given key. The same
|
|
|
|
* key is used for both sending and reception. However, both directions are
|
|
|
|
* encrypted independently of each other.
|
|
|
|
*/
|
2006-09-30 13:29:51 +00:00
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
|
2000-05-15 04:37:24 +00:00
|
|
|
{
|
2015-07-02 13:18:50 +00:00
|
|
|
#ifndef WITH_SSH1
|
|
|
|
fatal("no SSH protocol 1 support");
|
|
|
|
#else /* WITH_SSH1 */
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
const struct sshcipher *cipher = cipher_by_number(number);
|
|
|
|
int r;
|
|
|
|
const char *wmsg;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (cipher == NULL)
|
|
|
|
fatal("%s: unknown cipher number %d", __func__, number);
|
|
|
|
if (keylen < 20)
|
|
|
|
fatal("%s: keylen too small: %d", __func__, keylen);
|
|
|
|
if (keylen > SSH_SESSION_KEY_LENGTH)
|
|
|
|
fatal("%s: keylen too big: %d", __func__, keylen);
|
|
|
|
memcpy(state->ssh1_key, key, keylen);
|
|
|
|
state->ssh1_keylen = keylen;
|
|
|
|
if ((r = cipher_init(&state->send_context, cipher, key, keylen,
|
|
|
|
NULL, 0, CIPHER_ENCRYPT)) != 0 ||
|
|
|
|
(r = cipher_init(&state->receive_context, cipher, key, keylen,
|
|
|
|
NULL, 0, CIPHER_DECRYPT) != 0))
|
|
|
|
fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
|
|
|
|
if (!state->cipher_warning_done &&
|
|
|
|
((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
|
|
|
|
(wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
|
|
|
|
error("Warning: %s", wmsg);
|
|
|
|
state->cipher_warning_done = 1;
|
|
|
|
}
|
|
|
|
#endif /* WITH_SSH1 */
|
2011-02-17 11:47:40 +00:00
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Finalizes and sends the packet. If the encryption key has been set,
|
|
|
|
* encrypts the packet before sending.
|
|
|
|
*/
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_packet_send1(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
2002-03-18 09:55:03 +00:00
|
|
|
u_char buf[8], *cp;
|
2015-07-02 13:15:34 +00:00
|
|
|
int r, padding, len;
|
2001-05-04 03:57:05 +00:00
|
|
|
u_int checksum;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If using packet compression, compress the payload of the outgoing
|
|
|
|
* packet.
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->packet_compression) {
|
|
|
|
sshbuf_reset(state->compression_buffer);
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Skip padding. */
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
|
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
/* padding */
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_put(state->compression_buffer,
|
|
|
|
"\0\0\0\0\0\0\0\0", 8)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = compress_buffer(ssh, state->outgoing_packet,
|
|
|
|
state->compression_buffer)) != 0)
|
|
|
|
goto out;
|
|
|
|
sshbuf_reset(state->outgoing_packet);
|
|
|
|
if ((r = sshbuf_putb(state->outgoing_packet,
|
|
|
|
state->compression_buffer)) != 0)
|
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
/* Compute packet length without padding (add checksum, remove padding). */
|
2015-07-02 13:15:34 +00:00
|
|
|
len = sshbuf_len(state->outgoing_packet) + 4 - 8;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
/* Insert padding. Initialized to zero in packet_start1() */
|
2000-02-24 14:29:47 +00:00
|
|
|
padding = 8 - len % 8;
|
2015-07-02 13:15:34 +00:00
|
|
|
if (!state->send_context.plaintext) {
|
|
|
|
cp = sshbuf_mutable_ptr(state->outgoing_packet);
|
|
|
|
if (cp == NULL) {
|
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
arc4random_buf(cp + 8 - padding, padding);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
|
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* Add check bytes. */
|
2015-07-02 13:15:34 +00:00
|
|
|
checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
|
|
|
|
sshbuf_len(state->outgoing_packet));
|
|
|
|
POKE_U32(buf, checksum);
|
|
|
|
if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
|
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
fprintf(stderr, "packet_send plain: ");
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_dump(state->outgoing_packet, stderr);
|
2000-02-24 14:29:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Append to output. */
|
2015-07-02 13:15:34 +00:00
|
|
|
POKE_U32(buf, len);
|
|
|
|
if ((r = sshbuf_put(state->output, buf, 4)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_reserve(state->output,
|
|
|
|
sshbuf_len(state->outgoing_packet), &cp)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = cipher_crypt(&state->send_context, 0, cp,
|
|
|
|
sshbuf_ptr(state->outgoing_packet),
|
|
|
|
sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
|
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
fprintf(stderr, "encrypted: ");
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_dump(state->output, stderr);
|
2000-02-24 14:29:47 +00:00
|
|
|
#endif
|
2015-07-02 13:15:34 +00:00
|
|
|
state->p_send.packets++;
|
|
|
|
state->p_send.bytes += len +
|
|
|
|
sshbuf_len(state->outgoing_packet);
|
|
|
|
sshbuf_reset(state->outgoing_packet);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/*
|
2006-03-22 19:46:12 +00:00
|
|
|
* Note that the packet is now only buffered in output. It won't be
|
2015-07-02 13:15:34 +00:00
|
|
|
* actually sent until ssh_packet_write_wait or ssh_packet_write_poll
|
|
|
|
* is called.
|
2000-02-24 14:29:47 +00:00
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
return r;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_set_newkeys(struct ssh *ssh, int mode)
|
2001-05-04 03:57:05 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
struct sshenc *enc;
|
|
|
|
struct sshmac *mac;
|
|
|
|
struct sshcomp *comp;
|
|
|
|
struct sshcipher_ctx *cc;
|
2004-01-07 11:10:17 +00:00
|
|
|
u_int64_t *max_blocks;
|
2015-07-02 13:15:34 +00:00
|
|
|
const char *wmsg;
|
2015-01-05 16:09:55 +00:00
|
|
|
int r, crypt_type;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2003-04-23 16:53:02 +00:00
|
|
|
debug2("set_newkeys: mode %d", mode);
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
if (mode == MODE_OUT) {
|
2015-07-02 13:15:34 +00:00
|
|
|
cc = &state->send_context;
|
2004-10-28 16:03:53 +00:00
|
|
|
crypt_type = CIPHER_ENCRYPT;
|
2015-07-02 13:15:34 +00:00
|
|
|
state->p_send.packets = state->p_send.blocks = 0;
|
|
|
|
max_blocks = &state->max_blocks_out;
|
2002-03-18 09:55:03 +00:00
|
|
|
} else {
|
2015-07-02 13:15:34 +00:00
|
|
|
cc = &state->receive_context;
|
2004-10-28 16:03:53 +00:00
|
|
|
crypt_type = CIPHER_DECRYPT;
|
2015-07-02 13:15:34 +00:00
|
|
|
state->p_read.packets = state->p_read.blocks = 0;
|
|
|
|
max_blocks = &state->max_blocks_in;
|
2002-03-18 09:55:03 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->newkeys[mode] != NULL) {
|
2016-03-10 20:10:25 +00:00
|
|
|
debug("set_newkeys: rekeying, input %llu bytes %llu blocks, "
|
|
|
|
"output %llu bytes %llu blocks",
|
|
|
|
(unsigned long long)state->p_read.bytes,
|
|
|
|
(unsigned long long)state->p_read.blocks,
|
|
|
|
(unsigned long long)state->p_send.bytes,
|
|
|
|
(unsigned long long)state->p_send.blocks);
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = cipher_cleanup(cc)) != 0)
|
|
|
|
return r;
|
|
|
|
enc = &state->newkeys[mode]->enc;
|
|
|
|
mac = &state->newkeys[mode]->mac;
|
|
|
|
comp = &state->newkeys[mode]->comp;
|
2008-07-23 09:23:42 +00:00
|
|
|
mac_clear(mac);
|
2014-03-22 15:23:38 +00:00
|
|
|
explicit_bzero(enc->iv, enc->iv_len);
|
|
|
|
explicit_bzero(enc->key, enc->key_len);
|
|
|
|
explicit_bzero(mac->key, mac->key_len);
|
2013-09-18 17:27:38 +00:00
|
|
|
free(enc->name);
|
|
|
|
free(enc->iv);
|
|
|
|
free(enc->key);
|
|
|
|
free(mac->name);
|
|
|
|
free(mac->key);
|
|
|
|
free(comp->name);
|
2015-07-02 13:15:34 +00:00
|
|
|
free(state->newkeys[mode]);
|
|
|
|
}
|
|
|
|
/* move newkeys from kex to state */
|
|
|
|
if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
ssh->kex->newkeys[mode] = NULL;
|
|
|
|
enc = &state->newkeys[mode]->enc;
|
|
|
|
mac = &state->newkeys[mode]->mac;
|
|
|
|
comp = &state->newkeys[mode]->comp;
|
|
|
|
if (cipher_authlen(enc->cipher) == 0) {
|
|
|
|
if ((r = mac_init(mac)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
mac->enabled = 1;
|
2001-05-04 03:57:05 +00:00
|
|
|
DBG(debug("cipher_init_context: %d", mode));
|
2015-01-05 16:09:55 +00:00
|
|
|
if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
|
|
|
|
enc->iv, enc->iv_len, crypt_type)) != 0)
|
2015-07-02 13:15:34 +00:00
|
|
|
return r;
|
|
|
|
if (!state->cipher_warning_done &&
|
|
|
|
(wmsg = cipher_warning_message(cc)) != NULL) {
|
|
|
|
error("Warning: %s", wmsg);
|
|
|
|
state->cipher_warning_done = 1;
|
|
|
|
}
|
2002-06-23 14:01:54 +00:00
|
|
|
/* Deleting the keys does not gain extra security */
|
2014-03-22 15:23:38 +00:00
|
|
|
/* explicit_bzero(enc->iv, enc->block_size);
|
|
|
|
explicit_bzero(enc->key, enc->key_len);
|
|
|
|
explicit_bzero(mac->key, mac->key_len); */
|
2005-09-03 06:59:33 +00:00
|
|
|
if ((comp->type == COMP_ZLIB ||
|
2009-10-01 15:19:37 +00:00
|
|
|
(comp->type == COMP_DELAYED &&
|
2015-07-02 13:15:34 +00:00
|
|
|
state->after_authentication)) && comp->enabled == 0) {
|
|
|
|
if ((r = ssh_packet_init_compression(ssh)) < 0)
|
|
|
|
return r;
|
|
|
|
if (mode == MODE_OUT) {
|
|
|
|
if ((r = start_compression_out(ssh, 6)) != 0)
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
if ((r = start_compression_in(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
2001-05-04 03:57:05 +00:00
|
|
|
comp->enabled = 1;
|
|
|
|
}
|
2004-01-07 11:10:17 +00:00
|
|
|
/*
|
|
|
|
* The 2^(blocksize*2) limit is too expensive for 3DES,
|
|
|
|
* blowfish, etc, so enforce a 1GB limit for small blocksizes.
|
|
|
|
*/
|
|
|
|
if (enc->block_size >= 16)
|
|
|
|
*max_blocks = (u_int64_t)1 << (enc->block_size*2);
|
|
|
|
else
|
|
|
|
*max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->rekey_limit)
|
2009-10-01 15:19:37 +00:00
|
|
|
*max_blocks = MIN(*max_blocks,
|
2015-07-02 13:15:34 +00:00
|
|
|
state->rekey_limit / enc->block_size);
|
2016-03-10 20:10:25 +00:00
|
|
|
debug("rekey after %llu blocks", (unsigned long long)*max_blocks);
|
2015-07-02 13:15:34 +00:00
|
|
|
return 0;
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
#define MAX_PACKETS (1U<<31)
|
|
|
|
static int
|
|
|
|
ssh_packet_need_rekeying(struct ssh *ssh, u_int outbound_packet_len)
|
|
|
|
{
|
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
u_int32_t out_blocks;
|
|
|
|
|
|
|
|
/* XXX client can't cope with rekeying pre-auth */
|
|
|
|
if (!state->after_authentication)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Haven't keyed yet or KEX in progress. */
|
|
|
|
if (ssh->kex == NULL || ssh_packet_is_rekeying(ssh))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Peer can't rekey */
|
|
|
|
if (ssh->compat & SSH_BUG_NOREKEY)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Permit one packet in or out per rekey - this allows us to
|
|
|
|
* make progress when rekey limits are very small.
|
|
|
|
*/
|
|
|
|
if (state->p_send.packets == 0 && state->p_read.packets == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Time-based rekeying */
|
|
|
|
if (state->rekey_interval != 0 &&
|
|
|
|
state->rekey_time + state->rekey_interval <= monotime())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Always rekey when MAX_PACKETS sent in either direction */
|
|
|
|
if (state->p_send.packets > MAX_PACKETS ||
|
|
|
|
state->p_read.packets > MAX_PACKETS)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Rekey after (cipher-specific) maxiumum blocks */
|
|
|
|
out_blocks = roundup(outbound_packet_len,
|
|
|
|
state->newkeys[MODE_OUT]->enc.block_size);
|
|
|
|
return (state->max_blocks_out &&
|
|
|
|
(state->p_send.blocks + out_blocks > state->max_blocks_out)) ||
|
|
|
|
(state->max_blocks_in &&
|
|
|
|
(state->p_read.blocks > state->max_blocks_in));
|
|
|
|
}
|
|
|
|
|
2005-09-03 06:59:33 +00:00
|
|
|
/*
|
|
|
|
* Delayed compression for SSH2 is enabled after authentication:
|
2006-09-30 13:29:51 +00:00
|
|
|
* This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
|
2005-09-03 06:59:33 +00:00
|
|
|
* and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
static int
|
|
|
|
ssh_packet_enable_delayed_compress(struct ssh *ssh)
|
2005-09-03 06:59:33 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
struct sshcomp *comp = NULL;
|
|
|
|
int r, mode;
|
2005-09-03 06:59:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remember that we are past the authentication step, so rekeying
|
|
|
|
* with COMP_DELAYED will turn on compression immediately.
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
state->after_authentication = 1;
|
2005-09-03 06:59:33 +00:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2006-09-30 13:29:51 +00:00
|
|
|
/* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->newkeys[mode] == NULL)
|
2006-09-30 13:29:51 +00:00
|
|
|
continue;
|
2015-07-02 13:15:34 +00:00
|
|
|
comp = &state->newkeys[mode]->comp;
|
2005-09-03 06:59:33 +00:00
|
|
|
if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = ssh_packet_init_compression(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
if (mode == MODE_OUT) {
|
|
|
|
if ((r = start_compression_out(ssh, 6)) != 0)
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
if ((r = start_compression_in(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
2005-09-03 06:59:33 +00:00
|
|
|
comp->enabled = 1;
|
|
|
|
}
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
return 0;
|
2005-09-03 06:59:33 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
/* Used to mute debug logging for noisy packet types */
|
|
|
|
static int
|
|
|
|
ssh_packet_log_type(u_char type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case SSH2_MSG_CHANNEL_DATA:
|
|
|
|
case SSH2_MSG_CHANNEL_EXTENDED_DATA:
|
|
|
|
case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
/*
|
|
|
|
* Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_packet_send2_wrapped(struct ssh *ssh)
|
2000-05-15 04:37:24 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
|
2017-01-31 12:29:48 +00:00
|
|
|
u_char tmp, padlen, pad = 0;
|
2015-07-02 13:15:34 +00:00
|
|
|
u_int authlen = 0, aadlen = 0;
|
|
|
|
u_int len;
|
|
|
|
struct sshenc *enc = NULL;
|
|
|
|
struct sshmac *mac = NULL;
|
|
|
|
struct sshcomp *comp = NULL;
|
|
|
|
int r, block_size;
|
|
|
|
|
|
|
|
if (state->newkeys[MODE_OUT] != NULL) {
|
|
|
|
enc = &state->newkeys[MODE_OUT]->enc;
|
|
|
|
mac = &state->newkeys[MODE_OUT]->mac;
|
|
|
|
comp = &state->newkeys[MODE_OUT]->comp;
|
2013-03-22 11:19:48 +00:00
|
|
|
/* disable mac for authenticated encryption */
|
|
|
|
if ((authlen = cipher_authlen(enc->cipher)) != 0)
|
|
|
|
mac = NULL;
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
2002-03-18 09:55:03 +00:00
|
|
|
block_size = enc ? enc->block_size : 8;
|
2013-03-22 11:19:48 +00:00
|
|
|
aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
|
2000-05-15 04:37:24 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
type = (sshbuf_ptr(state->outgoing_packet))[5];
|
2016-03-10 20:10:25 +00:00
|
|
|
if (ssh_packet_log_type(type))
|
|
|
|
debug3("send packet: type %u", type);
|
2000-05-15 04:37:24 +00:00
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
fprintf(stderr, "plain: ");
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_dump(state->outgoing_packet, stderr);
|
2000-05-15 04:37:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (comp && comp->enabled) {
|
2015-07-02 13:15:34 +00:00
|
|
|
len = sshbuf_len(state->outgoing_packet);
|
2000-05-15 04:37:24 +00:00
|
|
|
/* skip header, compress only payload */
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
|
|
|
|
goto out;
|
|
|
|
sshbuf_reset(state->compression_buffer);
|
|
|
|
if ((r = compress_buffer(ssh, state->outgoing_packet,
|
|
|
|
state->compression_buffer)) != 0)
|
|
|
|
goto out;
|
|
|
|
sshbuf_reset(state->outgoing_packet);
|
|
|
|
if ((r = sshbuf_put(state->outgoing_packet,
|
|
|
|
"\0\0\0\0\0", 5)) != 0 ||
|
|
|
|
(r = sshbuf_putb(state->outgoing_packet,
|
|
|
|
state->compression_buffer)) != 0)
|
|
|
|
goto out;
|
|
|
|
DBG(debug("compression: raw %d compressed %zd", len,
|
|
|
|
sshbuf_len(state->outgoing_packet)));
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sizeof (packet_len + pad_len + payload) */
|
2015-07-02 13:15:34 +00:00
|
|
|
len = sshbuf_len(state->outgoing_packet);
|
2000-05-15 04:37:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* calc size of padding, alloc space, get random data,
|
|
|
|
* minimum padding is 4 bytes
|
|
|
|
*/
|
2013-03-22 11:19:48 +00:00
|
|
|
len -= aadlen; /* packet length is not encrypted for EtM modes */
|
2000-05-15 04:37:24 +00:00
|
|
|
padlen = block_size - (len % block_size);
|
|
|
|
if (padlen < 4)
|
|
|
|
padlen += block_size;
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->extra_pad) {
|
2017-01-31 12:29:48 +00:00
|
|
|
tmp = state->extra_pad;
|
2015-07-02 13:15:34 +00:00
|
|
|
state->extra_pad =
|
|
|
|
roundup(state->extra_pad, block_size);
|
2017-01-31 12:29:48 +00:00
|
|
|
/* check if roundup overflowed */
|
|
|
|
if (state->extra_pad < tmp)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
tmp = (len + padlen) % state->extra_pad;
|
|
|
|
/* Check whether pad calculation below will underflow */
|
|
|
|
if (tmp > state->extra_pad)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
pad = state->extra_pad - tmp;
|
2015-01-05 16:09:55 +00:00
|
|
|
DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
|
2015-07-02 13:15:34 +00:00
|
|
|
__func__, pad, len, padlen, state->extra_pad));
|
2017-01-31 12:29:48 +00:00
|
|
|
tmp = padlen;
|
2002-03-18 09:55:03 +00:00
|
|
|
padlen += pad;
|
2017-01-31 12:29:48 +00:00
|
|
|
/* Check whether padlen calculation overflowed */
|
|
|
|
if (padlen < tmp)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT; /* overflow */
|
2015-07-02 13:15:34 +00:00
|
|
|
state->extra_pad = 0;
|
2002-03-18 09:55:03 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (enc && !state->send_context.plaintext) {
|
2000-05-15 04:37:24 +00:00
|
|
|
/* random padding */
|
2015-07-02 13:15:34 +00:00
|
|
|
arc4random_buf(cp, padlen);
|
2000-05-15 04:37:24 +00:00
|
|
|
} else {
|
|
|
|
/* clear padding */
|
2014-03-22 15:23:38 +00:00
|
|
|
explicit_bzero(cp, padlen);
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
2013-03-22 11:19:48 +00:00
|
|
|
/* sizeof (packet_len + pad_len + payload + padding) */
|
2015-07-02 13:15:34 +00:00
|
|
|
len = sshbuf_len(state->outgoing_packet);
|
|
|
|
cp = sshbuf_mutable_ptr(state->outgoing_packet);
|
|
|
|
if (cp == NULL) {
|
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-03-22 11:19:48 +00:00
|
|
|
/* packet_length includes payload, padding and padding length field */
|
2015-07-02 13:15:34 +00:00
|
|
|
POKE_U32(cp, len - 4);
|
2002-03-18 09:55:03 +00:00
|
|
|
cp[4] = padlen;
|
2013-03-22 11:19:48 +00:00
|
|
|
DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
|
|
|
|
len, padlen, aadlen));
|
2000-05-15 04:37:24 +00:00
|
|
|
|
|
|
|
/* compute MAC over seqnr and packet(length fields, payload, padding) */
|
2013-03-22 11:19:48 +00:00
|
|
|
if (mac && mac->enabled && !mac->etm) {
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = mac_compute(mac, state->p_send.seqnr,
|
|
|
|
sshbuf_ptr(state->outgoing_packet), len,
|
|
|
|
macbuf, sizeof(macbuf))) != 0)
|
|
|
|
goto out;
|
|
|
|
DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
|
|
|
/* encrypt packet and append to output buffer. */
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_reserve(state->output,
|
|
|
|
sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
|
|
|
|
sshbuf_ptr(state->outgoing_packet),
|
|
|
|
len - aadlen, aadlen, authlen)) != 0)
|
|
|
|
goto out;
|
2000-05-15 04:37:24 +00:00
|
|
|
/* append unencrypted MAC */
|
2013-03-22 11:19:48 +00:00
|
|
|
if (mac && mac->enabled) {
|
|
|
|
if (mac->etm) {
|
|
|
|
/* EtM: compute mac over aadlen + cipher text */
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = mac_compute(mac, state->p_send.seqnr,
|
|
|
|
cp, len, macbuf, sizeof(macbuf))) != 0)
|
|
|
|
goto out;
|
2013-03-22 11:19:48 +00:00
|
|
|
DBG(debug("done calc MAC(EtM) out #%d",
|
2015-07-02 13:15:34 +00:00
|
|
|
state->p_send.seqnr));
|
2013-03-22 11:19:48 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
|
|
|
|
goto out;
|
2013-03-22 11:19:48 +00:00
|
|
|
}
|
2000-05-15 04:37:24 +00:00
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
fprintf(stderr, "encrypted: ");
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_dump(state->output, stderr);
|
2000-05-15 04:37:24 +00:00
|
|
|
#endif
|
|
|
|
/* increment sequence number for outgoing packets */
|
2015-07-02 13:15:34 +00:00
|
|
|
if (++state->p_send.seqnr == 0)
|
2004-01-07 11:10:17 +00:00
|
|
|
logit("outgoing seqnr wraps around");
|
2015-07-02 13:15:34 +00:00
|
|
|
if (++state->p_send.packets == 0)
|
|
|
|
if (!(ssh->compat & SSH_BUG_NOREKEY))
|
|
|
|
return SSH_ERR_NEED_REKEY;
|
|
|
|
state->p_send.blocks += len / block_size;
|
|
|
|
state->p_send.bytes += len;
|
|
|
|
sshbuf_reset(state->outgoing_packet);
|
2000-05-15 04:37:24 +00:00
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
if (type == SSH2_MSG_NEWKEYS)
|
2015-07-02 13:15:34 +00:00
|
|
|
r = ssh_set_newkeys(ssh, MODE_OUT);
|
|
|
|
else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
|
|
|
|
r = ssh_packet_enable_delayed_compress(ssh);
|
|
|
|
else
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
return r;
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
/* returns non-zero if the specified packet type is usec by KEX */
|
|
|
|
static int
|
|
|
|
ssh_packet_type_is_kex(u_char type)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
type >= SSH2_MSG_TRANSPORT_MIN &&
|
|
|
|
type <= SSH2_MSG_TRANSPORT_MAX &&
|
|
|
|
type != SSH2_MSG_SERVICE_REQUEST &&
|
|
|
|
type != SSH2_MSG_SERVICE_ACCEPT &&
|
|
|
|
type != SSH2_MSG_EXT_INFO;
|
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_packet_send2(struct ssh *ssh)
|
2004-01-07 11:10:17 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
2004-01-07 11:10:17 +00:00
|
|
|
struct packet *p;
|
2015-07-02 13:15:34 +00:00
|
|
|
u_char type;
|
2016-03-10 20:10:25 +00:00
|
|
|
int r, need_rekey;
|
2004-01-07 11:10:17 +00:00
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
if (sshbuf_len(state->outgoing_packet) < 6)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2015-07-02 13:15:34 +00:00
|
|
|
type = sshbuf_ptr(state->outgoing_packet)[5];
|
2016-03-10 20:10:25 +00:00
|
|
|
need_rekey = !ssh_packet_type_is_kex(type) &&
|
|
|
|
ssh_packet_need_rekeying(ssh, sshbuf_len(state->outgoing_packet));
|
2004-01-07 11:10:17 +00:00
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
/*
|
|
|
|
* During rekeying we can only send key exchange messages.
|
|
|
|
* Queue everything else.
|
|
|
|
*/
|
|
|
|
if ((need_rekey || state->rekeying) && !ssh_packet_type_is_kex(type)) {
|
|
|
|
if (need_rekey)
|
|
|
|
debug3("%s: rekex triggered", __func__);
|
|
|
|
debug("enqueue packet: %u", type);
|
|
|
|
p = calloc(1, sizeof(*p));
|
|
|
|
if (p == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
p->type = type;
|
|
|
|
p->payload = state->outgoing_packet;
|
|
|
|
TAILQ_INSERT_TAIL(&state->outgoing, p, next);
|
|
|
|
state->outgoing_packet = sshbuf_new();
|
|
|
|
if (state->outgoing_packet == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if (need_rekey) {
|
|
|
|
/*
|
|
|
|
* This packet triggered a rekey, so send the
|
|
|
|
* KEXINIT now.
|
|
|
|
* NB. reenters this function via kex_start_rekex().
|
|
|
|
*/
|
|
|
|
return kex_start_rekex(ssh);
|
2004-01-07 11:10:17 +00:00
|
|
|
}
|
2016-03-10 20:10:25 +00:00
|
|
|
return 0;
|
2004-01-07 11:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* rekeying starts with sending KEXINIT */
|
|
|
|
if (type == SSH2_MSG_KEXINIT)
|
2015-07-02 13:15:34 +00:00
|
|
|
state->rekeying = 1;
|
2004-01-07 11:10:17 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
|
|
|
|
return r;
|
2004-01-07 11:10:17 +00:00
|
|
|
|
|
|
|
/* after a NEWKEYS message we can send the complete queue */
|
|
|
|
if (type == SSH2_MSG_NEWKEYS) {
|
2015-07-02 13:15:34 +00:00
|
|
|
state->rekeying = 0;
|
|
|
|
state->rekey_time = monotime();
|
|
|
|
while ((p = TAILQ_FIRST(&state->outgoing))) {
|
2004-01-07 11:10:17 +00:00
|
|
|
type = p->type;
|
2016-03-10 20:10:25 +00:00
|
|
|
/*
|
|
|
|
* If this packet triggers a rekex, then skip the
|
|
|
|
* remaining packets in the queue for now.
|
|
|
|
* NB. re-enters this function via kex_start_rekex.
|
|
|
|
*/
|
|
|
|
if (ssh_packet_need_rekeying(ssh,
|
|
|
|
sshbuf_len(p->payload))) {
|
|
|
|
debug3("%s: queued packet triggered rekex",
|
|
|
|
__func__);
|
|
|
|
return kex_start_rekex(ssh);
|
|
|
|
}
|
2004-01-07 11:10:17 +00:00
|
|
|
debug("dequeue packet: %u", type);
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_free(state->outgoing_packet);
|
|
|
|
state->outgoing_packet = p->payload;
|
|
|
|
TAILQ_REMOVE(&state->outgoing, p, next);
|
2016-03-10 20:10:25 +00:00
|
|
|
memset(p, 0, sizeof(*p));
|
2013-09-18 17:27:38 +00:00
|
|
|
free(p);
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
|
|
|
|
return r;
|
2004-01-07 11:10:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
return 0;
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Waits until a packet has been received, and returns its type. Note that
|
|
|
|
* no other data is processed until this returns, so this function should not
|
|
|
|
* be used during the interactive session.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
2016-03-10 20:10:25 +00:00
|
|
|
int len, r, ms_remain;
|
2001-05-04 03:57:05 +00:00
|
|
|
fd_set *setp;
|
2000-02-24 14:29:47 +00:00
|
|
|
char buf[8192];
|
2008-07-23 09:33:08 +00:00
|
|
|
struct timeval timeout, start, *timeoutp = NULL;
|
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
DBG(debug("packet_read()"));
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2015-08-26 09:27:05 +00:00
|
|
|
setp = calloc(howmany(state->connection_in + 1,
|
2009-10-01 15:19:37 +00:00
|
|
|
NFDBITS), sizeof(fd_mask));
|
2015-07-02 13:15:34 +00:00
|
|
|
if (setp == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
/*
|
|
|
|
* Since we are blocking, ensure that all written packets have
|
|
|
|
* been sent.
|
|
|
|
*/
|
|
|
|
if ((r = ssh_packet_write_wait(ssh)) != 0)
|
2015-07-02 13:18:50 +00:00
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* Stay in the loop until we have received a complete packet. */
|
|
|
|
for (;;) {
|
|
|
|
/* Try to read a packet from the buffer. */
|
2015-07-02 13:15:34 +00:00
|
|
|
r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
|
|
|
|
if (r != 0)
|
|
|
|
break;
|
2002-03-18 09:55:03 +00:00
|
|
|
if (!compat20 && (
|
2015-07-02 13:15:34 +00:00
|
|
|
*typep == SSH_SMSG_SUCCESS
|
|
|
|
|| *typep == SSH_SMSG_FAILURE
|
|
|
|
|| *typep == SSH_CMSG_EOF
|
|
|
|
|| *typep == SSH_CMSG_EXIT_CONFIRMATION))
|
|
|
|
if ((r = sshpkt_get_end(ssh)) != 0)
|
|
|
|
break;
|
2000-02-24 14:29:47 +00:00
|
|
|
/* If we got a packet, return it. */
|
2015-07-02 13:15:34 +00:00
|
|
|
if (*typep != SSH_MSG_NONE)
|
|
|
|
break;
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Otherwise, wait for some data to arrive, add it to the
|
|
|
|
* buffer, and try again.
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
memset(setp, 0, howmany(state->connection_in + 1,
|
2009-10-01 15:19:37 +00:00
|
|
|
NFDBITS) * sizeof(fd_mask));
|
2015-07-02 13:15:34 +00:00
|
|
|
FD_SET(state->connection_in, setp);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->packet_timeout_ms > 0) {
|
|
|
|
ms_remain = state->packet_timeout_ms;
|
2008-07-23 09:33:08 +00:00
|
|
|
timeoutp = &timeout;
|
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Wait for some data to arrive. */
|
2008-07-23 09:33:08 +00:00
|
|
|
for (;;) {
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->packet_timeout_ms != -1) {
|
2008-07-23 09:33:08 +00:00
|
|
|
ms_to_timeval(&timeout, ms_remain);
|
|
|
|
gettimeofday(&start, NULL);
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = select(state->connection_in + 1, setp,
|
2009-10-01 15:19:37 +00:00
|
|
|
NULL, NULL, timeoutp)) >= 0)
|
2008-07-23 09:33:08 +00:00
|
|
|
break;
|
2009-10-01 15:19:37 +00:00
|
|
|
if (errno != EAGAIN && errno != EINTR &&
|
2008-07-23 09:33:08 +00:00
|
|
|
errno != EWOULDBLOCK)
|
|
|
|
break;
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->packet_timeout_ms == -1)
|
2008-07-23 09:33:08 +00:00
|
|
|
continue;
|
|
|
|
ms_subtract_diff(&start, &ms_remain);
|
|
|
|
if (ms_remain <= 0) {
|
2015-07-02 13:15:34 +00:00
|
|
|
r = 0;
|
2008-07-23 09:33:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
if (r == 0)
|
|
|
|
return SSH_ERR_CONN_TIMEOUT;
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Read data from the socket. */
|
2016-03-10 20:10:25 +00:00
|
|
|
len = read(state->connection_in, buf, sizeof(buf));
|
2015-07-02 13:18:50 +00:00
|
|
|
if (len == 0) {
|
|
|
|
r = SSH_ERR_CONN_CLOSED;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (len < 0) {
|
|
|
|
r = SSH_ERR_SYSTEM_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Append it to the buffer. */
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
|
2015-07-02 13:18:50 +00:00
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2015-07-02 13:18:50 +00:00
|
|
|
out:
|
2015-07-02 13:15:34 +00:00
|
|
|
free(setp);
|
|
|
|
return r;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_read(struct ssh *ssh)
|
2002-03-18 09:55:03 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
u_char type;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(r));
|
|
|
|
return type;
|
2002-03-18 09:55:03 +00:00
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Waits until a packet has been received, verifies that its type matches
|
|
|
|
* that given, and gives a fatal error and exits if there is a mismatch.
|
|
|
|
*/
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
int r;
|
|
|
|
u_char type;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
|
|
|
|
return r;
|
|
|
|
if (type != expected_type) {
|
|
|
|
if ((r = sshpkt_disconnect(ssh,
|
|
|
|
"Protocol error: expected packet type %d, got %d",
|
|
|
|
expected_type, type)) != 0)
|
|
|
|
return r;
|
|
|
|
return SSH_ERR_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
return 0;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Checks if a full packet is available in the data received so far via
|
|
|
|
* packet_process_incoming. If so, reads the packet; otherwise returns
|
|
|
|
* SSH_MSG_NONE. This does not wait for data from the connection.
|
|
|
|
*
|
|
|
|
* SSH_MSG_DISCONNECT is handled specially here. Also,
|
|
|
|
* SSH_MSG_IGNORE messages are skipped by this function and are never returned
|
|
|
|
* to higher levels.
|
|
|
|
*/
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
2001-05-04 03:57:05 +00:00
|
|
|
u_int len, padded_len;
|
2015-07-02 13:15:34 +00:00
|
|
|
const char *emsg;
|
|
|
|
const u_char *cp;
|
|
|
|
u_char *p;
|
2001-05-04 03:57:05 +00:00
|
|
|
u_int checksum, stored_checksum;
|
2015-07-02 13:15:34 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
*typep = SSH_MSG_NONE;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* Check if input size is less than minimum packet size. */
|
2015-07-02 13:15:34 +00:00
|
|
|
if (sshbuf_len(state->input) < 4 + 8)
|
|
|
|
return 0;
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Get length of incoming packet. */
|
2015-07-02 13:15:34 +00:00
|
|
|
len = PEEK_U32(sshbuf_ptr(state->input));
|
|
|
|
if (len < 1 + 2 + 2 || len > 256 * 1024) {
|
|
|
|
if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
|
|
|
|
len)) != 0)
|
|
|
|
return r;
|
|
|
|
return SSH_ERR_CONN_CORRUPT;
|
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
padded_len = (len + 8) & ~7;
|
|
|
|
|
|
|
|
/* Check if the packet has been entirely received. */
|
2015-07-02 13:15:34 +00:00
|
|
|
if (sshbuf_len(state->input) < 4 + padded_len)
|
|
|
|
return 0;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* The entire packet is in buffer. */
|
|
|
|
|
|
|
|
/* Consume packet length. */
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_consume(state->input, 4)) != 0)
|
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
/*
|
|
|
|
* Cryptographic attack detector for ssh
|
|
|
|
* (C)1998 CORE-SDI, Buenos Aires Argentina
|
|
|
|
* Ariel Futoransky(futo@core-sdi.com)
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
if (!state->receive_context.plaintext) {
|
|
|
|
emsg = NULL;
|
|
|
|
switch (detect_attack(&state->deattack,
|
|
|
|
sshbuf_ptr(state->input), padded_len)) {
|
|
|
|
case DEATTACK_OK:
|
|
|
|
break;
|
2006-09-30 13:29:51 +00:00
|
|
|
case DEATTACK_DETECTED:
|
2015-07-02 13:15:34 +00:00
|
|
|
emsg = "crc32 compensation attack detected";
|
|
|
|
break;
|
2006-09-30 13:29:51 +00:00
|
|
|
case DEATTACK_DOS_DETECTED:
|
2015-07-02 13:15:34 +00:00
|
|
|
emsg = "deattack denial of service detected";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
emsg = "deattack error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (emsg != NULL) {
|
|
|
|
error("%s", emsg);
|
|
|
|
if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
|
|
|
|
(r = ssh_packet_write_wait(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
return SSH_ERR_CONN_CORRUPT;
|
2006-09-30 13:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
2002-03-18 09:55:03 +00:00
|
|
|
|
|
|
|
/* Decrypt data to incoming_packet. */
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_reset(state->incoming_packet);
|
|
|
|
if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = cipher_crypt(&state->receive_context, 0, p,
|
|
|
|
sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
|
|
|
|
goto out;
|
2002-03-18 09:55:03 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_consume(state->input, padded_len)) != 0)
|
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
fprintf(stderr, "read_poll plain: ");
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_dump(state->incoming_packet, stderr);
|
2000-02-24 14:29:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Compute packet checksum. */
|
2015-07-02 13:15:34 +00:00
|
|
|
checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
|
|
|
|
sshbuf_len(state->incoming_packet) - 4);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* Skip padding. */
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
|
|
|
|
goto out;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* Test check bytes. */
|
2015-07-02 13:15:34 +00:00
|
|
|
if (len != sshbuf_len(state->incoming_packet)) {
|
|
|
|
error("%s: len %d != sshbuf_len %zd", __func__,
|
|
|
|
len, sshbuf_len(state->incoming_packet));
|
|
|
|
if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
|
|
|
|
(r = ssh_packet_write_wait(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
return SSH_ERR_CONN_CORRUPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp = sshbuf_ptr(state->incoming_packet) + len - 4;
|
|
|
|
stored_checksum = PEEK_U32(cp);
|
|
|
|
if (checksum != stored_checksum) {
|
|
|
|
error("Corrupted check bytes on input");
|
|
|
|
if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
|
|
|
|
(r = ssh_packet_write_wait(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
return SSH_ERR_CONN_CORRUPT;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (state->packet_compression) {
|
|
|
|
sshbuf_reset(state->compression_buffer);
|
|
|
|
if ((r = uncompress_buffer(ssh, state->incoming_packet,
|
|
|
|
state->compression_buffer)) != 0)
|
|
|
|
goto out;
|
|
|
|
sshbuf_reset(state->incoming_packet);
|
|
|
|
if ((r = sshbuf_putb(state->incoming_packet,
|
|
|
|
state->compression_buffer)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
state->p_read.packets++;
|
|
|
|
state->p_read.bytes += padded_len + 4;
|
|
|
|
if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
|
|
|
|
error("Invalid ssh1 packet type: %d", *typep);
|
|
|
|
if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
|
|
|
|
(r = ssh_packet_write_wait(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
return SSH_ERR_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
return r;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
|
2000-05-15 04:37:24 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
2001-05-04 03:57:05 +00:00
|
|
|
u_int padlen, need;
|
2017-01-31 12:29:48 +00:00
|
|
|
u_char *cp;
|
2015-07-02 13:15:34 +00:00
|
|
|
u_int maclen, aadlen = 0, authlen = 0, block_size;
|
|
|
|
struct sshenc *enc = NULL;
|
|
|
|
struct sshmac *mac = NULL;
|
|
|
|
struct sshcomp *comp = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
*typep = SSH_MSG_NONE;
|
|
|
|
|
|
|
|
if (state->packet_discard)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (state->newkeys[MODE_IN] != NULL) {
|
|
|
|
enc = &state->newkeys[MODE_IN]->enc;
|
|
|
|
mac = &state->newkeys[MODE_IN]->mac;
|
|
|
|
comp = &state->newkeys[MODE_IN]->comp;
|
2013-03-22 11:19:48 +00:00
|
|
|
/* disable mac for authenticated encryption */
|
|
|
|
if ((authlen = cipher_authlen(enc->cipher)) != 0)
|
|
|
|
mac = NULL;
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
|
|
|
maclen = mac && mac->enabled ? mac->mac_len : 0;
|
2002-03-18 09:55:03 +00:00
|
|
|
block_size = enc ? enc->block_size : 8;
|
2013-03-22 11:19:48 +00:00
|
|
|
aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
|
2000-05-15 04:37:24 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (aadlen && state->packlen == 0) {
|
|
|
|
if (cipher_get_length(&state->receive_context,
|
|
|
|
&state->packlen, state->p_read.seqnr,
|
|
|
|
sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
|
|
|
|
return 0;
|
|
|
|
if (state->packlen < 1 + 4 ||
|
|
|
|
state->packlen > PACKET_MAX_SIZE) {
|
2013-03-22 11:19:48 +00:00
|
|
|
#ifdef PACKET_DEBUG
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_dump(state->input, stderr);
|
2013-03-22 11:19:48 +00:00
|
|
|
#endif
|
2015-07-02 13:15:34 +00:00
|
|
|
logit("Bad packet length %u.", state->packlen);
|
|
|
|
if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
|
|
|
|
return r;
|
2016-01-19 10:10:58 +00:00
|
|
|
return SSH_ERR_CONN_CORRUPT;
|
2013-03-22 11:19:48 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_reset(state->incoming_packet);
|
|
|
|
} else if (state->packlen == 0) {
|
2000-05-15 04:37:24 +00:00
|
|
|
/*
|
|
|
|
* check if input size is less than the cipher block size,
|
|
|
|
* decrypt first block and extract length of incoming packet
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
if (sshbuf_len(state->input) < block_size)
|
|
|
|
return 0;
|
|
|
|
sshbuf_reset(state->incoming_packet);
|
|
|
|
if ((r = sshbuf_reserve(state->incoming_packet, block_size,
|
|
|
|
&cp)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = cipher_crypt(&state->receive_context,
|
|
|
|
state->p_send.seqnr, cp, sshbuf_ptr(state->input),
|
|
|
|
block_size, 0, 0)) != 0)
|
|
|
|
goto out;
|
|
|
|
state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
|
|
|
|
if (state->packlen < 1 + 4 ||
|
|
|
|
state->packlen > PACKET_MAX_SIZE) {
|
2004-01-07 11:10:17 +00:00
|
|
|
#ifdef PACKET_DEBUG
|
2015-07-02 13:15:34 +00:00
|
|
|
fprintf(stderr, "input: \n");
|
|
|
|
sshbuf_dump(state->input, stderr);
|
|
|
|
fprintf(stderr, "incoming_packet: \n");
|
|
|
|
sshbuf_dump(state->incoming_packet, stderr);
|
2004-01-07 11:10:17 +00:00
|
|
|
#endif
|
2015-07-02 13:15:34 +00:00
|
|
|
logit("Bad packet length %u.", state->packlen);
|
2017-01-31 12:29:48 +00:00
|
|
|
return ssh_packet_start_discard(ssh, enc, mac, 0,
|
|
|
|
PACKET_MAX_SIZE);
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_consume(state->input, block_size)) != 0)
|
|
|
|
goto out;
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
DBG(debug("input: packet len %u", state->packlen+4));
|
|
|
|
|
2013-03-22 11:19:48 +00:00
|
|
|
if (aadlen) {
|
|
|
|
/* only the payload is encrypted */
|
2015-07-02 13:15:34 +00:00
|
|
|
need = state->packlen;
|
2013-03-22 11:19:48 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* the payload size and the payload are encrypted, but we
|
|
|
|
* have a partial packet of block_size bytes
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
need = 4 + state->packlen - block_size;
|
2013-03-22 11:19:48 +00:00
|
|
|
}
|
|
|
|
DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
|
|
|
|
" aadlen %d", block_size, need, maclen, authlen, aadlen));
|
2009-02-24 18:49:27 +00:00
|
|
|
if (need % block_size != 0) {
|
|
|
|
logit("padding error: need %d block %d mod %d",
|
2000-05-15 04:37:24 +00:00
|
|
|
need, block_size, need % block_size);
|
2017-01-31 12:29:48 +00:00
|
|
|
return ssh_packet_start_discard(ssh, enc, mac, 0,
|
|
|
|
PACKET_MAX_SIZE - block_size);
|
2009-02-24 18:49:27 +00:00
|
|
|
}
|
2000-05-15 04:37:24 +00:00
|
|
|
/*
|
|
|
|
* check if the entire packet has been received and
|
2013-03-22 11:19:48 +00:00
|
|
|
* decrypt into incoming_packet:
|
|
|
|
* 'aadlen' bytes are unencrypted, but authenticated.
|
|
|
|
* 'need' bytes are encrypted, followed by either
|
|
|
|
* 'authlen' bytes of authentication tag or
|
|
|
|
* 'maclen' bytes of message authentication code.
|
2000-05-15 04:37:24 +00:00
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
|
2017-01-31 12:29:48 +00:00
|
|
|
return 0; /* packet is incomplete */
|
2000-05-15 04:37:24 +00:00
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
fprintf(stderr, "read_poll enc/full: ");
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_dump(state->input, stderr);
|
2000-05-15 04:37:24 +00:00
|
|
|
#endif
|
2017-01-31 12:29:48 +00:00
|
|
|
/* EtM: check mac over encrypted input */
|
2015-07-02 13:15:34 +00:00
|
|
|
if (mac && mac->enabled && mac->etm) {
|
2017-01-31 12:29:48 +00:00
|
|
|
if ((r = mac_check(mac, state->p_read.seqnr,
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_ptr(state->input), aadlen + need,
|
2017-01-31 12:29:48 +00:00
|
|
|
sshbuf_ptr(state->input) + aadlen + need + authlen,
|
|
|
|
maclen)) != 0) {
|
|
|
|
if (r == SSH_ERR_MAC_INVALID)
|
|
|
|
logit("Corrupted MAC on input.");
|
2015-07-02 13:15:34 +00:00
|
|
|
goto out;
|
2017-01-31 12:29:48 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
}
|
|
|
|
if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
|
|
|
|
&cp)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
|
|
|
|
sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
|
|
|
|
goto out;
|
2000-05-15 04:37:24 +00:00
|
|
|
if (mac && mac->enabled) {
|
2017-01-31 12:29:48 +00:00
|
|
|
/* Not EtM: check MAC over cleartext */
|
|
|
|
if (!mac->etm && (r = mac_check(mac, state->p_read.seqnr,
|
|
|
|
sshbuf_ptr(state->incoming_packet),
|
|
|
|
sshbuf_len(state->incoming_packet),
|
|
|
|
sshbuf_ptr(state->input), maclen)) != 0) {
|
|
|
|
if (r != SSH_ERR_MAC_INVALID)
|
2015-07-02 13:15:34 +00:00
|
|
|
goto out;
|
2009-02-24 18:49:27 +00:00
|
|
|
logit("Corrupted MAC on input.");
|
|
|
|
if (need > PACKET_MAX_SIZE)
|
2015-07-02 13:15:34 +00:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
return ssh_packet_start_discard(ssh, enc, mac,
|
2017-01-31 12:29:48 +00:00
|
|
|
sshbuf_len(state->incoming_packet),
|
|
|
|
PACKET_MAX_SIZE - need);
|
2009-02-24 18:49:27 +00:00
|
|
|
}
|
2017-01-31 12:29:48 +00:00
|
|
|
/* Remove MAC from input buffer */
|
2015-07-02 13:15:34 +00:00
|
|
|
DBG(debug("MAC #%d ok", state->p_read.seqnr));
|
|
|
|
if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
|
|
|
|
goto out;
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
2002-03-18 09:55:03 +00:00
|
|
|
if (seqnr_p != NULL)
|
2015-07-02 13:15:34 +00:00
|
|
|
*seqnr_p = state->p_read.seqnr;
|
|
|
|
if (++state->p_read.seqnr == 0)
|
2004-01-07 11:10:17 +00:00
|
|
|
logit("incoming seqnr wraps around");
|
2015-07-02 13:15:34 +00:00
|
|
|
if (++state->p_read.packets == 0)
|
|
|
|
if (!(ssh->compat & SSH_BUG_NOREKEY))
|
|
|
|
return SSH_ERR_NEED_REKEY;
|
|
|
|
state->p_read.blocks += (state->packlen + 4) / block_size;
|
|
|
|
state->p_read.bytes += state->packlen + 4;
|
2000-05-15 04:37:24 +00:00
|
|
|
|
|
|
|
/* get padlen */
|
2015-07-02 13:15:34 +00:00
|
|
|
padlen = sshbuf_ptr(state->incoming_packet)[4];
|
2000-05-15 04:37:24 +00:00
|
|
|
DBG(debug("input: padlen %d", padlen));
|
2015-07-02 13:15:34 +00:00
|
|
|
if (padlen < 4) {
|
|
|
|
if ((r = sshpkt_disconnect(ssh,
|
|
|
|
"Corrupted padlen %d on input.", padlen)) != 0 ||
|
|
|
|
(r = ssh_packet_write_wait(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
return SSH_ERR_CONN_CORRUPT;
|
|
|
|
}
|
2000-05-15 04:37:24 +00:00
|
|
|
|
|
|
|
/* skip packet size + padlen, discard padding */
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
|
|
|
|
((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
|
|
|
|
goto out;
|
2000-05-15 04:37:24 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
DBG(debug("input: len before de-compress %zd",
|
|
|
|
sshbuf_len(state->incoming_packet)));
|
2000-05-15 04:37:24 +00:00
|
|
|
if (comp && comp->enabled) {
|
2015-07-02 13:15:34 +00:00
|
|
|
sshbuf_reset(state->compression_buffer);
|
|
|
|
if ((r = uncompress_buffer(ssh, state->incoming_packet,
|
|
|
|
state->compression_buffer)) != 0)
|
|
|
|
goto out;
|
|
|
|
sshbuf_reset(state->incoming_packet);
|
|
|
|
if ((r = sshbuf_putb(state->incoming_packet,
|
|
|
|
state->compression_buffer)) != 0)
|
|
|
|
goto out;
|
|
|
|
DBG(debug("input: len after de-compress %zd",
|
|
|
|
sshbuf_len(state->incoming_packet)));
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* get packet type, implies consume.
|
|
|
|
* return length of payload (without type field)
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
|
|
|
|
goto out;
|
2016-03-10 20:10:25 +00:00
|
|
|
if (ssh_packet_log_type(*typep))
|
|
|
|
debug3("receive packet: type %u", *typep);
|
2015-07-02 13:15:34 +00:00
|
|
|
if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
|
|
|
|
if ((r = sshpkt_disconnect(ssh,
|
|
|
|
"Invalid ssh2 packet type: %d", *typep)) != 0 ||
|
|
|
|
(r = ssh_packet_write_wait(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
return SSH_ERR_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
if (*typep == SSH2_MSG_NEWKEYS)
|
|
|
|
r = ssh_set_newkeys(ssh, MODE_IN);
|
|
|
|
else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
|
|
|
|
r = ssh_packet_enable_delayed_compress(ssh);
|
|
|
|
else
|
|
|
|
r = 0;
|
2000-05-15 04:37:24 +00:00
|
|
|
#ifdef PACKET_DEBUG
|
2015-07-02 13:15:34 +00:00
|
|
|
fprintf(stderr, "read/plain[%d]:\r\n", *typep);
|
|
|
|
sshbuf_dump(state->incoming_packet, stderr);
|
2000-05-15 04:37:24 +00:00
|
|
|
#endif
|
2002-03-18 09:55:03 +00:00
|
|
|
/* reset for next packet */
|
2015-07-02 13:15:34 +00:00
|
|
|
state->packlen = 0;
|
2016-03-10 20:10:25 +00:00
|
|
|
|
|
|
|
/* do we need to rekey? */
|
|
|
|
if (ssh_packet_need_rekeying(ssh, 0)) {
|
|
|
|
debug3("%s: rekex triggered", __func__);
|
|
|
|
if ((r = kex_start_rekex(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
out:
|
|
|
|
return r;
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
|
2000-05-15 04:37:24 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
2002-06-29 11:34:13 +00:00
|
|
|
u_int reason, seqnr;
|
2015-07-02 13:15:34 +00:00
|
|
|
int r;
|
|
|
|
u_char *msg;
|
2000-05-15 04:37:24 +00:00
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
for (;;) {
|
2015-07-02 13:15:34 +00:00
|
|
|
msg = NULL;
|
2002-03-18 09:55:03 +00:00
|
|
|
if (compat20) {
|
2015-07-02 13:15:34 +00:00
|
|
|
r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
|
|
|
if (*typep) {
|
|
|
|
state->keep_alive_timeouts = 0;
|
|
|
|
DBG(debug("received packet type %d", *typep));
|
2008-07-23 09:33:08 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
switch (*typep) {
|
2000-05-15 04:37:24 +00:00
|
|
|
case SSH2_MSG_IGNORE:
|
2008-07-23 09:28:49 +00:00
|
|
|
debug3("Received SSH2_MSG_IGNORE");
|
2000-05-15 04:37:24 +00:00
|
|
|
break;
|
|
|
|
case SSH2_MSG_DEBUG:
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
|
|
|
|
(r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
|
|
|
|
(r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
|
2016-03-10 20:10:25 +00:00
|
|
|
free(msg);
|
2015-07-02 13:15:34 +00:00
|
|
|
return r;
|
|
|
|
}
|
2000-05-15 04:37:24 +00:00
|
|
|
debug("Remote: %.900s", msg);
|
2013-09-18 17:27:38 +00:00
|
|
|
free(msg);
|
2000-05-15 04:37:24 +00:00
|
|
|
break;
|
|
|
|
case SSH2_MSG_DISCONNECT:
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
|
|
|
|
(r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
|
|
|
|
return r;
|
2013-04-14 12:55:39 +00:00
|
|
|
/* Ignore normal client exit notifications */
|
2015-07-02 13:15:34 +00:00
|
|
|
do_log2(ssh->state->server_side &&
|
2013-04-14 12:55:39 +00:00
|
|
|
reason == SSH2_DISCONNECT_BY_APPLICATION ?
|
2013-05-17 08:55:26 +00:00
|
|
|
SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
|
2016-03-10 20:10:25 +00:00
|
|
|
"Received disconnect from %s port %d:"
|
|
|
|
"%u: %.400s", ssh_remote_ipaddr(ssh),
|
|
|
|
ssh_remote_port(ssh), reason, msg);
|
2013-09-18 17:27:38 +00:00
|
|
|
free(msg);
|
2015-07-02 13:15:34 +00:00
|
|
|
return SSH_ERR_DISCONNECTED;
|
2002-03-18 09:55:03 +00:00
|
|
|
case SSH2_MSG_UNIMPLEMENTED:
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
|
|
|
|
return r;
|
2002-06-29 11:34:13 +00:00
|
|
|
debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
|
|
|
|
seqnr);
|
2002-03-18 09:55:03 +00:00
|
|
|
break;
|
2000-05-15 04:37:24 +00:00
|
|
|
default:
|
2015-07-02 13:15:34 +00:00
|
|
|
return 0;
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
2000-05-15 04:37:24 +00:00
|
|
|
} else {
|
2015-07-02 13:15:34 +00:00
|
|
|
r = ssh_packet_read_poll1(ssh, typep);
|
|
|
|
switch (*typep) {
|
2013-09-18 17:27:38 +00:00
|
|
|
case SSH_MSG_NONE:
|
|
|
|
return SSH_MSG_NONE;
|
2000-05-15 04:37:24 +00:00
|
|
|
case SSH_MSG_IGNORE:
|
|
|
|
break;
|
|
|
|
case SSH_MSG_DEBUG:
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
|
|
|
|
return r;
|
2000-05-15 04:37:24 +00:00
|
|
|
debug("Remote: %.900s", msg);
|
2013-09-18 17:27:38 +00:00
|
|
|
free(msg);
|
2000-05-15 04:37:24 +00:00
|
|
|
break;
|
|
|
|
case SSH_MSG_DISCONNECT:
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
|
|
|
|
return r;
|
2016-03-10 20:10:25 +00:00
|
|
|
error("Received disconnect from %s port %d: "
|
|
|
|
"%.400s", ssh_remote_ipaddr(ssh),
|
|
|
|
ssh_remote_port(ssh), msg);
|
2015-07-02 13:15:34 +00:00
|
|
|
free(msg);
|
|
|
|
return SSH_ERR_DISCONNECTED;
|
2000-05-15 04:37:24 +00:00
|
|
|
default:
|
2015-07-02 13:15:34 +00:00
|
|
|
DBG(debug("received packet type %d", *typep));
|
|
|
|
return 0;
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Buffers the given amount of input characters. This is intended to be used
|
|
|
|
* together with packet_read_poll.
|
|
|
|
*/
|
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
|
2000-05-15 04:37:24 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
int r;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->packet_discard) {
|
|
|
|
state->keep_alive_timeouts = 0; /* ?? */
|
|
|
|
if (len >= state->packet_discard) {
|
|
|
|
if ((r = ssh_packet_stop_discard(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
state->packet_discard -= len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
|
|
|
|
return r;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
return 0;
|
2008-07-23 09:33:08 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_packet_remaining(struct ssh *ssh)
|
2011-02-17 11:47:40 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
return sshbuf_len(ssh->state->incoming_packet);
|
2011-02-17 11:47:40 +00:00
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Sends a diagnostic message from the server to the client. This message
|
|
|
|
* can be sent at any time (but not while constructing another message). The
|
|
|
|
* message is printed immediately, but only if the client is being executed
|
|
|
|
* in verbose mode. These messages are primarily intended to ease debugging
|
|
|
|
* authentication problems. The length of the formatted message must not
|
2015-07-02 13:15:34 +00:00
|
|
|
* exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
|
2000-02-24 14:29:47 +00:00
|
|
|
*/
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
va_list args;
|
2015-07-02 13:15:34 +00:00
|
|
|
int r;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
|
2001-05-04 03:57:05 +00:00
|
|
|
return;
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
if (compat20) {
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
|
|
|
|
(r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
|
|
|
|
(r = sshpkt_put_cstring(ssh, buf)) != 0 ||
|
|
|
|
(r = sshpkt_put_cstring(ssh, "")) != 0 ||
|
|
|
|
(r = sshpkt_send(ssh)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(r));
|
2000-05-15 04:37:24 +00:00
|
|
|
} else {
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
|
|
|
|
(r = sshpkt_put_cstring(ssh, buf)) != 0 ||
|
|
|
|
(r = sshpkt_send(ssh)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(r));
|
|
|
|
}
|
|
|
|
if ((r = ssh_packet_write_wait(ssh)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pretty-print connection-terminating errors and exit.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
|
|
|
|
{
|
|
|
|
switch (r) {
|
|
|
|
case SSH_ERR_CONN_CLOSED:
|
2017-01-31 12:29:48 +00:00
|
|
|
logdie("Connection closed by %.200s port %d",
|
2016-03-10 20:10:25 +00:00
|
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
|
2015-07-02 13:15:34 +00:00
|
|
|
case SSH_ERR_CONN_TIMEOUT:
|
2017-01-31 12:29:48 +00:00
|
|
|
logdie("Connection %s %.200s port %d timed out",
|
2016-03-10 20:10:25 +00:00
|
|
|
ssh->state->server_side ? "from" : "to",
|
|
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
|
2015-07-02 13:18:50 +00:00
|
|
|
case SSH_ERR_DISCONNECTED:
|
2017-01-31 12:29:48 +00:00
|
|
|
logdie("Disconnected from %.200s port %d",
|
2016-03-10 20:10:25 +00:00
|
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
|
2015-07-02 13:18:50 +00:00
|
|
|
case SSH_ERR_SYSTEM_ERROR:
|
2017-01-31 12:29:48 +00:00
|
|
|
if (errno == ECONNRESET)
|
|
|
|
logdie("Connection reset by %.200s port %d",
|
2016-03-10 20:10:25 +00:00
|
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
|
2015-07-02 13:18:50 +00:00
|
|
|
/* FALLTHROUGH */
|
2015-08-26 09:25:17 +00:00
|
|
|
case SSH_ERR_NO_CIPHER_ALG_MATCH:
|
|
|
|
case SSH_ERR_NO_MAC_ALG_MATCH:
|
|
|
|
case SSH_ERR_NO_COMPRESS_ALG_MATCH:
|
|
|
|
case SSH_ERR_NO_KEX_ALG_MATCH:
|
|
|
|
case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
|
|
|
|
if (ssh && ssh->kex && ssh->kex->failed_choice) {
|
2017-01-31 12:29:48 +00:00
|
|
|
logdie("Unable to negotiate with %.200s port %d: %s. "
|
2015-08-26 09:25:17 +00:00
|
|
|
"Their offer: %s", ssh_remote_ipaddr(ssh),
|
2016-03-10 20:10:25 +00:00
|
|
|
ssh_remote_port(ssh), ssh_err(r),
|
|
|
|
ssh->kex->failed_choice);
|
2015-08-26 09:25:17 +00:00
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2015-07-02 13:15:34 +00:00
|
|
|
default:
|
2017-01-31 12:29:48 +00:00
|
|
|
logdie("%s%sConnection %s %.200s port %d: %s",
|
2015-07-02 13:15:34 +00:00
|
|
|
tag != NULL ? tag : "", tag != NULL ? ": " : "",
|
2016-03-10 20:10:25 +00:00
|
|
|
ssh->state->server_side ? "from" : "to",
|
|
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
|
2000-05-15 04:37:24 +00:00
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Logs the error plus constructs and sends a disconnect packet, closes the
|
|
|
|
* connection, and exits. This function never returns. The error message
|
|
|
|
* should not contain a newline. The length of the formatted message must
|
|
|
|
* not exceed 1024 bytes.
|
|
|
|
*/
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
va_list args;
|
|
|
|
static int disconnecting = 0;
|
2015-07-02 13:15:34 +00:00
|
|
|
int r;
|
2002-10-29 09:43:00 +00:00
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
if (disconnecting) /* Guard against recursive invocations. */
|
|
|
|
fatal("packet_disconnect called recursively.");
|
|
|
|
disconnecting = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format the message. Note that the caller must make sure the
|
|
|
|
* message is of limited size.
|
|
|
|
*/
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2003-04-23 16:53:02 +00:00
|
|
|
/* Display the error locally */
|
2004-01-07 11:10:17 +00:00
|
|
|
logit("Disconnecting: %.100s", buf);
|
2003-04-23 16:53:02 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
/*
|
|
|
|
* Send the disconnect message to the other side, and wait
|
|
|
|
* for it to get sent.
|
|
|
|
*/
|
|
|
|
if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
|
|
|
|
sshpkt_fatal(ssh, __func__, r);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = ssh_packet_write_wait(ssh)) != 0)
|
|
|
|
sshpkt_fatal(ssh, __func__, r);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* Close the connection. */
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_close(ssh);
|
2004-02-26 10:38:49 +00:00
|
|
|
cleanup_exit(255);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
/*
|
|
|
|
* Checks if there is any buffered output, and tries to write some of
|
|
|
|
* the output.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ssh_packet_write_poll(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
int len = sshbuf_len(state->output);
|
2016-03-10 20:10:25 +00:00
|
|
|
int r;
|
2002-10-29 09:43:00 +00:00
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
if (len > 0) {
|
2016-03-10 20:10:25 +00:00
|
|
|
len = write(state->connection_out,
|
|
|
|
sshbuf_ptr(state->output), len);
|
2008-07-23 09:33:08 +00:00
|
|
|
if (len == -1) {
|
|
|
|
if (errno == EINTR || errno == EAGAIN ||
|
|
|
|
errno == EWOULDBLOCK)
|
2015-07-02 13:15:34 +00:00
|
|
|
return 0;
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2016-03-10 20:10:25 +00:00
|
|
|
if (len == 0)
|
2015-07-02 13:15:34 +00:00
|
|
|
return SSH_ERR_CONN_CLOSED;
|
|
|
|
if ((r = sshbuf_consume(state->output, len)) != 0)
|
|
|
|
return r;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
return 0;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calls packet_write_poll repeatedly until all pending output data has been
|
|
|
|
* written.
|
|
|
|
*/
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
ssh_packet_write_wait(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2001-05-04 03:57:05 +00:00
|
|
|
fd_set *setp;
|
2015-07-02 13:15:34 +00:00
|
|
|
int ret, r, ms_remain = 0;
|
2008-07-23 09:33:08 +00:00
|
|
|
struct timeval start, timeout, *timeoutp = NULL;
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2015-08-26 09:27:05 +00:00
|
|
|
setp = calloc(howmany(state->connection_out + 1,
|
2009-10-01 15:19:37 +00:00
|
|
|
NFDBITS), sizeof(fd_mask));
|
2015-07-02 13:15:34 +00:00
|
|
|
if (setp == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2016-03-10 20:10:25 +00:00
|
|
|
if ((r = ssh_packet_write_poll(ssh)) != 0) {
|
|
|
|
free(setp);
|
|
|
|
return r;
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
while (ssh_packet_have_data_to_write(ssh)) {
|
|
|
|
memset(setp, 0, howmany(state->connection_out + 1,
|
2009-10-01 15:19:37 +00:00
|
|
|
NFDBITS) * sizeof(fd_mask));
|
2015-07-02 13:15:34 +00:00
|
|
|
FD_SET(state->connection_out, setp);
|
2008-07-23 09:33:08 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->packet_timeout_ms > 0) {
|
|
|
|
ms_remain = state->packet_timeout_ms;
|
2008-07-23 09:33:08 +00:00
|
|
|
timeoutp = &timeout;
|
|
|
|
}
|
|
|
|
for (;;) {
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->packet_timeout_ms != -1) {
|
2008-07-23 09:33:08 +00:00
|
|
|
ms_to_timeval(&timeout, ms_remain);
|
|
|
|
gettimeofday(&start, NULL);
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((ret = select(state->connection_out + 1,
|
2009-10-01 15:19:37 +00:00
|
|
|
NULL, setp, NULL, timeoutp)) >= 0)
|
2008-07-23 09:33:08 +00:00
|
|
|
break;
|
2009-10-01 15:19:37 +00:00
|
|
|
if (errno != EAGAIN && errno != EINTR &&
|
2008-07-23 09:33:08 +00:00
|
|
|
errno != EWOULDBLOCK)
|
|
|
|
break;
|
2015-07-02 13:15:34 +00:00
|
|
|
if (state->packet_timeout_ms == -1)
|
2008-07-23 09:33:08 +00:00
|
|
|
continue;
|
|
|
|
ms_subtract_diff(&start, &ms_remain);
|
|
|
|
if (ms_remain <= 0) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
2015-07-02 13:15:34 +00:00
|
|
|
free(setp);
|
|
|
|
return SSH_ERR_CONN_TIMEOUT;
|
|
|
|
}
|
|
|
|
if ((r = ssh_packet_write_poll(ssh)) != 0) {
|
|
|
|
free(setp);
|
|
|
|
return r;
|
2008-07-23 09:33:08 +00:00
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2013-09-18 17:27:38 +00:00
|
|
|
free(setp);
|
2015-07-02 13:15:34 +00:00
|
|
|
return 0;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if there is buffered data to write to the connection. */
|
|
|
|
|
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_have_data_to_write(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
return sshbuf_len(ssh->state->output) != 0;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if there is not too much data to write to the connection. */
|
|
|
|
|
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
if (ssh->state->interactive_mode)
|
|
|
|
return sshbuf_len(ssh->state->output) < 16384;
|
2000-02-24 14:29:47 +00:00
|
|
|
else
|
2015-07-02 13:15:34 +00:00
|
|
|
return sshbuf_len(ssh->state->output) < 128 * 1024;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
void
|
|
|
|
ssh_packet_set_tos(struct ssh *ssh, int tos)
|
2003-04-23 16:53:02 +00:00
|
|
|
{
|
2011-09-28 08:14:41 +00:00
|
|
|
#ifndef IP_TOS_IS_BROKEN
|
2015-07-02 13:15:34 +00:00
|
|
|
if (!ssh_packet_connection_is_on_socket(ssh))
|
2003-04-23 16:53:02 +00:00
|
|
|
return;
|
2015-07-02 13:15:34 +00:00
|
|
|
switch (ssh_packet_connection_af(ssh)) {
|
2011-09-28 08:14:41 +00:00
|
|
|
# ifdef IP_TOS
|
|
|
|
case AF_INET:
|
|
|
|
debug3("%s: set IP_TOS 0x%02x", __func__, tos);
|
2015-07-02 13:15:34 +00:00
|
|
|
if (setsockopt(ssh->state->connection_in,
|
2011-09-28 08:14:41 +00:00
|
|
|
IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
|
|
|
|
error("setsockopt IP_TOS %d: %.100s:",
|
|
|
|
tos, strerror(errno));
|
|
|
|
break;
|
|
|
|
# endif /* IP_TOS */
|
|
|
|
# ifdef IPV6_TCLASS
|
|
|
|
case AF_INET6:
|
|
|
|
debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
|
2015-07-02 13:15:34 +00:00
|
|
|
if (setsockopt(ssh->state->connection_in,
|
2011-09-28 08:14:41 +00:00
|
|
|
IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
|
|
|
|
error("setsockopt IPV6_TCLASS %d: %.100s:",
|
|
|
|
tos, strerror(errno));
|
|
|
|
break;
|
|
|
|
# endif /* IPV6_TCLASS */
|
|
|
|
}
|
|
|
|
#endif /* IP_TOS_IS_BROKEN */
|
2004-02-26 10:38:49 +00:00
|
|
|
}
|
2003-04-23 16:53:02 +00:00
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Informs that the current session is interactive. Sets IP flags for that. */
|
|
|
|
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
|
|
|
|
if (state->set_interactive_called)
|
2001-05-04 03:57:05 +00:00
|
|
|
return;
|
2015-07-02 13:15:34 +00:00
|
|
|
state->set_interactive_called = 1;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Record that we are in interactive mode. */
|
2015-07-02 13:15:34 +00:00
|
|
|
state->interactive_mode = interactive;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* Only set socket options if using a socket. */
|
2015-07-02 13:15:34 +00:00
|
|
|
if (!ssh_packet_connection_is_on_socket(ssh))
|
2003-06-24 19:30:44 +00:00
|
|
|
return;
|
2015-07-02 13:15:34 +00:00
|
|
|
set_nodelay(state->connection_in);
|
|
|
|
ssh_packet_set_tos(ssh, interactive ? qos_interactive :
|
|
|
|
qos_bulk);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if the current connection is interactive. */
|
|
|
|
|
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_is_interactive(struct ssh *ssh)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
return ssh->state->interactive_mode;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2004-10-28 16:03:53 +00:00
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
|
|
|
|
if (state->set_maxsize_called) {
|
2004-01-07 11:10:17 +00:00
|
|
|
logit("packet_set_maxsize: called twice: old %d new %d",
|
2015-07-02 13:15:34 +00:00
|
|
|
state->max_packet_size, s);
|
2000-02-24 14:29:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (s < 4 * 1024 || s > 1024 * 1024) {
|
2004-01-07 11:10:17 +00:00
|
|
|
logit("packet_set_maxsize: bad size %d", s);
|
2000-02-24 14:29:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
state->set_maxsize_called = 1;
|
2002-03-18 09:55:03 +00:00
|
|
|
debug("packet_set_maxsize: setting to %d", s);
|
2015-07-02 13:15:34 +00:00
|
|
|
state->max_packet_size = s;
|
2000-02-24 14:29:47 +00:00
|
|
|
return s;
|
|
|
|
}
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2009-10-01 15:19:37 +00:00
|
|
|
int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_inc_alive_timeouts(struct ssh *ssh)
|
2009-10-01 15:19:37 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
return ++ssh->state->keep_alive_timeouts;
|
2009-10-01 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
|
2009-10-01 15:19:37 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh->state->keep_alive_timeouts = ka;
|
2009-10-01 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u_int
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_get_maxsize(struct ssh *ssh)
|
2002-03-18 09:55:03 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
return ssh->state->max_packet_size;
|
2002-03-18 09:55:03 +00:00
|
|
|
}
|
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
/*
|
|
|
|
* 9.2. Ignored Data Message
|
|
|
|
*
|
|
|
|
* byte SSH_MSG_IGNORE
|
|
|
|
* string data
|
|
|
|
*
|
|
|
|
* All implementations MUST understand (and ignore) this message at any
|
|
|
|
* time (after receiving the protocol version). No implementation is
|
|
|
|
* required to send them. This message can be used as an additional
|
|
|
|
* protection measure against advanced traffic analysis techniques.
|
|
|
|
*/
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
|
2001-05-04 03:57:05 +00:00
|
|
|
{
|
2004-10-28 16:03:53 +00:00
|
|
|
u_int32_t rnd = 0;
|
2015-07-02 13:15:34 +00:00
|
|
|
int r, i;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshpkt_start(ssh, compat20 ?
|
|
|
|
SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
|
|
|
|
(r = sshpkt_put_u32(ssh, nbytes)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(r));
|
2002-03-18 09:55:03 +00:00
|
|
|
for (i = 0; i < nbytes; i++) {
|
2001-05-04 03:57:05 +00:00
|
|
|
if (i % 4 == 0)
|
2004-10-28 16:03:53 +00:00
|
|
|
rnd = arc4random();
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(r));
|
2004-10-28 16:03:53 +00:00
|
|
|
rnd >>= 8;
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
2004-01-07 11:10:17 +00:00
|
|
|
|
|
|
|
void
|
2016-03-10 20:10:25 +00:00
|
|
|
ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds)
|
2004-01-07 11:10:17 +00:00
|
|
|
{
|
2016-03-10 20:10:25 +00:00
|
|
|
debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes,
|
2013-09-18 17:27:38 +00:00
|
|
|
(int)seconds);
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh->state->rekey_limit = bytes;
|
|
|
|
ssh->state->rekey_interval = seconds;
|
2013-09-18 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_get_rekey_timeout(struct ssh *ssh)
|
2013-09-18 17:27:38 +00:00
|
|
|
{
|
|
|
|
time_t seconds;
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
|
2013-09-18 17:27:38 +00:00
|
|
|
monotime();
|
|
|
|
return (seconds <= 0 ? 1 : seconds);
|
2004-01-07 11:10:17 +00:00
|
|
|
}
|
2005-09-03 06:59:33 +00:00
|
|
|
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_set_server(struct ssh *ssh)
|
2005-09-03 06:59:33 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh->state->server_side = 1;
|
2005-09-03 06:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_set_authenticated(struct ssh *ssh)
|
2005-09-03 06:59:33 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh->state->after_authentication = 1;
|
2009-10-01 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_get_input(struct ssh *ssh)
|
2009-10-01 15:19:37 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
return (void *)ssh->state->input;
|
2009-10-01 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh_packet_get_output(struct ssh *ssh)
|
2009-10-01 15:19:37 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
return (void *)ssh->state->output;
|
2009-10-01 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 16:09:55 +00:00
|
|
|
/* Reset after_authentication and reset compression in post-auth privsep */
|
2015-07-02 13:15:34 +00:00
|
|
|
static int
|
|
|
|
ssh_packet_set_postauth(struct ssh *ssh)
|
2015-01-05 16:09:55 +00:00
|
|
|
{
|
2015-07-02 13:15:34 +00:00
|
|
|
struct sshcomp *comp;
|
|
|
|
int r, mode;
|
2015-01-05 16:09:55 +00:00
|
|
|
|
|
|
|
debug("%s: called", __func__);
|
|
|
|
/* This was set in net child, but is not visible in user child */
|
2015-07-02 13:15:34 +00:00
|
|
|
ssh->state->after_authentication = 1;
|
|
|
|
ssh->state->rekeying = 0;
|
2015-01-05 16:09:55 +00:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2015-07-02 13:15:34 +00:00
|
|
|
if (ssh->state->newkeys[mode] == NULL)
|
2015-01-05 16:09:55 +00:00
|
|
|
continue;
|
2015-07-02 13:15:34 +00:00
|
|
|
comp = &ssh->state->newkeys[mode]->comp;
|
|
|
|
if (comp && comp->enabled &&
|
|
|
|
(r = ssh_packet_init_compression(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Packet state (de-)serialization for privsep */
|
|
|
|
|
|
|
|
/* turn kex into a blob for packet state serialization */
|
|
|
|
static int
|
|
|
|
kex_to_blob(struct sshbuf *m, struct kex *kex)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshbuf_put_string(m, kex->session_id,
|
|
|
|
kex->session_id_len)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(m, kex->my)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, kex->flags)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
|
|
|
|
return r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* turn key exchange results into a blob for packet state serialization */
|
|
|
|
static int
|
|
|
|
newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
|
|
|
|
{
|
|
|
|
struct sshbuf *b;
|
|
|
|
struct sshcipher_ctx *cc;
|
|
|
|
struct sshcomp *comp;
|
|
|
|
struct sshenc *enc;
|
|
|
|
struct sshmac *mac;
|
|
|
|
struct newkeys *newkey;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((newkey = ssh->state->newkeys[mode]) == NULL)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
enc = &newkey->enc;
|
|
|
|
mac = &newkey->mac;
|
|
|
|
comp = &newkey->comp;
|
|
|
|
cc = (mode == MODE_OUT) ? &ssh->state->send_context :
|
|
|
|
&ssh->state->receive_context;
|
|
|
|
if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
|
|
|
|
return r;
|
|
|
|
if ((b = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
/* The cipher struct is constant and shared, you export pointer */
|
|
|
|
if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
|
|
|
|
(r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (cipher_authlen(enc->cipher) == 0) {
|
|
|
|
if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(b, comp->name)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = sshbuf_put_stringb(m, b);
|
|
|
|
out:
|
2016-03-10 20:10:25 +00:00
|
|
|
sshbuf_free(b);
|
2015-07-02 13:15:34 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialize packet state into a blob */
|
|
|
|
int
|
|
|
|
ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
|
|
|
|
{
|
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
u_char *p;
|
|
|
|
size_t slen, rlen;
|
|
|
|
int r, ssh1cipher;
|
|
|
|
|
|
|
|
if (!compat20) {
|
|
|
|
ssh1cipher = cipher_get_number(state->receive_context.cipher);
|
|
|
|
slen = cipher_get_keyiv_len(&state->send_context);
|
|
|
|
rlen = cipher_get_keyiv_len(&state->receive_context);
|
|
|
|
if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, slen)) != 0 ||
|
|
|
|
(r = sshbuf_reserve(m, slen, &p)) != 0 ||
|
|
|
|
(r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, rlen)) != 0 ||
|
|
|
|
(r = sshbuf_reserve(m, rlen, &p)) != 0 ||
|
|
|
|
(r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
|
|
|
|
(r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
|
|
|
|
(r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
|
2016-03-10 20:10:25 +00:00
|
|
|
(r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
|
2015-07-02 13:15:34 +00:00
|
|
|
(r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
|
|
|
|
(r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
|
|
|
|
(r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
|
|
|
|
(r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
|
|
|
|
(r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
slen = cipher_get_keycontext(&state->send_context, NULL);
|
|
|
|
rlen = cipher_get_keycontext(&state->receive_context, NULL);
|
|
|
|
if ((r = sshbuf_put_u32(m, slen)) != 0 ||
|
|
|
|
(r = sshbuf_reserve(m, slen, &p)) != 0)
|
|
|
|
return r;
|
|
|
|
if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
|
|
|
|
(r = sshbuf_reserve(m, rlen, &p)) != 0)
|
|
|
|
return r;
|
|
|
|
if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(m, state->input)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(m, state->output)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore key exchange results from blob for packet state de-serialization */
|
|
|
|
static int
|
|
|
|
newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
|
|
|
|
{
|
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
struct sshcomp *comp;
|
|
|
|
struct sshenc *enc;
|
|
|
|
struct sshmac *mac;
|
|
|
|
struct newkeys *newkey = NULL;
|
|
|
|
size_t keylen, ivlen, maclen;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_froms(m, &b)) != 0)
|
|
|
|
goto out;
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
sshbuf_dump(b, stderr);
|
|
|
|
#endif
|
|
|
|
enc = &newkey->enc;
|
|
|
|
mac = &newkey->mac;
|
|
|
|
comp = &newkey->comp;
|
|
|
|
|
|
|
|
if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
|
|
|
|
(r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
|
|
|
|
(r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (cipher_authlen(enc->cipher) == 0) {
|
|
|
|
if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = mac_setup(mac, mac->name)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
|
|
|
|
(r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (maclen > mac->key_len) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
mac->key_len = maclen;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
|
|
|
|
(r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (enc->name == NULL ||
|
|
|
|
cipher_by_name(enc->name) != enc->cipher) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshbuf_len(b) != 0) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
enc->key_len = keylen;
|
|
|
|
enc->iv_len = ivlen;
|
|
|
|
ssh->kex->newkeys[mode] = newkey;
|
|
|
|
newkey = NULL;
|
|
|
|
r = 0;
|
|
|
|
out:
|
2016-03-10 20:10:25 +00:00
|
|
|
free(newkey);
|
|
|
|
sshbuf_free(b);
|
2015-07-02 13:15:34 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore kex from blob for packet state de-serialization */
|
|
|
|
static int
|
|
|
|
kex_from_blob(struct sshbuf *m, struct kex **kexp)
|
|
|
|
{
|
|
|
|
struct kex *kex;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
|
|
|
|
(kex->my = sshbuf_new()) == NULL ||
|
|
|
|
(kex->peer = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
|
|
|
|
(r = sshbuf_get_stringb(m, kex->my)) != 0 ||
|
|
|
|
(r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
|
|
|
|
(r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
|
|
|
|
goto out;
|
|
|
|
kex->server = 1;
|
|
|
|
kex->done = 1;
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
if (r != 0 || kexp == NULL) {
|
|
|
|
if (kex != NULL) {
|
2016-03-10 20:10:25 +00:00
|
|
|
sshbuf_free(kex->my);
|
|
|
|
sshbuf_free(kex->peer);
|
2015-07-02 13:15:34 +00:00
|
|
|
free(kex);
|
|
|
|
}
|
|
|
|
if (kexp != NULL)
|
|
|
|
*kexp = NULL;
|
|
|
|
} else {
|
|
|
|
*kexp = kex;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restore packet state from content of blob 'm' (de-serialization).
|
|
|
|
* Note that 'm' will be partially consumed on parsing or any other errors.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
|
|
|
|
{
|
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
|
|
|
|
size_t ssh1keylen, rlen, slen, ilen, olen;
|
|
|
|
int r;
|
|
|
|
u_int ssh1cipher = 0;
|
|
|
|
|
|
|
|
if (!compat20) {
|
|
|
|
if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
|
|
|
|
(r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
|
|
|
|
(r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
|
|
|
|
(r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
|
|
|
|
return r;
|
|
|
|
if (ssh1cipher > INT_MAX)
|
|
|
|
return SSH_ERR_KEY_UNKNOWN_CIPHER;
|
|
|
|
ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
|
|
|
|
(int)ssh1cipher);
|
|
|
|
if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
|
|
|
|
cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
|
|
|
|
(r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
|
|
|
|
(r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
|
|
|
|
(r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
|
2016-03-10 20:10:25 +00:00
|
|
|
(r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
|
2015-07-02 13:15:34 +00:00
|
|
|
(r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
|
|
|
|
(r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
|
|
|
|
(r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
|
|
|
|
(r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
|
|
|
|
(r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
|
|
|
|
return r;
|
|
|
|
/*
|
|
|
|
* We set the time here so that in post-auth privsep slave we
|
|
|
|
* count from the completion of the authentication.
|
|
|
|
*/
|
|
|
|
state->rekey_time = monotime();
|
|
|
|
/* XXX ssh_set_newkeys overrides p_read.packets? XXX */
|
|
|
|
if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
|
|
|
|
(r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
|
|
|
|
return r;
|
2015-01-05 16:09:55 +00:00
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
|
|
|
|
(r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
|
|
|
|
return r;
|
|
|
|
if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
|
|
|
|
cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
cipher_set_keycontext(&state->send_context, keyout);
|
|
|
|
cipher_set_keycontext(&state->receive_context, keyin);
|
|
|
|
|
|
|
|
if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
|
|
|
|
(r = ssh_packet_set_postauth(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
sshbuf_reset(state->input);
|
|
|
|
sshbuf_reset(state->output);
|
|
|
|
if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
|
|
|
|
(r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
|
|
|
|
(r = sshbuf_put(state->input, input, ilen)) != 0 ||
|
|
|
|
(r = sshbuf_put(state->output, output, olen)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (sshbuf_len(m))
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
debug3("%s: done", __func__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NEW API */
|
|
|
|
|
|
|
|
/* put data to the outgoing packet */
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_put(struct ssh *ssh, const void *v, size_t len)
|
|
|
|
{
|
|
|
|
return sshbuf_put(ssh->state->outgoing_packet, v, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
|
|
|
|
{
|
|
|
|
return sshbuf_putb(ssh->state->outgoing_packet, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_put_u8(struct ssh *ssh, u_char val)
|
|
|
|
{
|
|
|
|
return sshbuf_put_u8(ssh->state->outgoing_packet, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
|
|
|
|
{
|
|
|
|
return sshbuf_put_u32(ssh->state->outgoing_packet, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
|
|
|
|
{
|
|
|
|
return sshbuf_put_u64(ssh->state->outgoing_packet, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
|
|
|
|
{
|
|
|
|
return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_put_cstring(struct ssh *ssh, const void *v)
|
|
|
|
{
|
|
|
|
return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
|
|
|
|
{
|
|
|
|
return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
|
|
|
|
}
|
|
|
|
|
2015-07-02 13:18:50 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
|
|
|
|
{
|
|
|
|
return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
|
|
|
|
}
|
2015-07-02 13:18:50 +00:00
|
|
|
#endif /* OPENSSL_HAS_ECC */
|
2015-07-02 13:15:34 +00:00
|
|
|
|
|
|
|
#ifdef WITH_SSH1
|
|
|
|
int
|
|
|
|
sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
|
|
|
|
{
|
|
|
|
return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
|
|
|
|
}
|
|
|
|
#endif /* WITH_SSH1 */
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
|
|
|
|
{
|
|
|
|
return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
|
|
|
|
}
|
|
|
|
#endif /* WITH_OPENSSL */
|
|
|
|
|
|
|
|
/* fetch data from the incoming packet */
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_get(struct ssh *ssh, void *valp, size_t len)
|
|
|
|
{
|
|
|
|
return sshbuf_get(ssh->state->incoming_packet, valp, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_get_u8(struct ssh *ssh, u_char *valp)
|
|
|
|
{
|
|
|
|
return sshbuf_get_u8(ssh->state->incoming_packet, valp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
|
|
|
|
{
|
|
|
|
return sshbuf_get_u32(ssh->state->incoming_packet, valp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
|
|
|
|
{
|
|
|
|
return sshbuf_get_u64(ssh->state->incoming_packet, valp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
|
|
|
|
{
|
|
|
|
return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
|
|
|
|
{
|
|
|
|
return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
|
|
|
|
{
|
|
|
|
return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
|
|
|
|
}
|
|
|
|
|
2015-07-02 13:18:50 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2015-07-02 13:15:34 +00:00
|
|
|
int
|
|
|
|
sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
|
|
|
|
{
|
|
|
|
return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
|
|
|
|
}
|
2015-07-02 13:18:50 +00:00
|
|
|
#endif /* OPENSSL_HAS_ECC */
|
2015-07-02 13:15:34 +00:00
|
|
|
|
|
|
|
#ifdef WITH_SSH1
|
|
|
|
int
|
|
|
|
sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
|
|
|
|
{
|
|
|
|
return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
|
|
|
|
}
|
|
|
|
#endif /* WITH_SSH1 */
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
|
|
|
|
{
|
|
|
|
return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
|
|
|
|
}
|
|
|
|
#endif /* WITH_OPENSSL */
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_get_end(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
if (sshbuf_len(ssh->state->incoming_packet) > 0)
|
|
|
|
return SSH_ERR_UNEXPECTED_TRAILING_DATA;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const u_char *
|
|
|
|
sshpkt_ptr(struct ssh *ssh, size_t *lenp)
|
|
|
|
{
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = sshbuf_len(ssh->state->incoming_packet);
|
|
|
|
return sshbuf_ptr(ssh->state->incoming_packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start a new packet */
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_start(struct ssh *ssh, u_char type)
|
|
|
|
{
|
|
|
|
u_char buf[9];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
DBG(debug("packet_start[%d]", type));
|
|
|
|
len = compat20 ? 6 : 9;
|
|
|
|
memset(buf, 0, len - 1);
|
|
|
|
buf[len - 1] = type;
|
|
|
|
sshbuf_reset(ssh->state->outgoing_packet);
|
|
|
|
return sshbuf_put(ssh->state->outgoing_packet, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send it */
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_send(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
if (compat20)
|
|
|
|
return ssh_packet_send2(ssh);
|
|
|
|
else
|
|
|
|
return ssh_packet_send1(ssh);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
va_list args;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (compat20) {
|
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
|
|
|
|
(r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
|
|
|
|
(r = sshpkt_put_cstring(ssh, buf)) != 0 ||
|
|
|
|
(r = sshpkt_put_cstring(ssh, "")) != 0 ||
|
|
|
|
(r = sshpkt_send(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
|
|
|
|
(r = sshpkt_put_cstring(ssh, buf)) != 0 ||
|
|
|
|
(r = sshpkt_send(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* roundup current message to pad bytes */
|
|
|
|
int
|
|
|
|
sshpkt_add_padding(struct ssh *ssh, u_char pad)
|
|
|
|
{
|
|
|
|
ssh->state->extra_pad = pad;
|
|
|
|
return 0;
|
2015-01-05 16:09:55 +00:00
|
|
|
}
|