blob: 2f3a2ec7075c13f6d7a1e225b6df0ee553f83835 [file] [log] [blame]
markus@openbsd.org0fb1a612017-03-11 13:07:35 +00001/* $OpenBSD: packet.c,v 1.247 2017/03/11 13:07:35 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +000041
Damien Millere3b60b52006-07-10 21:08:03 +100042#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100043#include "openbsd-compat/sys-queue.h"
Damien Miller8ec8c3e2006-07-10 20:35:38 +100044#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100045#ifdef HAVE_SYS_TIME_H
46# include <sys/time.h>
47#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100048
Damien Miller8ec8c3e2006-07-10 20:35:38 +100049#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110050#include <netinet/ip.h>
Darren Tuckerdace2332006-09-22 19:22:17 +100051#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Darren Tucker39972492006-07-12 22:22:46 +100053#include <errno.h>
djm@openbsd.org95767262016-03-07 19:02:43 +000054#include <netdb.h>
Darren Tucker5d196262006-07-12 22:15:16 +100055#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100056#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100057#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100058#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100059#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000060#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100061#include <signal.h>
Darren Tuckerc53c2af2013-05-16 20:28:16 +100062#include <time.h>
Darren Tucker5d196262006-07-12 22:15:16 +100063
markus@openbsd.org091c3022015-01-19 19:52:16 +000064#include <zlib.h>
65
66#include "buffer.h" /* typedefs XXX */
67#include "key.h" /* typedefs XXX */
68
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "deattack.h"
Damien Miller33b13562000-04-04 14:38:59 +100072#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100074#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110075#include "cipher.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000076#include "sshkey.h"
Damien Miller33b13562000-04-04 14:38:59 +100077#include "kex.h"
markus@openbsd.org128343b2015-01-13 19:31:40 +000078#include "digest.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000079#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "log.h"
81#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110082#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100083#include "channels.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000084#include "ssh.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000085#include "packet.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000086#include "ssherr.h"
87#include "sshbuf.h"
Damien Miller33b13562000-04-04 14:38:59 +100088
89#ifdef PACKET_DEBUG
90#define DBG(x) x
91#else
92#define DBG(x)
93#endif
94
Damien Miller13ae44c2009-01-28 16:38:41 +110095#define PACKET_MAX_SIZE (256 * 1024)
96
Darren Tuckerf7288d72009-06-21 18:12:20 +100097struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100098 u_int32_t seqnr;
99 u_int32_t packets;
100 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000101 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000102};
Damien Miller13ae44c2009-01-28 16:38:41 +1100103
Damien Millera5539d22003-04-09 20:50:06 +1000104struct packet {
105 TAILQ_ENTRY(packet) next;
106 u_char type;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000107 struct sshbuf *payload;
Damien Millera5539d22003-04-09 20:50:06 +1000108};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000109
110struct session_state {
111 /*
112 * This variable contains the file descriptors used for
113 * communicating with the other side. connection_in is used for
114 * reading; connection_out for writing. These can be the same
115 * descriptor, in which case it is assumed to be a socket.
116 */
117 int connection_in;
118 int connection_out;
119
120 /* Protocol flags for the remote side. */
121 u_int remote_protocol_flags;
122
123 /* Encryption context for receiving data. Only used for decryption. */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000124 struct sshcipher_ctx *receive_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000125
126 /* Encryption context for sending data. Only used for encryption. */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000127 struct sshcipher_ctx *send_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000128
129 /* Buffer for raw input data from the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000130 struct sshbuf *input;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000131
132 /* Buffer for raw output data going to the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000133 struct sshbuf *output;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000134
135 /* Buffer for the partial outgoing packet being constructed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000136 struct sshbuf *outgoing_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000137
138 /* Buffer for the incoming packet currently being processed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000139 struct sshbuf *incoming_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000140
141 /* Scratch buffer for packet compression/decompression. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000142 struct sshbuf *compression_buffer;
143
144 /* Incoming/outgoing compression dictionaries */
145 z_stream compression_in_stream;
146 z_stream compression_out_stream;
147 int compression_in_started;
148 int compression_out_started;
149 int compression_in_failures;
150 int compression_out_failures;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000151
152 /*
153 * Flag indicating whether packet compression/decompression is
154 * enabled.
155 */
156 int packet_compression;
157
158 /* default maximum packet size */
159 u_int max_packet_size;
160
161 /* Flag indicating whether this module has been initialized. */
162 int initialized;
163
164 /* Set to true if the connection is interactive. */
165 int interactive_mode;
166
167 /* Set to true if we are the server side. */
168 int server_side;
169
170 /* Set to true if we are authenticated. */
171 int after_authentication;
172
173 int keep_alive_timeouts;
174
175 /* The maximum time that we will wait to send or receive a packet */
176 int packet_timeout_ms;
177
178 /* Session key information for Encryption and MAC */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000179 struct newkeys *newkeys[MODE_MAX];
Darren Tuckerf7288d72009-06-21 18:12:20 +1000180 struct packet_state p_read, p_send;
181
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000182 /* Volume-based rekeying */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000183 u_int64_t max_blocks_in, max_blocks_out, rekey_limit;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000184
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000185 /* Time-based rekeying */
markus@openbsd.org02db4682015-02-13 18:57:00 +0000186 u_int32_t rekey_interval; /* how often in seconds */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000187 time_t rekey_time; /* time of last rekeying */
188
Darren Tuckerf7288d72009-06-21 18:12:20 +1000189 /* Session key for protocol v1 */
190 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
191 u_int ssh1_keylen;
192
193 /* roundup current message to extra_pad bytes */
194 u_char extra_pad;
195
196 /* XXX discard incoming data after MAC error */
197 u_int packet_discard;
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000198 size_t packet_discard_mac_already;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000199 struct sshmac *packet_discard_mac;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000200
201 /* Used in packet_read_poll2() */
202 u_int packlen;
203
Darren Tucker7b935c72009-06-21 18:59:36 +1000204 /* Used in packet_send2 */
205 int rekeying;
206
markus@openbsd.org8d057842016-09-30 09:19:13 +0000207 /* Used in ssh_packet_send_mux() */
208 int mux;
209
Darren Tucker7b935c72009-06-21 18:59:36 +1000210 /* Used in packet_set_interactive */
211 int set_interactive_called;
212
213 /* Used in packet_set_maxsize */
214 int set_maxsize_called;
215
markus@openbsd.org091c3022015-01-19 19:52:16 +0000216 /* One-off warning about weak ciphers */
217 int cipher_warning_done;
218
219 /* SSH1 CRC compensation attack detector */
220 struct deattack_ctx deattack;
221
djm@openbsd.org39af7b42016-10-11 21:47:45 +0000222 /* Hook for fuzzing inbound packets */
223 ssh_packet_hook_fn *hook_in;
224 void *hook_in_ctx;
225
Darren Tuckerf7288d72009-06-21 18:12:20 +1000226 TAILQ_HEAD(, packet) outgoing;
227};
228
markus@openbsd.org091c3022015-01-19 19:52:16 +0000229struct ssh *
230ssh_alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000231{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000232 struct ssh *ssh = NULL;
233 struct session_state *state = NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000234
markus@openbsd.org091c3022015-01-19 19:52:16 +0000235 if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
236 (state = calloc(1, sizeof(*state))) == NULL ||
237 (state->input = sshbuf_new()) == NULL ||
238 (state->output = sshbuf_new()) == NULL ||
239 (state->outgoing_packet = sshbuf_new()) == NULL ||
240 (state->incoming_packet = sshbuf_new()) == NULL)
241 goto fail;
242 TAILQ_INIT(&state->outgoing);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000243 TAILQ_INIT(&ssh->private_keys);
244 TAILQ_INIT(&ssh->public_keys);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000245 state->connection_in = -1;
246 state->connection_out = -1;
247 state->max_packet_size = 32768;
248 state->packet_timeout_ms = -1;
249 state->p_send.packets = state->p_read.packets = 0;
250 state->initialized = 1;
251 /*
252 * ssh_packet_send2() needs to queue packets until
253 * we've done the initial key exchange.
254 */
255 state->rekeying = 1;
256 ssh->state = state;
257 return ssh;
258 fail:
259 if (state) {
260 sshbuf_free(state->input);
261 sshbuf_free(state->output);
262 sshbuf_free(state->incoming_packet);
263 sshbuf_free(state->outgoing_packet);
264 free(state);
265 }
266 free(ssh);
267 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000268}
Damien Millera5539d22003-04-09 20:50:06 +1000269
djm@openbsd.org39af7b42016-10-11 21:47:45 +0000270void
271ssh_packet_set_input_hook(struct ssh *ssh, ssh_packet_hook_fn *hook, void *ctx)
272{
273 ssh->state->hook_in = hook;
274 ssh->state->hook_in_ctx = ctx;
275}
276
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000277/* Returns nonzero if rekeying is in progress */
278int
279ssh_packet_is_rekeying(struct ssh *ssh)
280{
djm@openbsd.org292a8de2016-02-17 22:20:14 +0000281 return compat20 &&
282 (ssh->state->rekeying || (ssh->kex != NULL && ssh->kex->done == 0));
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +0000283}
284
Damien Miller5428f641999-11-25 11:54:57 +1100285/*
286 * Sets the descriptors used for communication. Disables encryption until
287 * packet_set_encryption_key is called.
288 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000289struct ssh *
290ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000292 struct session_state *state;
293 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000294 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000295
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000296 if (none == NULL) {
297 error("%s: cannot load cipher 'none'", __func__);
298 return NULL;
299 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000300 if (ssh == NULL)
301 ssh = ssh_alloc_session_state();
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000302 if (ssh == NULL) {
303 error("%s: cound not allocate state", __func__);
304 return NULL;
305 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000306 state = ssh->state;
307 state->connection_in = fd_in;
308 state->connection_out = fd_out;
309 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000310 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000311 (r = cipher_init(&state->receive_context, none,
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000312 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
313 error("%s: cipher_init failed: %s", __func__, ssh_err(r));
djm@openbsd.org95767262016-03-07 19:02:43 +0000314 free(ssh); /* XXX need ssh_free_session_state? */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000315 return NULL;
316 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000317 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
318 deattack_init(&state->deattack);
djm@openbsd.orgd4c02952015-02-11 01:20:38 +0000319 /*
320 * Cache the IP address of the remote connection for use in error
321 * messages that might be generated after the connection has closed.
322 */
323 (void)ssh_remote_ipaddr(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000324 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325}
326
Darren Tucker3fc464e2008-06-13 06:42:45 +1000327void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000328ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000329{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000330 struct session_state *state = ssh->state;
331
Damien Miller8ed4de82011-12-19 10:52:50 +1100332 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000333 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000334 return;
335 }
336 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000337 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000338 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000339 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000340}
341
markus@openbsd.org8d057842016-09-30 09:19:13 +0000342void
343ssh_packet_set_mux(struct ssh *ssh)
344{
345 ssh->state->mux = 1;
346 ssh->state->rekeying = 0;
347}
348
349int
350ssh_packet_get_mux(struct ssh *ssh)
351{
352 return ssh->state->mux;
353}
354
markus@openbsd.org091c3022015-01-19 19:52:16 +0000355int
djm@openbsd.org07edd7e2017-02-03 23:03:33 +0000356ssh_packet_set_log_preamble(struct ssh *ssh, const char *fmt, ...)
357{
358 va_list args;
359 int r;
360
361 free(ssh->log_preamble);
362 if (fmt == NULL)
363 ssh->log_preamble = NULL;
364 else {
365 va_start(args, fmt);
366 r = vasprintf(&ssh->log_preamble, fmt, args);
367 va_end(args);
368 if (r < 0 || ssh->log_preamble == NULL)
369 return SSH_ERR_ALLOC_FAIL;
370 }
371 return 0;
372}
373
374int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000375ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100376{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000377 struct session_state *state = ssh->state;
378 int r;
379
380 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100381 char buf[1024];
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000382 size_t dlen = PACKET_MAX_SIZE;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000383
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000384 if (dlen > state->packet_discard_mac_already)
385 dlen -= state->packet_discard_mac_already;
Damien Miller13ae44c2009-01-28 16:38:41 +1100386 memset(buf, 'a', sizeof(buf));
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000387 while (sshbuf_len(state->incoming_packet) < dlen)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000388 if ((r = sshbuf_put(state->incoming_packet, buf,
389 sizeof(buf))) != 0)
390 return r;
391 (void) mac_compute(state->packet_discard_mac,
392 state->p_read.seqnr,
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000393 sshbuf_ptr(state->incoming_packet), dlen,
markus@openbsd.org091c3022015-01-19 19:52:16 +0000394 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100395 }
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000396 logit("Finished discarding for %.200s port %d",
397 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000398 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100399}
400
markus@openbsd.org091c3022015-01-19 19:52:16 +0000401static int
402ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000403 struct sshmac *mac, size_t mac_already, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100404{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000405 struct session_state *state = ssh->state;
406 int r;
407
408 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
409 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
410 return r;
411 return SSH_ERR_MAC_INVALID;
412 }
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000413 /*
414 * Record number of bytes over which the mac has already
415 * been computed in order to minimize timing attacks.
416 */
417 if (mac && mac->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000418 state->packet_discard_mac = mac;
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000419 state->packet_discard_mac_already = mac_already;
420 }
421 if (sshbuf_len(state->input) >= discard)
422 return ssh_packet_stop_discard(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000423 state->packet_discard = discard - sshbuf_len(state->input);
424 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100425}
426
Damien Miller34132e52000-01-14 15:45:46 +1100427/* Returns 1 if remote host is connected via socket, 0 if not. */
428
429int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000430ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100431{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000432 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100433 struct sockaddr_storage from, to;
434 socklen_t fromlen, tolen;
435
djm@openbsd.org95767262016-03-07 19:02:43 +0000436 if (state->connection_in == -1 || state->connection_out == -1)
437 return 0;
438
Damien Miller34132e52000-01-14 15:45:46 +1100439 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000440 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100441 return 1;
442 fromlen = sizeof(from);
443 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000444 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000445 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100446 return 0;
447 tolen = sizeof(to);
448 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000449 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000450 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100451 return 0;
452 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
453 return 0;
454 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
455 return 0;
456 return 1;
457}
458
Ben Lindstromf6027d32002-03-22 01:42:04 +0000459void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000460ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000461{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000462 if (ibytes)
463 *ibytes = ssh->state->p_read.bytes;
464 if (obytes)
465 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000466}
467
468int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000469ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100470{
471 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100472 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100473
474 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000475 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000476 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100477 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000478#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100479 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000480 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000481 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000482#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000483 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100484}
485
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486/* Sets the connection into non-blocking mode. */
487
488void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000489ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490{
Damien Miller95def091999-11-25 00:26:21 +1100491 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000492 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493
markus@openbsd.org091c3022015-01-19 19:52:16 +0000494 if (ssh->state->connection_out != ssh->state->connection_in)
495 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496}
497
498/* Returns the socket used for reading. */
499
500int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000501ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000503 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504}
505
506/* Returns the descriptor used for writing. */
507
508int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000509ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000511 return ssh->state->connection_out;
512}
513
514/*
515 * Returns the IP-address of the remote host as a string. The returned
516 * string must not be freed.
517 */
518
519const char *
520ssh_remote_ipaddr(struct ssh *ssh)
521{
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000522 const int sock = ssh->state->connection_in;
523
markus@openbsd.org091c3022015-01-19 19:52:16 +0000524 /* Check whether we have cached the ipaddr. */
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000525 if (ssh->remote_ipaddr == NULL) {
526 if (ssh_packet_connection_is_on_socket(ssh)) {
527 ssh->remote_ipaddr = get_peer_ipaddr(sock);
djm@openbsd.org95767262016-03-07 19:02:43 +0000528 ssh->remote_port = get_peer_port(sock);
529 ssh->local_ipaddr = get_local_ipaddr(sock);
530 ssh->local_port = get_local_port(sock);
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000531 } else {
532 ssh->remote_ipaddr = strdup("UNKNOWN");
djm@openbsd.org95767262016-03-07 19:02:43 +0000533 ssh->remote_port = 65535;
534 ssh->local_ipaddr = strdup("UNKNOWN");
535 ssh->local_port = 65535;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000536 }
537 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000538 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539}
540
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000541/* Returns the port number of the remote host. */
542
543int
544ssh_remote_port(struct ssh *ssh)
545{
546 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
547 return ssh->remote_port;
548}
549
djm@openbsd.org95767262016-03-07 19:02:43 +0000550/*
551 * Returns the IP-address of the local host as a string. The returned
552 * string must not be freed.
553 */
554
555const char *
556ssh_local_ipaddr(struct ssh *ssh)
557{
558 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
559 return ssh->local_ipaddr;
560}
561
562/* Returns the port number of the local host. */
563
564int
565ssh_local_port(struct ssh *ssh)
566{
567 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
568 return ssh->local_port;
569}
570
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571/* Closes the connection and clears and frees internal data structures. */
572
573void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000574ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000576 struct session_state *state = ssh->state;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000577 u_int mode;
578
579 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100580 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000581 state->initialized = 0;
582 if (state->connection_in == state->connection_out) {
583 shutdown(state->connection_out, SHUT_RDWR);
584 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100585 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000586 close(state->connection_in);
587 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100588 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000589 sshbuf_free(state->input);
590 sshbuf_free(state->output);
591 sshbuf_free(state->outgoing_packet);
592 sshbuf_free(state->incoming_packet);
593 for (mode = 0; mode < MODE_MAX; mode++)
594 kex_free_newkeys(state->newkeys[mode]);
595 if (state->compression_buffer) {
596 sshbuf_free(state->compression_buffer);
597 if (state->compression_out_started) {
598 z_streamp stream = &state->compression_out_stream;
599 debug("compress outgoing: "
600 "raw data %llu, compressed %llu, factor %.2f",
601 (unsigned long long)stream->total_in,
602 (unsigned long long)stream->total_out,
603 stream->total_in == 0 ? 0.0 :
604 (double) stream->total_out / stream->total_in);
605 if (state->compression_out_failures == 0)
606 deflateEnd(stream);
607 }
608 if (state->compression_in_started) {
609 z_streamp stream = &state->compression_out_stream;
610 debug("compress incoming: "
611 "raw data %llu, compressed %llu, factor %.2f",
612 (unsigned long long)stream->total_out,
613 (unsigned long long)stream->total_in,
614 stream->total_out == 0 ? 0.0 :
615 (double) stream->total_in / stream->total_out);
616 if (state->compression_in_failures == 0)
617 inflateEnd(stream);
618 }
Damien Miller95def091999-11-25 00:26:21 +1100619 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000620 cipher_free(state->send_context);
621 cipher_free(state->receive_context);
622 state->send_context = state->receive_context = NULL;
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000623 free(ssh->remote_ipaddr);
624 ssh->remote_ipaddr = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000625 free(ssh->state);
626 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627}
628
629/* Sets remote side protocol flags. */
630
631void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000632ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000634 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635}
636
637/* Returns the remote protocol flags set earlier by the above function. */
638
Ben Lindstrom46c16222000-12-22 01:43:59 +0000639u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000640ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000641{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000642 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643}
644
Damien Miller5428f641999-11-25 11:54:57 +1100645/*
646 * Starts packet compression from the next packet on in both directions.
647 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
648 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000649
markus@openbsd.org091c3022015-01-19 19:52:16 +0000650static int
651ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000652{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000653 if (!ssh->state->compression_buffer &&
654 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
655 return SSH_ERR_ALLOC_FAIL;
656 return 0;
657}
658
659static int
660start_compression_out(struct ssh *ssh, int level)
661{
662 if (level < 1 || level > 9)
663 return SSH_ERR_INVALID_ARGUMENT;
664 debug("Enabling compression at level %d.", level);
665 if (ssh->state->compression_out_started == 1)
666 deflateEnd(&ssh->state->compression_out_stream);
667 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
668 case Z_OK:
669 ssh->state->compression_out_started = 1;
670 break;
671 case Z_MEM_ERROR:
672 return SSH_ERR_ALLOC_FAIL;
673 default:
674 return SSH_ERR_INTERNAL_ERROR;
675 }
676 return 0;
677}
678
679static int
680start_compression_in(struct ssh *ssh)
681{
682 if (ssh->state->compression_in_started == 1)
683 inflateEnd(&ssh->state->compression_in_stream);
684 switch (inflateInit(&ssh->state->compression_in_stream)) {
685 case Z_OK:
686 ssh->state->compression_in_started = 1;
687 break;
688 case Z_MEM_ERROR:
689 return SSH_ERR_ALLOC_FAIL;
690 default:
691 return SSH_ERR_INTERNAL_ERROR;
692 }
693 return 0;
694}
695
696int
697ssh_packet_start_compression(struct ssh *ssh, int level)
698{
699 int r;
700
701 if (ssh->state->packet_compression && !compat20)
702 return SSH_ERR_INTERNAL_ERROR;
703 ssh->state->packet_compression = 1;
704 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
705 (r = start_compression_in(ssh)) != 0 ||
706 (r = start_compression_out(ssh, level)) != 0)
707 return r;
708 return 0;
709}
710
711/* XXX remove need for separate compression buffer */
712static int
713compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
714{
715 u_char buf[4096];
716 int r, status;
717
718 if (ssh->state->compression_out_started != 1)
719 return SSH_ERR_INTERNAL_ERROR;
720
721 /* This case is not handled below. */
722 if (sshbuf_len(in) == 0)
723 return 0;
724
725 /* Input is the contents of the input buffer. */
726 if ((ssh->state->compression_out_stream.next_in =
727 sshbuf_mutable_ptr(in)) == NULL)
728 return SSH_ERR_INTERNAL_ERROR;
729 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
730
731 /* Loop compressing until deflate() returns with avail_out != 0. */
732 do {
733 /* Set up fixed-size output buffer. */
734 ssh->state->compression_out_stream.next_out = buf;
735 ssh->state->compression_out_stream.avail_out = sizeof(buf);
736
737 /* Compress as much data into the buffer as possible. */
738 status = deflate(&ssh->state->compression_out_stream,
739 Z_PARTIAL_FLUSH);
740 switch (status) {
741 case Z_MEM_ERROR:
742 return SSH_ERR_ALLOC_FAIL;
743 case Z_OK:
744 /* Append compressed data to output_buffer. */
745 if ((r = sshbuf_put(out, buf, sizeof(buf) -
746 ssh->state->compression_out_stream.avail_out)) != 0)
747 return r;
748 break;
749 case Z_STREAM_ERROR:
750 default:
751 ssh->state->compression_out_failures++;
752 return SSH_ERR_INVALID_FORMAT;
753 }
754 } while (ssh->state->compression_out_stream.avail_out == 0);
755 return 0;
756}
757
758static int
759uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
760{
761 u_char buf[4096];
762 int r, status;
763
764 if (ssh->state->compression_in_started != 1)
765 return SSH_ERR_INTERNAL_ERROR;
766
767 if ((ssh->state->compression_in_stream.next_in =
768 sshbuf_mutable_ptr(in)) == NULL)
769 return SSH_ERR_INTERNAL_ERROR;
770 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
771
772 for (;;) {
773 /* Set up fixed-size output buffer. */
774 ssh->state->compression_in_stream.next_out = buf;
775 ssh->state->compression_in_stream.avail_out = sizeof(buf);
776
777 status = inflate(&ssh->state->compression_in_stream,
778 Z_PARTIAL_FLUSH);
779 switch (status) {
780 case Z_OK:
781 if ((r = sshbuf_put(out, buf, sizeof(buf) -
782 ssh->state->compression_in_stream.avail_out)) != 0)
783 return r;
784 break;
785 case Z_BUF_ERROR:
786 /*
787 * Comments in zlib.h say that we should keep calling
788 * inflate() until we get an error. This appears to
789 * be the error that we get.
790 */
791 return 0;
792 case Z_DATA_ERROR:
793 return SSH_ERR_INVALID_FORMAT;
794 case Z_MEM_ERROR:
795 return SSH_ERR_ALLOC_FAIL;
796 case Z_STREAM_ERROR:
797 default:
798 ssh->state->compression_in_failures++;
799 return SSH_ERR_INTERNAL_ERROR;
800 }
801 }
802 /* NOTREACHED */
803}
804
Damien Miller5428f641999-11-25 11:54:57 +1100805/*
Damien Miller5428f641999-11-25 11:54:57 +1100806 * Causes any further packets to be encrypted using the given key. The same
807 * key is used for both sending and reception. However, both directions are
808 * encrypted independently of each other.
809 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000810
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000812ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000813{
djm@openbsd.org734226b2015-04-27 01:52:30 +0000814#ifndef WITH_SSH1
815 fatal("no SSH protocol 1 support");
816#else /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000817 struct session_state *state = ssh->state;
818 const struct sshcipher *cipher = cipher_by_number(number);
819 int r;
820 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100821
markus@openbsd.org091c3022015-01-19 19:52:16 +0000822 if (cipher == NULL)
823 fatal("%s: unknown cipher number %d", __func__, number);
824 if (keylen < 20)
825 fatal("%s: keylen too small: %d", __func__, keylen);
826 if (keylen > SSH_SESSION_KEY_LENGTH)
827 fatal("%s: keylen too big: %d", __func__, keylen);
828 memcpy(state->ssh1_key, key, keylen);
829 state->ssh1_keylen = keylen;
830 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
831 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
832 (r = cipher_init(&state->receive_context, cipher, key, keylen,
833 NULL, 0, CIPHER_DECRYPT) != 0))
834 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
835 if (!state->cipher_warning_done &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000836 ((wmsg = cipher_warning_message(state->send_context)) != NULL ||
837 (wmsg = cipher_warning_message(state->send_context)) != NULL)) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000838 error("Warning: %s", wmsg);
839 state->cipher_warning_done = 1;
840 }
Damien Miller773dda22015-01-30 23:10:17 +1100841#endif /* WITH_SSH1 */
Damien Millereb8b60e2010-08-31 22:41:14 +1000842}
843
Damien Miller5428f641999-11-25 11:54:57 +1100844/*
845 * Finalizes and sends the packet. If the encryption key has been set,
846 * encrypts the packet before sending.
847 */
Damien Miller95def091999-11-25 00:26:21 +1100848
markus@openbsd.org091c3022015-01-19 19:52:16 +0000849int
850ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000852 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000853 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000854 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000855 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856
Damien Miller5428f641999-11-25 11:54:57 +1100857 /*
858 * If using packet compression, compress the payload of the outgoing
859 * packet.
860 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000861 if (state->packet_compression) {
862 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100863 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000864 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
865 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100866 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000867 if ((r = sshbuf_put(state->compression_buffer,
868 "\0\0\0\0\0\0\0\0", 8)) != 0)
869 goto out;
870 if ((r = compress_buffer(ssh, state->outgoing_packet,
871 state->compression_buffer)) != 0)
872 goto out;
873 sshbuf_reset(state->outgoing_packet);
874 if ((r = sshbuf_putb(state->outgoing_packet,
875 state->compression_buffer)) != 0)
876 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100877 }
878 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000879 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880
Damien Millere247cc42000-05-07 12:03:14 +1000881 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100882 padding = 8 - len % 8;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000883 if (!cipher_ctx_is_plaintext(state->send_context)) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000884 cp = sshbuf_mutable_ptr(state->outgoing_packet);
885 if (cp == NULL) {
886 r = SSH_ERR_INTERNAL_ERROR;
887 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100888 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000889 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100890 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000891 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
892 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100893
894 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000895 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
896 sshbuf_len(state->outgoing_packet));
897 POKE_U32(buf, checksum);
898 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
899 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900
901#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100902 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000903 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904#endif
905
Damien Miller95def091999-11-25 00:26:21 +1100906 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000907 POKE_U32(buf, len);
908 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
909 goto out;
910 if ((r = sshbuf_reserve(state->output,
911 sshbuf_len(state->outgoing_packet), &cp)) != 0)
912 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000913 if ((r = cipher_crypt(state->send_context, 0, cp,
markus@openbsd.org091c3022015-01-19 19:52:16 +0000914 sshbuf_ptr(state->outgoing_packet),
915 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
916 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100917
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100919 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000920 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000921#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000922 state->p_send.packets++;
923 state->p_send.bytes += len +
924 sshbuf_len(state->outgoing_packet);
925 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926
Damien Miller5428f641999-11-25 11:54:57 +1100927 /*
Damien Miller788f2122005-11-05 15:14:59 +1100928 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000929 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
930 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100931 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000932 r = 0;
933 out:
934 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935}
936
markus@openbsd.org091c3022015-01-19 19:52:16 +0000937int
938ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000939{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000940 struct session_state *state = ssh->state;
941 struct sshenc *enc;
942 struct sshmac *mac;
943 struct sshcomp *comp;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000944 struct sshcipher_ctx **ccp;
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000945 struct packet_state *ps;
Damien Millera5539d22003-04-09 20:50:06 +1000946 u_int64_t *max_blocks;
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000947 const char *wmsg, *dir;
Damien Miller86687062014-07-02 15:28:02 +1000948 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000949
Ben Lindstrom064496f2002-12-23 02:04:22 +0000950 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000951
Damien Miller963f6b22002-02-19 15:21:23 +1100952 if (mode == MODE_OUT) {
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000953 dir = "output";
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000954 ccp = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000955 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000956 ps = &state->p_send;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000957 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100958 } else {
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000959 dir = "input";
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000960 ccp = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000961 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000962 ps = &state->p_read;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000963 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100964 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000965 if (state->newkeys[mode] != NULL) {
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000966 debug("%s: rekeying after %llu %s blocks"
967 " (%llu bytes total)", __func__,
968 (unsigned long long)ps->blocks, dir,
969 (unsigned long long)ps->bytes);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000970 cipher_free(*ccp);
971 *ccp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000972 enc = &state->newkeys[mode]->enc;
973 mac = &state->newkeys[mode]->mac;
974 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000975 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100976 explicit_bzero(enc->iv, enc->iv_len);
977 explicit_bzero(enc->key, enc->key_len);
978 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000979 free(enc->name);
980 free(enc->iv);
981 free(enc->key);
982 free(mac->name);
983 free(mac->key);
984 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000985 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000986 }
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000987 /* note that both bytes and the seqnr are not reset */
988 ps->packets = ps->blocks = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000989 /* move newkeys from kex to state */
990 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
991 return SSH_ERR_INTERNAL_ERROR;
992 ssh->kex->newkeys[mode] = NULL;
993 enc = &state->newkeys[mode]->enc;
994 mac = &state->newkeys[mode]->mac;
995 comp = &state->newkeys[mode]->comp;
996 if (cipher_authlen(enc->cipher) == 0) {
997 if ((r = mac_init(mac)) != 0)
998 return r;
999 }
1000 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001001 DBG(debug("cipher_init_context: %d", mode));
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001002 if ((r = cipher_init(ccp, enc->cipher, enc->key, enc->key_len,
Damien Miller86687062014-07-02 15:28:02 +10001003 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001004 return r;
1005 if (!state->cipher_warning_done &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001006 (wmsg = cipher_warning_message(*ccp)) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001007 error("Warning: %s", wmsg);
1008 state->cipher_warning_done = 1;
1009 }
Ben Lindstromf6027d32002-03-22 01:42:04 +00001010 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +11001011 /* explicit_bzero(enc->iv, enc->block_size);
1012 explicit_bzero(enc->key, enc->key_len);
1013 explicit_bzero(mac->key, mac->key_len); */
djm@openbsd.orgb7689152016-09-28 21:44:52 +00001014 if ((comp->type == COMP_ZLIB ||
1015 (comp->type == COMP_DELAYED &&
1016 state->after_authentication)) && comp->enabled == 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001017 if ((r = ssh_packet_init_compression(ssh)) < 0)
1018 return r;
1019 if (mode == MODE_OUT) {
1020 if ((r = start_compression_out(ssh, 6)) != 0)
1021 return r;
1022 } else {
1023 if ((r = start_compression_in(ssh)) != 0)
1024 return r;
1025 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001026 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001027 }
Darren Tucker81a0b372003-07-14 17:31:06 +10001028 /*
1029 * The 2^(blocksize*2) limit is too expensive for 3DES,
1030 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1031 */
1032 if (enc->block_size >= 16)
1033 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1034 else
1035 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001036 if (state->rekey_limit)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001037 *max_blocks = MINIMUM(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001038 state->rekey_limit / enc->block_size);
djm@openbsd.org696d1262016-02-04 23:43:48 +00001039 debug("rekey after %llu blocks", (unsigned long long)*max_blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001040 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001041}
1042
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001043#define MAX_PACKETS (1U<<31)
1044static int
1045ssh_packet_need_rekeying(struct ssh *ssh, u_int outbound_packet_len)
1046{
1047 struct session_state *state = ssh->state;
1048 u_int32_t out_blocks;
1049
1050 /* XXX client can't cope with rekeying pre-auth */
1051 if (!state->after_authentication)
1052 return 0;
1053
1054 /* Haven't keyed yet or KEX in progress. */
1055 if (ssh->kex == NULL || ssh_packet_is_rekeying(ssh))
1056 return 0;
1057
1058 /* Peer can't rekey */
1059 if (ssh->compat & SSH_BUG_NOREKEY)
1060 return 0;
1061
1062 /*
1063 * Permit one packet in or out per rekey - this allows us to
1064 * make progress when rekey limits are very small.
1065 */
1066 if (state->p_send.packets == 0 && state->p_read.packets == 0)
1067 return 0;
1068
1069 /* Time-based rekeying */
1070 if (state->rekey_interval != 0 &&
dtucker@openbsd.orgc998bf02017-02-03 02:56:00 +00001071 (int64_t)state->rekey_time + state->rekey_interval <= monotime())
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001072 return 1;
1073
1074 /* Always rekey when MAX_PACKETS sent in either direction */
1075 if (state->p_send.packets > MAX_PACKETS ||
1076 state->p_read.packets > MAX_PACKETS)
1077 return 1;
1078
1079 /* Rekey after (cipher-specific) maxiumum blocks */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001080 out_blocks = ROUNDUP(outbound_packet_len,
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001081 state->newkeys[MODE_OUT]->enc.block_size);
1082 return (state->max_blocks_out &&
1083 (state->p_send.blocks + out_blocks > state->max_blocks_out)) ||
1084 (state->max_blocks_in &&
1085 (state->p_read.blocks > state->max_blocks_in));
1086}
1087
Damien Miller5428f641999-11-25 11:54:57 +11001088/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001089 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001090 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001091 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1092 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001093static int
1094ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001095{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001096 struct session_state *state = ssh->state;
1097 struct sshcomp *comp = NULL;
1098 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001099
1100 /*
1101 * Remember that we are past the authentication step, so rekeying
1102 * with COMP_DELAYED will turn on compression immediately.
1103 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001104 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001105 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001106 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001107 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001108 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001109 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001110 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001111 if ((r = ssh_packet_init_compression(ssh)) != 0)
1112 return r;
1113 if (mode == MODE_OUT) {
1114 if ((r = start_compression_out(ssh, 6)) != 0)
1115 return r;
1116 } else {
1117 if ((r = start_compression_in(ssh)) != 0)
1118 return r;
1119 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001120 comp->enabled = 1;
1121 }
1122 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001123 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001124}
1125
djm@openbsd.org28136472016-01-29 05:46:01 +00001126/* Used to mute debug logging for noisy packet types */
markus@openbsd.org8d057842016-09-30 09:19:13 +00001127int
djm@openbsd.org28136472016-01-29 05:46:01 +00001128ssh_packet_log_type(u_char type)
1129{
1130 switch (type) {
1131 case SSH2_MSG_CHANNEL_DATA:
1132 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
1133 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
1134 return 0;
1135 default:
1136 return 1;
1137 }
1138}
1139
Damien Miller9786e6e2005-07-26 21:54:56 +10001140/*
Damien Miller33b13562000-04-04 14:38:59 +10001141 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1142 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001143int
1144ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001145{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001146 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001147 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
djm@openbsd.orgeb999a42016-07-18 06:08:01 +00001148 u_char tmp, padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001149 u_int authlen = 0, aadlen = 0;
1150 u_int len;
1151 struct sshenc *enc = NULL;
1152 struct sshmac *mac = NULL;
1153 struct sshcomp *comp = NULL;
1154 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001155
markus@openbsd.org091c3022015-01-19 19:52:16 +00001156 if (state->newkeys[MODE_OUT] != NULL) {
1157 enc = &state->newkeys[MODE_OUT]->enc;
1158 mac = &state->newkeys[MODE_OUT]->mac;
1159 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001160 /* disable mac for authenticated encryption */
1161 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1162 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001163 }
Damien Miller963f6b22002-02-19 15:21:23 +11001164 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001165 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001166
markus@openbsd.org091c3022015-01-19 19:52:16 +00001167 type = (sshbuf_ptr(state->outgoing_packet))[5];
djm@openbsd.org28136472016-01-29 05:46:01 +00001168 if (ssh_packet_log_type(type))
1169 debug3("send packet: type %u", type);
Damien Miller33b13562000-04-04 14:38:59 +10001170#ifdef PACKET_DEBUG
1171 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001172 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001173#endif
1174
1175 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001176 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001177 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001178 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1179 goto out;
1180 sshbuf_reset(state->compression_buffer);
1181 if ((r = compress_buffer(ssh, state->outgoing_packet,
1182 state->compression_buffer)) != 0)
1183 goto out;
1184 sshbuf_reset(state->outgoing_packet);
1185 if ((r = sshbuf_put(state->outgoing_packet,
1186 "\0\0\0\0\0", 5)) != 0 ||
1187 (r = sshbuf_putb(state->outgoing_packet,
1188 state->compression_buffer)) != 0)
1189 goto out;
1190 DBG(debug("compression: raw %d compressed %zd", len,
1191 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001192 }
1193
1194 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001195 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001196
1197 /*
1198 * calc size of padding, alloc space, get random data,
1199 * minimum padding is 4 bytes
1200 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001201 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001202 padlen = block_size - (len % block_size);
1203 if (padlen < 4)
1204 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001205 if (state->extra_pad) {
djm@openbsd.orgeb999a42016-07-18 06:08:01 +00001206 tmp = state->extra_pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001207 state->extra_pad =
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001208 ROUNDUP(state->extra_pad, block_size);
djm@openbsd.orgeb999a42016-07-18 06:08:01 +00001209 /* check if roundup overflowed */
1210 if (state->extra_pad < tmp)
1211 return SSH_ERR_INVALID_ARGUMENT;
1212 tmp = (len + padlen) % state->extra_pad;
1213 /* Check whether pad calculation below will underflow */
1214 if (tmp > state->extra_pad)
1215 return SSH_ERR_INVALID_ARGUMENT;
1216 pad = state->extra_pad - tmp;
Damien Miller2a328432014-04-20 13:24:01 +10001217 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001218 __func__, pad, len, padlen, state->extra_pad));
djm@openbsd.orgeb999a42016-07-18 06:08:01 +00001219 tmp = padlen;
Damien Miller9f643902001-11-12 11:02:52 +11001220 padlen += pad;
djm@openbsd.orgeb999a42016-07-18 06:08:01 +00001221 /* Check whether padlen calculation overflowed */
1222 if (padlen < tmp)
1223 return SSH_ERR_INVALID_ARGUMENT; /* overflow */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001224 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001225 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001226 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1227 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001228 if (enc && !cipher_ctx_is_plaintext(state->send_context)) {
Damien Millere247cc42000-05-07 12:03:14 +10001229 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001230 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001231 } else {
1232 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001233 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001234 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001235 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001236 len = sshbuf_len(state->outgoing_packet);
1237 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1238 if (cp == NULL) {
1239 r = SSH_ERR_INTERNAL_ERROR;
1240 goto out;
1241 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001242 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001243 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001244 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001245 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1246 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001247
1248 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001249 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001250 if ((r = mac_compute(mac, state->p_send.seqnr,
1251 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001252 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001253 goto out;
1254 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001255 }
1256 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001257 if ((r = sshbuf_reserve(state->output,
1258 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1259 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001260 if ((r = cipher_crypt(state->send_context, state->p_send.seqnr, cp,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001261 sshbuf_ptr(state->outgoing_packet),
1262 len - aadlen, aadlen, authlen)) != 0)
1263 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001264 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001265 if (mac && mac->enabled) {
1266 if (mac->etm) {
1267 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001268 if ((r = mac_compute(mac, state->p_send.seqnr,
1269 cp, len, macbuf, sizeof(macbuf))) != 0)
1270 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001271 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001272 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001273 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001274 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1275 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001276 }
Damien Miller33b13562000-04-04 14:38:59 +10001277#ifdef PACKET_DEBUG
1278 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001279 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001280#endif
Damien Miller4af51302000-04-16 11:18:38 +10001281 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001282 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001283 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001284 if (++state->p_send.packets == 0)
1285 if (!(ssh->compat & SSH_BUG_NOREKEY))
1286 return SSH_ERR_NEED_REKEY;
1287 state->p_send.blocks += len / block_size;
1288 state->p_send.bytes += len;
1289 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001290
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001291 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001292 r = ssh_set_newkeys(ssh, MODE_OUT);
1293 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1294 r = ssh_packet_enable_delayed_compress(ssh);
1295 else
1296 r = 0;
1297 out:
1298 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001299}
1300
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001301/* returns non-zero if the specified packet type is usec by KEX */
1302static int
1303ssh_packet_type_is_kex(u_char type)
1304{
1305 return
1306 type >= SSH2_MSG_TRANSPORT_MIN &&
1307 type <= SSH2_MSG_TRANSPORT_MAX &&
1308 type != SSH2_MSG_SERVICE_REQUEST &&
1309 type != SSH2_MSG_SERVICE_ACCEPT &&
1310 type != SSH2_MSG_EXT_INFO;
1311}
1312
markus@openbsd.org091c3022015-01-19 19:52:16 +00001313int
1314ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001315{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001316 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001317 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001318 u_char type;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001319 int r, need_rekey;
Damien Millera5539d22003-04-09 20:50:06 +10001320
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001321 if (sshbuf_len(state->outgoing_packet) < 6)
1322 return SSH_ERR_INTERNAL_ERROR;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001323 type = sshbuf_ptr(state->outgoing_packet)[5];
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001324 need_rekey = !ssh_packet_type_is_kex(type) &&
1325 ssh_packet_need_rekeying(ssh, sshbuf_len(state->outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +10001326
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001327 /*
1328 * During rekeying we can only send key exchange messages.
1329 * Queue everything else.
1330 */
1331 if ((need_rekey || state->rekeying) && !ssh_packet_type_is_kex(type)) {
1332 if (need_rekey)
1333 debug3("%s: rekex triggered", __func__);
1334 debug("enqueue packet: %u", type);
1335 p = calloc(1, sizeof(*p));
1336 if (p == NULL)
1337 return SSH_ERR_ALLOC_FAIL;
1338 p->type = type;
1339 p->payload = state->outgoing_packet;
1340 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1341 state->outgoing_packet = sshbuf_new();
1342 if (state->outgoing_packet == NULL)
1343 return SSH_ERR_ALLOC_FAIL;
1344 if (need_rekey) {
1345 /*
1346 * This packet triggered a rekey, so send the
1347 * KEXINIT now.
1348 * NB. reenters this function via kex_start_rekex().
1349 */
1350 return kex_start_rekex(ssh);
Damien Millera5539d22003-04-09 20:50:06 +10001351 }
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001352 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001353 }
1354
1355 /* rekeying starts with sending KEXINIT */
1356 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001357 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001358
markus@openbsd.org091c3022015-01-19 19:52:16 +00001359 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1360 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001361
1362 /* after a NEWKEYS message we can send the complete queue */
1363 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001364 state->rekeying = 0;
1365 state->rekey_time = monotime();
1366 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001367 type = p->type;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001368 /*
1369 * If this packet triggers a rekex, then skip the
1370 * remaining packets in the queue for now.
1371 * NB. re-enters this function via kex_start_rekex.
1372 */
1373 if (ssh_packet_need_rekeying(ssh,
1374 sshbuf_len(p->payload))) {
1375 debug3("%s: queued packet triggered rekex",
1376 __func__);
1377 return kex_start_rekex(ssh);
1378 }
Damien Millera5539d22003-04-09 20:50:06 +10001379 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001380 sshbuf_free(state->outgoing_packet);
1381 state->outgoing_packet = p->payload;
1382 TAILQ_REMOVE(&state->outgoing, p, next);
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001383 memset(p, 0, sizeof(*p));
Darren Tuckera627d422013-06-02 07:31:17 +10001384 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001385 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1386 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001387 }
1388 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001389 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001390}
1391
Damien Miller33b13562000-04-04 14:38:59 +10001392/*
Damien Miller5428f641999-11-25 11:54:57 +11001393 * Waits until a packet has been received, and returns its type. Note that
1394 * no other data is processed until this returns, so this function should not
1395 * be used during the interactive session.
1396 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001397
1398int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001399ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001401 struct session_state *state = ssh->state;
markus@openbsd.orga3068632016-01-14 16:17:39 +00001402 int len, r, ms_remain;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001403 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001404 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001405 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406
Darren Tucker99bb7612008-06-13 22:02:50 +10001407 DBG(debug("packet_read()"));
1408
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001409 setp = calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001410 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001411 if (setp == NULL)
1412 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001413
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001414 /*
1415 * Since we are blocking, ensure that all written packets have
1416 * been sent.
1417 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001418 if ((r = ssh_packet_write_wait(ssh)) != 0)
1419 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420
Damien Miller95def091999-11-25 00:26:21 +11001421 /* Stay in the loop until we have received a complete packet. */
1422 for (;;) {
1423 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001424 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1425 if (r != 0)
1426 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001427 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001428 *typep == SSH_SMSG_SUCCESS
1429 || *typep == SSH_SMSG_FAILURE
1430 || *typep == SSH_CMSG_EOF
1431 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1432 if ((r = sshpkt_get_end(ssh)) != 0)
1433 break;
Damien Miller95def091999-11-25 00:26:21 +11001434 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001435 if (*typep != SSH_MSG_NONE)
1436 break;
Damien Miller5428f641999-11-25 11:54:57 +11001437 /*
1438 * Otherwise, wait for some data to arrive, add it to the
1439 * buffer, and try again.
1440 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001441 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001442 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001443 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001444
markus@openbsd.org091c3022015-01-19 19:52:16 +00001445 if (state->packet_timeout_ms > 0) {
1446 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001447 timeoutp = &timeout;
1448 }
Damien Miller95def091999-11-25 00:26:21 +11001449 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001450 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001451 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001452 ms_to_timeval(&timeout, ms_remain);
1453 gettimeofday(&start, NULL);
1454 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001455 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001456 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001457 break;
Damien Millerea437422009-10-02 11:49:03 +10001458 if (errno != EAGAIN && errno != EINTR &&
1459 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001460 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001461 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001462 continue;
1463 ms_subtract_diff(&start, &ms_remain);
1464 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001465 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001466 break;
1467 }
1468 }
djm@openbsd.orgd7abb772017-02-28 06:10:08 +00001469 if (r == 0) {
1470 r = SSH_ERR_CONN_TIMEOUT;
1471 goto out;
1472 }
Damien Miller95def091999-11-25 00:26:21 +11001473 /* Read data from the socket. */
markus@openbsd.orga3068632016-01-14 16:17:39 +00001474 len = read(state->connection_in, buf, sizeof(buf));
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001475 if (len == 0) {
1476 r = SSH_ERR_CONN_CLOSED;
1477 goto out;
1478 }
1479 if (len < 0) {
1480 r = SSH_ERR_SYSTEM_ERROR;
1481 goto out;
1482 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001483
Damien Miller95def091999-11-25 00:26:21 +11001484 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001485 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001486 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001487 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001488 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001489 free(setp);
1490 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491}
1492
Damien Miller278f9072001-12-21 15:00:19 +11001493int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001494ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001495{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001496 u_char type;
1497 int r;
1498
1499 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1500 fatal("%s: %s", __func__, ssh_err(r));
1501 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001502}
1503
Damien Miller5428f641999-11-25 11:54:57 +11001504/*
1505 * Waits until a packet has been received, verifies that its type matches
1506 * that given, and gives a fatal error and exits if there is a mismatch.
1507 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001508
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001509int
1510ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001511{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001512 int r;
1513 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001514
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001515 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1516 return r;
1517 if (type != expected_type) {
1518 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001519 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001520 expected_type, type)) != 0)
1521 return r;
1522 return SSH_ERR_PROTOCOL_ERROR;
1523 }
1524 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001525}
1526
1527/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001528 * packet_process_incoming. If so, reads the packet; otherwise returns
1529 * SSH_MSG_NONE. This does not wait for data from the connection.
1530 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001531 * SSH_MSG_DISCONNECT is handled specially here. Also,
1532 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1533 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001534 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001535
markus@openbsd.org091c3022015-01-19 19:52:16 +00001536int
1537ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001538{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001539 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001540 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001541 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001542 const u_char *cp;
1543 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001544 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001545 int r;
1546
1547 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001548
Damien Miller95def091999-11-25 00:26:21 +11001549 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001550 if (sshbuf_len(state->input) < 4 + 8)
1551 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001552 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001553 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001554 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1555 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1556 len)) != 0)
1557 return r;
1558 return SSH_ERR_CONN_CORRUPT;
1559 }
Damien Miller95def091999-11-25 00:26:21 +11001560 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001561
Damien Miller95def091999-11-25 00:26:21 +11001562 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001563 if (sshbuf_len(state->input) < 4 + padded_len)
1564 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001565
Damien Miller95def091999-11-25 00:26:21 +11001566 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567
Damien Miller95def091999-11-25 00:26:21 +11001568 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001569 if ((r = sshbuf_consume(state->input, 4)) != 0)
1570 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001571
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001572 /*
1573 * Cryptographic attack detector for ssh
1574 * (C)1998 CORE-SDI, Buenos Aires Argentina
1575 * Ariel Futoransky(futo@core-sdi.com)
1576 */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001577 if (!cipher_ctx_is_plaintext(state->receive_context)) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001578 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001579 switch (detect_attack(&state->deattack,
1580 sshbuf_ptr(state->input), padded_len)) {
1581 case DEATTACK_OK:
1582 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001583 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001584 emsg = "crc32 compensation attack detected";
1585 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001586 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001587 emsg = "deattack denial of service detected";
1588 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001589 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001590 emsg = "deattack error";
1591 break;
1592 }
1593 if (emsg != NULL) {
1594 error("%s", emsg);
1595 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1596 (r = ssh_packet_write_wait(ssh)) != 0)
1597 return r;
1598 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001599 }
1600 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001601
1602 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001603 sshbuf_reset(state->incoming_packet);
1604 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1605 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001606 if ((r = cipher_crypt(state->receive_context, 0, p,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001607 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1608 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001609
markus@openbsd.org091c3022015-01-19 19:52:16 +00001610 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1611 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001612
1613#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001614 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001615 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001616#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001617
Damien Miller95def091999-11-25 00:26:21 +11001618 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001619 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1620 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621
Damien Miller95def091999-11-25 00:26:21 +11001622 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001623 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1624 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001625
Damien Miller95def091999-11-25 00:26:21 +11001626 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001627 if (len != sshbuf_len(state->incoming_packet)) {
1628 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001629 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001630 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1631 (r = ssh_packet_write_wait(ssh)) != 0)
1632 return r;
1633 return SSH_ERR_CONN_CORRUPT;
1634 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001635
markus@openbsd.org091c3022015-01-19 19:52:16 +00001636 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1637 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001638 if (checksum != stored_checksum) {
1639 error("Corrupted check bytes on input");
1640 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1641 (r = ssh_packet_write_wait(ssh)) != 0)
1642 return r;
1643 return SSH_ERR_CONN_CORRUPT;
1644 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001645 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1646 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001647
markus@openbsd.org091c3022015-01-19 19:52:16 +00001648 if (state->packet_compression) {
1649 sshbuf_reset(state->compression_buffer);
1650 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1651 state->compression_buffer)) != 0)
1652 goto out;
1653 sshbuf_reset(state->incoming_packet);
1654 if ((r = sshbuf_putb(state->incoming_packet,
1655 state->compression_buffer)) != 0)
1656 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001657 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001658 state->p_read.packets++;
1659 state->p_read.bytes += padded_len + 4;
1660 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1661 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001662 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1663 error("Invalid ssh1 packet type: %d", *typep);
1664 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1665 (r = ssh_packet_write_wait(ssh)) != 0)
1666 return r;
1667 return SSH_ERR_PROTOCOL_ERROR;
1668 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001669 r = 0;
1670 out:
1671 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001672}
Damien Miller95def091999-11-25 00:26:21 +11001673
markus@openbsd.org8d057842016-09-30 09:19:13 +00001674static int
1675ssh_packet_read_poll2_mux(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
1676{
1677 struct session_state *state = ssh->state;
1678 const u_char *cp;
1679 size_t need;
1680 int r;
1681
1682 if (ssh->kex)
1683 return SSH_ERR_INTERNAL_ERROR;
1684 *typep = SSH_MSG_NONE;
1685 cp = sshbuf_ptr(state->input);
1686 if (state->packlen == 0) {
1687 if (sshbuf_len(state->input) < 4 + 1)
1688 return 0; /* packet is incomplete */
1689 state->packlen = PEEK_U32(cp);
1690 if (state->packlen < 4 + 1 ||
1691 state->packlen > PACKET_MAX_SIZE)
1692 return SSH_ERR_MESSAGE_INCOMPLETE;
1693 }
1694 need = state->packlen + 4;
1695 if (sshbuf_len(state->input) < need)
1696 return 0; /* packet is incomplete */
1697 sshbuf_reset(state->incoming_packet);
1698 if ((r = sshbuf_put(state->incoming_packet, cp + 4,
1699 state->packlen)) != 0 ||
1700 (r = sshbuf_consume(state->input, need)) != 0 ||
1701 (r = sshbuf_get_u8(state->incoming_packet, NULL)) != 0 ||
1702 (r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1703 return r;
1704 if (ssh_packet_log_type(*typep))
1705 debug3("%s: type %u", __func__, *typep);
1706 /* sshbuf_dump(state->incoming_packet, stderr); */
1707 /* reset for next packet */
1708 state->packlen = 0;
1709 return r;
1710}
1711
markus@openbsd.org091c3022015-01-19 19:52:16 +00001712int
1713ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001714{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001715 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001716 u_int padlen, need;
djm@openbsd.org6d311932016-07-08 03:44:42 +00001717 u_char *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001718 u_int maclen, aadlen = 0, authlen = 0, block_size;
1719 struct sshenc *enc = NULL;
1720 struct sshmac *mac = NULL;
1721 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001722 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001723
markus@openbsd.org8d057842016-09-30 09:19:13 +00001724 if (state->mux)
1725 return ssh_packet_read_poll2_mux(ssh, typep, seqnr_p);
1726
markus@openbsd.org091c3022015-01-19 19:52:16 +00001727 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001728
markus@openbsd.org091c3022015-01-19 19:52:16 +00001729 if (state->packet_discard)
1730 return 0;
1731
1732 if (state->newkeys[MODE_IN] != NULL) {
1733 enc = &state->newkeys[MODE_IN]->enc;
1734 mac = &state->newkeys[MODE_IN]->mac;
1735 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001736 /* disable mac for authenticated encryption */
1737 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1738 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001739 }
1740 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001741 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001742 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001743
markus@openbsd.org091c3022015-01-19 19:52:16 +00001744 if (aadlen && state->packlen == 0) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001745 if (cipher_get_length(state->receive_context,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001746 &state->packlen, state->p_read.seqnr,
1747 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1748 return 0;
1749 if (state->packlen < 1 + 4 ||
1750 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001751#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001752 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001753#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001754 logit("Bad packet length %u.", state->packlen);
1755 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1756 return r;
djm@openbsd.org2fecfd42015-11-08 21:59:11 +00001757 return SSH_ERR_CONN_CORRUPT;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001758 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001759 sshbuf_reset(state->incoming_packet);
1760 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001761 /*
1762 * check if input size is less than the cipher block size,
1763 * decrypt first block and extract length of incoming packet
1764 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001765 if (sshbuf_len(state->input) < block_size)
1766 return 0;
1767 sshbuf_reset(state->incoming_packet);
1768 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1769 &cp)) != 0)
1770 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001771 if ((r = cipher_crypt(state->receive_context,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001772 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1773 block_size, 0, 0)) != 0)
1774 goto out;
1775 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1776 if (state->packlen < 1 + 4 ||
1777 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001778#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001779 fprintf(stderr, "input: \n");
1780 sshbuf_dump(state->input, stderr);
1781 fprintf(stderr, "incoming_packet: \n");
1782 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001783#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001784 logit("Bad packet length %u.", state->packlen);
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +00001785 return ssh_packet_start_discard(ssh, enc, mac, 0,
1786 PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001787 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001788 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1789 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001790 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001791 DBG(debug("input: packet len %u", state->packlen+4));
1792
Damien Milleraf43a7a2012-12-12 10:46:31 +11001793 if (aadlen) {
1794 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001795 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001796 } else {
1797 /*
1798 * the payload size and the payload are encrypted, but we
1799 * have a partial packet of block_size bytes
1800 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001801 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001802 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001803 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1804 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001805 if (need % block_size != 0) {
1806 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001807 need, block_size, need % block_size);
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +00001808 return ssh_packet_start_discard(ssh, enc, mac, 0,
1809 PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001810 }
Damien Miller33b13562000-04-04 14:38:59 +10001811 /*
1812 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001813 * decrypt into incoming_packet:
1814 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001815 * 'need' bytes are encrypted, followed by either
1816 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001817 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001818 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001819 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
djm@openbsd.org6d311932016-07-08 03:44:42 +00001820 return 0; /* packet is incomplete */
Damien Miller33b13562000-04-04 14:38:59 +10001821#ifdef PACKET_DEBUG
1822 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001823 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001824#endif
djm@openbsd.org6d311932016-07-08 03:44:42 +00001825 /* EtM: check mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001826 if (mac && mac->enabled && mac->etm) {
djm@openbsd.org6d311932016-07-08 03:44:42 +00001827 if ((r = mac_check(mac, state->p_read.seqnr,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001828 sshbuf_ptr(state->input), aadlen + need,
djm@openbsd.org6d311932016-07-08 03:44:42 +00001829 sshbuf_ptr(state->input) + aadlen + need + authlen,
1830 maclen)) != 0) {
1831 if (r == SSH_ERR_MAC_INVALID)
1832 logit("Corrupted MAC on input.");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001833 goto out;
djm@openbsd.org6d311932016-07-08 03:44:42 +00001834 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001835 }
1836 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1837 &cp)) != 0)
1838 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001839 if ((r = cipher_crypt(state->receive_context, state->p_read.seqnr, cp,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001840 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1841 goto out;
1842 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1843 goto out;
Damien Miller4af51302000-04-16 11:18:38 +10001844 if (mac && mac->enabled) {
djm@openbsd.org6d311932016-07-08 03:44:42 +00001845 /* Not EtM: check MAC over cleartext */
1846 if (!mac->etm && (r = mac_check(mac, state->p_read.seqnr,
1847 sshbuf_ptr(state->incoming_packet),
1848 sshbuf_len(state->incoming_packet),
1849 sshbuf_ptr(state->input), maclen)) != 0) {
1850 if (r != SSH_ERR_MAC_INVALID)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001851 goto out;
Damien Miller13ae44c2009-01-28 16:38:41 +11001852 logit("Corrupted MAC on input.");
markus@openbsd.org0fb1a612017-03-11 13:07:35 +00001853 if (need + block_size > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001854 return SSH_ERR_INTERNAL_ERROR;
1855 return ssh_packet_start_discard(ssh, enc, mac,
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +00001856 sshbuf_len(state->incoming_packet),
markus@openbsd.org0fb1a612017-03-11 13:07:35 +00001857 PACKET_MAX_SIZE - need - block_size);
Damien Miller13ae44c2009-01-28 16:38:41 +11001858 }
djm@openbsd.org6d311932016-07-08 03:44:42 +00001859 /* Remove MAC from input buffer */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001860 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1861 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1862 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001863 }
Damien Miller278f9072001-12-21 15:00:19 +11001864 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001865 *seqnr_p = state->p_read.seqnr;
1866 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001867 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001868 if (++state->p_read.packets == 0)
1869 if (!(ssh->compat & SSH_BUG_NOREKEY))
1870 return SSH_ERR_NEED_REKEY;
1871 state->p_read.blocks += (state->packlen + 4) / block_size;
1872 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001873
1874 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001875 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001876 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001877 if (padlen < 4) {
1878 if ((r = sshpkt_disconnect(ssh,
1879 "Corrupted padlen %d on input.", padlen)) != 0 ||
1880 (r = ssh_packet_write_wait(ssh)) != 0)
1881 return r;
1882 return SSH_ERR_CONN_CORRUPT;
1883 }
Damien Miller33b13562000-04-04 14:38:59 +10001884
1885 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001886 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1887 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1888 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001889
markus@openbsd.org091c3022015-01-19 19:52:16 +00001890 DBG(debug("input: len before de-compress %zd",
1891 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001892 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001893 sshbuf_reset(state->compression_buffer);
1894 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1895 state->compression_buffer)) != 0)
1896 goto out;
1897 sshbuf_reset(state->incoming_packet);
1898 if ((r = sshbuf_putb(state->incoming_packet,
1899 state->compression_buffer)) != 0)
1900 goto out;
1901 DBG(debug("input: len after de-compress %zd",
1902 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001903 }
1904 /*
1905 * get packet type, implies consume.
1906 * return length of payload (without type field)
1907 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001908 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1909 goto out;
djm@openbsd.org28136472016-01-29 05:46:01 +00001910 if (ssh_packet_log_type(*typep))
1911 debug3("receive packet: type %u", *typep);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001912 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1913 if ((r = sshpkt_disconnect(ssh,
1914 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1915 (r = ssh_packet_write_wait(ssh)) != 0)
1916 return r;
1917 return SSH_ERR_PROTOCOL_ERROR;
1918 }
djm@openbsd.org39af7b42016-10-11 21:47:45 +00001919 if (state->hook_in != NULL &&
1920 (r = state->hook_in(ssh, state->incoming_packet, typep,
1921 state->hook_in_ctx)) != 0)
1922 return r;
markus@openbsd.org28652bc2016-09-19 19:02:19 +00001923 if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001924 r = ssh_packet_enable_delayed_compress(ssh);
1925 else
1926 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001927#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001928 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1929 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001930#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001931 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001932 state->packlen = 0;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001933
1934 /* do we need to rekey? */
1935 if (ssh_packet_need_rekeying(ssh, 0)) {
1936 debug3("%s: rekex triggered", __func__);
1937 if ((r = kex_start_rekex(ssh)) != 0)
1938 return r;
1939 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001940 out:
1941 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001942}
1943
1944int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001945ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001946{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001947 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001948 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001949 int r;
1950 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001951
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001952 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001953 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001954 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001955 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1956 if (r != 0)
1957 return r;
1958 if (*typep) {
1959 state->keep_alive_timeouts = 0;
1960 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001961 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001962 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001963 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001964 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001965 break;
Damien Miller33b13562000-04-04 14:38:59 +10001966 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001967 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1968 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1969 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001970 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001971 return r;
1972 }
Damien Miller33b13562000-04-04 14:38:59 +10001973 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001974 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001975 break;
1976 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001977 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1978 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1979 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001980 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001981 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001982 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1983 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001984 "Received disconnect from %s port %d:"
1985 "%u: %.400s", ssh_remote_ipaddr(ssh),
1986 ssh_remote_port(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001987 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001988 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001989 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001990 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1991 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001992 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1993 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001994 break;
Damien Miller33b13562000-04-04 14:38:59 +10001995 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001996 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001997 }
Damien Miller33b13562000-04-04 14:38:59 +10001998 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001999 r = ssh_packet_read_poll1(ssh, typep);
2000 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10002001 case SSH_MSG_NONE:
2002 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10002003 case SSH_MSG_IGNORE:
2004 break;
2005 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00002006 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
2007 return r;
Damien Miller33b13562000-04-04 14:38:59 +10002008 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10002009 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10002010 break;
2011 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00002012 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
2013 return r;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002014 error("Received disconnect from %s port %d: "
2015 "%.400s", ssh_remote_ipaddr(ssh),
2016 ssh_remote_port(ssh), msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002017 free(msg);
2018 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10002019 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00002020 DBG(debug("received packet type %d", *typep));
2021 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00002022 }
Damien Miller33b13562000-04-04 14:38:59 +10002023 }
2024 }
2025}
2026
Damien Miller5428f641999-11-25 11:54:57 +11002027/*
2028 * Buffers the given amount of input characters. This is intended to be used
2029 * together with packet_read_poll.
2030 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002031
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002032int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002033ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002034{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002035 struct session_state *state = ssh->state;
2036 int r;
2037
2038 if (state->packet_discard) {
2039 state->keep_alive_timeouts = 0; /* ?? */
2040 if (len >= state->packet_discard) {
2041 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002042 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002043 }
2044 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002045 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11002046 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002047 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002048 return r;
2049
2050 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002051}
2052
Damien Miller4af51302000-04-16 11:18:38 +10002053int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002054ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10002055{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002056 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10002057}
2058
Damien Miller5428f641999-11-25 11:54:57 +11002059/*
2060 * Sends a diagnostic message from the server to the client. This message
2061 * can be sent at any time (but not while constructing another message). The
2062 * message is printed immediately, but only if the client is being executed
2063 * in verbose mode. These messages are primarily intended to ease debugging
2064 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002065 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11002066 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002067void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002068ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002069{
Damien Miller95def091999-11-25 00:26:21 +11002070 char buf[1024];
2071 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002072 int r;
Damien Miller95def091999-11-25 00:26:21 +11002073
markus@openbsd.org091c3022015-01-19 19:52:16 +00002074 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00002075 return;
2076
Damien Miller95def091999-11-25 00:26:21 +11002077 va_start(args, fmt);
2078 vsnprintf(buf, sizeof(buf), fmt, args);
2079 va_end(args);
2080
Damien Miller7c8af4f2000-05-01 08:24:07 +10002081 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002082 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
2083 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
2084 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2085 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2086 (r = sshpkt_send(ssh)) != 0)
2087 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10002088 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002089 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
2090 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2091 (r = sshpkt_send(ssh)) != 0)
2092 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10002093 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002094 if ((r = ssh_packet_write_wait(ssh)) != 0)
2095 fatal("%s: %s", __func__, ssh_err(r));
2096}
2097
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002098static void
2099fmt_connection_id(struct ssh *ssh, char *s, size_t l)
2100{
2101 snprintf(s, l, "%.200s%s%s port %d",
2102 ssh->log_preamble ? ssh->log_preamble : "",
2103 ssh->log_preamble ? " " : "",
2104 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
2105}
2106
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002107/*
2108 * Pretty-print connection-terminating errors and exit.
2109 */
2110void
2111sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
2112{
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002113 char remote_id[512];
2114
2115 fmt_connection_id(ssh, remote_id, sizeof(remote_id));
2116
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002117 switch (r) {
2118 case SSH_ERR_CONN_CLOSED:
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002119 logdie("Connection closed by %s", remote_id);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002120 case SSH_ERR_CONN_TIMEOUT:
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002121 logdie("Connection %s %s timed out",
2122 ssh->state->server_side ? "from" : "to", remote_id);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002123 case SSH_ERR_DISCONNECTED:
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002124 logdie("Disconnected from %s", remote_id);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002125 case SSH_ERR_SYSTEM_ERROR:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002126 if (errno == ECONNRESET)
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002127 logdie("Connection reset by %s", remote_id);
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002128 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002129 case SSH_ERR_NO_CIPHER_ALG_MATCH:
2130 case SSH_ERR_NO_MAC_ALG_MATCH:
2131 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
2132 case SSH_ERR_NO_KEX_ALG_MATCH:
2133 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
2134 if (ssh && ssh->kex && ssh->kex->failed_choice) {
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002135 logdie("Unable to negotiate with %s: %s. "
2136 "Their offer: %s", remote_id, ssh_err(r),
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002137 ssh->kex->failed_choice);
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002138 }
2139 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002140 default:
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002141 logdie("%s%sConnection %s %s: %s",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002142 tag != NULL ? tag : "", tag != NULL ? ": " : "",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002143 ssh->state->server_side ? "from" : "to",
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002144 remote_id, ssh_err(r));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002145 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002146}
2147
Damien Miller5428f641999-11-25 11:54:57 +11002148/*
2149 * Logs the error plus constructs and sends a disconnect packet, closes the
2150 * connection, and exits. This function never returns. The error message
2151 * should not contain a newline. The length of the formatted message must
2152 * not exceed 1024 bytes.
2153 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002154void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002155ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002156{
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002157 char buf[1024], remote_id[512];
Damien Miller95def091999-11-25 00:26:21 +11002158 va_list args;
2159 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002160 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002161
Damien Miller95def091999-11-25 00:26:21 +11002162 if (disconnecting) /* Guard against recursive invocations. */
2163 fatal("packet_disconnect called recursively.");
2164 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002165
Damien Miller5428f641999-11-25 11:54:57 +11002166 /*
2167 * Format the message. Note that the caller must make sure the
2168 * message is of limited size.
2169 */
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002170 fmt_connection_id(ssh, remote_id, sizeof(remote_id));
Damien Miller95def091999-11-25 00:26:21 +11002171 va_start(args, fmt);
2172 vsnprintf(buf, sizeof(buf), fmt, args);
2173 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002174
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002175 /* Display the error locally */
djm@openbsd.org07edd7e2017-02-03 23:03:33 +00002176 logit("Disconnecting %s: %.100s", remote_id, buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002177
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002178 /*
2179 * Send the disconnect message to the other side, and wait
2180 * for it to get sent.
2181 */
2182 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
2183 sshpkt_fatal(ssh, __func__, r);
2184
2185 if ((r = ssh_packet_write_wait(ssh)) != 0)
2186 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002187
Damien Miller95def091999-11-25 00:26:21 +11002188 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002189 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002190 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002191}
2192
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002193/*
2194 * Checks if there is any buffered output, and tries to write some of
2195 * the output.
2196 */
2197int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002198ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002199{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002200 struct session_state *state = ssh->state;
2201 int len = sshbuf_len(state->output);
markus@openbsd.orga3068632016-01-14 16:17:39 +00002202 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002203
Damien Miller95def091999-11-25 00:26:21 +11002204 if (len > 0) {
markus@openbsd.orga3068632016-01-14 16:17:39 +00002205 len = write(state->connection_out,
2206 sshbuf_ptr(state->output), len);
Damien Millerd874fa52008-07-05 09:40:56 +10002207 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002208 if (errno == EINTR || errno == EAGAIN ||
2209 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002210 return 0;
2211 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002212 }
markus@openbsd.orga3068632016-01-14 16:17:39 +00002213 if (len == 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002214 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002215 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002216 return r;
Damien Miller95def091999-11-25 00:26:21 +11002217 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002218 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002219}
2220
Damien Miller5428f641999-11-25 11:54:57 +11002221/*
2222 * Calls packet_write_poll repeatedly until all pending output data has been
2223 * written.
2224 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002225int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002226ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002227{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002228 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002229 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002230 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002231 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002232
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002233 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002234 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002235 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002236 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002237 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2238 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002239 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002240 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002241 while (ssh_packet_have_data_to_write(ssh)) {
2242 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002243 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002244 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002245
markus@openbsd.org091c3022015-01-19 19:52:16 +00002246 if (state->packet_timeout_ms > 0) {
2247 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002248 timeoutp = &timeout;
2249 }
2250 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002251 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002252 ms_to_timeval(&timeout, ms_remain);
2253 gettimeofday(&start, NULL);
2254 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002255 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002256 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002257 break;
Damien Millerea437422009-10-02 11:49:03 +10002258 if (errno != EAGAIN && errno != EINTR &&
2259 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002260 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002261 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002262 continue;
2263 ms_subtract_diff(&start, &ms_remain);
2264 if (ms_remain <= 0) {
2265 ret = 0;
2266 break;
2267 }
2268 }
2269 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002270 free(setp);
2271 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002272 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002273 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2274 free(setp);
2275 return r;
2276 }
Damien Miller95def091999-11-25 00:26:21 +11002277 }
Darren Tuckera627d422013-06-02 07:31:17 +10002278 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002279 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002280}
2281
2282/* Returns true if there is buffered data to write to the connection. */
2283
2284int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002285ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002286{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002287 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002288}
2289
2290/* Returns true if there is not too much data to write to the connection. */
2291
2292int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002293ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002294{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002295 if (ssh->state->interactive_mode)
2296 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002297 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002298 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002299}
2300
markus@openbsd.org091c3022015-01-19 19:52:16 +00002301void
2302ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002303{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002304#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002305 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002306 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002307 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002308# ifdef IP_TOS
2309 case AF_INET:
2310 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002311 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002312 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2313 error("setsockopt IP_TOS %d: %.100s:",
2314 tos, strerror(errno));
2315 break;
2316# endif /* IP_TOS */
2317# ifdef IPV6_TCLASS
2318 case AF_INET6:
2319 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002320 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002321 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2322 error("setsockopt IPV6_TCLASS %d: %.100s:",
2323 tos, strerror(errno));
2324 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002325# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002326 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002327#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002328}
Ben Lindstroma7433982002-12-23 02:41:41 +00002329
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002330/* Informs that the current session is interactive. Sets IP flags for that. */
2331
2332void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002333ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002334{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002335 struct session_state *state = ssh->state;
2336
2337 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002338 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002339 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002340
Damien Miller95def091999-11-25 00:26:21 +11002341 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002342 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002343
Damien Miller34132e52000-01-14 15:45:46 +11002344 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002345 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002346 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002347 set_nodelay(state->connection_in);
2348 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2349 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002350}
2351
2352/* Returns true if the current connection is interactive. */
2353
2354int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002355ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002356{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002357 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002358}
Damien Miller6162d121999-11-21 13:23:52 +11002359
Darren Tucker1f8311c2004-05-13 16:39:33 +10002360int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002361ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002362{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002363 struct session_state *state = ssh->state;
2364
2365 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002366 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002367 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002368 return -1;
2369 }
2370 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002371 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002372 return -1;
2373 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002374 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002375 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002376 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002377 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002378}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002379
Darren Tuckerf7288d72009-06-21 18:12:20 +10002380int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002381ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002382{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002383 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002384}
2385
2386void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002387ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002388{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002389 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002390}
2391
2392u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002393ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002394{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002395 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002396}
2397
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002398/*
2399 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002400 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002401 * byte SSH_MSG_IGNORE
2402 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002403 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002404 * All implementations MUST understand (and ignore) this message at any
2405 * time (after receiving the protocol version). No implementation is
2406 * required to send them. This message can be used as an additional
2407 * protection measure against advanced traffic analysis techniques.
2408 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002409void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002410ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002411{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002412 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002413 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002414
markus@openbsd.org091c3022015-01-19 19:52:16 +00002415 if ((r = sshpkt_start(ssh, compat20 ?
2416 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2417 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2418 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002419 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002420 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002421 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002422 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2423 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002424 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002425 }
2426}
Damien Millera5539d22003-04-09 20:50:06 +10002427
Damien Millera5539d22003-04-09 20:50:06 +10002428void
dtucker@openbsd.orgc998bf02017-02-03 02:56:00 +00002429ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, u_int32_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002430{
dtucker@openbsd.orgc998bf02017-02-03 02:56:00 +00002431 debug3("rekey after %llu bytes, %u seconds", (unsigned long long)bytes,
2432 (unsigned int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002433 ssh->state->rekey_limit = bytes;
2434 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002435}
2436
2437time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002438ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002439{
2440 time_t seconds;
2441
markus@openbsd.org091c3022015-01-19 19:52:16 +00002442 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002443 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002444 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002445}
Damien Miller9786e6e2005-07-26 21:54:56 +10002446
2447void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002448ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002449{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002450 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002451}
2452
2453void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002454ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002455{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002456 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002457}
2458
2459void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002460ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002461{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002462 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002463}
2464
2465void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002466ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002467{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002468 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002469}
2470
Damien Millerc31a0cd2014-05-15 14:37:39 +10002471/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002472static int
2473ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002474{
djm@openbsd.org0082fba2016-09-28 16:33:06 +00002475 int r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002476
2477 debug("%s: called", __func__);
2478 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002479 ssh->state->after_authentication = 1;
2480 ssh->state->rekeying = 0;
djm@openbsd.org0082fba2016-09-28 16:33:06 +00002481 if ((r = ssh_packet_enable_delayed_compress(ssh)) != 0)
2482 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002483 return 0;
2484}
2485
2486/* Packet state (de-)serialization for privsep */
2487
2488/* turn kex into a blob for packet state serialization */
2489static int
2490kex_to_blob(struct sshbuf *m, struct kex *kex)
2491{
2492 int r;
2493
2494 if ((r = sshbuf_put_string(m, kex->session_id,
2495 kex->session_id_len)) != 0 ||
2496 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2497 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2498 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2499 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2500 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2501 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2502 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2503 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2504 return r;
2505 return 0;
2506}
2507
2508/* turn key exchange results into a blob for packet state serialization */
2509static int
2510newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2511{
2512 struct sshbuf *b;
2513 struct sshcipher_ctx *cc;
2514 struct sshcomp *comp;
2515 struct sshenc *enc;
2516 struct sshmac *mac;
2517 struct newkeys *newkey;
2518 int r;
2519
2520 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2521 return SSH_ERR_INTERNAL_ERROR;
2522 enc = &newkey->enc;
2523 mac = &newkey->mac;
2524 comp = &newkey->comp;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002525 cc = (mode == MODE_OUT) ? ssh->state->send_context :
2526 ssh->state->receive_context;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002527 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2528 return r;
2529 if ((b = sshbuf_new()) == NULL)
2530 return SSH_ERR_ALLOC_FAIL;
2531 /* The cipher struct is constant and shared, you export pointer */
2532 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2533 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2534 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2535 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2536 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2537 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2538 goto out;
2539 if (cipher_authlen(enc->cipher) == 0) {
2540 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2541 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2542 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2543 goto out;
2544 }
2545 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002546 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2547 goto out;
2548 r = sshbuf_put_stringb(m, b);
2549 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002550 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002551 return r;
2552}
2553
2554/* serialize packet state into a blob */
2555int
2556ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2557{
2558 struct session_state *state = ssh->state;
2559 u_char *p;
2560 size_t slen, rlen;
2561 int r, ssh1cipher;
2562
2563 if (!compat20) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002564 ssh1cipher = cipher_ctx_get_number(state->receive_context);
2565 slen = cipher_get_keyiv_len(state->send_context);
2566 rlen = cipher_get_keyiv_len(state->receive_context);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002567 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2568 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2569 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2570 (r = sshbuf_put_u32(m, slen)) != 0 ||
2571 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002572 (r = cipher_get_keyiv(state->send_context, p, slen)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002573 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2574 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002575 (r = cipher_get_keyiv(state->receive_context, p, rlen)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002576 return r;
2577 } else {
2578 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2579 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2580 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002581 (r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002582 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002583 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2584 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2585 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2586 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2587 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2588 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2589 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2590 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2591 return r;
2592 }
2593
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002594 slen = cipher_get_keycontext(state->send_context, NULL);
2595 rlen = cipher_get_keycontext(state->receive_context, NULL);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002596 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2597 (r = sshbuf_reserve(m, slen, &p)) != 0)
2598 return r;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002599 if (cipher_get_keycontext(state->send_context, p) != (int)slen)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002600 return SSH_ERR_INTERNAL_ERROR;
2601 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2602 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2603 return r;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002604 if (cipher_get_keycontext(state->receive_context, p) != (int)rlen)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002605 return SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org0082fba2016-09-28 16:33:06 +00002606 if ((r = sshbuf_put_stringb(m, state->input)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002607 (r = sshbuf_put_stringb(m, state->output)) != 0)
2608 return r;
2609
markus@openbsd.org091c3022015-01-19 19:52:16 +00002610 return 0;
2611}
2612
2613/* restore key exchange results from blob for packet state de-serialization */
2614static int
2615newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2616{
2617 struct sshbuf *b = NULL;
2618 struct sshcomp *comp;
2619 struct sshenc *enc;
2620 struct sshmac *mac;
2621 struct newkeys *newkey = NULL;
2622 size_t keylen, ivlen, maclen;
2623 int r;
2624
2625 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2626 r = SSH_ERR_ALLOC_FAIL;
2627 goto out;
2628 }
2629 if ((r = sshbuf_froms(m, &b)) != 0)
2630 goto out;
2631#ifdef DEBUG_PK
2632 sshbuf_dump(b, stderr);
2633#endif
2634 enc = &newkey->enc;
2635 mac = &newkey->mac;
2636 comp = &newkey->comp;
2637
2638 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2639 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2640 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2641 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2642 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2643 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2644 goto out;
2645 if (cipher_authlen(enc->cipher) == 0) {
2646 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2647 goto out;
2648 if ((r = mac_setup(mac, mac->name)) != 0)
2649 goto out;
2650 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2651 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2652 goto out;
2653 if (maclen > mac->key_len) {
2654 r = SSH_ERR_INVALID_FORMAT;
2655 goto out;
2656 }
2657 mac->key_len = maclen;
2658 }
2659 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002660 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2661 goto out;
2662 if (enc->name == NULL ||
2663 cipher_by_name(enc->name) != enc->cipher) {
2664 r = SSH_ERR_INVALID_FORMAT;
2665 goto out;
2666 }
2667 if (sshbuf_len(b) != 0) {
2668 r = SSH_ERR_INVALID_FORMAT;
2669 goto out;
2670 }
2671 enc->key_len = keylen;
2672 enc->iv_len = ivlen;
2673 ssh->kex->newkeys[mode] = newkey;
2674 newkey = NULL;
2675 r = 0;
2676 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002677 free(newkey);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002678 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002679 return r;
2680}
2681
2682/* restore kex from blob for packet state de-serialization */
2683static int
2684kex_from_blob(struct sshbuf *m, struct kex **kexp)
2685{
2686 struct kex *kex;
2687 int r;
2688
2689 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2690 (kex->my = sshbuf_new()) == NULL ||
2691 (kex->peer = sshbuf_new()) == NULL) {
2692 r = SSH_ERR_ALLOC_FAIL;
2693 goto out;
2694 }
2695 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2696 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2697 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2698 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2699 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2700 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2701 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2702 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2703 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2704 goto out;
2705 kex->server = 1;
2706 kex->done = 1;
2707 r = 0;
2708 out:
2709 if (r != 0 || kexp == NULL) {
2710 if (kex != NULL) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002711 sshbuf_free(kex->my);
2712 sshbuf_free(kex->peer);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002713 free(kex);
2714 }
2715 if (kexp != NULL)
2716 *kexp = NULL;
2717 } else {
2718 *kexp = kex;
2719 }
2720 return r;
2721}
2722
2723/*
2724 * Restore packet state from content of blob 'm' (de-serialization).
2725 * Note that 'm' will be partially consumed on parsing or any other errors.
2726 */
2727int
2728ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2729{
2730 struct session_state *state = ssh->state;
2731 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2732 size_t ssh1keylen, rlen, slen, ilen, olen;
2733 int r;
2734 u_int ssh1cipher = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002735
2736 if (!compat20) {
2737 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2738 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2739 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2740 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2741 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2742 return r;
2743 if (ssh1cipher > INT_MAX)
2744 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2745 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2746 (int)ssh1cipher);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002747 if (cipher_get_keyiv_len(state->send_context) != (int)slen ||
2748 cipher_get_keyiv_len(state->receive_context) != (int)rlen)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002749 return SSH_ERR_INVALID_FORMAT;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002750 if ((r = cipher_set_keyiv(state->send_context, ivout)) != 0 ||
2751 (r = cipher_set_keyiv(state->receive_context, ivin)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002752 return r;
2753 } else {
2754 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2755 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2756 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002757 (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002758 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002759 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2760 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2761 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2762 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2763 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2764 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2765 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2766 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2767 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002768 /*
2769 * We set the time here so that in post-auth privsep slave we
2770 * count from the completion of the authentication.
2771 */
2772 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002773 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2774 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2775 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2776 return r;
2777 }
2778 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2779 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2780 return r;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002781 if (cipher_get_keycontext(state->send_context, NULL) != (int)slen ||
2782 cipher_get_keycontext(state->receive_context, NULL) != (int)rlen)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002783 return SSH_ERR_INVALID_FORMAT;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002784 cipher_set_keycontext(state->send_context, keyout);
2785 cipher_set_keycontext(state->receive_context, keyin);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002786
djm@openbsd.org0082fba2016-09-28 16:33:06 +00002787 if ((r = ssh_packet_set_postauth(ssh)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002788 return r;
2789
2790 sshbuf_reset(state->input);
2791 sshbuf_reset(state->output);
2792 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2793 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2794 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2795 (r = sshbuf_put(state->output, output, olen)) != 0)
2796 return r;
2797
markus@openbsd.org091c3022015-01-19 19:52:16 +00002798 if (sshbuf_len(m))
2799 return SSH_ERR_INVALID_FORMAT;
2800 debug3("%s: done", __func__);
2801 return 0;
2802}
2803
2804/* NEW API */
2805
2806/* put data to the outgoing packet */
2807
2808int
2809sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2810{
2811 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2812}
2813
2814int
2815sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2816{
2817 return sshbuf_putb(ssh->state->outgoing_packet, b);
2818}
2819
2820int
2821sshpkt_put_u8(struct ssh *ssh, u_char val)
2822{
2823 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2824}
2825
2826int
2827sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2828{
2829 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2830}
2831
2832int
2833sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2834{
2835 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2836}
2837
2838int
2839sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2840{
2841 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2842}
2843
2844int
2845sshpkt_put_cstring(struct ssh *ssh, const void *v)
2846{
2847 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2848}
2849
2850int
2851sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2852{
2853 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2854}
2855
djm@openbsd.org734226b2015-04-27 01:52:30 +00002856#ifdef WITH_OPENSSL
2857#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002858int
2859sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2860{
2861 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2862}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002863#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002864
Damien Miller773dda22015-01-30 23:10:17 +11002865#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002866int
2867sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2868{
2869 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2870}
Damien Miller773dda22015-01-30 23:10:17 +11002871#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002872
2873int
2874sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2875{
2876 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2877}
Damien Miller773dda22015-01-30 23:10:17 +11002878#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002879
2880/* fetch data from the incoming packet */
2881
2882int
2883sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2884{
2885 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2886}
2887
2888int
2889sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2890{
2891 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2892}
2893
2894int
2895sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2896{
2897 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2898}
2899
2900int
2901sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2902{
2903 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2904}
2905
2906int
2907sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2908{
2909 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2910}
2911
2912int
2913sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2914{
2915 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2916}
2917
2918int
2919sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2920{
2921 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2922}
2923
djm@openbsd.org734226b2015-04-27 01:52:30 +00002924#ifdef WITH_OPENSSL
2925#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002926int
2927sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2928{
2929 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2930}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002931#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002932
Damien Miller773dda22015-01-30 23:10:17 +11002933#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002934int
2935sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2936{
2937 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2938}
Damien Miller773dda22015-01-30 23:10:17 +11002939#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002940
2941int
2942sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2943{
2944 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2945}
Damien Miller773dda22015-01-30 23:10:17 +11002946#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002947
2948int
2949sshpkt_get_end(struct ssh *ssh)
2950{
2951 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2952 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2953 return 0;
2954}
2955
2956const u_char *
2957sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2958{
2959 if (lenp != NULL)
2960 *lenp = sshbuf_len(ssh->state->incoming_packet);
2961 return sshbuf_ptr(ssh->state->incoming_packet);
2962}
2963
2964/* start a new packet */
2965
2966int
2967sshpkt_start(struct ssh *ssh, u_char type)
2968{
2969 u_char buf[9];
2970 int len;
2971
2972 DBG(debug("packet_start[%d]", type));
2973 len = compat20 ? 6 : 9;
2974 memset(buf, 0, len - 1);
2975 buf[len - 1] = type;
2976 sshbuf_reset(ssh->state->outgoing_packet);
2977 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2978}
2979
markus@openbsd.org8d057842016-09-30 09:19:13 +00002980static int
2981ssh_packet_send_mux(struct ssh *ssh)
2982{
2983 struct session_state *state = ssh->state;
2984 u_char type, *cp;
2985 size_t len;
2986 int r;
2987
2988 if (ssh->kex)
2989 return SSH_ERR_INTERNAL_ERROR;
2990 len = sshbuf_len(state->outgoing_packet);
2991 if (len < 6)
2992 return SSH_ERR_INTERNAL_ERROR;
2993 cp = sshbuf_mutable_ptr(state->outgoing_packet);
2994 type = cp[5];
2995 if (ssh_packet_log_type(type))
2996 debug3("%s: type %u", __func__, type);
2997 /* drop everything, but the connection protocol */
2998 if (type >= SSH2_MSG_CONNECTION_MIN &&
2999 type <= SSH2_MSG_CONNECTION_MAX) {
3000 POKE_U32(cp, len - 4);
3001 if ((r = sshbuf_putb(state->output,
3002 state->outgoing_packet)) != 0)
3003 return r;
3004 /* sshbuf_dump(state->output, stderr); */
3005 }
3006 sshbuf_reset(state->outgoing_packet);
3007 return 0;
3008}
3009
markus@openbsd.org091c3022015-01-19 19:52:16 +00003010/* send it */
3011
3012int
3013sshpkt_send(struct ssh *ssh)
3014{
markus@openbsd.org8d057842016-09-30 09:19:13 +00003015 if (ssh->state && ssh->state->mux)
3016 return ssh_packet_send_mux(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +00003017 if (compat20)
3018 return ssh_packet_send2(ssh);
3019 else
3020 return ssh_packet_send1(ssh);
3021}
3022
3023int
3024sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
3025{
3026 char buf[1024];
3027 va_list args;
3028 int r;
3029
3030 va_start(args, fmt);
3031 vsnprintf(buf, sizeof(buf), fmt, args);
3032 va_end(args);
3033
3034 if (compat20) {
3035 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
3036 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
3037 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
3038 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
3039 (r = sshpkt_send(ssh)) != 0)
3040 return r;
3041 } else {
3042 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
3043 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
3044 (r = sshpkt_send(ssh)) != 0)
3045 return r;
3046 }
3047 return 0;
3048}
3049
3050/* roundup current message to pad bytes */
3051int
3052sshpkt_add_padding(struct ssh *ssh, u_char pad)
3053{
3054 ssh->state->extra_pad = pad;
3055 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10003056}