2023-04-06 05:21:31 +02:00
|
|
|
/* $OpenBSD: packet.c,v 1.310 2023/04/06 03:21:31 djm Exp $ */
|
1999-10-27 05:42:43 +02:00
|
|
|
/*
|
1999-11-24 14:26:21 +01: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-04-04 06:38:59 +02:00
|
|
|
*
|
2000-09-16 04:29:08 +02: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-04-04 06:38:59 +02:00
|
|
|
* SSH2 packet format added by Markus Friedl.
|
2001-07-04 05:32:30 +02:00
|
|
|
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
2000-09-16 04:29:08 +02: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.
|
2000-04-04 06:38:59 +02:00
|
|
|
*
|
2000-09-16 04:29:08 +02:00
|
|
|
* 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.
|
1999-11-24 14:26:21 +01:00
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
|
|
|
#include "includes.h"
|
2016-09-12 03:22:38 +02:00
|
|
|
|
2006-07-10 13:08:03 +02:00
|
|
|
#include <sys/types.h>
|
2003-04-09 13:05:52 +02:00
|
|
|
#include "openbsd-compat/sys-queue.h"
|
2006-07-10 12:35:38 +02:00
|
|
|
#include <sys/socket.h>
|
2006-08-05 02:57:45 +02:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2006-07-10 12:35:38 +02:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2006-03-15 01:24:12 +01:00
|
|
|
#include <netinet/ip.h>
|
2006-09-22 11:22:17 +02:00
|
|
|
#include <arpa/inet.h>
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2006-07-12 14:22:46 +02:00
|
|
|
#include <errno.h>
|
2016-03-07 20:02:43 +01:00
|
|
|
#include <netdb.h>
|
2006-07-12 14:15:16 +02:00
|
|
|
#include <stdarg.h>
|
2006-08-05 03:37:59 +02:00
|
|
|
#include <stdio.h>
|
2006-08-05 03:34:19 +02:00
|
|
|
#include <stdlib.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <string.h>
|
2006-07-24 06:01:23 +02:00
|
|
|
#include <unistd.h>
|
2015-01-21 00:14:00 +01:00
|
|
|
#include <limits.h>
|
2019-10-28 05:53:25 +01:00
|
|
|
#ifdef HAVE_POLL_H
|
2018-12-27 04:25:24 +01:00
|
|
|
#include <poll.h>
|
2019-10-28 05:53:25 +01:00
|
|
|
#endif
|
2006-08-05 04:39:39 +02:00
|
|
|
#include <signal.h>
|
2013-05-16 12:28:16 +02:00
|
|
|
#include <time.h>
|
2006-07-12 14:15:16 +02:00
|
|
|
|
2018-07-20 05:48:51 +02:00
|
|
|
/*
|
|
|
|
* Explicitly include OpenSSL before zlib as some versions of OpenSSL have
|
|
|
|
* "free_func" in their headers, which zlib typedefs.
|
|
|
|
*/
|
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
# include <openssl/bn.h>
|
|
|
|
# include <openssl/evp.h>
|
|
|
|
# ifdef OPENSSL_HAS_ECC
|
|
|
|
# include <openssl/ec.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2020-01-23 11:24:29 +01:00
|
|
|
#ifdef WITH_ZLIB
|
2015-01-19 20:52:16 +01:00
|
|
|
#include <zlib.h>
|
2020-01-23 11:24:29 +01:00
|
|
|
#endif
|
2015-01-19 20:52:16 +01:00
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
#include "xmalloc.h"
|
2000-04-04 06:38:59 +02:00
|
|
|
#include "compat.h"
|
|
|
|
#include "ssh2.h"
|
2000-10-14 07:23:11 +02:00
|
|
|
#include "cipher.h"
|
2015-01-19 20:52:16 +01:00
|
|
|
#include "sshkey.h"
|
2000-04-04 06:38:59 +02:00
|
|
|
#include "kex.h"
|
2015-01-13 20:31:40 +01:00
|
|
|
#include "digest.h"
|
2001-02-15 04:01:59 +01:00
|
|
|
#include "mac.h"
|
2001-01-22 06:34:40 +01:00
|
|
|
#include "log.h"
|
|
|
|
#include "canohost.h"
|
2002-02-05 01:52:54 +01:00
|
|
|
#include "misc.h"
|
2014-07-18 06:11:24 +02:00
|
|
|
#include "channels.h"
|
2002-06-21 02:43:42 +02:00
|
|
|
#include "ssh.h"
|
2015-01-19 20:52:16 +01:00
|
|
|
#include "packet.h"
|
|
|
|
#include "ssherr.h"
|
|
|
|
#include "sshbuf.h"
|
2000-04-04 06:38:59 +02:00
|
|
|
|
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
#define DBG(x) x
|
|
|
|
#else
|
|
|
|
#define DBG(x)
|
|
|
|
#endif
|
|
|
|
|
2009-01-28 06:38:41 +01:00
|
|
|
#define PACKET_MAX_SIZE (256 * 1024)
|
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
struct packet_state {
|
|
|
|
u_int32_t seqnr;
|
|
|
|
u_int32_t packets;
|
|
|
|
u_int64_t blocks;
|
|
|
|
u_int64_t bytes;
|
|
|
|
};
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
struct packet {
|
|
|
|
TAILQ_ENTRY(packet) next;
|
|
|
|
u_char type;
|
2015-01-19 20:52:16 +01:00
|
|
|
struct sshbuf *payload;
|
2009-06-21 10:12:20 +02:00
|
|
|
};
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2009-06-21 10:12:20 +02: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;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Protocol flags for the remote side. */
|
|
|
|
u_int remote_protocol_flags;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Encryption context for receiving data. Only used for decryption. */
|
2016-08-03 07:41:57 +02:00
|
|
|
struct sshcipher_ctx *receive_context;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Encryption context for sending data. Only used for encryption. */
|
2016-08-03 07:41:57 +02:00
|
|
|
struct sshcipher_ctx *send_context;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Buffer for raw input data from the socket. */
|
2015-01-19 20:52:16 +01:00
|
|
|
struct sshbuf *input;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Buffer for raw output data going to the socket. */
|
2015-01-19 20:52:16 +01:00
|
|
|
struct sshbuf *output;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Buffer for the partial outgoing packet being constructed. */
|
2015-01-19 20:52:16 +01:00
|
|
|
struct sshbuf *outgoing_packet;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Buffer for the incoming packet currently being processed. */
|
2015-01-19 20:52:16 +01:00
|
|
|
struct sshbuf *incoming_packet;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Scratch buffer for packet compression/decompression. */
|
2015-01-19 20:52:16 +01:00
|
|
|
struct sshbuf *compression_buffer;
|
|
|
|
|
2020-01-23 11:24:29 +01:00
|
|
|
#ifdef WITH_ZLIB
|
2015-01-19 20:52:16 +01:00
|
|
|
/* Incoming/outgoing compression dictionaries */
|
|
|
|
z_stream compression_in_stream;
|
|
|
|
z_stream compression_out_stream;
|
2020-01-23 11:24:29 +01:00
|
|
|
#endif
|
2015-01-19 20:52:16 +01:00
|
|
|
int compression_in_started;
|
|
|
|
int compression_out_started;
|
|
|
|
int compression_in_failures;
|
|
|
|
int compression_out_failures;
|
1999-11-21 03:23:52 +01:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* default maximum packet size */
|
|
|
|
u_int max_packet_size;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Flag indicating whether this module has been initialized. */
|
|
|
|
int initialized;
|
2005-07-26 13:54:56 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Set to true if the connection is interactive. */
|
|
|
|
int interactive_mode;
|
2005-07-26 13:54:56 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Set to true if we are the server side. */
|
|
|
|
int server_side;
|
2008-03-07 08:33:30 +01:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Set to true if we are authenticated. */
|
|
|
|
int after_authentication;
|
2008-06-12 22:42:45 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
int keep_alive_timeouts;
|
2003-04-09 12:50:06 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* The maximum time that we will wait to send or receive a packet */
|
|
|
|
int packet_timeout_ms;
|
2000-04-04 06:38:59 +02:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* Session key information for Encryption and MAC */
|
2015-01-19 20:52:16 +01:00
|
|
|
struct newkeys *newkeys[MODE_MAX];
|
2009-06-21 10:12:20 +02:00
|
|
|
struct packet_state p_read, p_send;
|
2002-06-21 02:43:42 +02:00
|
|
|
|
2013-05-16 12:28:16 +02:00
|
|
|
/* Volume-based rekeying */
|
2016-01-29 03:54:45 +01:00
|
|
|
u_int64_t max_blocks_in, max_blocks_out, rekey_limit;
|
2001-11-12 01:02:52 +01:00
|
|
|
|
2013-05-16 12:28:16 +02:00
|
|
|
/* Time-based rekeying */
|
2015-02-13 19:57:00 +01:00
|
|
|
u_int32_t rekey_interval; /* how often in seconds */
|
2013-05-16 12:28:16 +02:00
|
|
|
time_t rekey_time; /* time of last rekeying */
|
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
/* roundup current message to extra_pad bytes */
|
|
|
|
u_char extra_pad;
|
|
|
|
|
|
|
|
/* XXX discard incoming data after MAC error */
|
|
|
|
u_int packet_discard;
|
2016-07-18 13:35:33 +02:00
|
|
|
size_t packet_discard_mac_already;
|
2015-01-19 20:52:16 +01:00
|
|
|
struct sshmac *packet_discard_mac;
|
2009-06-21 10:12:20 +02:00
|
|
|
|
|
|
|
/* Used in packet_read_poll2() */
|
|
|
|
u_int packlen;
|
|
|
|
|
2009-06-21 10:59:36 +02:00
|
|
|
/* Used in packet_send2 */
|
|
|
|
int rekeying;
|
|
|
|
|
2016-09-30 11:19:13 +02:00
|
|
|
/* Used in ssh_packet_send_mux() */
|
|
|
|
int mux;
|
|
|
|
|
2009-06-21 10:59:36 +02:00
|
|
|
/* Used in packet_set_interactive */
|
|
|
|
int set_interactive_called;
|
|
|
|
|
|
|
|
/* Used in packet_set_maxsize */
|
|
|
|
int set_maxsize_called;
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
/* One-off warning about weak ciphers */
|
|
|
|
int cipher_warning_done;
|
2009-06-21 10:12:20 +02:00
|
|
|
|
2016-10-11 23:47:45 +02:00
|
|
|
/* Hook for fuzzing inbound packets */
|
|
|
|
ssh_packet_hook_fn *hook_in;
|
|
|
|
void *hook_in_ctx;
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
TAILQ_HEAD(, packet) outgoing;
|
|
|
|
};
|
2009-06-21 10:12:20 +02:00
|
|
|
|
2015-01-19 20:52:16 +01: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 ||
|
2018-12-27 04:25:24 +01:00
|
|
|
(ssh->kex = kex_new()) == NULL ||
|
2015-01-19 20:52:16 +01:00
|
|
|
(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);
|
2015-01-19 21:30:23 +01:00
|
|
|
TAILQ_INIT(&ssh->private_keys);
|
|
|
|
TAILQ_INIT(&ssh->public_keys);
|
2015-01-19 20:52:16 +01:00
|
|
|
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:
|
2018-12-27 04:25:24 +01:00
|
|
|
if (ssh) {
|
|
|
|
kex_free(ssh->kex);
|
|
|
|
free(ssh);
|
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
if (state) {
|
|
|
|
sshbuf_free(state->input);
|
|
|
|
sshbuf_free(state->output);
|
|
|
|
sshbuf_free(state->incoming_packet);
|
|
|
|
sshbuf_free(state->outgoing_packet);
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
return NULL;
|
2009-06-21 10:12:20 +02:00
|
|
|
}
|
2003-04-09 12:50:06 +02:00
|
|
|
|
2016-10-11 23:47:45 +02:00
|
|
|
void
|
|
|
|
ssh_packet_set_input_hook(struct ssh *ssh, ssh_packet_hook_fn *hook, void *ctx)
|
|
|
|
{
|
|
|
|
ssh->state->hook_in = hook;
|
|
|
|
ssh->state->hook_in_ctx = ctx;
|
|
|
|
}
|
|
|
|
|
2016-02-08 11:57:07 +01:00
|
|
|
/* Returns nonzero if rekeying is in progress */
|
|
|
|
int
|
|
|
|
ssh_packet_is_rekeying(struct ssh *ssh)
|
|
|
|
{
|
2020-06-24 17:10:38 +02:00
|
|
|
return ssh->state->rekeying ||
|
|
|
|
(ssh->kex != NULL && ssh->kex->done == 0);
|
2016-02-08 11:57:07 +01:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
2017-05-03 23:08:09 +02:00
|
|
|
* Sets the descriptors used for communication.
|
1999-11-25 01:54:57 +01:00
|
|
|
*/
|
2015-01-19 20:52:16 +01:00
|
|
|
struct ssh *
|
|
|
|
ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state;
|
|
|
|
const struct sshcipher *none = cipher_by_name("none");
|
2014-07-02 07:28:02 +02:00
|
|
|
int r;
|
2002-07-08 00:11:51 +02:00
|
|
|
|
2015-01-30 02:13:33 +01:00
|
|
|
if (none == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("cannot load cipher 'none'");
|
2015-01-30 02:13:33 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
if (ssh == NULL)
|
|
|
|
ssh = ssh_alloc_session_state();
|
2015-01-30 02:13:33 +01:00
|
|
|
if (ssh == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("could not allocate state");
|
2015-01-30 02:13:33 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
state = ssh->state;
|
|
|
|
state->connection_in = fd_in;
|
|
|
|
state->connection_out = fd_out;
|
|
|
|
if ((r = cipher_init(&state->send_context, none,
|
2014-07-02 07:28:02 +02:00
|
|
|
(const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
|
2015-01-19 20:52:16 +01:00
|
|
|
(r = cipher_init(&state->receive_context, none,
|
2015-01-30 02:13:33 +01:00
|
|
|
(const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_fr(r, "cipher_init failed");
|
2016-03-07 20:02:43 +01:00
|
|
|
free(ssh); /* XXX need ssh_free_session_state? */
|
2015-01-30 02:13:33 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
|
2015-02-11 02:20:38 +01:00
|
|
|
/*
|
|
|
|
* 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);
|
2015-01-19 20:52:16 +01:00
|
|
|
return ssh;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2008-06-12 22:42:45 +02:00
|
|
|
void
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
|
2008-06-12 22:42:45 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
|
2011-12-19 00:52:50 +01:00
|
|
|
if (timeout <= 0 || count <= 0) {
|
2015-01-19 20:52:16 +01:00
|
|
|
state->packet_timeout_ms = -1;
|
2008-06-12 22:42:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((INT_MAX / 1000) / count < timeout)
|
2015-01-19 20:52:16 +01:00
|
|
|
state->packet_timeout_ms = INT_MAX;
|
2008-06-12 22:42:45 +02:00
|
|
|
else
|
2015-01-19 20:52:16 +01:00
|
|
|
state->packet_timeout_ms = timeout * count * 1000;
|
2008-06-12 22:42:45 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 11:19:13 +02:00
|
|
|
void
|
|
|
|
ssh_packet_set_mux(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
ssh->state->mux = 1;
|
|
|
|
ssh->state->rekeying = 0;
|
2020-06-24 17:10:38 +02:00
|
|
|
kex_free(ssh->kex);
|
|
|
|
ssh->kex = NULL;
|
2016-09-30 11:19:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ssh_packet_get_mux(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
return ssh->state->mux;
|
|
|
|
}
|
|
|
|
|
2017-02-04 00:03:33 +01:00
|
|
|
int
|
|
|
|
ssh_packet_set_log_preamble(struct ssh *ssh, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
free(ssh->log_preamble);
|
|
|
|
if (fmt == NULL)
|
|
|
|
ssh->log_preamble = NULL;
|
|
|
|
else {
|
|
|
|
va_start(args, fmt);
|
|
|
|
r = vasprintf(&ssh->log_preamble, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
if (r < 0 || ssh->log_preamble == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
int
|
|
|
|
ssh_packet_stop_discard(struct ssh *ssh)
|
2009-01-28 06:38:41 +01:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (state->packet_discard_mac) {
|
2009-01-28 06:38:41 +01:00
|
|
|
char buf[1024];
|
2016-07-18 13:35:33 +02:00
|
|
|
size_t dlen = PACKET_MAX_SIZE;
|
2015-01-19 20:52:16 +01:00
|
|
|
|
2016-07-18 13:35:33 +02:00
|
|
|
if (dlen > state->packet_discard_mac_already)
|
|
|
|
dlen -= state->packet_discard_mac_already;
|
2009-01-28 06:38:41 +01:00
|
|
|
memset(buf, 'a', sizeof(buf));
|
2016-07-18 13:35:33 +02:00
|
|
|
while (sshbuf_len(state->incoming_packet) < dlen)
|
2015-01-19 20:52:16 +01: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,
|
2016-07-18 13:35:33 +02:00
|
|
|
sshbuf_ptr(state->incoming_packet), dlen,
|
2015-01-19 20:52:16 +01:00
|
|
|
NULL, 0);
|
|
|
|
}
|
2015-12-11 04:24:25 +01:00
|
|
|
logit("Finished discarding for %.200s port %d",
|
|
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
|
2015-01-19 20:52:16 +01:00
|
|
|
return SSH_ERR_MAC_INVALID;
|
2009-01-28 06:38:41 +01:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
static int
|
|
|
|
ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
|
2016-07-18 13:35:33 +02:00
|
|
|
struct sshmac *mac, size_t mac_already, u_int discard)
|
2009-01-28 06:38:41 +01:00
|
|
|
{
|
2015-01-19 20:52:16 +01: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;
|
|
|
|
}
|
2016-07-18 13:35:33 +02:00
|
|
|
/*
|
|
|
|
* Record number of bytes over which the mac has already
|
|
|
|
* been computed in order to minimize timing attacks.
|
|
|
|
*/
|
|
|
|
if (mac && mac->enabled) {
|
2015-01-19 20:52:16 +01:00
|
|
|
state->packet_discard_mac = mac;
|
2016-07-18 13:35:33 +02:00
|
|
|
state->packet_discard_mac_already = mac_already;
|
|
|
|
}
|
|
|
|
if (sshbuf_len(state->input) >= discard)
|
|
|
|
return ssh_packet_stop_discard(ssh);
|
2015-01-19 20:52:16 +01:00
|
|
|
state->packet_discard = discard - sshbuf_len(state->input);
|
|
|
|
return 0;
|
2009-01-28 06:38:41 +01:00
|
|
|
}
|
|
|
|
|
2000-01-14 05:45:46 +01:00
|
|
|
/* Returns 1 if remote host is connected via socket, 0 if not. */
|
|
|
|
|
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_connection_is_on_socket(struct ssh *ssh)
|
2000-01-14 05:45:46 +01:00
|
|
|
{
|
2018-06-01 06:05:29 +02:00
|
|
|
struct session_state *state;
|
2000-01-14 05:45:46 +01:00
|
|
|
struct sockaddr_storage from, to;
|
|
|
|
socklen_t fromlen, tolen;
|
|
|
|
|
2018-06-01 06:05:29 +02:00
|
|
|
if (ssh == NULL || ssh->state == NULL)
|
2016-03-07 20:02:43 +01:00
|
|
|
return 0;
|
|
|
|
|
2018-06-01 06:05:29 +02:00
|
|
|
state = ssh->state;
|
|
|
|
if (state->connection_in == -1 || state->connection_out == -1)
|
|
|
|
return 0;
|
2000-01-14 05:45:46 +01:00
|
|
|
/* filedescriptors in and out are the same, so it's a socket */
|
2015-01-19 20:52:16 +01:00
|
|
|
if (state->connection_in == state->connection_out)
|
2000-01-14 05:45:46 +01:00
|
|
|
return 1;
|
|
|
|
fromlen = sizeof(from);
|
|
|
|
memset(&from, 0, sizeof(from));
|
2015-01-19 20:52:16 +01:00
|
|
|
if (getpeername(state->connection_in, (struct sockaddr *)&from,
|
2019-06-28 15:35:04 +02:00
|
|
|
&fromlen) == -1)
|
2000-01-14 05:45:46 +01:00
|
|
|
return 0;
|
|
|
|
tolen = sizeof(to);
|
|
|
|
memset(&to, 0, sizeof(to));
|
2015-01-19 20:52:16 +01:00
|
|
|
if (getpeername(state->connection_out, (struct sockaddr *)&to,
|
2019-06-28 15:35:04 +02:00
|
|
|
&tolen) == -1)
|
2000-01-14 05:45:46 +01: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-03-22 02:42:04 +01:00
|
|
|
void
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
|
2002-03-22 02:42:04 +01:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
if (ibytes)
|
|
|
|
*ibytes = ssh->state->p_read.bytes;
|
|
|
|
if (obytes)
|
|
|
|
*obytes = ssh->state->p_send.bytes;
|
2002-03-22 02:42:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_connection_af(struct ssh *ssh)
|
2000-01-14 05:45:46 +01:00
|
|
|
{
|
2020-11-27 01:49:58 +01:00
|
|
|
return get_sock_af(ssh->state->connection_out);
|
2000-01-14 05:45:46 +01:00
|
|
|
}
|
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
/* Sets the connection into non-blocking mode. */
|
|
|
|
|
|
|
|
void
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_set_nonblocking(struct ssh *ssh)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Set the socket into non-blocking mode. */
|
2015-01-19 20:52:16 +01:00
|
|
|
set_nonblock(ssh->state->connection_in);
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
if (ssh->state->connection_out != ssh->state->connection_in)
|
|
|
|
set_nonblock(ssh->state->connection_out);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the socket used for reading. */
|
|
|
|
|
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_get_connection_in(struct ssh *ssh)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
return ssh->state->connection_in;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the descriptor used for writing. */
|
|
|
|
|
|
|
|
int
|
2015-01-19 20:52:16 +01: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)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2018-06-01 06:05:29 +02:00
|
|
|
int sock;
|
2015-12-11 04:24:25 +01:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
/* Check whether we have cached the ipaddr. */
|
2015-12-11 04:24:25 +01:00
|
|
|
if (ssh->remote_ipaddr == NULL) {
|
|
|
|
if (ssh_packet_connection_is_on_socket(ssh)) {
|
2018-06-01 06:05:29 +02:00
|
|
|
sock = ssh->state->connection_in;
|
2015-12-11 04:24:25 +01:00
|
|
|
ssh->remote_ipaddr = get_peer_ipaddr(sock);
|
2016-03-07 20:02:43 +01:00
|
|
|
ssh->remote_port = get_peer_port(sock);
|
|
|
|
ssh->local_ipaddr = get_local_ipaddr(sock);
|
|
|
|
ssh->local_port = get_local_port(sock);
|
2015-12-11 04:24:25 +01:00
|
|
|
} else {
|
2019-12-16 14:58:53 +01:00
|
|
|
ssh->remote_ipaddr = xstrdup("UNKNOWN");
|
2016-03-07 20:02:43 +01:00
|
|
|
ssh->remote_port = 65535;
|
2019-12-16 14:58:53 +01:00
|
|
|
ssh->local_ipaddr = xstrdup("UNKNOWN");
|
2016-03-07 20:02:43 +01:00
|
|
|
ssh->local_port = 65535;
|
2015-12-11 04:24:25 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
return ssh->remote_ipaddr;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2015-12-11 04:24:25 +01: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;
|
|
|
|
}
|
|
|
|
|
2016-03-07 20:02:43 +01: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;
|
|
|
|
}
|
|
|
|
|
2017-10-25 02:17:08 +02:00
|
|
|
/* Returns the routing domain of the input socket, or NULL if unavailable */
|
|
|
|
const char *
|
|
|
|
ssh_packet_rdomain_in(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
if (ssh->rdomain_in != NULL)
|
|
|
|
return ssh->rdomain_in;
|
|
|
|
if (!ssh_packet_connection_is_on_socket(ssh))
|
|
|
|
return NULL;
|
|
|
|
ssh->rdomain_in = get_rdomain(ssh->state->connection_in);
|
|
|
|
return ssh->rdomain_in;
|
|
|
|
}
|
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
/* Closes the connection and clears and frees internal data structures. */
|
|
|
|
|
2017-05-31 10:09:45 +02:00
|
|
|
static void
|
|
|
|
ssh_packet_close_internal(struct ssh *ssh, int do_close)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
u_int mode;
|
|
|
|
|
|
|
|
if (!state->initialized)
|
1999-11-24 14:26:21 +01:00
|
|
|
return;
|
2015-01-19 20:52:16 +01:00
|
|
|
state->initialized = 0;
|
2017-05-31 10:09:45 +02:00
|
|
|
if (do_close) {
|
|
|
|
if (state->connection_in == state->connection_out) {
|
|
|
|
close(state->connection_out);
|
|
|
|
} else {
|
|
|
|
close(state->connection_in);
|
|
|
|
close(state->connection_out);
|
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
}
|
|
|
|
sshbuf_free(state->input);
|
|
|
|
sshbuf_free(state->output);
|
|
|
|
sshbuf_free(state->outgoing_packet);
|
|
|
|
sshbuf_free(state->incoming_packet);
|
2017-05-31 10:09:45 +02:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
|
|
|
kex_free_newkeys(state->newkeys[mode]); /* current keys */
|
|
|
|
state->newkeys[mode] = NULL;
|
|
|
|
ssh_clear_newkeys(ssh, mode); /* next keys */
|
|
|
|
}
|
2020-01-23 11:24:29 +01:00
|
|
|
#ifdef WITH_ZLIB
|
2020-01-23 11:53:04 +01:00
|
|
|
/* compression state is in shared mem, so we can only release it once */
|
2017-05-31 10:09:45 +02:00
|
|
|
if (do_close && state->compression_buffer) {
|
2015-01-19 20:52:16 +01:00
|
|
|
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) {
|
2017-06-06 11:12:17 +02:00
|
|
|
z_streamp stream = &state->compression_in_stream;
|
2015-01-19 20:52:16 +01:00
|
|
|
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);
|
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
2020-01-23 11:24:29 +01:00
|
|
|
#endif /* WITH_ZLIB */
|
2016-08-03 07:41:57 +02:00
|
|
|
cipher_free(state->send_context);
|
|
|
|
cipher_free(state->receive_context);
|
|
|
|
state->send_context = state->receive_context = NULL;
|
2017-05-31 10:09:45 +02:00
|
|
|
if (do_close) {
|
2018-07-16 05:09:13 +02:00
|
|
|
free(ssh->local_ipaddr);
|
|
|
|
ssh->local_ipaddr = NULL;
|
2017-05-31 10:09:45 +02:00
|
|
|
free(ssh->remote_ipaddr);
|
|
|
|
ssh->remote_ipaddr = NULL;
|
|
|
|
free(ssh->state);
|
|
|
|
ssh->state = NULL;
|
2020-07-01 18:28:31 +02:00
|
|
|
kex_free(ssh->kex);
|
|
|
|
ssh->kex = NULL;
|
2017-05-31 10:09:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ssh_packet_close(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
ssh_packet_close_internal(ssh, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ssh_packet_clear_keys(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
ssh_packet_close_internal(ssh, 0);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets remote side protocol flags. */
|
|
|
|
|
|
|
|
void
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh->state->remote_protocol_flags = protocol_flags;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the remote protocol flags set earlier by the above function. */
|
|
|
|
|
2000-12-22 02:43:59 +01:00
|
|
|
u_int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_get_protocol_flags(struct ssh *ssh)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
return ssh->state->remote_protocol_flags;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
|
|
|
* Starts packet compression from the next packet on in both directions.
|
|
|
|
* Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
|
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
static int
|
|
|
|
ssh_packet_init_compression(struct ssh *ssh)
|
2001-04-06 01:20:46 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
if (!ssh->state->compression_buffer &&
|
2021-04-03 08:18:40 +02:00
|
|
|
((ssh->state->compression_buffer = sshbuf_new()) == NULL))
|
2015-01-19 20:52:16 +01:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
return 0;
|
2001-04-06 01:20:46 +02:00
|
|
|
}
|
|
|
|
|
2020-01-23 11:24:29 +01:00
|
|
|
#ifdef WITH_ZLIB
|
2015-01-19 20:52:16 +01: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;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:52:16 +01: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-21 02:43:42 +02:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
/* XXX remove need for separate compression buffer */
|
|
|
|
static int
|
|
|
|
compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
|
2002-06-21 02:43:42 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
u_char buf[4096];
|
|
|
|
int r, status;
|
2002-06-21 02:43:42 +02:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
if (ssh->state->compression_out_started != 1)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2000-04-04 06:38:59 +02:00
|
|
|
|
2015-01-19 20:52:16 +01: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-04-04 06:38:59 +02:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
static int
|
|
|
|
uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
u_char buf[4096];
|
|
|
|
int r, status;
|
2002-07-08 00:11:51 +02:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
if (ssh->state->compression_in_started != 1)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2006-03-26 05:10:14 +02:00
|
|
|
|
2015-01-19 20:52:16 +01: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);
|
2006-03-26 05:10:14 +02:00
|
|
|
|
2015-01-19 20:52:16 +01: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,
|
2022-01-17 23:56:04 +01:00
|
|
|
Z_SYNC_FLUSH);
|
2015-01-19 20:52:16 +01:00
|
|
|
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 */
|
2009-06-21 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-23 11:24:29 +01:00
|
|
|
#else /* WITH_ZLIB */
|
|
|
|
|
|
|
|
static int
|
|
|
|
start_compression_out(struct ssh *ssh, int level)
|
|
|
|
{
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
start_compression_in(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
|
|
|
|
{
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
|
|
|
|
{
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
#endif /* WITH_ZLIB */
|
|
|
|
|
2017-05-31 10:09:45 +02:00
|
|
|
void
|
|
|
|
ssh_clear_newkeys(struct ssh *ssh, int mode)
|
|
|
|
{
|
2017-06-01 08:16:43 +02:00
|
|
|
if (ssh->kex && ssh->kex->newkeys[mode]) {
|
2017-05-31 10:09:45 +02:00
|
|
|
kex_free_newkeys(ssh->kex->newkeys[mode]);
|
|
|
|
ssh->kex->newkeys[mode] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
int
|
|
|
|
ssh_set_newkeys(struct ssh *ssh, int mode)
|
2001-04-04 04:00:54 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
struct sshenc *enc;
|
|
|
|
struct sshmac *mac;
|
|
|
|
struct sshcomp *comp;
|
2016-08-03 07:41:57 +02:00
|
|
|
struct sshcipher_ctx **ccp;
|
2016-09-06 11:22:56 +02:00
|
|
|
struct packet_state *ps;
|
2003-04-09 12:50:06 +02:00
|
|
|
u_int64_t *max_blocks;
|
2017-06-01 08:16:43 +02:00
|
|
|
const char *wmsg;
|
2014-07-02 07:28:02 +02:00
|
|
|
int r, crypt_type;
|
2019-03-01 04:29:32 +01:00
|
|
|
const char *dir = mode == MODE_OUT ? "out" : "in";
|
2001-04-04 04:00:54 +02:00
|
|
|
|
2021-11-26 00:02:24 +01:00
|
|
|
debug2_f("mode %d", mode);
|
2001-04-04 04:00:54 +02:00
|
|
|
|
2002-02-19 05:21:23 +01:00
|
|
|
if (mode == MODE_OUT) {
|
2016-08-03 07:41:57 +02:00
|
|
|
ccp = &state->send_context;
|
2004-06-22 04:56:01 +02:00
|
|
|
crypt_type = CIPHER_ENCRYPT;
|
2016-09-06 11:22:56 +02:00
|
|
|
ps = &state->p_send;
|
2015-01-19 20:52:16 +01:00
|
|
|
max_blocks = &state->max_blocks_out;
|
2002-02-19 05:21:23 +01:00
|
|
|
} else {
|
2016-08-03 07:41:57 +02:00
|
|
|
ccp = &state->receive_context;
|
2004-06-22 04:56:01 +02:00
|
|
|
crypt_type = CIPHER_DECRYPT;
|
2016-09-06 11:22:56 +02:00
|
|
|
ps = &state->p_read;
|
2015-01-19 20:52:16 +01:00
|
|
|
max_blocks = &state->max_blocks_in;
|
2002-02-19 05:21:23 +01:00
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
if (state->newkeys[mode] != NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
debug_f("rekeying %s, input %llu bytes %llu blocks, "
|
2021-04-03 08:18:40 +02:00
|
|
|
"output %llu bytes %llu blocks", dir,
|
|
|
|
(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);
|
2017-05-31 10:09:45 +02:00
|
|
|
kex_free_newkeys(state->newkeys[mode]);
|
|
|
|
state->newkeys[mode] = NULL;
|
2015-01-19 20:52:16 +01:00
|
|
|
}
|
2016-09-06 11:22:56 +02:00
|
|
|
/* note that both bytes and the seqnr are not reset */
|
|
|
|
ps->packets = ps->blocks = 0;
|
2015-01-19 20:52:16 +01:00
|
|
|
/* 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;
|
2021-11-26 00:02:24 +01:00
|
|
|
DBG(debug_f("cipher_init: %s", dir));
|
2019-01-04 04:23:00 +01:00
|
|
|
cipher_free(*ccp);
|
|
|
|
*ccp = NULL;
|
2016-08-03 07:41:57 +02:00
|
|
|
if ((r = cipher_init(ccp, enc->cipher, enc->key, enc->key_len,
|
2014-07-02 07:28:02 +02:00
|
|
|
enc->iv, enc->iv_len, crypt_type)) != 0)
|
2015-01-19 20:52:16 +01:00
|
|
|
return r;
|
|
|
|
if (!state->cipher_warning_done &&
|
2016-08-03 07:41:57 +02:00
|
|
|
(wmsg = cipher_warning_message(*ccp)) != NULL) {
|
2015-01-19 20:52:16 +01:00
|
|
|
error("Warning: %s", wmsg);
|
|
|
|
state->cipher_warning_done = 1;
|
|
|
|
}
|
2002-03-22 02:42:04 +01:00
|
|
|
/* Deleting the keys does not gain extra security */
|
2014-02-04 01:20:14 +01:00
|
|
|
/* explicit_bzero(enc->iv, enc->block_size);
|
|
|
|
explicit_bzero(enc->key, enc->key_len);
|
|
|
|
explicit_bzero(mac->key, mac->key_len); */
|
2018-07-09 15:37:10 +02:00
|
|
|
if ((comp->type == COMP_ZLIB ||
|
|
|
|
(comp->type == COMP_DELAYED &&
|
2021-04-03 08:18:40 +02:00
|
|
|
state->after_authentication)) && comp->enabled == 0) {
|
2015-01-19 20:52:16 +01: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;
|
|
|
|
}
|
2001-04-04 04:00:54 +02:00
|
|
|
comp->enabled = 1;
|
|
|
|
}
|
2003-07-14 09:31:06 +02:00
|
|
|
/*
|
|
|
|
* The 2^(blocksize*2) limit is too expensive for 3DES,
|
2017-05-08 01:12:57 +02:00
|
|
|
* so enforce a 1GB limit for small blocksizes.
|
2017-06-09 06:40:04 +02:00
|
|
|
* See RFC4344 section 3.2.
|
2003-07-14 09:31:06 +02:00
|
|
|
*/
|
|
|
|
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-01-19 20:52:16 +01:00
|
|
|
if (state->rekey_limit)
|
2016-09-12 03:22:38 +02:00
|
|
|
*max_blocks = MINIMUM(*max_blocks,
|
2015-01-19 20:52:16 +01:00
|
|
|
state->rekey_limit / enc->block_size);
|
2019-03-01 04:29:32 +01:00
|
|
|
debug("rekey %s after %llu blocks", dir,
|
|
|
|
(unsigned long long)*max_blocks);
|
2015-01-19 20:52:16 +01:00
|
|
|
return 0;
|
2001-04-04 04:00:54 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 11:57:07 +01: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. */
|
2018-12-27 04:25:24 +01:00
|
|
|
if (ssh_packet_is_rekeying(ssh))
|
2016-02-08 11:57:07 +01:00
|
|
|
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 &&
|
2017-02-03 03:56:00 +01:00
|
|
|
(int64_t)state->rekey_time + state->rekey_interval <= monotime())
|
2016-02-08 11:57:07 +01:00
|
|
|
return 1;
|
|
|
|
|
2017-06-09 06:40:04 +02:00
|
|
|
/*
|
Merge 9.1 (#626)
* upstream: fix poll() spin when a channel's output fd closes without
data in the channel buffer. Introduce more exact packing of channel fds into
the pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@
OpenBSD-Commit-ID: 06740737849c9047785622ad5d472cb6a3907d10
* upstream: select post-quantum KEX
sntrup761x25519-sha512@openssh.com as the default; ok markus@
OpenBSD-Commit-ID: f02d99cbfce22dffec2e2ab1b60905fbddf48fb9
* upstream: add support for the "corp-data" protocol extension to
allow server-side copies to be performed without having to go via the client.
Patch by Mike Frysinger, ok dtucker@
OpenBSD-Commit-ID: 00aa510940fedd66dab1843b58682de4eb7156d5
* upstream: add a sftp client "cp" command that supports server-side
copying of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@
OpenBSD-Commit-ID: 1bb1b950af0d49f0d5425b1f267e197aa1b57444
* depend
* Skip slow tests on (very) slow test targets.
* Set Makefile SHELL as determined by configure.
This should improve compatibility for users with non-POSIX shells. If
using Makefile.in directly (eg make -f Makefile.in distprep) then SHELL
will need to be specified on the command line (along with MANFMT in that
particular case). ok djm@
* Use bash or ksh if available for SH in Makefile.
* Increase test timeout to allow slow VMs to finish
* Only run regression tests on slow VMs.
* Only return events from ppoll that were requested.
If the underlying system's select() returns bits that were not in the
request set, our ppoll() implementation can return revents for events
not requested, which can apparently cause a hang. Only return revents
for activity in the requested event set. bz#3416, analysis and fix by
yaroslav.kuzmin at vmssoftware com, ok djm@
* Specify TEST_SHELL=bash on AIX.
The system shells cause the agent-restrict test to fail due to some
quoting so explicitly specify bash until we can get configure to
autmatically work around that.
* Disable security key on fbsd6 test host.
* upstream: man pages: add missing commas between subordinate and
main clauses
jmc@ dislikes a comma before "then" in a conditional, so leave those
untouched.
ok jmc@
OpenBSD-Commit-ID: 9520801729bebcb3c9fe43ad7f9776ab4dd05ea3
* upstream: ssh: document sntrup761x25519-sha512@openssh.com as
default KEX
OpenBSD-Commit-ID: 12545bfa10bcbf552d04d9d9520d0f4e98b0e171
* upstream: openssh-9.0
OpenBSD-Commit-ID: 0dfb461188f4513ec024c1534da8c1ce14c20b64
* update version numbers for release
* update build-aux files to match autoconf-2.71
i.e. config.guess, config.sub and install-sh
* Revert "update build-aux files to match autoconf-2.71"
This reverts commit 0a8ca39fac6ad19096b6c263436f8b2dd51606f2.
It turns out that the checked-in copies of these files are actually newer
than autoconf-2.71's copies, so this was effectively a downgrade.
Spotted by Bo Anderson via github
* upstream: two defensive changes from Tobias Stoeckmann via GHPR287
enforce stricter invarient for sshbuf_set_parent() - never allow
a buffer to have a previously-set parent changed.
In sshbuf_reset(), if the reallocation fails, then zero the entire
buffer and not the (potentially smaller) default initial alloc size.
OpenBSD-Commit-ID: 14583203aa5d50ad38d2e209ae10abaf8955e6a9
* upstream: Note that curve25519-sha256 was later published in
RFC8731. ok djm@
OpenBSD-Commit-ID: 2ac2b5d642d4cf5918eaec8653cad9a4460b2743
* upstream: clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are reused
across cycles. ok markus@ deraadt@
OpenBSD-Commit-ID: 91034f855b7c73cd2591657c49ac30f10322b967
* Retire fbsd6 test VM.
It's long since out of support, relatively slow (it's i686) and the
compiler has trouble with PIE.
* Resync moduli.5 with upstream.
1.18: remove duplicate publication year; carsten dot kunze at arcor dot de
1.19: ssh-keygen's -G/-T have been replaced with -M generate/screen.
* upstream: Correct path for system known hosts file in description
of IgnoreUserKnownHosts. Patch from Martin Vahlensieck via tech@
OpenBSD-Commit-ID: 9b7784f054fa5aa4d63cb36bd563889477127215
* upstream: list the correct version number
for when usage of the sftp protocol became default and fix a typo
from ed maste
OpenBSD-Commit-ID: 24e1795ed2283fdeacf16413c2f07503bcdebb31
* upstream: Import regenerated moduli
OpenBSD-Commit-ID: f9a0726d957cf10692a231996a1f34e7f9cdfeb0
* upstream: Try to continue running local I/O for channels in state
OPEN during SSH transport rekeying. The most visible benefit is that it
should make ~-escapes work in the client (e.g. to exit) if the connection
happened to have stalled during a rekey event. Based work by and ok dtucker@
OpenBSD-Commit-ID: a66e8f254e92edd4ce09c9f750883ec8f1ea5f45
* upstream: Import regenerated moduli
OpenBSD-Commit-ID: f9a0726d957cf10692a231996a1f34e7f9cdfeb0
* upstream: regression test for sftp cp command
OpenBSD-Regress-ID: c96bea9edde3a384b254785e7f9b2b24a81cdf82
* upstream: Simplify forward-control test.
Since we no longer need to support SSH1 we don't need to run shell
commands on the other end of the connection and can use ssh -N instead.
This also makes the test less racy.
OpenBSD-Regress-ID: 32e94ce272820cc398f30b848b2b0f080d10302c
* upstream: Use ssh -f and ControlPersist ..
to start up test forwards and ssh -O stop to shut them down intead of
sleep loops. This speeds up the test by an order of magnitude.
OpenBSD-Regress-ID: eb3db5f805100919b092a3b2579c611fba3e83e7
* upstream: It looks like we can't completely avoid
waiting for processes to exit so retrieve the pid via controlmaster and
use that.
OpenBSD-Regress-ID: 8246f00f22b14e49d2ff1744c94897ead33d457b
* Cache timezone data in capsicum sandbox.
From emaste at freebsd.org, originally part of FreeBSD commit r339216
/ fc3c19a9 with autoconf bits added by me.
* Include stdlib.h for free() prototype.
... which is used inside the CUSTOM_SYS_AUTH_GET_LASTLOGIN_MSG block.
* Update OpenSSL and LibreSSL versions in tests.
* Add debian-riscv64 test target.
* upstream: Avoid an unnecessary xstrdup in rm_env() when matching
patterns. Since match_pattern() doesn't modify its arguments (they are
const), there is no need to make an extra copy of the strings in
options->send_env. From Martin Vahlensieck
OpenBSD-Commit-ID: 2c9db31e3f4d3403b49642c64ee048b2a0a39351
* upstream: Add missing includes of stdlib.h and stdint.h. We need
stdlib.h for malloc(3) and stdint.h for SIZE_MAX. Unlike the other xmss
files, ssh-xmss.c does not include xmss_commons.h so ssh-xmss.c must include
those headers itself. From Martin Vahlensieck
OpenBSD-Commit-ID: 70e28a9818cee3da1be2ef6503d4b396dd421e6b
* upstream: Remove unnecessary includes: openssl/hmac.h and
openssl/evp.h. From Martin Vahlensieck.
OpenBSD-Commit-ID: a6debb5fb0c8a44e43e8d5ca7cc70ad2f3ea31c3
* upstream: Check sshauthopt_new() for NULL. bz#3425, from
tessgauthier at microsoft.com. ok djm@
OpenBSD-Commit-ID: af0315bc3e44aa406daa7e0ae7c2d719a974483f
* upstream: Add authfd path to debug output. ok markus@
OpenBSD-Commit-ID: f735a17d1a6f2bee63bfc609d76ef8db8c090890
* upstream: avoid printing hash algorithm twice; from lucas AT sexy.is
OpenBSD-Commit-ID: 9d24671e10a84141b7c504396cabad600e47a941
* upstream: fix memleak on session-bind path; from Pedro Martelletto, ok
dtucker@
OpenBSD-Commit-ID: e85899a26ba402b4c0717b531317e8fc258f0a7e
* upstream: Don't leak SK device. Patch from Pedro Martelletto via
github PR#316. ok djm@
OpenBSD-Commit-ID: 17d11327545022e727d95fd08b213171c5a4585d
* upstream: mention that the helpers are used by ssh(1), ssh-agent(1)
and ssh-keygen(1). Previously only ssh(1) was mentioned. From Pedro
Martelletto
OpenBSD-Commit-ID: 30f880f989d4b329589c1c404315685960a5f153
* Remove now-empty int32_minmax.inc.
* Only run tests when source files change.
Also run tests on changes to V_9_0 branch.
* Add Mac OS X 12 test target.
* upstream: be stricter in which characters will be accepted in
specifying a mask length; allow only 0-9. From khaleesicodes via GHPR#278; ok
dtucker@
OpenBSD-Commit-ID: e267746c047ea86665cdeccef795a8a56082eeb2
* upstream: fix some integer overflows in sieve_large() that show up when
trying to generate modp groups > 16k bits. Reported via GHPR#306 by Bertram
Felgenhauer, but fixed in a different way. feedback/ok tb@
OpenBSD-Commit-ID: 81cbc6dd3a21c57bd6fadea10e44afe37bca558e
* upstream: remove an obsolete rsa1 format example from an example;
from megan batty
ok djm
OpenBSD-Commit-ID: db2c89879c29bf083df996bd830abfb1e70d62bf
* upstream: Add FIDO AUTHENTICATOR section and explain a bit how FIDO
works. The wording came mostly from the 8.2 OpenSSH release notes, addapted
to fit the man page. Then move the -O bits into the new section as is already
done for CERTIFICATES and MODULI GENERATION. Finally we can explain the
trade-offs of resident keys. While here, consistently refer to the FIDO
thingies as "FIDO authenticators", not "FIDO tokens".
input & OK jmc, naddy
OpenBSD-Commit-ID: dd98748d7644df048f78dcf793b3b63db9ab1d25
* upstream: make sure stdout is non-blocking; ok djm@
OpenBSD-Commit-ID: 64940fffbd1b882eda2d7c8c7a43c79368309c0d
* upstream: mux.c: mark argument as const; from Martin Vahlensieck
OpenBSD-Commit-ID: 69a1a93a55986c7c2ad9f733c093b46a47184341
* upstream: channel_new no longer frees remote_name. So update the
comment accordingly. As remote_name is not modified, it can be const as
well. From Martin Vahlensieck
OpenBSD-Commit-ID: e4e10dc8dc9f40c166ea5a8e991942bedc75a76a
* upstream: sshkey_unshield_private() contains a exact duplicate of
the code in private2_check_padding(). Pull private2_check_padding() up so the
code can be reused. From Martin Vahlensieck, ok deraadt@
OpenBSD-Commit-ID: 876884c3f0e62e8fd8d1594bab06900f971c9c85
* Add ubsan minimal testcase on OpenBSD.
As suggested by djm@.
* Note that, for now, we need variadic macros.
* Also retest OpenBSD upstream on .yml changes.
* upstream: When performing operations that glob(3) a remote path, ensure
that the implicit working directory used to construct that path escapes
glob(3) characters.
This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.
Reported by Lusia Kundel; ok markus@
OpenBSD-Commit-ID: 4f647f58482cbad3d58b1eab7f6a1691433deeef
* Remove duplicate bcrypt_pbkdf.o from Makefile
bcrypt_pbkdf.o is duplicated in the openbsd-compat Makefile's object
file list.
* upstream: improve error message when 'ssh-keygen -Y sign' is unable to
load a private key; bz3429, reported by Adam Szkoda ok dtucker@
OpenBSD-Commit-ID: bb57b285e67bea536ef81b1055467be2fc380e74
* upstream: Allow existing -U (use agent) flag to work with "-Y sign"
operations, where it will be interpreted to require that the private keys is
hosted in an agent; bz3429, suggested by Adam Szkoda; ok dtucker@
OpenBSD-Commit-ID: a7bc69873b99c32c42c7628ed9ea91565ba08c2f
* upstream: Remove errant apostrophe. From haruyama at queen-ml org.
OpenBSD-Commit-ID: dc6b294567cb84b384ad6ced9ca469f2bbf0bd10
* upstream: arrange for scp, when in sftp mode, to not ftruncate(3) files
early
previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.
spotted by solene@ sthen@, also bz3431; ok dtucker@
OpenBSD-Commit-ID: ca39fdd39e0ec1466b9666f15cbcfddea6aaa179
* upstream: fix in-place copies; r1.163 incorrectly skipped truncation in
all cases, not just at the start of a transfer. This could cause overwrites
of larger files to leave junk at the end. Spotted by tb@
OpenBSD-Commit-ID: b189f19cd68119548c8e24e39c79f61e115bf92c
* upstream: Only run agent-ptrace.sh if gdb is available as all
architectures do not ship with gdb.
OpenBSD-Regress-ID: ec53e928803e6b87f9ac142d38888ca79a45348d
* upstream: regress test for in-place transfers and clobbering larger
files with smaller ones; would have caught last regression in scp(1)
OpenBSD-Regress-ID: 19de4e88dd3a4f7e5c1618c9be3c32415bd93bc2
* configure.ac: Add missing AC_DEFINE for caph_cache_tzdata test causing
HAVE_CAPH_CACHE_TZDATA to be missing from config.h.in.
Spotted by Bryan Drewery
* upstream: make SSHBUF_DBG/SSHBUF_TELL (off by default and only enabled
via #define) dump to stderr rather than stdout
OpenBSD-Commit-ID: 10298513ee32db8390aecb0397d782d68cb14318
* upstream: revert previous; it was broken (spotted by Theo)
OpenBSD-Commit-ID: 457c79afaca2f89ec2606405c1059b98b30d8b0d
* upstream: Note that ProxyJump also accepts the same tokens as
ProxyCommand. From pallxk via github PR#305.
OpenBSD-Commit-ID: 7115ac351b129205f1f1ffa6bbfd62abd76be7c5
* upstream: Avoid kill with -1 argument. The out_ctx label can be
reached before fork has been called. If this happens, then kill -1 would be
called, sending SIGTERM to all processes reachable by the current process.
From tobias@ and c3h2_ctf via github PR#286, ok djm@
OpenBSD-Commit-ID: 6277af1207d81202f5daffdccfeeaed4c763b1a8
* upstream: f sshpkt functions fail, then password is not cleared
with freezero. Unconditionally call freezero to guarantee that password is
removed from RAM.
From tobias@ and c3h2_ctf via github PR#286, ok djm@
OpenBSD-Commit-ID: 6b093619c9515328e25b0f8093779c52402c89cd
* upstream: refactor authorized_keys/principals handling
remove "struct ssh *" from arguments - this was only used to pass the
remote host/address. These can be passed in instead and the resulting
code is less tightly coupled to ssh_api.[ch]
ok dtucker@
OpenBSD-Commit-ID: 9d4373d013edc4cc4b5c21a599e1837ac31dda0d
* upstream: split the low-level file handling functions out from
auth2-pubkey.c
Put them in a new auth2-pubkeyfile.c to make it easier to refer to them
(e.g. in unit/fuzz tests) without having to refer to everything else
pubkey auth brings in.
ok dtucker@
OpenBSD-Commit-ID: 3fdca2c61ad97dc1b8d4a7346816f83dc4ce2217
* fuzzer for authorized_keys parsing
mostly redundant to authopt_fuzz, but it's sensitive code so IMO it
makes sense to test this layer too
* Test against LibreSSL 3.5.3.
* Test against OpenSSL 1.1.1o and 3.0.3.
* fix some bugs in the fuzzer
* upstream: keywords ref ssh_config.5;
from caspar schutijser
OpenBSD-Commit-ID: f146a19d7d5c9374c3b9c520da43b2732d7d1a4e
* upstream: ssh-keygen: implement "verify-required" certificate option.
This was already documented when support for user-verified FIDO
keys was added, but the ssh-keygen(1) code was missing.
ok djm@
OpenBSD-Commit-ID: f660f973391b593fea4b7b25913c9a15c3eb8a06
* upstream: ssh-keygen -A: do not generate DSA keys by default.
Based on github PR#303 from jsegitz with man page text from jmc@, ok markus@
djm@
OpenBSD-Commit-ID: 5c4c57bdd7063ff03381cfb6696659dd3f9f5b9f
* upstream: Add period at end of "not known by any other names"
message. github PR#320 from jschauma, ok djm@
OpenBSD-Commit-ID: bd60809803c4bfd3ebb7c5c4d918b10e275266f2
* upstream: Add missing *-sk types to ssh-keyscan manpage. From
skazi0 via github PR#294.
OpenBSD-Commit-ID: fda2c869cdb871f3c90a89fb3f985370bb5d25c0
* upstream: Make SetEnv directives first-match-wins in both
sshd_config and sshd_config; previously if the same name was reused then the
last would win (which is the opposite to how the config is supposed to work).
While there, make the ssh_config parsing more like sshd_config.
bz3438, ok dtucker
OpenBSD-Commit-ID: 797909c1e0262c0d00e09280459d7ab00f18273b
* upstream: test setenv in both client and server, test first-match-wins
too
OpenBSD-Regress-ID: 4c8804f9db38a02db480b9923317457b377fe34b
* upstream: move auth_openprincipals() and auth_openkeyfile() over to
auth2-pubkeyfile.c too; they make more sense there.
OpenBSD-Commit-ID: 9970d99f900e1117fdaab13e9e910a621b7c60ee
* upstream: make sure that UseDNS hostname lookup happens in the monitor
and not in the pledge(2)'d unprivileged process; fixes regression caused by
recent refactoring spotted by henning@
OpenBSD-Commit-ID: a089870b95101cd8881a2dff65b2f1627d13e88d
* fix possible NULL deref when built without FIDO
Analysis/fix from kircher in bz3443; ok dtucker@
* automatically enable built-in FIDO support
If libfido2 is found and usable, then enable the built-in
security key support unless --without-security-key-builtin
was requested.
ok dtucker@
* upstream: Log an error if pipe() fails while accepting a
connection. bz#3447, from vincent-openssh at vinc17 net, ok djm@
OpenBSD-Commit-ID: 9d59f19872b94900a5c79da2d57850241ac5df94
* upstream: Don't attempt to fprintf a null identity comment. From
Martin Vahlensieck via tech@.
OpenBSD-Commit-ID: 4c54d20a8e8e4e9912c38a7b4ef5bfc5ca2e05c2
* upstream: Make sure not to fclose() the same fd twice in case of an
error.
ok dtucker@
OpenBSD-Commit-ID: e384c4e05d5521e7866b3d53ca59acd2a86eef99
* upstream: make it clear that RekeyLimit applies to both transmitted
and received data. GHPR#328 from Jan Pazdziora
OpenBSD-Commit-ID: d180a905fec9ff418a75c07bb96ea41c9308c3f9
* request 1.1x API compatibility for OpenSSL >=3.x
idea/patch from Pedro Martelletto via GHPR#322; ok dtucker@
* fix broken case statement in previous
* Disable SK support if FIDO libs not found.
* Zero out LIBFIDO2 when SK support not usable.
Prevents us from trying to link them into ssh-sk-helper and failing to
build.
* upstream: Don't leak the strings allocated by order_hostkeyalgs()
and list_hostkey_types() that are passed to compat_pkalg_proposal(). Part of
github PR#324 from ZoltanFridrich, ok djm@
OpenBSD-Commit-ID: b2f6e5f60f2bba293b831654328a8a0035ef4a1b
* upstream: Roll back previous KEX changes as they aren't safe until
compat_pkalg_proposal and friends always allocate their returned strings.
Reported by Qualys.
OpenBSD-Commit-ID: 1c7a88a0d5033f42f88ab9bec58ef1cf72c81ad0
* upstream: allow arguments to sftp -D option, e.g. sftp -D
"/usr/libexec/sftp-server -el debug3"
ok markus@
OpenBSD-Commit-ID: 5a002b9f3a7aef2731fc0ffa9c921cf15f38ecce
* Update OpenSSL tests to the most recent releases.
* upstream: reflect the update to -D arg name in usage();
OpenBSD-Commit-ID: abdcde4f92b1ef094ae44210ee99d3b0155aad9c
* upstream: ignore SIGPIPE earlier in main(), specifically before
muxclient() which performs operations that could cause one; Reported by Noam
Lewis via bz3454, ok dtucker@
OpenBSD-Commit-ID: 63d8e13276869eebac6d7a05d5a96307f9026e47
* upstream: Always return allocated strings from the kex filtering so
that we can free them later. Fix one leak in compat_kex_proposal. Based on
github PR#324 from ZoltanFridrich with some simplications by me. ok djm@
OpenBSD-Commit-ID: 9171616da3307612d0ede086fd511142f91246e4
* upstream: Don't leak the strings allocated by order_hostkeyalgs()
and list_hostkey_types() that are passed to compat_pkalg_proposal(). Part of
github PR#324 from ZoltanFridrich, ok djm@
This is a roll-forward of the previous rollback now that the required
changes in compat.c have been done.
OpenBSD-Commit-ID: c7cd93730b3b9f53cdad3ae32462922834ef73eb
* upstream: bump up loglevel from debug to info when unable to open
authorized keys/principals file for errno != ENOENT; bz2042 ok dtucker
OpenBSD-Commit-ID: e79aa550d91ade6a80f081bda689da24c086d66b
* Skip select+rlimit check if sandboxing is disabled
It's not needed in that case, and the test can fail when being built
with some compiler memory sanitizer flags. bz#3441
* upstream: use consistent field names (s/char/byte)
in format description
OpenBSD-Commit-ID: 3de33572733ee7fcfd7db33d37db23d2280254f0
* upstream: Remove leftover line.
Remove extra line leftover from merge conflict. ok djm@
OpenBSD-Commit-ID: 460e2290875d7ae64971a7e669c244b1d1c0ae2e
* Move checks for pollfd.fd and nfds_t.
Move the checks for struct pollfd.fd and nfds_t to before the sandboxing
checks. This groups all the sandbox checks together so we can skip them
all when sandboxing is disabled.
* Skip all rlimit tests when sandboxing disabled.
The rlimit tests can hang when being run with some compiler sanitizers
so skip all of them if sandbox=no.
* Add clang sanitizer tests.
* upstream: Add TEST_REGRESS_CACHE_DIR.
If set, it is used to cache regress test names that have succeeded and
skip those on a re-run.
OpenBSD-Regress-ID: a7570dd29a58df59f2cca647c3c2ec989b49f247
* Move sanitizer logs into regress for collection.
* Add GCC address sanitizer build/test.
* Update sanitizer test targets:
- remove clang-sanitize-memory for now. It takes so long that the test
times out.
- add gcc sanitize-address and sanitize-undefined test targets.
* Test against openssl-3.0.5.
* Move unset to before we set anything.
* Refuse to use OpenSSL 3.0.4 due to potential RCE.
OpenSSL has a potential RCE in its RSA implementation (CVE-2022-2274)
so refuse to use that specific version.
* Capture stderr output from configure.
* Only refuse to use OpenSSL 3.0.4 on x86_64.
The potential RCE only impacts x86_64, so only refuse to use it if we're
targetting a potentially impacted architecture. ok djm@
* Remove special casing of crypt().
Configure goes to some lengths to pick crypt() from either libcrypt
or OpenSSL's libcrypto because they can more or less featureful (eg
supporting md5-style passwords).
OpenSSL removed its crypt() interface in 2002:
https://github.com/openssl/openssl/commit/69deec58 so these hijinks
should no longer be necessary. This also only links sshd with libcrypt
which is the only thing that needs it. ok djm@
* Clarify README.md text.
Clarify the text about the implications of building without OpenSSL, and
prefix the "configure --help" example command with a "./" so it's likely
to work as-is in more shells. From bz#3461.
* Split README.platform into its own line.
README.platform has general platform-specific information, having it
following text about FIDO2 on the same line could imply that it only
has information about FIDO2.
* Return ERANGE from getcwd() if buffer size is 1.
If getcwd() is supplied a buffer size of exactly 1 and a path of "/", it
could result in a nul byte being written out of array bounds. POSIX says
it should return ERANGE if the path will not fit in the available buffer
(with terminating nul). 1 byte cannot fit any possible path with its nul,
so immediately return ERANGE in that case.
OpenSSH never uses getcwd() with this buffer size, and all current
(and even quite old) platforms that we are currently known to work
on have a native getcwd() so this code is not used on those anyway.
Reported by Qualys, ok djm@
* Remove unintended changes.
I inadvertently included a couple of local changes with the OpenSSL
3.0.4 change. Revert, anything that should be there will be committed
separately.
* Add AUDIT_ARCH_PPC to supported seccomp arches.
Patch from dries.deschout at dodeco.eu.
* Rename bbone test target to ARM.
* Move vmshutdown to first step.
If a previous run on a physical runner has failed to clean up, the next
run will fail because it'll try to check out the code to a broken
directory mount. Make cleanup the first step.
* upstream: pull passphrase reading and confirmation into a separate
function so it can be used for FIDO2 PINs; no functional change
OpenBSD-Commit-ID: bf34f76b8283cc1d3f54633e0d4f13613d87bb2f
* upstream: when enrolling a resident key on a security token, check
if a credential with matching application and user ID strings already exists.
if so, prompt the user for confirmation before overwriting the credential.
patch from Pedro Martelletto via GHPR329
NB. cranks SSH_SK_VERSION_MAJOR, so any third-party FIDO middleware
implementations will need to adjust
OpenBSD-Commit-ID: e45e9f1bf2b2f32d9850669e7a8dbd64acc5fca4
* upstream: sk-usbhid: preserve error code returned by key_lookup()
it conveys useful information, such as the supplied pin being wrong.
Part of GHPR329 from Pedro Martelletto
OpenBSD-Commit-ID: c0647eb9290f793add363d81378439b273756c1b
* upstream: ssh-keygen: fix touch prompt, pin retries;
part of GHPR329 from Pedro Martelletto
OpenBSD-Commit-ID: 75d1005bd2ef8f29fa834c90d2684e73556fffe8
* crank SSH_SK_VERSION_MAJOR in sk-dummy.so
* Skip scp3 test if there's no scp on remote path.
scp -3 ends up using the scp that's in the remote path and will fail if
one is not available. Based on a patch from rapier at psc.edu.
* Convert "have_prog" function into "which".
"which" and its behaviour is not standardized, so convert the existing
have_prog function into "which" so we can rely on it being available
and what its semantics are. Add a have_prog wrapper that maintains the
existing behaviour.
* upstream: Test TEST_SSH_ELAPSED_TIMES for empty string not
executable. No-op on most platforms but should prevent warnings in -portable
on systems that don't have 'date %s'.
OpenBSD-Regress-ID: e39d79867b8065e33d0c5926fa1a31f85659d2a4
* upstream: Restore missing "!" in TEST_SSH_ELAPSED_TIMES test.
OpenBSD-Regress-ID: 38783f9676ec348c5a792caecee9a16e354b37b0
* Remove workarounds for OpenSSL missing AES-GCM.
We have some compatibility hacks that were added to support OpenSSL
versions that do not support AES GCM mode. Since that time, however,
the minimum OpenSSL version that we support has moved to 1.0.1 which
*does* have GCM, so this is no longer needed. ok djm@
* Remove workarounds for OpenSSL missing AES-CTR.
We have some compatibility hacks that were added to support OpenSSL
versions that do not support AES CTR mode. Since that time, however,
the minimum OpenSSL version that we support has moved to 1.0.1 which
*does* have CTR, so this is no longer needed. ok djm@
* Do not link scp, sftp and sftp-server w/ zlib.
Some of our binaries (eg sftp, sftp-server, scp) do not interact with
the channels code and thus do use libraries such as zlib and libcrypto
although they are linked with them. This adds a CHANNELLIBS and starts
by moving zlib into it, which means the aformentioned binaries are no
longer linked against zlib. ok djm@
* Group libcrypto and PRNGD checks together.
They're related more than the libcrypt or libiaf checks which are
currently between them. ok djm@
* Remove seed_rng calls from scp, sftp, sftp-server.
These binaries don't use OpenSSL's random functions. The next step
will be to stop linking them against libcrypto. ok djm@
* Move libcrypto into CHANNELLIBS.
This will result in sftp, sftp-server and scp no longer being linked
against libcrypto. ok djm@
* Move stale-configure check as early as possible.
We added a check in Makefile to catch the case where configure needs to
be rebuilt, however this did not happen until a build was attempted in
which case all of the work done by configure was wasted. Move this check
to the start of configure to catch it as early as possible. ok djm@
* Remove deprecated MacOS 10.15 runners.
* upstream: avoid double-free in error path introduced in r1.70; report
and fix based on GHPR#332 by v-rzh ok dtucker@
OpenBSD-Commit-ID: 3d21aa127b1f37cfc5bdc21461db369a663a951f
* Include CHANNEL and FIDO2 libs in configure output
* Factor out getrnd() and rename to getentropy().
Factor out the arc4random seeding into its own file and change the
interface to match getentropy. Use native getentropy if available.
This will make it easier to resync OpenBSD changes to arc4random.
Prompted by bz#3467, ok djm@.
* compat code for fido_dev_is_winhello()
Signed-off-by: Corinna Vinschen <vinschen@redhat.com>
* check_sk_options: add temporary WinHello workaround
Up to libfido 1.10.0, WinHello advertises "clientPin" rather
than "uv" capability. This is fixed in 1.11.0. For the time
being, workaround it here.
Signed-off-by: Corinna Vinschen <vinschen@redhat.com>
* sk_sign: set FIDO2 uv attribute explicitely for WinHello
WinHello via libfido2 performs user verification by default.
However, if we stick to that, there's no way to differentiate
between keys created with or without "-O verify-required".
Set FIDO2 uv attribute explicitely to FIDO_OPT_FALSE, then check
if user verification has been requested.
Signed-off-by: Corinna Vinschen <vinschen@redhat.com>
* upstream: don't prompt for FIDO passphrase before attempting to enroll
the credential, just let the enroll operating fail and we'll attempt to get a
PIN anyway. Might avoid some unneccessary PIN prompts.
Part of GHPR#302 from Corinna Vinschen; ok dtucker@
OpenBSD-Commit-ID: bd5342ffc353ee37d39617906867c305564d1ce2
* Give unused param a name.
Fixes builds on platforms that do have fido2 but don't have
fido_dev_is_winhello.
* Actually put HAVE_STDINT_H around the stdint.h.
* Rename our getentropy to prevent possible loops.
Since arc4random seeds from getentropy, and we use OpenSSL for that
if enabled, there's the possibility that if we build on a system that
does not have getentropy then run on a system that does have it, then
OpenSSL could end up calling our getentropy and getting stuck in a loop.
Pointed out by deraadt@, ok djm@
* Test hostbased auth on github runners.
* fix SANDBOX_SECCOMP_FILTER_DEBUG
* Fix conditional for running hostbased tests.
* upstream: allow certificate validity intervals, sshsig verification
times and authorized_keys expiry-time options to accept dates in the UTC time
zone in addition to the default of interpreting them in the system time zone.
YYYYMMDD and YYMMDDHHMM[SS] dates/times will be interpreted as UTC if
suffixed with a 'Z' character.
Also allow certificate validity intervals to be specified in raw
seconds-since-epoch as hex value, e.g. -V 0x1234:0x4567890. This
is intended for use by regress tests and other tools that call
ssh-keygen as part of a CA workflow.
bz3468 ok dtucker
OpenBSD-Commit-ID: 454db1cdffa9fa346aea5211223a2ce0588dfe13
* upstream: add some tests for parse_absolute_time(), including cases
where it is forced to the UTC timezone. bz3468 ok dtucker
OpenBSD-Regress-ID: ea07ca31c2f3847a38df028ca632763ae44e8759
* Skip hostbased during Valgrind tests.
Valgrind doesn't let ssh exec ssh-keysign (because it's setuid) so skip
it during the Valgrind based tests.
See https://bugs.kde.org/show_bug.cgi?id=119404 for a discussion of this
(ironically there the problematic binary was ssh(1) back when it could
still be setuid).
* Rerun tests if any .github config file changes.
* Add a timegm implementation from Heimdal via Samba.
Fixes build on (at least Solaris 10).
* Replace deprecated ubuntu-18.04 runners with 22.04
* upstream: sftp-server: support home-directory request
Add support to the sftp-server for the home-directory extension defined
in draft-ietf-secsh-filexfer-extensions-00. This overlaps a bit with the
existing expand-path@openssh.com, but uses a more official protocol name,
and so is a bit more likely to be implemented by non-OpenSSH clients.
From Mike Frysinger, ok dtucker@
OpenBSD-Commit-ID: bfc580d05cc0c817831ae7ecbac4a481c23566ab
* fido_dev_is_winhello: return 0, not "false"
"false" is not used anywhere in OpenSSH, so return 0 like
everywhere else.
Signed-off-by: Corinna Vinschen <vinschen@redhat.com>
* Revert "check_sk_options: add temporary WinHello workaround"
Cygwin now comes with libfido2 1.11.0, so this workaround
isn't required anymore.
This reverts commit 242c044ab111a37aad3b0775727c36a4c5f0102c.
Signed-off-by: Corinna Vinschen <vinschen@redhat.com>
* upstream: use .Cm for "sign"; from josiah frentsos
OpenBSD-Commit-ID: 7f80a53d54857ac6ae49ea6ad93c5bd12231d1e4
* upstream: add an extra flag to sk_probe() to indicate whether we're
probing for a FIDO resident key or not. Unused here, but will make like
easier for portable
OpenBSD-Commit-ID: 432c8ff70e270378df9dbceb9bdeaa5b43b5a832
* on Cygwin, prefer WinHello FIDO device
If no FIDO device was explictly specified, then prefer the
windows://hello FIDO device. An exception to this is when
probing resident FIDO keys, in which case hardware FIDO
devices are preferred.
* Check for perms to run agent-getpeereid test.
Ubuntu 22.04 defaults to private home dirs which prevents "nobody"
running ssh-add during the agent-getpeereid test. Check for this and
add the necessary permissions.
* upstream: double free() in error path; from Eusgor via GHPR333
OpenBSD-Commit-ID: 39f35e16ba878c8d02b4d01d8826d9b321be26d4
* Add Cygwin (on windows-2019) test target.
In addition to installing the requisite Cygwin packages, we also need to
explicitly invoke "sh" for steps that run other scripts since the runner
environment doesn't understand #! paths.
* Add a bit more debug output.
* Fix cygwin conditional steps.
* upstream: Strictly enforce the maximum allowed SSH2 banner size in
ssh-keyscan and prevent a one-byte buffer overflow. Patch from Qualys, ok
djm@
OpenBSD-Commit-ID: 6ae664f9f4db6e8a0589425f74cd0bbf3aeef4e4
* upstream: remove incorrect check that can break enrolling a
resident key (introduced in r1.40)
OpenBSD-Commit-ID: 4cab364d518470e29e624af3d3f9ffa9c92b6f01
* upstream: attemp FIDO key signing without PIN and use the error
code returned to fall back only if necessary. Avoids PIN prompts for FIDO
tokens that don't require them; part of GHPR#302
OpenBSD-Commit-ID: 4f752aaf9f2e7c28bcaaf3d4f8fc290131bd038e
* Install Cygwin packages based on OS not config.
* initial list of allowed signers
* upstream: whitespace
OpenBSD-Commit-ID: d297e4387935d4aef091c5e9432578c2e513f538
* upstream: whitespace
OpenBSD-Commit-ID: a5d015efbfd228dc598ffdef612d2da3a579e5d8
* Add cygwin-release test target.
This also moves the cygwin package install from the workflow file to
setup_ci.sh so that we can install different sets of Cygwin packages
for different test configs.
* Add Windows 2022 test targets.
* Add libcrypt-devel to cygwin-release deps.
Based on feedback from vinschen at redhat.com.
* cross-sign allowed_signers with PGP key
Provides continuity of trust from legacy PGP release key to
the SSHSIG signing keys that we will use henceforth for git
signing.
* additional keys
* upstream: whitespace
OpenBSD-Commit-ID: c2bcbf93610d3d62ed206cdf9bf9ff98c6aaf232
* Move sftp from valgrind-2 to 3 to rebalance.
* upstream: sk-usbhid: fix key_lookup() on tokens with built-in UV
explicitly test whether the token performs built-in UV (e.g. biometric
tokens) and enable UV in that case. From Pedro Martelletto via GHPR#388
OpenBSD-Commit-ID: 007eb7e387d27cf3029ab06b88224e03eca62ccd
* Remove arc4random_uniform from arc4random.c
This was previously moved into its own file (matching OpenBSD) which
prematurely committed in commit 73541f2.
* Move OPENBSD ORIGINAL marker.
Putting this after the copyright statement (which doesn't change)
instead of before the version identifier (which does) prevents merge
conflicts when resyncing changes.
* Resync arc4random with OpenBSD.
This brings us up to current, including djm's random-reseeding change,
as prompted by logan at cyberstorm.mu in bz#3467. It brings the
platform-specific hooks from LibreSSL Portable, simplified to match our
use case. ok djm@.
* Remove DEF_WEAK, it's already in defines.h.
* openbsd-compat/bsd-asprintf: add <stdio.h> include for vsnprintf
Fixes the following build failure with Clang 15 on musl:
```
bsd-asprintf.c:51:8: error: call to undeclared library function 'vsnprintf' with type 'int (char *, unsigned long, const char *, struct __va_list_tag *)'; ISO C99 and laterclang -O2 -pipe -fdiagnostics-color=always -frecord-gcc-switches -pipe -Wunknown-warning-option -Qunused-arguments -Wall -Wpointer-arith -Wuninitialized -Wsign-compare -Wformat-security -Wsizeof-pointer-memaccess -Wno-pointer-sign -Wno-unused-result -Wmisleading-indentation -Wbitwise-instead-of-logical -fno-strict-aliasing -mretpoline -ftrapv -fzero-call-used-regs=all -fno-builtin-memset -fstack-protector-strong -fPIE -I. -I. -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_DEFAULT_SOURCE -DSSHDIR=\"/etc/ssh\" -D_PATH_SSH_PROGRAM=\"/usr/bin/ssh\" -D_PATH_SSH_ASKPASS_DEFAULT=\"/usr/lib/misc/ssh-askpass\" -D_PATH_SFTP_SERVER=\"/usr/lib/misc/sftp-server\" -D_PATH_SSH_KEY_SIGN=\"/usr/lib/misc/ssh-keysign\" -D_PATH_SSH_PKCS11_HELPER=\"/usr/lib/misc/ssh-pkcs11-helper\" -D_PATH_SSH_SK_HELPER=\"/usr/lib/misc/ssh-sk-helper\" -D_PATH_SSH_PIDDIR=\"/run\" -D_PATH_PRIVSEP_CHROOT_DIR=\"/var/empty\" -DHAVE_CONFIG_H -c cipher-aes.c -o cipher-aes.o
do not support
implicit function declarations [-Wimplicit-function-declaration]
ret = vsnprintf(string, INIT_SZ, fmt, ap2);
^
bsd-asprintf.c:51:8: note: include the header <stdio.h> or explicitly provide a declaration for 'vsnprintf'
1 error generated.
```
* upstream: notifier_complete(NULL, ...) is a noop, so no need to test
that ctx!=NULL; from Corinna Vinschen
OpenBSD-Commit-ID: ade2f2e9cc519d01a586800c25621d910bce384a
* fix pester test failures
* upstream: fix repeated words ok miod@ jmc@
OpenBSD-Commit-ID: 6765daefe26a6b648cc15cadbbe337596af709b7
* upstream: .Li -> .Vt where appropriate; from josiah frentsos,
tweaked by schwarze
ok schwarze
OpenBSD-Commit-ID: 565046e3ce68b46c2f440a93d67c2a92726de8ed
* upstream: ssh-agent: attempt FIDO key signing without PIN and use
the error to determine whether a PIN is required and prompt only if
necessary. from Corinna Vinschen
OpenBSD-Commit-ID: dd6be6a0b7148608e834ee737c3479b3270b00dd
* upstream: a little extra debugging
OpenBSD-Commit-ID: edf1601c1d0905f6da4c713f4d9cecc7d1c0295a
* upstream: sk_enroll: never drop SSH_SK_USER_VERIFICATION_REQD flag
from response
Now that all FIDO signing calls attempt first without PIN and then
fall back to trying PIN only if that attempt fails, we can remove the
hack^wtrick that removed the UV flag from the keys returned during
enroll.
By Corinna Vinschen
OpenBSD-Commit-ID: 684517608c8491503bf80cd175425f0178d91d7f
* define HAVE_KILLPG
* upstream: sftp: Don't attempt to complete arguments for
non-existent commands
If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.
From Michal Privoznik
OpenBSD-Commit-ID: cf39c811a68cde2aeb98fc85addea4000ef6b07a
* upstream: sftp: Be a bit more clever about completions
There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.
By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.
By Michal Privoznik, ok dtucker@
OpenBSD-Commit-ID: 1396d921c4eb1befd531f5c4a8ab47e7a74b610b
* upstream: correct error value
OpenBSD-Commit-ID: 780efcbad76281f11f14b2a5ff04eb6db3dfdad4
* upstream: actually hook up restrict_websafe; the command-line flag
was never actually used. Spotted by Matthew Garrett
OpenBSD-Commit-ID: 0b363518ac4c2819dbaa3dfad4028633ab9cdff1
* upstream: Add a sshkey_check_rsa_length() call for checking the
length of an RSA key; ok markus@
OpenBSD-Commit-ID: de77cd5b11594297eda82edc594b0d32b8535134
* upstream: add a RequiredRSASize for checking RSA key length in
ssh(1). User authentication keys that fall beneath this limit will be
ignored. If a host presents a host key beneath this limit then the connection
will be terminated (unfortunately there are no fallbacks in the protocol for
host authentication).
feedback deraadt, Dmitry Belyavskiy; ok markus@
OpenBSD-Commit-ID: 430e339b2a79fa9ecc63f2837b06fdd88a7da13a
* upstream: Add RequiredRSASize for sshd(8); RSA keys that fall
beneath this limit will be ignored for user and host-based authentication.
Feedback deraadt@ ok markus@
OpenBSD-Commit-ID: 187931dfc19d51873df5930a04f2d972adf1f7f1
* upstream: better debugging for connect_next()
OpenBSD-Commit-ID: d16a307a0711499c971807f324484ed3a6036640
* upstream: sftp-server(8): add a "users-groups-by-id@openssh.com"
extension request that allows the client to obtain user/group names that
correspond to a set of uids/gids.
Will be used to make directory listings more useful and consistent
in sftp(1).
ok markus@
OpenBSD-Commit-ID: 7ebabde0bcb95ef949c4840fe89e697e30df47d3
* upstream: extend sftp-common.c:extend ls_file() to support supplied
user/group names; ok markus@
OpenBSD-Commit-ID: c70c70498b1fdcf158531117e405b6245863bfb0
* upstream: sftp client library support for
users-groups-by-id@openssh.com; ok markus@
OpenBSD-Commit-ID: ddb2f33a2da6349a9a89a8b5bcb9ca7c999394de
* upstream: use users-groups-by-id@openssh.com sftp-server extension
(when available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names. ok markus@
OpenBSD-Commit-ID: f239aeeadfa925a37ceee36ee8b256b8ccf4466e
* avoid Wuninitialized false positive in gcc-12ish
* no need for glob.h here
it also causes portability problems
* add debug on appveyor
* add sleep to pester test
* upstream: add RequiredRSASize to the list of keywords accepted by
-o; spotted by jmc@
OpenBSD-Commit-ID: fe871408cf6f9d3699afeda876f8adbac86a035e
* upstream: Fix typo. From AlexanderStohr via github PR#343.
OpenBSD-Commit-ID: a134c9b4039e48803fc6a87f955b0f4a03181497
* upstream: openssh-9.1
OpenBSD-Commit-ID: 5a467b2ee81da01a86adf1ad93b62b1728494e56
* crank versions in RPM spec files
* update release notes URL
* update .depend
* fix 9.1 compilation errors
* disable -p pester tests due to unreliability on older Windows versions
* remove extra sleep time from debugging scp pester tests
* modify -p tests to only run for Windows OS version 10 and above
* add windows specific code back into method moved from auth.c to auth2-pubkeyfile.c
* add preprocessor for WinHello
* revert preprocessor definition for winhello
* add windows preprocessor definition in key_lookup
* remove rdp block from appveyor since we are no longer debugging
* add ifdef to sftp-server.c
* make key_lookup compatible with winhello
* appveyor.yml
* increase debug of failing pester test
* add #ifdef SUPPORT_CRLF back into auth_check_principals_line method that was moved/renamed
* modify new scp.sh tests for windows
* remove in place tests from scp.sh
* remove rdp debug from appveyor
* retrigger appveyor
* change check of OS version in scp test
Signed-off-by: Corinna Vinschen <vinschen@redhat.com>
Co-authored-by: djm@openbsd.org <djm@openbsd.org>
Co-authored-by: Damien Miller <djm@mindrot.org>
Co-authored-by: Darren Tucker <dtucker@dtucker.net>
Co-authored-by: naddy@openbsd.org <naddy@openbsd.org>
Co-authored-by: dtucker@openbsd.org <dtucker@openbsd.org>
Co-authored-by: tj@openbsd.org <tj@openbsd.org>
Co-authored-by: millert@openbsd.org <millert@openbsd.org>
Co-authored-by: jmc@openbsd.org <jmc@openbsd.org>
Co-authored-by: florian@openbsd.org <florian@openbsd.org>
Co-authored-by: markus@openbsd.org <markus@openbsd.org>
Co-authored-by: Tobias Heider <me@tobhe.de>
Co-authored-by: anton@openbsd.org <anton@openbsd.org>
Co-authored-by: Tim Rice <tim@multitalents.net>
Co-authored-by: tobhe@openbsd.org <tobhe@openbsd.org>
Co-authored-by: Corinna Vinschen <vinschen@redhat.com>
Co-authored-by: Sam James <sam@gentoo.org>
Co-authored-by: jsg@openbsd.org <jsg@openbsd.org>
2022-11-02 17:06:45 +01:00
|
|
|
* Always rekey when MAX_PACKETS sent in either direction
|
2017-06-09 06:40:04 +02:00
|
|
|
* As per RFC4344 section 3.1 we do this after 2^31 packets.
|
|
|
|
*/
|
2016-02-08 11:57:07 +01:00
|
|
|
if (state->p_send.packets > MAX_PACKETS ||
|
|
|
|
state->p_read.packets > MAX_PACKETS)
|
|
|
|
return 1;
|
|
|
|
|
2018-04-10 02:19:02 +02:00
|
|
|
/* Rekey after (cipher-specific) maximum blocks */
|
2016-09-12 03:22:38 +02:00
|
|
|
out_blocks = ROUNDUP(outbound_packet_len,
|
2016-02-08 11:57:07 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-07-16 11:00:23 +02:00
|
|
|
int
|
|
|
|
ssh_packet_check_rekey(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
if (!ssh_packet_need_rekeying(ssh, 0))
|
|
|
|
return 0;
|
|
|
|
debug3_f("rekex triggered");
|
|
|
|
return kex_start_rekex(ssh);
|
|
|
|
}
|
|
|
|
|
2005-07-26 13:54:56 +02:00
|
|
|
/*
|
|
|
|
* Delayed compression for SSH2 is enabled after authentication:
|
2006-08-05 10:51:08 +02:00
|
|
|
* This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
|
2005-07-26 13:54:56 +02:00
|
|
|
* and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
|
|
|
|
*/
|
2015-01-19 20:52:16 +01:00
|
|
|
static int
|
|
|
|
ssh_packet_enable_delayed_compress(struct ssh *ssh)
|
2005-07-26 13:54:56 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
struct sshcomp *comp = NULL;
|
|
|
|
int r, mode;
|
2005-07-26 13:54:56 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remember that we are past the authentication step, so rekeying
|
2018-07-09 15:37:10 +02:00
|
|
|
* with COMP_DELAYED will turn on compression immediately.
|
2005-07-26 13:54:56 +02:00
|
|
|
*/
|
2015-01-19 20:52:16 +01:00
|
|
|
state->after_authentication = 1;
|
2005-07-26 13:54:56 +02:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2006-09-21 05:00:25 +02:00
|
|
|
/* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
|
2015-01-19 20:52:16 +01:00
|
|
|
if (state->newkeys[mode] == NULL)
|
2006-09-21 05:00:25 +02:00
|
|
|
continue;
|
2015-01-19 20:52:16 +01:00
|
|
|
comp = &state->newkeys[mode]->comp;
|
2018-07-09 15:37:10 +02:00
|
|
|
if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
|
2015-01-19 20:52:16 +01: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-07-26 13:54:56 +02:00
|
|
|
comp->enabled = 1;
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
return 0;
|
2005-07-26 13:54:56 +02:00
|
|
|
}
|
|
|
|
|
2016-01-29 06:46:01 +01:00
|
|
|
/* Used to mute debug logging for noisy packet types */
|
2016-09-30 11:19:13 +02:00
|
|
|
int
|
2016-01-29 06:46:01 +01:00
|
|
|
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-04-04 06:38:59 +02:00
|
|
|
/*
|
|
|
|
* Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
|
|
|
|
*/
|
2015-01-19 20:52:16 +01:00
|
|
|
int
|
|
|
|
ssh_packet_send2_wrapped(struct ssh *ssh)
|
2000-04-04 06:38:59 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
2015-01-13 20:31:40 +01:00
|
|
|
u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
|
2016-07-18 08:08:01 +02:00
|
|
|
u_char tmp, padlen, pad = 0;
|
2015-01-19 20:52:16 +01: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-01-09 06:12:19 +01:00
|
|
|
/* disable mac for authenticated encryption */
|
|
|
|
if ((authlen = cipher_authlen(enc->cipher)) != 0)
|
|
|
|
mac = NULL;
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
2002-02-19 05:21:23 +01:00
|
|
|
block_size = enc ? enc->block_size : 8;
|
2013-01-09 06:12:19 +01:00
|
|
|
aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
|
2000-04-04 06:38:59 +02:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
type = (sshbuf_ptr(state->outgoing_packet))[5];
|
2016-01-29 06:46:01 +01:00
|
|
|
if (ssh_packet_log_type(type))
|
|
|
|
debug3("send packet: type %u", type);
|
2000-04-04 06:38:59 +02:00
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
fprintf(stderr, "plain: ");
|
2015-01-19 20:52:16 +01:00
|
|
|
sshbuf_dump(state->outgoing_packet, stderr);
|
2000-04-04 06:38:59 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (comp && comp->enabled) {
|
2015-01-19 20:52:16 +01:00
|
|
|
len = sshbuf_len(state->outgoing_packet);
|
2000-04-04 06:38:59 +02:00
|
|
|
/* skip header, compress only payload */
|
2015-01-19 20:52:16 +01: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-04-04 06:38:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sizeof (packet_len + pad_len + payload) */
|
2015-01-19 20:52:16 +01:00
|
|
|
len = sshbuf_len(state->outgoing_packet);
|
2000-04-04 06:38:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* calc size of padding, alloc space, get random data,
|
|
|
|
* minimum padding is 4 bytes
|
|
|
|
*/
|
2012-12-12 00:46:31 +01:00
|
|
|
len -= aadlen; /* packet length is not encrypted for EtM modes */
|
2000-04-04 06:38:59 +02:00
|
|
|
padlen = block_size - (len % block_size);
|
|
|
|
if (padlen < 4)
|
|
|
|
padlen += block_size;
|
2015-01-19 20:52:16 +01:00
|
|
|
if (state->extra_pad) {
|
2016-07-18 08:08:01 +02:00
|
|
|
tmp = state->extra_pad;
|
2015-01-19 20:52:16 +01:00
|
|
|
state->extra_pad =
|
2016-09-12 03:22:38 +02:00
|
|
|
ROUNDUP(state->extra_pad, block_size);
|
2016-07-18 08:08:01 +02: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;
|
2020-10-18 13:32:01 +02:00
|
|
|
DBG(debug3_f("adding %d (len %d padlen %d extra_pad %d)",
|
|
|
|
pad, len, padlen, state->extra_pad));
|
2016-07-18 08:08:01 +02:00
|
|
|
tmp = padlen;
|
2001-11-12 01:02:52 +01:00
|
|
|
padlen += pad;
|
2016-07-18 08:08:01 +02:00
|
|
|
/* Check whether padlen calculation overflowed */
|
|
|
|
if (padlen < tmp)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT; /* overflow */
|
2015-01-19 20:52:16 +01:00
|
|
|
state->extra_pad = 0;
|
2001-11-12 01:02:52 +01:00
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
|
|
|
|
goto out;
|
2016-08-03 07:41:57 +02:00
|
|
|
if (enc && !cipher_ctx_is_plaintext(state->send_context)) {
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 04:03:14 +02:00
|
|
|
/* random padding */
|
2015-01-19 20:52:16 +01:00
|
|
|
arc4random_buf(cp, padlen);
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 04:03:14 +02:00
|
|
|
} else {
|
|
|
|
/* clear padding */
|
2014-02-04 01:20:14 +01:00
|
|
|
explicit_bzero(cp, padlen);
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
2012-12-12 00:46:31 +01:00
|
|
|
/* sizeof (packet_len + pad_len + payload + padding) */
|
2015-01-19 20:52:16 +01:00
|
|
|
len = sshbuf_len(state->outgoing_packet);
|
|
|
|
cp = sshbuf_mutable_ptr(state->outgoing_packet);
|
|
|
|
if (cp == NULL) {
|
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-12-12 00:46:31 +01:00
|
|
|
/* packet_length includes payload, padding and padding length field */
|
2015-01-19 20:52:16 +01:00
|
|
|
POKE_U32(cp, len - 4);
|
2002-02-26 19:04:38 +01:00
|
|
|
cp[4] = padlen;
|
2012-12-12 00:46:31 +01:00
|
|
|
DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
|
|
|
|
len, padlen, aadlen));
|
2000-04-04 06:38:59 +02:00
|
|
|
|
|
|
|
/* compute MAC over seqnr and packet(length fields, payload, padding) */
|
2012-12-12 00:46:31 +01:00
|
|
|
if (mac && mac->enabled && !mac->etm) {
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = mac_compute(mac, state->p_send.seqnr,
|
|
|
|
sshbuf_ptr(state->outgoing_packet), len,
|
2015-01-13 20:31:40 +01:00
|
|
|
macbuf, sizeof(macbuf))) != 0)
|
2015-01-19 20:52:16 +01:00
|
|
|
goto out;
|
|
|
|
DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
|
|
|
/* encrypt packet and append to output buffer. */
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = sshbuf_reserve(state->output,
|
|
|
|
sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
|
|
|
|
goto out;
|
2016-08-03 07:41:57 +02:00
|
|
|
if ((r = cipher_crypt(state->send_context, state->p_send.seqnr, cp,
|
2015-01-19 20:52:16 +01:00
|
|
|
sshbuf_ptr(state->outgoing_packet),
|
|
|
|
len - aadlen, aadlen, authlen)) != 0)
|
|
|
|
goto out;
|
2000-04-04 06:38:59 +02:00
|
|
|
/* append unencrypted MAC */
|
2012-12-12 00:46:31 +01:00
|
|
|
if (mac && mac->enabled) {
|
|
|
|
if (mac->etm) {
|
|
|
|
/* EtM: compute mac over aadlen + cipher text */
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = mac_compute(mac, state->p_send.seqnr,
|
|
|
|
cp, len, macbuf, sizeof(macbuf))) != 0)
|
|
|
|
goto out;
|
2012-12-12 00:46:31 +01:00
|
|
|
DBG(debug("done calc MAC(EtM) out #%d",
|
2015-01-19 20:52:16 +01:00
|
|
|
state->p_send.seqnr));
|
2012-12-12 00:46:31 +01:00
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
|
|
|
|
goto out;
|
2012-12-12 00:46:31 +01:00
|
|
|
}
|
2000-04-04 06:38:59 +02:00
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
fprintf(stderr, "encrypted: ");
|
2015-01-19 20:52:16 +01:00
|
|
|
sshbuf_dump(state->output, stderr);
|
2000-04-04 06:38:59 +02:00
|
|
|
#endif
|
2000-04-16 03:18:38 +02:00
|
|
|
/* increment sequence number for outgoing packets */
|
2015-01-19 20:52:16 +01:00
|
|
|
if (++state->p_send.seqnr == 0)
|
2003-04-09 12:59:48 +02:00
|
|
|
logit("outgoing seqnr wraps around");
|
2015-01-19 20:52:16 +01: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-04-04 06:38:59 +02:00
|
|
|
|
2001-04-04 04:00:54 +02:00
|
|
|
if (type == SSH2_MSG_NEWKEYS)
|
2015-01-19 20:52:16 +01: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-04-04 06:38:59 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 11:57:07 +01: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-01-19 20:52:16 +01:00
|
|
|
int
|
|
|
|
ssh_packet_send2(struct ssh *ssh)
|
2003-04-09 12:50:06 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
2003-04-09 12:50:06 +02:00
|
|
|
struct packet *p;
|
2015-01-19 20:52:16 +01:00
|
|
|
u_char type;
|
2016-02-08 11:57:07 +01:00
|
|
|
int r, need_rekey;
|
2003-04-09 12:50:06 +02:00
|
|
|
|
2016-02-08 11:57:07 +01:00
|
|
|
if (sshbuf_len(state->outgoing_packet) < 6)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2015-01-19 20:52:16 +01:00
|
|
|
type = sshbuf_ptr(state->outgoing_packet)[5];
|
2016-02-08 11:57:07 +01:00
|
|
|
need_rekey = !ssh_packet_type_is_kex(type) &&
|
|
|
|
ssh_packet_need_rekeying(ssh, sshbuf_len(state->outgoing_packet));
|
2003-04-09 12:50:06 +02:00
|
|
|
|
2016-02-08 11:57:07 +01: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)
|
2020-10-18 13:32:01 +02:00
|
|
|
debug3_f("rekex triggered");
|
2016-02-08 11:57:07 +01:00
|
|
|
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);
|
2003-04-09 12:50:06 +02:00
|
|
|
}
|
2016-02-08 11:57:07 +01:00
|
|
|
return 0;
|
2003-04-09 12:50:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* rekeying starts with sending KEXINIT */
|
|
|
|
if (type == SSH2_MSG_KEXINIT)
|
2015-01-19 20:52:16 +01:00
|
|
|
state->rekeying = 1;
|
2003-04-09 12:50:06 +02:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
|
|
|
|
return r;
|
2003-04-09 12:50:06 +02:00
|
|
|
|
|
|
|
/* after a NEWKEYS message we can send the complete queue */
|
|
|
|
if (type == SSH2_MSG_NEWKEYS) {
|
2015-01-19 20:52:16 +01:00
|
|
|
state->rekeying = 0;
|
|
|
|
state->rekey_time = monotime();
|
|
|
|
while ((p = TAILQ_FIRST(&state->outgoing))) {
|
2003-04-09 12:50:06 +02:00
|
|
|
type = p->type;
|
2016-02-08 11:57:07 +01: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))) {
|
2020-10-18 13:32:01 +02:00
|
|
|
debug3_f("queued packet triggered rekex");
|
2016-02-08 11:57:07 +01:00
|
|
|
return kex_start_rekex(ssh);
|
|
|
|
}
|
2003-04-09 12:50:06 +02:00
|
|
|
debug("dequeue packet: %u", type);
|
2015-01-19 20:52:16 +01:00
|
|
|
sshbuf_free(state->outgoing_packet);
|
|
|
|
state->outgoing_packet = p->payload;
|
|
|
|
TAILQ_REMOVE(&state->outgoing, p, next);
|
2016-02-08 11:57:07 +01:00
|
|
|
memset(p, 0, sizeof(*p));
|
2013-06-01 23:31:17 +02:00
|
|
|
free(p);
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
|
|
|
|
return r;
|
2003-04-09 12:50:06 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
return 0;
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01: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.
|
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
2023-03-03 11:23:42 +01:00
|
|
|
int len, r, ms_remain = 0;
|
2021-11-13 22:14:13 +01:00
|
|
|
struct pollfd pfd;
|
1999-11-24 14:26:21 +01:00
|
|
|
char buf[8192];
|
2021-11-13 22:14:13 +01:00
|
|
|
struct timeval start;
|
|
|
|
struct timespec timespec, *timespecp = NULL;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2008-06-13 14:02:50 +02:00
|
|
|
DBG(debug("packet_read()"));
|
|
|
|
|
2015-01-30 02:13:33 +01:00
|
|
|
/*
|
|
|
|
* Since we are blocking, ensure that all written packets have
|
|
|
|
* been sent.
|
|
|
|
*/
|
2015-03-24 21:10:08 +01:00
|
|
|
if ((r = ssh_packet_write_wait(ssh)) != 0)
|
|
|
|
goto out;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
|
|
|
/* Stay in the loop until we have received a complete packet. */
|
|
|
|
for (;;) {
|
|
|
|
/* Try to read a packet from the buffer. */
|
2015-01-19 20:52:16 +01:00
|
|
|
r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
|
|
|
|
if (r != 0)
|
|
|
|
break;
|
1999-11-24 14:26:21 +01:00
|
|
|
/* If we got a packet, return it. */
|
2015-01-19 20:52:16 +01:00
|
|
|
if (*typep != SSH_MSG_NONE)
|
|
|
|
break;
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
|
|
|
* Otherwise, wait for some data to arrive, add it to the
|
|
|
|
* buffer, and try again.
|
|
|
|
*/
|
2021-11-13 22:14:13 +01:00
|
|
|
pfd.fd = state->connection_in;
|
|
|
|
pfd.events = POLLIN;
|
1999-11-25 01:54:57 +01:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
if (state->packet_timeout_ms > 0) {
|
|
|
|
ms_remain = state->packet_timeout_ms;
|
2021-11-13 22:14:13 +01:00
|
|
|
timespecp = ×pec;
|
2008-06-12 22:42:45 +02:00
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Wait for some data to arrive. */
|
2008-06-12 22:42:45 +02:00
|
|
|
for (;;) {
|
2020-03-06 19:20:44 +01:00
|
|
|
if (state->packet_timeout_ms > 0) {
|
2021-11-13 22:14:13 +01:00
|
|
|
ms_to_timespec(×pec, ms_remain);
|
2017-11-25 07:46:22 +01:00
|
|
|
monotime_tv(&start);
|
2008-06-12 22:42:45 +02:00
|
|
|
}
|
2021-11-13 22:14:13 +01:00
|
|
|
if ((r = ppoll(&pfd, 1, timespecp, NULL)) >= 0)
|
2008-06-12 22:42:45 +02:00
|
|
|
break;
|
2009-10-02 03:49:03 +02:00
|
|
|
if (errno != EAGAIN && errno != EINTR &&
|
2018-05-25 05:20:59 +02:00
|
|
|
errno != EWOULDBLOCK) {
|
|
|
|
r = SSH_ERR_SYSTEM_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-03-06 19:20:44 +01:00
|
|
|
if (state->packet_timeout_ms <= 0)
|
2008-06-12 22:42:45 +02:00
|
|
|
continue;
|
|
|
|
ms_subtract_diff(&start, &ms_remain);
|
|
|
|
if (ms_remain <= 0) {
|
2015-01-19 20:52:16 +01:00
|
|
|
r = 0;
|
2008-06-12 22:42:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 07:10:08 +01:00
|
|
|
if (r == 0) {
|
|
|
|
r = SSH_ERR_CONN_TIMEOUT;
|
|
|
|
goto out;
|
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Read data from the socket. */
|
2016-01-14 17:17:39 +01:00
|
|
|
len = read(state->connection_in, buf, sizeof(buf));
|
2015-03-24 21:10:08 +01:00
|
|
|
if (len == 0) {
|
|
|
|
r = SSH_ERR_CONN_CLOSED;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-06-28 15:35:04 +02:00
|
|
|
if (len == -1) {
|
2015-03-24 21:10:08 +01:00
|
|
|
r = SSH_ERR_SYSTEM_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2015-01-28 22:15:47 +01:00
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Append it to the buffer. */
|
2015-01-28 22:15:47 +01:00
|
|
|
if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
|
2015-03-24 21:10:08 +01:00
|
|
|
goto out;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
2015-03-24 21:10:08 +01:00
|
|
|
out:
|
2015-01-19 20:52:16 +01:00
|
|
|
return r;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2001-12-21 05:00:19 +01:00
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_read(struct ssh *ssh)
|
2001-12-21 05:00:19 +01:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
u_char type;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
|
2020-10-18 13:32:01 +02:00
|
|
|
fatal_fr(r, "read");
|
2015-01-19 20:52:16 +01:00
|
|
|
return type;
|
2001-12-21 05:00:19 +01:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01: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.
|
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2015-01-30 02:13:33 +01:00
|
|
|
int
|
|
|
|
ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-30 02:13:33 +01:00
|
|
|
int r;
|
|
|
|
u_char type;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2015-01-30 02:13:33 +01:00
|
|
|
if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
|
|
|
|
return r;
|
|
|
|
if (type != expected_type) {
|
|
|
|
if ((r = sshpkt_disconnect(ssh,
|
2015-01-19 20:52:16 +01:00
|
|
|
"Protocol error: expected packet type %d, got %d",
|
2015-01-30 02:13:33 +01:00
|
|
|
expected_type, type)) != 0)
|
|
|
|
return r;
|
|
|
|
return SSH_ERR_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
return 0;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 11:19:13 +02:00
|
|
|
static int
|
|
|
|
ssh_packet_read_poll2_mux(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
|
|
|
|
{
|
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
const u_char *cp;
|
|
|
|
size_t need;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (ssh->kex)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
*typep = SSH_MSG_NONE;
|
|
|
|
cp = sshbuf_ptr(state->input);
|
2022-12-02 19:59:24 +01:00
|
|
|
if (cp == NULL) { // fix CodeQL SM02311
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
2016-09-30 11:19:13 +02:00
|
|
|
if (state->packlen == 0) {
|
|
|
|
if (sshbuf_len(state->input) < 4 + 1)
|
|
|
|
return 0; /* packet is incomplete */
|
|
|
|
state->packlen = PEEK_U32(cp);
|
|
|
|
if (state->packlen < 4 + 1 ||
|
|
|
|
state->packlen > PACKET_MAX_SIZE)
|
|
|
|
return SSH_ERR_MESSAGE_INCOMPLETE;
|
|
|
|
}
|
|
|
|
need = state->packlen + 4;
|
|
|
|
if (sshbuf_len(state->input) < need)
|
|
|
|
return 0; /* packet is incomplete */
|
|
|
|
sshbuf_reset(state->incoming_packet);
|
|
|
|
if ((r = sshbuf_put(state->incoming_packet, cp + 4,
|
|
|
|
state->packlen)) != 0 ||
|
|
|
|
(r = sshbuf_consume(state->input, need)) != 0 ||
|
|
|
|
(r = sshbuf_get_u8(state->incoming_packet, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
|
|
|
|
return r;
|
|
|
|
if (ssh_packet_log_type(*typep))
|
2020-10-18 13:32:01 +02:00
|
|
|
debug3_f("type %u", *typep);
|
2016-09-30 11:19:13 +02:00
|
|
|
/* sshbuf_dump(state->incoming_packet, stderr); */
|
|
|
|
/* reset for next packet */
|
|
|
|
state->packlen = 0;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
int
|
|
|
|
ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
|
2000-04-04 06:38:59 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
2000-12-22 02:43:59 +01:00
|
|
|
u_int padlen, need;
|
2016-07-08 05:44:42 +02:00
|
|
|
u_char *cp;
|
2015-01-19 20:52:16 +01:00
|
|
|
u_int maclen, aadlen = 0, authlen = 0, block_size;
|
|
|
|
struct sshenc *enc = NULL;
|
|
|
|
struct sshmac *mac = NULL;
|
|
|
|
struct sshcomp *comp = NULL;
|
2015-01-13 20:31:40 +01:00
|
|
|
int r;
|
2000-04-04 06:38:59 +02:00
|
|
|
|
2016-09-30 11:19:13 +02:00
|
|
|
if (state->mux)
|
|
|
|
return ssh_packet_read_poll2_mux(ssh, typep, seqnr_p);
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
*typep = SSH_MSG_NONE;
|
|
|
|
|
|
|
|
if (state->packet_discard)
|
|
|
|
return 0;
|
2009-01-28 06:38:41 +01:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
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-01-09 06:12:19 +01:00
|
|
|
/* disable mac for authenticated encryption */
|
|
|
|
if ((authlen = cipher_authlen(enc->cipher)) != 0)
|
|
|
|
mac = NULL;
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
|
|
|
maclen = mac && mac->enabled ? mac->mac_len : 0;
|
2002-02-19 05:21:23 +01:00
|
|
|
block_size = enc ? enc->block_size : 8;
|
2013-01-09 06:12:19 +01:00
|
|
|
aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
|
2000-04-04 06:38:59 +02:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
if (aadlen && state->packlen == 0) {
|
2016-08-03 07:41:57 +02:00
|
|
|
if (cipher_get_length(state->receive_context,
|
2015-01-19 20:52:16 +01:00
|
|
|
&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) {
|
2012-12-12 00:46:31 +01:00
|
|
|
#ifdef PACKET_DEBUG
|
2015-01-19 20:52:16 +01:00
|
|
|
sshbuf_dump(state->input, stderr);
|
2012-12-12 00:46:31 +01:00
|
|
|
#endif
|
2015-01-19 20:52:16 +01:00
|
|
|
logit("Bad packet length %u.", state->packlen);
|
|
|
|
if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
|
|
|
|
return r;
|
2015-11-08 22:59:11 +01:00
|
|
|
return SSH_ERR_CONN_CORRUPT;
|
2012-12-12 00:46:31 +01:00
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
sshbuf_reset(state->incoming_packet);
|
|
|
|
} else if (state->packlen == 0) {
|
2000-04-04 06:38:59 +02:00
|
|
|
/*
|
|
|
|
* check if input size is less than the cipher block size,
|
|
|
|
* decrypt first block and extract length of incoming packet
|
|
|
|
*/
|
2015-01-19 20:52:16 +01: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;
|
2016-08-03 07:41:57 +02:00
|
|
|
if ((r = cipher_crypt(state->receive_context,
|
2015-01-19 20:52:16 +01:00
|
|
|
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) {
|
2003-09-22 13:06:46 +02:00
|
|
|
#ifdef PACKET_DEBUG
|
2015-01-19 20:52:16 +01:00
|
|
|
fprintf(stderr, "input: \n");
|
|
|
|
sshbuf_dump(state->input, stderr);
|
|
|
|
fprintf(stderr, "incoming_packet: \n");
|
|
|
|
sshbuf_dump(state->incoming_packet, stderr);
|
2003-09-22 13:06:46 +02:00
|
|
|
#endif
|
2015-01-19 20:52:16 +01:00
|
|
|
logit("Bad packet length %u.", state->packlen);
|
2016-07-18 13:35:33 +02:00
|
|
|
return ssh_packet_start_discard(ssh, enc, mac, 0,
|
|
|
|
PACKET_MAX_SIZE);
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = sshbuf_consume(state->input, block_size)) != 0)
|
|
|
|
goto out;
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
DBG(debug("input: packet len %u", state->packlen+4));
|
|
|
|
|
2012-12-12 00:46:31 +01:00
|
|
|
if (aadlen) {
|
|
|
|
/* only the payload is encrypted */
|
2015-01-19 20:52:16 +01:00
|
|
|
need = state->packlen;
|
2012-12-12 00:46:31 +01:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* the payload size and the payload are encrypted, but we
|
|
|
|
* have a partial packet of block_size bytes
|
|
|
|
*/
|
2015-01-19 20:52:16 +01:00
|
|
|
need = 4 + state->packlen - block_size;
|
2012-12-12 00:46:31 +01:00
|
|
|
}
|
2013-01-09 06:12:19 +01:00
|
|
|
DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
|
|
|
|
" aadlen %d", block_size, need, maclen, authlen, aadlen));
|
2008-12-01 11:40:48 +01:00
|
|
|
if (need % block_size != 0) {
|
|
|
|
logit("padding error: need %d block %d mod %d",
|
2000-04-04 06:38:59 +02:00
|
|
|
need, block_size, need % block_size);
|
2016-07-18 13:35:33 +02:00
|
|
|
return ssh_packet_start_discard(ssh, enc, mac, 0,
|
|
|
|
PACKET_MAX_SIZE - block_size);
|
2008-12-01 11:40:48 +01:00
|
|
|
}
|
2000-04-04 06:38:59 +02:00
|
|
|
/*
|
|
|
|
* check if the entire packet has been received and
|
2012-12-12 00:46:31 +01:00
|
|
|
* decrypt into incoming_packet:
|
|
|
|
* 'aadlen' bytes are unencrypted, but authenticated.
|
2013-01-09 06:12:19 +01:00
|
|
|
* 'need' bytes are encrypted, followed by either
|
|
|
|
* 'authlen' bytes of authentication tag or
|
2012-12-12 00:46:31 +01:00
|
|
|
* 'maclen' bytes of message authentication code.
|
2000-04-04 06:38:59 +02:00
|
|
|
*/
|
2015-01-19 20:52:16 +01:00
|
|
|
if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
|
2016-07-08 05:44:42 +02:00
|
|
|
return 0; /* packet is incomplete */
|
2000-04-04 06:38:59 +02:00
|
|
|
#ifdef PACKET_DEBUG
|
|
|
|
fprintf(stderr, "read_poll enc/full: ");
|
2015-01-19 20:52:16 +01:00
|
|
|
sshbuf_dump(state->input, stderr);
|
2000-04-04 06:38:59 +02:00
|
|
|
#endif
|
2016-07-08 05:44:42 +02:00
|
|
|
/* EtM: check mac over encrypted input */
|
2015-01-19 20:52:16 +01:00
|
|
|
if (mac && mac->enabled && mac->etm) {
|
2016-07-08 05:44:42 +02:00
|
|
|
if ((r = mac_check(mac, state->p_read.seqnr,
|
2015-01-19 20:52:16 +01:00
|
|
|
sshbuf_ptr(state->input), aadlen + need,
|
2016-07-08 05:44:42 +02:00
|
|
|
sshbuf_ptr(state->input) + aadlen + need + authlen,
|
|
|
|
maclen)) != 0) {
|
|
|
|
if (r == SSH_ERR_MAC_INVALID)
|
|
|
|
logit("Corrupted MAC on input.");
|
2015-01-19 20:52:16 +01:00
|
|
|
goto out;
|
2016-07-08 05:44:42 +02:00
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
}
|
|
|
|
if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
|
|
|
|
&cp)) != 0)
|
|
|
|
goto out;
|
2016-08-03 07:41:57 +02:00
|
|
|
if ((r = cipher_crypt(state->receive_context, state->p_read.seqnr, cp,
|
2015-01-19 20:52:16 +01:00
|
|
|
sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
|
|
|
|
goto out;
|
2000-04-16 03:18:38 +02:00
|
|
|
if (mac && mac->enabled) {
|
2016-07-08 05:44:42 +02: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-01-19 20:52:16 +01:00
|
|
|
goto out;
|
2009-01-28 06:38:41 +01:00
|
|
|
logit("Corrupted MAC on input.");
|
2017-03-11 14:07:35 +01:00
|
|
|
if (need + block_size > PACKET_MAX_SIZE)
|
2015-01-19 20:52:16 +01:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
return ssh_packet_start_discard(ssh, enc, mac,
|
2016-07-18 13:35:33 +02:00
|
|
|
sshbuf_len(state->incoming_packet),
|
2017-03-11 14:07:35 +01:00
|
|
|
PACKET_MAX_SIZE - need - block_size);
|
2009-01-28 06:38:41 +01:00
|
|
|
}
|
2016-07-08 05:44:42 +02:00
|
|
|
/* Remove MAC from input buffer */
|
2015-01-19 20:52:16 +01:00
|
|
|
DBG(debug("MAC #%d ok", state->p_read.seqnr));
|
|
|
|
if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
|
|
|
|
goto out;
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
2001-12-21 05:00:19 +01:00
|
|
|
if (seqnr_p != NULL)
|
2015-01-19 20:52:16 +01:00
|
|
|
*seqnr_p = state->p_read.seqnr;
|
|
|
|
if (++state->p_read.seqnr == 0)
|
2003-04-09 12:59:48 +02:00
|
|
|
logit("incoming seqnr wraps around");
|
2015-01-19 20:52:16 +01: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-04-04 06:38:59 +02:00
|
|
|
|
|
|
|
/* get padlen */
|
2015-01-19 20:52:16 +01:00
|
|
|
padlen = sshbuf_ptr(state->incoming_packet)[4];
|
2000-04-04 06:38:59 +02:00
|
|
|
DBG(debug("input: padlen %d", padlen));
|
2015-01-30 02:13:33 +01: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-04-04 06:38:59 +02:00
|
|
|
|
|
|
|
/* skip packet size + padlen, discard padding */
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
|
|
|
|
((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
|
|
|
|
goto out;
|
2000-04-04 06:38:59 +02:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
DBG(debug("input: len before de-compress %zd",
|
|
|
|
sshbuf_len(state->incoming_packet)));
|
2000-04-04 06:38:59 +02:00
|
|
|
if (comp && comp->enabled) {
|
2015-01-19 20:52:16 +01: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-04-04 06:38:59 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* get packet type, implies consume.
|
|
|
|
* return length of payload (without type field)
|
|
|
|
*/
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
|
|
|
|
goto out;
|
2016-01-29 06:46:01 +01:00
|
|
|
if (ssh_packet_log_type(*typep))
|
|
|
|
debug3("receive packet: type %u", *typep);
|
2015-01-30 02:13:33 +01: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;
|
|
|
|
}
|
2016-10-11 23:47:45 +02:00
|
|
|
if (state->hook_in != NULL &&
|
|
|
|
(r = state->hook_in(ssh, state->incoming_packet, typep,
|
|
|
|
state->hook_in_ctx)) != 0)
|
|
|
|
return r;
|
2016-09-19 21:02:19 +02:00
|
|
|
if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
|
2015-01-19 20:52:16 +01:00
|
|
|
r = ssh_packet_enable_delayed_compress(ssh);
|
|
|
|
else
|
|
|
|
r = 0;
|
2000-04-04 06:38:59 +02:00
|
|
|
#ifdef PACKET_DEBUG
|
2015-01-19 20:52:16 +01:00
|
|
|
fprintf(stderr, "read/plain[%d]:\r\n", *typep);
|
|
|
|
sshbuf_dump(state->incoming_packet, stderr);
|
2000-04-04 06:38:59 +02:00
|
|
|
#endif
|
2001-06-05 23:09:18 +02:00
|
|
|
/* reset for next packet */
|
2015-01-19 20:52:16 +01:00
|
|
|
state->packlen = 0;
|
2016-02-08 11:57:07 +01:00
|
|
|
|
2021-07-16 11:00:23 +02:00
|
|
|
if ((r = ssh_packet_check_rekey(ssh)) != 0)
|
|
|
|
return r;
|
2015-01-19 20:52:16 +01:00
|
|
|
out:
|
|
|
|
return r;
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
|
2000-04-04 06:38:59 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
2002-06-23 23:49:25 +02:00
|
|
|
u_int reason, seqnr;
|
2015-01-19 20:52:16 +01:00
|
|
|
int r;
|
|
|
|
u_char *msg;
|
2000-04-04 06:38:59 +02:00
|
|
|
|
2001-06-05 23:09:18 +02:00
|
|
|
for (;;) {
|
2015-01-19 20:52:16 +01:00
|
|
|
msg = NULL;
|
2017-05-01 01:13:25 +02: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));
|
|
|
|
}
|
|
|
|
switch (*typep) {
|
|
|
|
case SSH2_MSG_IGNORE:
|
|
|
|
debug3("Received SSH2_MSG_IGNORE");
|
|
|
|
break;
|
|
|
|
case SSH2_MSG_DEBUG:
|
|
|
|
if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
|
|
|
|
(r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
|
|
|
|
(r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
|
2015-01-19 20:52:16 +01:00
|
|
|
free(msg);
|
2017-05-01 01:13:25 +02:00
|
|
|
return r;
|
2001-02-05 13:42:17 +01:00
|
|
|
}
|
2017-05-01 01:13:25 +02:00
|
|
|
debug("Remote: %.900s", msg);
|
|
|
|
free(msg);
|
|
|
|
break;
|
|
|
|
case SSH2_MSG_DISCONNECT:
|
|
|
|
if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
|
|
|
|
(r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
|
|
|
|
return r;
|
|
|
|
/* Ignore normal client exit notifications */
|
|
|
|
do_log2(ssh->state->server_side &&
|
|
|
|
reason == SSH2_DISCONNECT_BY_APPLICATION ?
|
|
|
|
SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
|
|
|
|
"Received disconnect from %s port %d:"
|
|
|
|
"%u: %.400s", ssh_remote_ipaddr(ssh),
|
|
|
|
ssh_remote_port(ssh), reason, msg);
|
|
|
|
free(msg);
|
|
|
|
return SSH_ERR_DISCONNECTED;
|
|
|
|
case SSH2_MSG_UNIMPLEMENTED:
|
|
|
|
if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
|
|
|
|
return r;
|
|
|
|
debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
|
|
|
|
seqnr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
2022-01-21 07:58:06 +01:00
|
|
|
* Buffers the supplied input data. This is intended to be used together
|
|
|
|
* with packet_read_poll().
|
1999-11-25 01:54:57 +01:00
|
|
|
*/
|
2015-01-28 22:15:47 +01:00
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (state->packet_discard) {
|
|
|
|
state->keep_alive_timeouts = 0; /* ?? */
|
|
|
|
if (len >= state->packet_discard) {
|
|
|
|
if ((r = ssh_packet_stop_discard(ssh)) != 0)
|
2015-01-28 22:15:47 +01:00
|
|
|
return r;
|
2015-01-19 20:52:16 +01:00
|
|
|
}
|
|
|
|
state->packet_discard -= len;
|
2015-01-28 22:15:47 +01:00
|
|
|
return 0;
|
2009-01-28 06:38:41 +01:00
|
|
|
}
|
2022-01-21 07:58:06 +01:00
|
|
|
if ((r = sshbuf_put(state->input, buf, len)) != 0)
|
2015-01-28 22:15:47 +01:00
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
2000-04-04 06:38:59 +02:00
|
|
|
}
|
|
|
|
|
2022-01-22 01:49:34 +01:00
|
|
|
/* Reads and buffers data from the specified fd */
|
|
|
|
int
|
|
|
|
ssh_packet_process_read(struct ssh *ssh, int fd)
|
|
|
|
{
|
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
int r;
|
|
|
|
size_t rlen;
|
|
|
|
|
|
|
|
if ((r = sshbuf_read(fd, state->input, PACKET_MAX_SIZE, &rlen)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (state->packet_discard) {
|
|
|
|
if ((r = sshbuf_consume_end(state->input, rlen)) != 0)
|
|
|
|
return r;
|
|
|
|
state->keep_alive_timeouts = 0; /* ?? */
|
|
|
|
if (rlen >= state->packet_discard) {
|
|
|
|
if ((r = ssh_packet_stop_discard(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
state->packet_discard -= rlen;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-04-16 03:18:38 +02:00
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_remaining(struct ssh *ssh)
|
2000-04-16 03:18:38 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
return sshbuf_len(ssh->state->incoming_packet);
|
2010-08-31 14:36:39 +02:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01: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-01-30 02:13:33 +01:00
|
|
|
* exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
|
1999-11-25 01:54:57 +01:00
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
void
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
1999-11-24 14:26:21 +01:00
|
|
|
char buf[1024];
|
|
|
|
va_list args;
|
2015-01-19 20:52:16 +01:00
|
|
|
int r;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2017-05-01 01:13:25 +02:00
|
|
|
if ((ssh->compat & SSH_BUG_DEBUG))
|
2000-12-07 02:24:58 +01:00
|
|
|
return;
|
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2017-10-13 23:13:54 +02:00
|
|
|
debug3("sending debug message: %s", buf);
|
|
|
|
|
2017-05-01 01:13:25 +02: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 ||
|
|
|
|
(r = ssh_packet_write_wait(ssh)) != 0)
|
2020-10-18 13:32:01 +02:00
|
|
|
fatal_fr(r, "send DEBUG");
|
2015-01-30 02:13:33 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 06:55:29 +01:00
|
|
|
void
|
|
|
|
sshpkt_fmt_connection_id(struct ssh *ssh, char *s, size_t l)
|
2017-02-04 00:03:33 +01:00
|
|
|
{
|
|
|
|
snprintf(s, l, "%.200s%s%s port %d",
|
|
|
|
ssh->log_preamble ? ssh->log_preamble : "",
|
|
|
|
ssh->log_preamble ? " " : "",
|
|
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
|
|
|
|
}
|
|
|
|
|
2015-01-30 02:13:33 +01:00
|
|
|
/*
|
|
|
|
* Pretty-print connection-terminating errors and exit.
|
|
|
|
*/
|
2019-01-19 22:33:13 +01:00
|
|
|
static void
|
|
|
|
sshpkt_vfatal(struct ssh *ssh, int r, const char *fmt, va_list ap)
|
2015-01-30 02:13:33 +01:00
|
|
|
{
|
2019-01-19 22:33:13 +01:00
|
|
|
char *tag = NULL, remote_id[512];
|
2020-01-30 08:20:05 +01:00
|
|
|
int oerrno = errno;
|
2017-02-04 00:03:33 +01:00
|
|
|
|
2017-12-10 06:55:29 +01:00
|
|
|
sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
|
2017-02-04 00:03:33 +01:00
|
|
|
|
2015-01-30 02:13:33 +01:00
|
|
|
switch (r) {
|
|
|
|
case SSH_ERR_CONN_CLOSED:
|
2017-05-31 10:09:45 +02:00
|
|
|
ssh_packet_clear_keys(ssh);
|
2017-02-04 00:03:33 +01:00
|
|
|
logdie("Connection closed by %s", remote_id);
|
2015-01-30 02:13:33 +01:00
|
|
|
case SSH_ERR_CONN_TIMEOUT:
|
2017-05-31 10:09:45 +02:00
|
|
|
ssh_packet_clear_keys(ssh);
|
2017-02-04 00:03:33 +01:00
|
|
|
logdie("Connection %s %s timed out",
|
|
|
|
ssh->state->server_side ? "from" : "to", remote_id);
|
2015-05-01 09:10:01 +02:00
|
|
|
case SSH_ERR_DISCONNECTED:
|
2017-05-31 10:09:45 +02:00
|
|
|
ssh_packet_clear_keys(ssh);
|
2017-02-04 00:03:33 +01:00
|
|
|
logdie("Disconnected from %s", remote_id);
|
2015-05-01 09:10:01 +02:00
|
|
|
case SSH_ERR_SYSTEM_ERROR:
|
2017-05-31 10:09:45 +02:00
|
|
|
if (errno == ECONNRESET) {
|
|
|
|
ssh_packet_clear_keys(ssh);
|
2017-02-04 00:03:33 +01:00
|
|
|
logdie("Connection reset by %s", remote_id);
|
2017-05-31 10:09:45 +02:00
|
|
|
}
|
2015-05-01 09:10:01 +02:00
|
|
|
/* FALLTHROUGH */
|
2015-07-29 06:43:06 +02: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:
|
2023-04-06 05:21:31 +02:00
|
|
|
if (ssh->kex && ssh->kex->failed_choice) {
|
2017-05-31 10:09:45 +02:00
|
|
|
ssh_packet_clear_keys(ssh);
|
2020-01-30 08:20:05 +01:00
|
|
|
errno = oerrno;
|
2017-02-04 00:03:33 +01:00
|
|
|
logdie("Unable to negotiate with %s: %s. "
|
|
|
|
"Their offer: %s", remote_id, ssh_err(r),
|
2015-12-11 04:24:25 +01:00
|
|
|
ssh->kex->failed_choice);
|
2015-07-29 06:43:06 +02:00
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2015-01-30 02:13:33 +01:00
|
|
|
default:
|
2019-01-19 22:33:13 +01:00
|
|
|
if (vasprintf(&tag, fmt, ap) == -1) {
|
|
|
|
ssh_packet_clear_keys(ssh);
|
2020-10-18 13:32:01 +02:00
|
|
|
logdie_f("could not allocate failure message");
|
2019-01-19 22:33:13 +01:00
|
|
|
}
|
2017-05-31 10:09:45 +02:00
|
|
|
ssh_packet_clear_keys(ssh);
|
2020-01-30 08:20:05 +01:00
|
|
|
errno = oerrno;
|
2020-10-18 13:32:01 +02:00
|
|
|
logdie_r(r, "%s%sConnection %s %s",
|
2015-01-30 02:13:33 +01:00
|
|
|
tag != NULL ? tag : "", tag != NULL ? ": " : "",
|
2020-10-18 13:32:01 +02:00
|
|
|
ssh->state->server_side ? "from" : "to", remote_id);
|
2015-01-30 02:13:33 +01:00
|
|
|
}
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2019-01-19 22:33:13 +01:00
|
|
|
void
|
|
|
|
sshpkt_fatal(struct ssh *ssh, int r, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
sshpkt_vfatal(ssh, r, fmt, ap);
|
|
|
|
/* NOTREACHED */
|
|
|
|
va_end(ap);
|
2020-10-18 13:32:01 +02:00
|
|
|
logdie_f("should have exited");
|
2019-01-19 22:33:13 +01:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01: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.
|
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
void
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
|
1999-11-24 14:26:21 +01:00
|
|
|
{
|
2017-02-04 00:03:33 +01:00
|
|
|
char buf[1024], remote_id[512];
|
1999-11-24 14:26:21 +01:00
|
|
|
va_list args;
|
|
|
|
static int disconnecting = 0;
|
2015-01-19 20:52:16 +01:00
|
|
|
int r;
|
2002-07-08 00:11:51 +02:00
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
if (disconnecting) /* Guard against recursive invocations. */
|
|
|
|
fatal("packet_disconnect called recursively.");
|
|
|
|
disconnecting = 1;
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
|
|
|
* Format the message. Note that the caller must make sure the
|
|
|
|
* message is of limited size.
|
|
|
|
*/
|
2017-12-10 06:55:29 +01:00
|
|
|
sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
|
1999-11-24 14:26:21 +01:00
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2002-11-09 16:46:24 +01:00
|
|
|
/* Display the error locally */
|
2017-02-04 00:03:33 +01:00
|
|
|
logit("Disconnecting %s: %.100s", remote_id, buf);
|
2002-11-09 16:46:24 +01:00
|
|
|
|
2015-01-30 02:13:33 +01:00
|
|
|
/*
|
|
|
|
* Send the disconnect message to the other side, and wait
|
|
|
|
* for it to get sent.
|
|
|
|
*/
|
|
|
|
if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
|
2019-01-19 22:33:13 +01:00
|
|
|
sshpkt_fatal(ssh, r, "%s", __func__);
|
2015-01-30 02:13:33 +01:00
|
|
|
|
|
|
|
if ((r = ssh_packet_write_wait(ssh)) != 0)
|
2019-01-19 22:33:13 +01:00
|
|
|
sshpkt_fatal(ssh, r, "%s", __func__);
|
1999-11-24 14:26:21 +01:00
|
|
|
|
|
|
|
/* Close the connection. */
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_close(ssh);
|
2003-10-02 08:12:36 +02:00
|
|
|
cleanup_exit(255);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2015-01-30 02:13:33 +01:00
|
|
|
/*
|
|
|
|
* Checks if there is any buffered output, and tries to write some of
|
|
|
|
* the output.
|
|
|
|
*/
|
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_write_poll(struct ssh *ssh)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
int len = sshbuf_len(state->output);
|
2016-01-14 17:17:39 +01:00
|
|
|
int r;
|
2002-07-08 00:11:51 +02:00
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
if (len > 0) {
|
2016-01-14 17:17:39 +01:00
|
|
|
len = write(state->connection_out,
|
|
|
|
sshbuf_ptr(state->output), len);
|
2008-07-05 01:40:56 +02:00
|
|
|
if (len == -1) {
|
2009-10-02 03:49:03 +02:00
|
|
|
if (errno == EINTR || errno == EAGAIN ||
|
|
|
|
errno == EWOULDBLOCK)
|
2015-01-30 02:13:33 +01:00
|
|
|
return 0;
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
2016-01-14 17:17:39 +01:00
|
|
|
if (len == 0)
|
2015-01-30 02:13:33 +01:00
|
|
|
return SSH_ERR_CONN_CLOSED;
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = sshbuf_consume(state->output, len)) != 0)
|
2015-01-30 02:13:33 +01:00
|
|
|
return r;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
2015-01-30 02:13:33 +01:00
|
|
|
return 0;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
|
|
|
* Calls packet_write_poll repeatedly until all pending output data has been
|
|
|
|
* written.
|
|
|
|
*/
|
2015-01-30 02:13:33 +01:00
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_write_wait(struct ssh *ssh)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-30 02:13:33 +01:00
|
|
|
int ret, r, ms_remain = 0;
|
2021-11-13 22:14:13 +01:00
|
|
|
struct timeval start;
|
|
|
|
struct timespec timespec, *timespecp = NULL;
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
2021-11-13 22:14:13 +01:00
|
|
|
struct pollfd pfd;
|
2001-03-05 08:07:49 +01:00
|
|
|
|
2021-11-13 22:14:13 +01:00
|
|
|
if ((r = ssh_packet_write_poll(ssh)) != 0)
|
2015-09-21 06:31:00 +02:00
|
|
|
return r;
|
2015-01-19 20:52:16 +01:00
|
|
|
while (ssh_packet_have_data_to_write(ssh)) {
|
2021-11-13 22:14:13 +01:00
|
|
|
pfd.fd = state->connection_out;
|
|
|
|
pfd.events = POLLOUT;
|
2008-06-12 22:42:45 +02:00
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
if (state->packet_timeout_ms > 0) {
|
|
|
|
ms_remain = state->packet_timeout_ms;
|
2021-11-13 22:14:13 +01:00
|
|
|
timespecp = ×pec;
|
2008-06-12 22:42:45 +02:00
|
|
|
}
|
|
|
|
for (;;) {
|
2020-03-06 19:20:44 +01:00
|
|
|
if (state->packet_timeout_ms > 0) {
|
2021-11-13 22:14:13 +01:00
|
|
|
ms_to_timespec(×pec, ms_remain);
|
2017-11-25 07:46:22 +01:00
|
|
|
monotime_tv(&start);
|
2008-06-12 22:42:45 +02:00
|
|
|
}
|
2021-11-13 22:14:13 +01:00
|
|
|
if ((ret = ppoll(&pfd, 1, timespecp, NULL)) >= 0)
|
2008-06-12 22:42:45 +02:00
|
|
|
break;
|
2009-10-02 03:49:03 +02:00
|
|
|
if (errno != EAGAIN && errno != EINTR &&
|
|
|
|
errno != EWOULDBLOCK)
|
2008-06-12 22:42:45 +02:00
|
|
|
break;
|
2020-03-06 19:20:44 +01:00
|
|
|
if (state->packet_timeout_ms <= 0)
|
2008-06-12 22:42:45 +02:00
|
|
|
continue;
|
|
|
|
ms_subtract_diff(&start, &ms_remain);
|
|
|
|
if (ms_remain <= 0) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 22:14:13 +01:00
|
|
|
if (ret == 0)
|
2015-01-30 02:13:33 +01:00
|
|
|
return SSH_ERR_CONN_TIMEOUT;
|
2021-11-13 22:14:13 +01:00
|
|
|
if ((r = ssh_packet_write_poll(ssh)) != 0)
|
2015-01-30 02:13:33 +01:00
|
|
|
return r;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
2015-01-30 02:13:33 +01:00
|
|
|
return 0;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if there is buffered data to write to the connection. */
|
|
|
|
|
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_have_data_to_write(struct ssh *ssh)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
return sshbuf_len(ssh->state->output) != 0;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if there is not too much data to write to the connection. */
|
|
|
|
|
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
if (ssh->state->interactive_mode)
|
|
|
|
return sshbuf_len(ssh->state->output) < 16384;
|
1999-11-24 14:26:21 +01:00
|
|
|
else
|
2015-01-19 20:52:16 +01:00
|
|
|
return sshbuf_len(ssh->state->output) < 128 * 1024;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
void
|
|
|
|
ssh_packet_set_tos(struct ssh *ssh, int tos)
|
2002-12-23 03:41:41 +01:00
|
|
|
{
|
2017-07-24 01:37:02 +02:00
|
|
|
if (!ssh_packet_connection_is_on_socket(ssh) || tos == INT_MAX)
|
2002-12-23 03:41:41 +01:00
|
|
|
return;
|
2020-11-27 01:49:58 +01:00
|
|
|
set_sock_tos(ssh->state->connection_in, tos);
|
2003-11-22 05:02:42 +01:00
|
|
|
}
|
2002-12-23 03:41:41 +01:00
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
/* Informs that the current session is interactive. Sets IP flags for that. */
|
|
|
|
|
|
|
|
void
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
|
|
|
|
if (state->set_interactive_called)
|
2001-01-18 03:04:35 +01:00
|
|
|
return;
|
2015-01-19 20:52:16 +01:00
|
|
|
state->set_interactive_called = 1;
|
2001-01-18 03:04:35 +01:00
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Record that we are in interactive mode. */
|
2015-01-19 20:52:16 +01:00
|
|
|
state->interactive_mode = interactive;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2000-01-14 05:45:46 +01:00
|
|
|
/* Only set socket options if using a socket. */
|
2015-01-19 20:52:16 +01:00
|
|
|
if (!ssh_packet_connection_is_on_socket(ssh))
|
2003-04-27 19:55:33 +02:00
|
|
|
return;
|
2015-01-19 20:52:16 +01:00
|
|
|
set_nodelay(state->connection_in);
|
2020-11-27 01:49:58 +01:00
|
|
|
ssh_packet_set_tos(ssh, interactive ? qos_interactive : qos_bulk);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if the current connection is interactive. */
|
|
|
|
|
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_is_interactive(struct ssh *ssh)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
return ssh->state->interactive_mode;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
1999-11-21 03:23:52 +01:00
|
|
|
|
2004-05-13 08:39:33 +02:00
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
|
1999-11-21 03:23:52 +01:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
|
|
|
|
if (state->set_maxsize_called) {
|
2021-11-26 00:02:24 +01:00
|
|
|
logit_f("called twice: old %d new %d",
|
2015-01-19 20:52:16 +01:00
|
|
|
state->max_packet_size, s);
|
1999-11-24 14:26:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (s < 4 * 1024 || s > 1024 * 1024) {
|
2021-11-26 00:02:24 +01:00
|
|
|
logit_f("bad size %d", s);
|
1999-11-24 14:26:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
state->set_maxsize_called = 1;
|
2021-11-26 00:02:24 +01:00
|
|
|
debug_f("setting to %d", s);
|
2015-01-19 20:52:16 +01:00
|
|
|
state->max_packet_size = s;
|
1999-11-24 14:26:21 +01:00
|
|
|
return s;
|
1999-11-21 03:23:52 +01:00
|
|
|
}
|
2001-03-05 07:17:49 +01:00
|
|
|
|
2009-06-21 10:12:20 +02:00
|
|
|
int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_inc_alive_timeouts(struct ssh *ssh)
|
2009-06-21 10:12:20 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
return ++ssh->state->keep_alive_timeouts;
|
2009-06-21 10:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
|
2009-06-21 10:12:20 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh->state->keep_alive_timeouts = ka;
|
2009-06-21 10:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u_int
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_get_maxsize(struct ssh *ssh)
|
2001-11-12 01:02:52 +01:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
return ssh->state->max_packet_size;
|
2001-11-12 01:02:52 +01:00
|
|
|
}
|
|
|
|
|
2003-04-09 12:50:06 +02:00
|
|
|
void
|
2017-02-03 03:56:00 +01:00
|
|
|
ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, u_int32_t seconds)
|
2003-04-09 12:50:06 +02:00
|
|
|
{
|
2017-02-03 03:56:00 +01:00
|
|
|
debug3("rekey after %llu bytes, %u seconds", (unsigned long long)bytes,
|
|
|
|
(unsigned int)seconds);
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh->state->rekey_limit = bytes;
|
|
|
|
ssh->state->rekey_interval = seconds;
|
2013-05-16 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_get_rekey_timeout(struct ssh *ssh)
|
2013-05-16 12:28:16 +02:00
|
|
|
{
|
|
|
|
time_t seconds;
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
|
2013-06-01 23:46:16 +02:00
|
|
|
monotime();
|
2013-05-16 12:29:28 +02:00
|
|
|
return (seconds <= 0 ? 1 : seconds);
|
2003-04-09 12:50:06 +02:00
|
|
|
}
|
2005-07-26 13:54:56 +02:00
|
|
|
|
|
|
|
void
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_set_server(struct ssh *ssh)
|
2005-07-26 13:54:56 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh->state->server_side = 1;
|
2018-12-27 04:25:24 +01:00
|
|
|
ssh->kex->server = 1; /* XXX unify? */
|
2005-07-26 13:54:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_set_authenticated(struct ssh *ssh)
|
2005-07-26 13:54:56 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh->state->after_authentication = 1;
|
2009-06-21 10:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_get_input(struct ssh *ssh)
|
2009-06-21 10:12:20 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
return (void *)ssh->state->input;
|
2009-06-21 10:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh_packet_get_output(struct ssh *ssh)
|
2009-06-21 10:12:20 +02:00
|
|
|
{
|
2015-01-19 20:52:16 +01:00
|
|
|
return (void *)ssh->state->output;
|
2005-07-26 13:54:56 +02:00
|
|
|
}
|
2009-07-05 23:11:13 +02:00
|
|
|
|
2014-05-15 06:37:39 +02:00
|
|
|
/* Reset after_authentication and reset compression in post-auth privsep */
|
2015-01-19 20:52:16 +01:00
|
|
|
static int
|
|
|
|
ssh_packet_set_postauth(struct ssh *ssh)
|
2014-05-15 06:37:39 +02:00
|
|
|
{
|
2016-09-28 18:33:06 +02:00
|
|
|
int r;
|
2014-05-15 06:37:39 +02:00
|
|
|
|
2020-10-18 13:32:01 +02:00
|
|
|
debug_f("called");
|
2014-05-15 06:37:39 +02:00
|
|
|
/* This was set in net child, but is not visible in user child */
|
2015-01-19 20:52:16 +01:00
|
|
|
ssh->state->after_authentication = 1;
|
|
|
|
ssh->state->rekeying = 0;
|
2016-09-28 18:33:06 +02:00
|
|
|
if ((r = ssh_packet_enable_delayed_compress(ssh)) != 0)
|
|
|
|
return r;
|
2015-01-19 20:52:16 +01:00
|
|
|
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;
|
|
|
|
|
2021-01-27 11:05:28 +01:00
|
|
|
if ((r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
|
2017-12-19 00:13:42 +01:00
|
|
|
(r = sshbuf_put_cstring(m, kex->hostkey_alg)) != 0 ||
|
2015-01-19 20:52:16 +01:00
|
|
|
(r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
|
2017-12-19 00:13:42 +01:00
|
|
|
(r = sshbuf_put_u32(m, kex->hostkey_nid)) != 0 ||
|
2015-01-19 20:52:16 +01:00
|
|
|
(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 ||
|
2018-12-27 04:25:24 +01:00
|
|
|
(r = sshbuf_put_stringb(m, kex->client_version)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(m, kex->server_version)) != 0 ||
|
2021-01-27 11:05:28 +01:00
|
|
|
(r = sshbuf_put_stringb(m, kex->session_id)) != 0 ||
|
2018-12-27 04:25:24 +01:00
|
|
|
(r = sshbuf_put_u32(m, kex->flags)) != 0)
|
2015-01-19 20:52:16 +01:00
|
|
|
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;
|
2016-08-03 07:41:57 +02:00
|
|
|
cc = (mode == MODE_OUT) ? ssh->state->send_context :
|
|
|
|
ssh->state->receive_context;
|
2015-01-19 20:52:16 +01:00
|
|
|
if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
|
|
|
|
return r;
|
|
|
|
if ((b = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_put_cstring(b, enc->name)) != 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_cstring(b, comp->name)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = sshbuf_put_stringb(m, b);
|
|
|
|
out:
|
2015-12-11 05:21:11 +01:00
|
|
|
sshbuf_free(b);
|
2015-01-19 20:52:16 +01: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;
|
2017-05-01 01:13:25 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
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 ||
|
|
|
|
(r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
|
|
|
|
(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 ||
|
2017-05-08 02:21:36 +02:00
|
|
|
(r = sshbuf_put_u64(m, state->p_read.bytes)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(m, state->input)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(m, state->output)) != 0)
|
2017-05-08 08:03:39 +02:00
|
|
|
return r;
|
2015-01-19 20:52:16 +01:00
|
|
|
|
|
|
|
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_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;
|
2017-06-24 08:38:11 +02:00
|
|
|
if ((enc->cipher = cipher_by_name(enc->name)) == NULL) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2015-01-19 20:52:16 +01:00
|
|
|
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_cstring(b, &comp->name, NULL)) != 0)
|
|
|
|
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:
|
2015-12-10 18:08:40 +01:00
|
|
|
free(newkey);
|
2015-12-11 05:21:11 +01:00
|
|
|
sshbuf_free(b);
|
2015-01-19 20:52:16 +01: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;
|
|
|
|
|
2018-12-27 04:25:24 +01:00
|
|
|
if ((kex = kex_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2021-01-27 11:05:28 +01:00
|
|
|
if ((r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
|
2017-12-19 00:13:42 +01:00
|
|
|
(r = sshbuf_get_cstring(m, &kex->hostkey_alg, NULL)) != 0 ||
|
2015-01-19 20:52:16 +01:00
|
|
|
(r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
|
2017-12-19 00:13:42 +01:00
|
|
|
(r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_nid)) != 0 ||
|
2015-01-19 20:52:16 +01:00
|
|
|
(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 ||
|
2018-12-27 04:25:24 +01:00
|
|
|
(r = sshbuf_get_stringb(m, kex->client_version)) != 0 ||
|
|
|
|
(r = sshbuf_get_stringb(m, kex->server_version)) != 0 ||
|
2021-01-27 11:05:28 +01:00
|
|
|
(r = sshbuf_get_stringb(m, kex->session_id)) != 0 ||
|
2018-12-27 04:25:24 +01:00
|
|
|
(r = sshbuf_get_u32(m, &kex->flags)) != 0)
|
2015-01-19 20:52:16 +01:00
|
|
|
goto out;
|
|
|
|
kex->server = 1;
|
|
|
|
kex->done = 1;
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
if (r != 0 || kexp == NULL) {
|
2018-12-27 04:25:24 +01:00
|
|
|
kex_free(kex);
|
2015-01-19 20:52:16 +01:00
|
|
|
if (kexp != NULL)
|
|
|
|
*kexp = NULL;
|
|
|
|
} else {
|
2018-12-27 04:25:24 +01:00
|
|
|
kex_free(*kexp);
|
2015-01-19 20:52:16 +01:00
|
|
|
*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;
|
2017-05-08 01:12:57 +02:00
|
|
|
const u_char *input, *output;
|
|
|
|
size_t ilen, olen;
|
2015-01-19 20:52:16 +01:00
|
|
|
int r;
|
2017-05-01 01:13:25 +02:00
|
|
|
|
|
|
|
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 ||
|
|
|
|
(r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
|
|
|
|
(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;
|
|
|
|
/*
|
2020-07-06 01:59:45 +02:00
|
|
|
* We set the time here so that in post-auth privsep child we
|
2017-05-01 01:13:25 +02:00
|
|
|
* 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;
|
|
|
|
|
2016-09-28 18:33:06 +02:00
|
|
|
if ((r = ssh_packet_set_postauth(ssh)) != 0)
|
2015-01-19 20:52:16 +01:00
|
|
|
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;
|
2020-10-18 13:32:01 +02:00
|
|
|
debug3_f("done");
|
2015-01-19 20:52:16 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-01-21 11:35:09 +01:00
|
|
|
int
|
|
|
|
sshpkt_getb_froms(struct ssh *ssh, struct sshbuf **valp)
|
|
|
|
{
|
|
|
|
return sshbuf_froms(ssh->state->incoming_packet, valp);
|
|
|
|
}
|
|
|
|
|
2015-04-27 03:52:30 +02:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2015-01-19 20:52:16 +01: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-04-27 03:52:30 +02:00
|
|
|
#endif /* OPENSSL_HAS_ECC */
|
2015-01-19 20:52:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
|
|
|
|
{
|
|
|
|
return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
|
|
|
|
}
|
2015-01-30 13:10:17 +01:00
|
|
|
#endif /* WITH_OPENSSL */
|
2015-01-19 20:52:16 +01:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:32:07 +02:00
|
|
|
int
|
|
|
|
sshpkt_peek_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
|
|
|
|
{
|
|
|
|
return sshbuf_peek_string_direct(ssh->state->incoming_packet, valp, lenp);
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
int
|
|
|
|
sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
|
|
|
|
{
|
|
|
|
return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
|
|
|
|
}
|
|
|
|
|
2015-04-27 03:52:30 +02:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2015-01-19 20:52:16 +01: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-04-27 03:52:30 +02:00
|
|
|
#endif /* OPENSSL_HAS_ECC */
|
2015-01-19 20:52:16 +01:00
|
|
|
|
|
|
|
int
|
2019-01-21 10:54:11 +01:00
|
|
|
sshpkt_get_bignum2(struct ssh *ssh, BIGNUM **valp)
|
2015-01-19 20:52:16 +01:00
|
|
|
{
|
2019-01-21 10:54:11 +01:00
|
|
|
return sshbuf_get_bignum2(ssh->state->incoming_packet, valp);
|
2015-01-19 20:52:16 +01:00
|
|
|
}
|
2015-01-30 13:10:17 +01:00
|
|
|
#endif /* WITH_OPENSSL */
|
2015-01-19 20:52:16 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2017-05-01 01:13:25 +02:00
|
|
|
u_char buf[6]; /* u32 packet length, u8 pad len, u8 type */
|
2015-01-19 20:52:16 +01:00
|
|
|
|
|
|
|
DBG(debug("packet_start[%d]", type));
|
2017-05-01 01:13:25 +02:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
buf[sizeof(buf) - 1] = type;
|
2015-01-19 20:52:16 +01:00
|
|
|
sshbuf_reset(ssh->state->outgoing_packet);
|
2017-05-01 01:13:25 +02:00
|
|
|
return sshbuf_put(ssh->state->outgoing_packet, buf, sizeof(buf));
|
2015-01-19 20:52:16 +01:00
|
|
|
}
|
|
|
|
|
2016-09-30 11:19:13 +02:00
|
|
|
static int
|
|
|
|
ssh_packet_send_mux(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
struct session_state *state = ssh->state;
|
|
|
|
u_char type, *cp;
|
|
|
|
size_t len;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (ssh->kex)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
len = sshbuf_len(state->outgoing_packet);
|
|
|
|
if (len < 6)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
cp = sshbuf_mutable_ptr(state->outgoing_packet);
|
2022-11-30 17:57:01 +01:00
|
|
|
if (cp == NULL) // fix CodeQL SM02313
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2016-09-30 11:19:13 +02:00
|
|
|
type = cp[5];
|
|
|
|
if (ssh_packet_log_type(type))
|
2020-10-18 13:32:01 +02:00
|
|
|
debug3_f("type %u", type);
|
2016-09-30 11:19:13 +02:00
|
|
|
/* drop everything, but the connection protocol */
|
|
|
|
if (type >= SSH2_MSG_CONNECTION_MIN &&
|
|
|
|
type <= SSH2_MSG_CONNECTION_MAX) {
|
|
|
|
POKE_U32(cp, len - 4);
|
|
|
|
if ((r = sshbuf_putb(state->output,
|
|
|
|
state->outgoing_packet)) != 0)
|
|
|
|
return r;
|
|
|
|
/* sshbuf_dump(state->output, stderr); */
|
|
|
|
}
|
|
|
|
sshbuf_reset(state->outgoing_packet);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:32:07 +02: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.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sshpkt_msg_ignore(struct ssh *ssh, u_int nbytes)
|
|
|
|
{
|
|
|
|
u_int32_t rnd = 0;
|
|
|
|
int r;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
|
|
|
|
(r = sshpkt_put_u32(ssh, nbytes)) != 0)
|
|
|
|
return r;
|
|
|
|
for (i = 0; i < nbytes; i++) {
|
|
|
|
if (i % 4 == 0)
|
|
|
|
rnd = arc4random();
|
|
|
|
if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
|
|
|
|
return r;
|
|
|
|
rnd >>= 8;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:52:16 +01:00
|
|
|
/* send it */
|
|
|
|
|
|
|
|
int
|
|
|
|
sshpkt_send(struct ssh *ssh)
|
|
|
|
{
|
2016-09-30 11:19:13 +02:00
|
|
|
if (ssh->state && ssh->state->mux)
|
|
|
|
return ssh_packet_send_mux(ssh);
|
2017-05-01 01:13:25 +02:00
|
|
|
return ssh_packet_send2(ssh);
|
2015-01-19 20:52:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-05-01 01:13:25 +02:00
|
|
|
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;
|
2015-01-19 20:52:16 +01:00
|
|
|
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;
|
2014-05-15 06:37:39 +02:00
|
|
|
}
|