blob: ad1f6b4971b202be168471bb649f5c37b6a28acf [file] [log] [blame]
djm@openbsd.org39af7b42016-10-11 21:47:45 +00001/* $OpenBSD: packet.c,v 1.243 2016/10/11 21:47:45 djm 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
356ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100357{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000358 struct session_state *state = ssh->state;
359 int r;
360
361 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100362 char buf[1024];
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000363 size_t dlen = PACKET_MAX_SIZE;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000364
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000365 if (dlen > state->packet_discard_mac_already)
366 dlen -= state->packet_discard_mac_already;
Damien Miller13ae44c2009-01-28 16:38:41 +1100367 memset(buf, 'a', sizeof(buf));
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000368 while (sshbuf_len(state->incoming_packet) < dlen)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000369 if ((r = sshbuf_put(state->incoming_packet, buf,
370 sizeof(buf))) != 0)
371 return r;
372 (void) mac_compute(state->packet_discard_mac,
373 state->p_read.seqnr,
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000374 sshbuf_ptr(state->incoming_packet), dlen,
markus@openbsd.org091c3022015-01-19 19:52:16 +0000375 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100376 }
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000377 logit("Finished discarding for %.200s port %d",
378 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000379 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100380}
381
markus@openbsd.org091c3022015-01-19 19:52:16 +0000382static int
383ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000384 struct sshmac *mac, size_t mac_already, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100385{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000386 struct session_state *state = ssh->state;
387 int r;
388
389 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
390 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
391 return r;
392 return SSH_ERR_MAC_INVALID;
393 }
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000394 /*
395 * Record number of bytes over which the mac has already
396 * been computed in order to minimize timing attacks.
397 */
398 if (mac && mac->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000399 state->packet_discard_mac = mac;
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +0000400 state->packet_discard_mac_already = mac_already;
401 }
402 if (sshbuf_len(state->input) >= discard)
403 return ssh_packet_stop_discard(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000404 state->packet_discard = discard - sshbuf_len(state->input);
405 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100406}
407
Damien Miller34132e52000-01-14 15:45:46 +1100408/* Returns 1 if remote host is connected via socket, 0 if not. */
409
410int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000411ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100412{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000413 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100414 struct sockaddr_storage from, to;
415 socklen_t fromlen, tolen;
416
djm@openbsd.org95767262016-03-07 19:02:43 +0000417 if (state->connection_in == -1 || state->connection_out == -1)
418 return 0;
419
Damien Miller34132e52000-01-14 15:45:46 +1100420 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000421 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100422 return 1;
423 fromlen = sizeof(from);
424 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000425 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000426 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100427 return 0;
428 tolen = sizeof(to);
429 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000430 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000431 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100432 return 0;
433 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
434 return 0;
435 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
436 return 0;
437 return 1;
438}
439
Ben Lindstromf6027d32002-03-22 01:42:04 +0000440void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000441ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000442{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000443 if (ibytes)
444 *ibytes = ssh->state->p_read.bytes;
445 if (obytes)
446 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000447}
448
449int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000450ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100451{
452 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100453 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100454
455 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000456 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000457 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100458 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000459#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100460 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000461 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000462 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000463#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000464 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100465}
466
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467/* Sets the connection into non-blocking mode. */
468
469void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000470ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471{
Damien Miller95def091999-11-25 00:26:21 +1100472 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000473 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
markus@openbsd.org091c3022015-01-19 19:52:16 +0000475 if (ssh->state->connection_out != ssh->state->connection_in)
476 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477}
478
479/* Returns the socket used for reading. */
480
481int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000482ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000484 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485}
486
487/* Returns the descriptor used for writing. */
488
489int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000490ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000492 return ssh->state->connection_out;
493}
494
495/*
496 * Returns the IP-address of the remote host as a string. The returned
497 * string must not be freed.
498 */
499
500const char *
501ssh_remote_ipaddr(struct ssh *ssh)
502{
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000503 const int sock = ssh->state->connection_in;
504
markus@openbsd.org091c3022015-01-19 19:52:16 +0000505 /* Check whether we have cached the ipaddr. */
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000506 if (ssh->remote_ipaddr == NULL) {
507 if (ssh_packet_connection_is_on_socket(ssh)) {
508 ssh->remote_ipaddr = get_peer_ipaddr(sock);
djm@openbsd.org95767262016-03-07 19:02:43 +0000509 ssh->remote_port = get_peer_port(sock);
510 ssh->local_ipaddr = get_local_ipaddr(sock);
511 ssh->local_port = get_local_port(sock);
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000512 } else {
513 ssh->remote_ipaddr = strdup("UNKNOWN");
djm@openbsd.org95767262016-03-07 19:02:43 +0000514 ssh->remote_port = 65535;
515 ssh->local_ipaddr = strdup("UNKNOWN");
516 ssh->local_port = 65535;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000517 }
518 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000519 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520}
521
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +0000522/* Returns the port number of the remote host. */
523
524int
525ssh_remote_port(struct ssh *ssh)
526{
527 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
528 return ssh->remote_port;
529}
530
djm@openbsd.org95767262016-03-07 19:02:43 +0000531/*
532 * Returns the IP-address of the local host as a string. The returned
533 * string must not be freed.
534 */
535
536const char *
537ssh_local_ipaddr(struct ssh *ssh)
538{
539 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
540 return ssh->local_ipaddr;
541}
542
543/* Returns the port number of the local host. */
544
545int
546ssh_local_port(struct ssh *ssh)
547{
548 (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
549 return ssh->local_port;
550}
551
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552/* Closes the connection and clears and frees internal data structures. */
553
554void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000555ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000557 struct session_state *state = ssh->state;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000558 u_int mode;
559
560 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100561 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000562 state->initialized = 0;
563 if (state->connection_in == state->connection_out) {
564 shutdown(state->connection_out, SHUT_RDWR);
565 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100566 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000567 close(state->connection_in);
568 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100569 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000570 sshbuf_free(state->input);
571 sshbuf_free(state->output);
572 sshbuf_free(state->outgoing_packet);
573 sshbuf_free(state->incoming_packet);
574 for (mode = 0; mode < MODE_MAX; mode++)
575 kex_free_newkeys(state->newkeys[mode]);
576 if (state->compression_buffer) {
577 sshbuf_free(state->compression_buffer);
578 if (state->compression_out_started) {
579 z_streamp stream = &state->compression_out_stream;
580 debug("compress outgoing: "
581 "raw data %llu, compressed %llu, factor %.2f",
582 (unsigned long long)stream->total_in,
583 (unsigned long long)stream->total_out,
584 stream->total_in == 0 ? 0.0 :
585 (double) stream->total_out / stream->total_in);
586 if (state->compression_out_failures == 0)
587 deflateEnd(stream);
588 }
589 if (state->compression_in_started) {
590 z_streamp stream = &state->compression_out_stream;
591 debug("compress incoming: "
592 "raw data %llu, compressed %llu, factor %.2f",
593 (unsigned long long)stream->total_out,
594 (unsigned long long)stream->total_in,
595 stream->total_out == 0 ? 0.0 :
596 (double) stream->total_in / stream->total_out);
597 if (state->compression_in_failures == 0)
598 inflateEnd(stream);
599 }
Damien Miller95def091999-11-25 00:26:21 +1100600 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000601 cipher_free(state->send_context);
602 cipher_free(state->receive_context);
603 state->send_context = state->receive_context = NULL;
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000604 free(ssh->remote_ipaddr);
605 ssh->remote_ipaddr = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000606 free(ssh->state);
607 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608}
609
610/* Sets remote side protocol flags. */
611
612void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000613ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000615 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616}
617
618/* Returns the remote protocol flags set earlier by the above function. */
619
Ben Lindstrom46c16222000-12-22 01:43:59 +0000620u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000621ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000623 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624}
625
Damien Miller5428f641999-11-25 11:54:57 +1100626/*
627 * Starts packet compression from the next packet on in both directions.
628 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
629 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630
markus@openbsd.org091c3022015-01-19 19:52:16 +0000631static int
632ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000633{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000634 if (!ssh->state->compression_buffer &&
635 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
636 return SSH_ERR_ALLOC_FAIL;
637 return 0;
638}
639
640static int
641start_compression_out(struct ssh *ssh, int level)
642{
643 if (level < 1 || level > 9)
644 return SSH_ERR_INVALID_ARGUMENT;
645 debug("Enabling compression at level %d.", level);
646 if (ssh->state->compression_out_started == 1)
647 deflateEnd(&ssh->state->compression_out_stream);
648 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
649 case Z_OK:
650 ssh->state->compression_out_started = 1;
651 break;
652 case Z_MEM_ERROR:
653 return SSH_ERR_ALLOC_FAIL;
654 default:
655 return SSH_ERR_INTERNAL_ERROR;
656 }
657 return 0;
658}
659
660static int
661start_compression_in(struct ssh *ssh)
662{
663 if (ssh->state->compression_in_started == 1)
664 inflateEnd(&ssh->state->compression_in_stream);
665 switch (inflateInit(&ssh->state->compression_in_stream)) {
666 case Z_OK:
667 ssh->state->compression_in_started = 1;
668 break;
669 case Z_MEM_ERROR:
670 return SSH_ERR_ALLOC_FAIL;
671 default:
672 return SSH_ERR_INTERNAL_ERROR;
673 }
674 return 0;
675}
676
677int
678ssh_packet_start_compression(struct ssh *ssh, int level)
679{
680 int r;
681
682 if (ssh->state->packet_compression && !compat20)
683 return SSH_ERR_INTERNAL_ERROR;
684 ssh->state->packet_compression = 1;
685 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
686 (r = start_compression_in(ssh)) != 0 ||
687 (r = start_compression_out(ssh, level)) != 0)
688 return r;
689 return 0;
690}
691
692/* XXX remove need for separate compression buffer */
693static int
694compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
695{
696 u_char buf[4096];
697 int r, status;
698
699 if (ssh->state->compression_out_started != 1)
700 return SSH_ERR_INTERNAL_ERROR;
701
702 /* This case is not handled below. */
703 if (sshbuf_len(in) == 0)
704 return 0;
705
706 /* Input is the contents of the input buffer. */
707 if ((ssh->state->compression_out_stream.next_in =
708 sshbuf_mutable_ptr(in)) == NULL)
709 return SSH_ERR_INTERNAL_ERROR;
710 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
711
712 /* Loop compressing until deflate() returns with avail_out != 0. */
713 do {
714 /* Set up fixed-size output buffer. */
715 ssh->state->compression_out_stream.next_out = buf;
716 ssh->state->compression_out_stream.avail_out = sizeof(buf);
717
718 /* Compress as much data into the buffer as possible. */
719 status = deflate(&ssh->state->compression_out_stream,
720 Z_PARTIAL_FLUSH);
721 switch (status) {
722 case Z_MEM_ERROR:
723 return SSH_ERR_ALLOC_FAIL;
724 case Z_OK:
725 /* Append compressed data to output_buffer. */
726 if ((r = sshbuf_put(out, buf, sizeof(buf) -
727 ssh->state->compression_out_stream.avail_out)) != 0)
728 return r;
729 break;
730 case Z_STREAM_ERROR:
731 default:
732 ssh->state->compression_out_failures++;
733 return SSH_ERR_INVALID_FORMAT;
734 }
735 } while (ssh->state->compression_out_stream.avail_out == 0);
736 return 0;
737}
738
739static int
740uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
741{
742 u_char buf[4096];
743 int r, status;
744
745 if (ssh->state->compression_in_started != 1)
746 return SSH_ERR_INTERNAL_ERROR;
747
748 if ((ssh->state->compression_in_stream.next_in =
749 sshbuf_mutable_ptr(in)) == NULL)
750 return SSH_ERR_INTERNAL_ERROR;
751 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
752
753 for (;;) {
754 /* Set up fixed-size output buffer. */
755 ssh->state->compression_in_stream.next_out = buf;
756 ssh->state->compression_in_stream.avail_out = sizeof(buf);
757
758 status = inflate(&ssh->state->compression_in_stream,
759 Z_PARTIAL_FLUSH);
760 switch (status) {
761 case Z_OK:
762 if ((r = sshbuf_put(out, buf, sizeof(buf) -
763 ssh->state->compression_in_stream.avail_out)) != 0)
764 return r;
765 break;
766 case Z_BUF_ERROR:
767 /*
768 * Comments in zlib.h say that we should keep calling
769 * inflate() until we get an error. This appears to
770 * be the error that we get.
771 */
772 return 0;
773 case Z_DATA_ERROR:
774 return SSH_ERR_INVALID_FORMAT;
775 case Z_MEM_ERROR:
776 return SSH_ERR_ALLOC_FAIL;
777 case Z_STREAM_ERROR:
778 default:
779 ssh->state->compression_in_failures++;
780 return SSH_ERR_INTERNAL_ERROR;
781 }
782 }
783 /* NOTREACHED */
784}
785
Damien Miller5428f641999-11-25 11:54:57 +1100786/*
Damien Miller5428f641999-11-25 11:54:57 +1100787 * Causes any further packets to be encrypted using the given key. The same
788 * key is used for both sending and reception. However, both directions are
789 * encrypted independently of each other.
790 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000791
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000793ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794{
djm@openbsd.org734226b2015-04-27 01:52:30 +0000795#ifndef WITH_SSH1
796 fatal("no SSH protocol 1 support");
797#else /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000798 struct session_state *state = ssh->state;
799 const struct sshcipher *cipher = cipher_by_number(number);
800 int r;
801 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100802
markus@openbsd.org091c3022015-01-19 19:52:16 +0000803 if (cipher == NULL)
804 fatal("%s: unknown cipher number %d", __func__, number);
805 if (keylen < 20)
806 fatal("%s: keylen too small: %d", __func__, keylen);
807 if (keylen > SSH_SESSION_KEY_LENGTH)
808 fatal("%s: keylen too big: %d", __func__, keylen);
809 memcpy(state->ssh1_key, key, keylen);
810 state->ssh1_keylen = keylen;
811 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
812 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
813 (r = cipher_init(&state->receive_context, cipher, key, keylen,
814 NULL, 0, CIPHER_DECRYPT) != 0))
815 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
816 if (!state->cipher_warning_done &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000817 ((wmsg = cipher_warning_message(state->send_context)) != NULL ||
818 (wmsg = cipher_warning_message(state->send_context)) != NULL)) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000819 error("Warning: %s", wmsg);
820 state->cipher_warning_done = 1;
821 }
Damien Miller773dda22015-01-30 23:10:17 +1100822#endif /* WITH_SSH1 */
Damien Millereb8b60e2010-08-31 22:41:14 +1000823}
824
Damien Miller5428f641999-11-25 11:54:57 +1100825/*
826 * Finalizes and sends the packet. If the encryption key has been set,
827 * encrypts the packet before sending.
828 */
Damien Miller95def091999-11-25 00:26:21 +1100829
markus@openbsd.org091c3022015-01-19 19:52:16 +0000830int
831ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000833 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000834 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000835 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000836 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000837
Damien Miller5428f641999-11-25 11:54:57 +1100838 /*
839 * If using packet compression, compress the payload of the outgoing
840 * packet.
841 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000842 if (state->packet_compression) {
843 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100844 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000845 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
846 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100847 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000848 if ((r = sshbuf_put(state->compression_buffer,
849 "\0\0\0\0\0\0\0\0", 8)) != 0)
850 goto out;
851 if ((r = compress_buffer(ssh, state->outgoing_packet,
852 state->compression_buffer)) != 0)
853 goto out;
854 sshbuf_reset(state->outgoing_packet);
855 if ((r = sshbuf_putb(state->outgoing_packet,
856 state->compression_buffer)) != 0)
857 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100858 }
859 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000860 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000861
Damien Millere247cc42000-05-07 12:03:14 +1000862 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100863 padding = 8 - len % 8;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000864 if (!cipher_ctx_is_plaintext(state->send_context)) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000865 cp = sshbuf_mutable_ptr(state->outgoing_packet);
866 if (cp == NULL) {
867 r = SSH_ERR_INTERNAL_ERROR;
868 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100869 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000870 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100871 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000872 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
873 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100874
875 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000876 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
877 sshbuf_len(state->outgoing_packet));
878 POKE_U32(buf, checksum);
879 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
880 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881
882#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100883 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000884 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885#endif
886
Damien Miller95def091999-11-25 00:26:21 +1100887 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000888 POKE_U32(buf, len);
889 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
890 goto out;
891 if ((r = sshbuf_reserve(state->output,
892 sshbuf_len(state->outgoing_packet), &cp)) != 0)
893 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000894 if ((r = cipher_crypt(state->send_context, 0, cp,
markus@openbsd.org091c3022015-01-19 19:52:16 +0000895 sshbuf_ptr(state->outgoing_packet),
896 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
897 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100898
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100900 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000901 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000903 state->p_send.packets++;
904 state->p_send.bytes += len +
905 sshbuf_len(state->outgoing_packet);
906 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907
Damien Miller5428f641999-11-25 11:54:57 +1100908 /*
Damien Miller788f2122005-11-05 15:14:59 +1100909 * Note that the packet is now only buffered in output. It won't be
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000910 * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
911 * is called.
Damien Miller5428f641999-11-25 11:54:57 +1100912 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000913 r = 0;
914 out:
915 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916}
917
markus@openbsd.org091c3022015-01-19 19:52:16 +0000918int
919ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000920{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000921 struct session_state *state = ssh->state;
922 struct sshenc *enc;
923 struct sshmac *mac;
924 struct sshcomp *comp;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000925 struct sshcipher_ctx **ccp;
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000926 struct packet_state *ps;
Damien Millera5539d22003-04-09 20:50:06 +1000927 u_int64_t *max_blocks;
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000928 const char *wmsg, *dir;
Damien Miller86687062014-07-02 15:28:02 +1000929 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000930
Ben Lindstrom064496f2002-12-23 02:04:22 +0000931 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000932
Damien Miller963f6b22002-02-19 15:21:23 +1100933 if (mode == MODE_OUT) {
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000934 dir = "output";
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000935 ccp = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000936 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000937 ps = &state->p_send;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000938 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100939 } else {
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000940 dir = "input";
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000941 ccp = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000942 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000943 ps = &state->p_read;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000944 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100945 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000946 if (state->newkeys[mode] != NULL) {
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000947 debug("%s: rekeying after %llu %s blocks"
948 " (%llu bytes total)", __func__,
949 (unsigned long long)ps->blocks, dir,
950 (unsigned long long)ps->bytes);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000951 cipher_free(*ccp);
952 *ccp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000953 enc = &state->newkeys[mode]->enc;
954 mac = &state->newkeys[mode]->mac;
955 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000956 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100957 explicit_bzero(enc->iv, enc->iv_len);
958 explicit_bzero(enc->key, enc->key_len);
959 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000960 free(enc->name);
961 free(enc->iv);
962 free(enc->key);
963 free(mac->name);
964 free(mac->key);
965 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000966 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000967 }
markus@openbsd.org06ce56b2016-09-06 09:22:56 +0000968 /* note that both bytes and the seqnr are not reset */
969 ps->packets = ps->blocks = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000970 /* move newkeys from kex to state */
971 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
972 return SSH_ERR_INTERNAL_ERROR;
973 ssh->kex->newkeys[mode] = NULL;
974 enc = &state->newkeys[mode]->enc;
975 mac = &state->newkeys[mode]->mac;
976 comp = &state->newkeys[mode]->comp;
977 if (cipher_authlen(enc->cipher) == 0) {
978 if ((r = mac_init(mac)) != 0)
979 return r;
980 }
981 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000982 DBG(debug("cipher_init_context: %d", mode));
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000983 if ((r = cipher_init(ccp, enc->cipher, enc->key, enc->key_len,
Damien Miller86687062014-07-02 15:28:02 +1000984 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000985 return r;
986 if (!state->cipher_warning_done &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000987 (wmsg = cipher_warning_message(*ccp)) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000988 error("Warning: %s", wmsg);
989 state->cipher_warning_done = 1;
990 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000991 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100992 /* explicit_bzero(enc->iv, enc->block_size);
993 explicit_bzero(enc->key, enc->key_len);
994 explicit_bzero(mac->key, mac->key_len); */
djm@openbsd.orgb7689152016-09-28 21:44:52 +0000995 if ((comp->type == COMP_ZLIB ||
996 (comp->type == COMP_DELAYED &&
997 state->after_authentication)) && comp->enabled == 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000998 if ((r = ssh_packet_init_compression(ssh)) < 0)
999 return r;
1000 if (mode == MODE_OUT) {
1001 if ((r = start_compression_out(ssh, 6)) != 0)
1002 return r;
1003 } else {
1004 if ((r = start_compression_in(ssh)) != 0)
1005 return r;
1006 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001007 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001008 }
Darren Tucker81a0b372003-07-14 17:31:06 +10001009 /*
1010 * The 2^(blocksize*2) limit is too expensive for 3DES,
1011 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
1012 */
1013 if (enc->block_size >= 16)
1014 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
1015 else
1016 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001017 if (state->rekey_limit)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001018 *max_blocks = MINIMUM(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001019 state->rekey_limit / enc->block_size);
djm@openbsd.org696d1262016-02-04 23:43:48 +00001020 debug("rekey after %llu blocks", (unsigned long long)*max_blocks);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001021 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001022}
1023
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001024#define MAX_PACKETS (1U<<31)
1025static int
1026ssh_packet_need_rekeying(struct ssh *ssh, u_int outbound_packet_len)
1027{
1028 struct session_state *state = ssh->state;
1029 u_int32_t out_blocks;
1030
1031 /* XXX client can't cope with rekeying pre-auth */
1032 if (!state->after_authentication)
1033 return 0;
1034
1035 /* Haven't keyed yet or KEX in progress. */
1036 if (ssh->kex == NULL || ssh_packet_is_rekeying(ssh))
1037 return 0;
1038
1039 /* Peer can't rekey */
1040 if (ssh->compat & SSH_BUG_NOREKEY)
1041 return 0;
1042
1043 /*
1044 * Permit one packet in or out per rekey - this allows us to
1045 * make progress when rekey limits are very small.
1046 */
1047 if (state->p_send.packets == 0 && state->p_read.packets == 0)
1048 return 0;
1049
1050 /* Time-based rekeying */
1051 if (state->rekey_interval != 0 &&
1052 state->rekey_time + state->rekey_interval <= monotime())
1053 return 1;
1054
1055 /* Always rekey when MAX_PACKETS sent in either direction */
1056 if (state->p_send.packets > MAX_PACKETS ||
1057 state->p_read.packets > MAX_PACKETS)
1058 return 1;
1059
1060 /* Rekey after (cipher-specific) maxiumum blocks */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001061 out_blocks = ROUNDUP(outbound_packet_len,
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001062 state->newkeys[MODE_OUT]->enc.block_size);
1063 return (state->max_blocks_out &&
1064 (state->p_send.blocks + out_blocks > state->max_blocks_out)) ||
1065 (state->max_blocks_in &&
1066 (state->p_read.blocks > state->max_blocks_in));
1067}
1068
Damien Miller5428f641999-11-25 11:54:57 +11001069/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001070 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001071 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001072 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1073 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001074static int
1075ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001076{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001077 struct session_state *state = ssh->state;
1078 struct sshcomp *comp = NULL;
1079 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001080
1081 /*
1082 * Remember that we are past the authentication step, so rekeying
1083 * with COMP_DELAYED will turn on compression immediately.
1084 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001085 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001086 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001087 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001088 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001089 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001090 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001091 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001092 if ((r = ssh_packet_init_compression(ssh)) != 0)
1093 return r;
1094 if (mode == MODE_OUT) {
1095 if ((r = start_compression_out(ssh, 6)) != 0)
1096 return r;
1097 } else {
1098 if ((r = start_compression_in(ssh)) != 0)
1099 return r;
1100 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001101 comp->enabled = 1;
1102 }
1103 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001104 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001105}
1106
djm@openbsd.org28136472016-01-29 05:46:01 +00001107/* Used to mute debug logging for noisy packet types */
markus@openbsd.org8d057842016-09-30 09:19:13 +00001108int
djm@openbsd.org28136472016-01-29 05:46:01 +00001109ssh_packet_log_type(u_char type)
1110{
1111 switch (type) {
1112 case SSH2_MSG_CHANNEL_DATA:
1113 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
1114 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
1115 return 0;
1116 default:
1117 return 1;
1118 }
1119}
1120
Damien Miller9786e6e2005-07-26 21:54:56 +10001121/*
Damien Miller33b13562000-04-04 14:38:59 +10001122 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1123 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001124int
1125ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001126{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001127 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001128 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
djm@openbsd.orgeb999a42016-07-18 06:08:01 +00001129 u_char tmp, padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001130 u_int authlen = 0, aadlen = 0;
1131 u_int len;
1132 struct sshenc *enc = NULL;
1133 struct sshmac *mac = NULL;
1134 struct sshcomp *comp = NULL;
1135 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001136
markus@openbsd.org091c3022015-01-19 19:52:16 +00001137 if (state->newkeys[MODE_OUT] != NULL) {
1138 enc = &state->newkeys[MODE_OUT]->enc;
1139 mac = &state->newkeys[MODE_OUT]->mac;
1140 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001141 /* disable mac for authenticated encryption */
1142 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1143 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001144 }
Damien Miller963f6b22002-02-19 15:21:23 +11001145 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001146 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001147
markus@openbsd.org091c3022015-01-19 19:52:16 +00001148 type = (sshbuf_ptr(state->outgoing_packet))[5];
djm@openbsd.org28136472016-01-29 05:46:01 +00001149 if (ssh_packet_log_type(type))
1150 debug3("send packet: type %u", type);
Damien Miller33b13562000-04-04 14:38:59 +10001151#ifdef PACKET_DEBUG
1152 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001153 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001154#endif
1155
1156 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001157 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001158 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001159 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1160 goto out;
1161 sshbuf_reset(state->compression_buffer);
1162 if ((r = compress_buffer(ssh, state->outgoing_packet,
1163 state->compression_buffer)) != 0)
1164 goto out;
1165 sshbuf_reset(state->outgoing_packet);
1166 if ((r = sshbuf_put(state->outgoing_packet,
1167 "\0\0\0\0\0", 5)) != 0 ||
1168 (r = sshbuf_putb(state->outgoing_packet,
1169 state->compression_buffer)) != 0)
1170 goto out;
1171 DBG(debug("compression: raw %d compressed %zd", len,
1172 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001173 }
1174
1175 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001176 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001177
1178 /*
1179 * calc size of padding, alloc space, get random data,
1180 * minimum padding is 4 bytes
1181 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001182 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001183 padlen = block_size - (len % block_size);
1184 if (padlen < 4)
1185 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001186 if (state->extra_pad) {
djm@openbsd.orgeb999a42016-07-18 06:08:01 +00001187 tmp = state->extra_pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001188 state->extra_pad =
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001189 ROUNDUP(state->extra_pad, block_size);
djm@openbsd.orgeb999a42016-07-18 06:08:01 +00001190 /* check if roundup overflowed */
1191 if (state->extra_pad < tmp)
1192 return SSH_ERR_INVALID_ARGUMENT;
1193 tmp = (len + padlen) % state->extra_pad;
1194 /* Check whether pad calculation below will underflow */
1195 if (tmp > state->extra_pad)
1196 return SSH_ERR_INVALID_ARGUMENT;
1197 pad = state->extra_pad - tmp;
Damien Miller2a328432014-04-20 13:24:01 +10001198 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001199 __func__, pad, len, padlen, state->extra_pad));
djm@openbsd.orgeb999a42016-07-18 06:08:01 +00001200 tmp = padlen;
Damien Miller9f643902001-11-12 11:02:52 +11001201 padlen += pad;
djm@openbsd.orgeb999a42016-07-18 06:08:01 +00001202 /* Check whether padlen calculation overflowed */
1203 if (padlen < tmp)
1204 return SSH_ERR_INVALID_ARGUMENT; /* overflow */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001205 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001206 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001207 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1208 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001209 if (enc && !cipher_ctx_is_plaintext(state->send_context)) {
Damien Millere247cc42000-05-07 12:03:14 +10001210 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001211 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001212 } else {
1213 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001214 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001215 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001216 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001217 len = sshbuf_len(state->outgoing_packet);
1218 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1219 if (cp == NULL) {
1220 r = SSH_ERR_INTERNAL_ERROR;
1221 goto out;
1222 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001223 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001224 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001225 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001226 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1227 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001228
1229 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001230 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001231 if ((r = mac_compute(mac, state->p_send.seqnr,
1232 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001233 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001234 goto out;
1235 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001236 }
1237 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001238 if ((r = sshbuf_reserve(state->output,
1239 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1240 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001241 if ((r = cipher_crypt(state->send_context, state->p_send.seqnr, cp,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001242 sshbuf_ptr(state->outgoing_packet),
1243 len - aadlen, aadlen, authlen)) != 0)
1244 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001245 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001246 if (mac && mac->enabled) {
1247 if (mac->etm) {
1248 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001249 if ((r = mac_compute(mac, state->p_send.seqnr,
1250 cp, len, macbuf, sizeof(macbuf))) != 0)
1251 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001252 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001253 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001254 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001255 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1256 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001257 }
Damien Miller33b13562000-04-04 14:38:59 +10001258#ifdef PACKET_DEBUG
1259 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001260 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001261#endif
Damien Miller4af51302000-04-16 11:18:38 +10001262 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001263 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001264 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001265 if (++state->p_send.packets == 0)
1266 if (!(ssh->compat & SSH_BUG_NOREKEY))
1267 return SSH_ERR_NEED_REKEY;
1268 state->p_send.blocks += len / block_size;
1269 state->p_send.bytes += len;
1270 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001271
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001272 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001273 r = ssh_set_newkeys(ssh, MODE_OUT);
1274 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1275 r = ssh_packet_enable_delayed_compress(ssh);
1276 else
1277 r = 0;
1278 out:
1279 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001280}
1281
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001282/* returns non-zero if the specified packet type is usec by KEX */
1283static int
1284ssh_packet_type_is_kex(u_char type)
1285{
1286 return
1287 type >= SSH2_MSG_TRANSPORT_MIN &&
1288 type <= SSH2_MSG_TRANSPORT_MAX &&
1289 type != SSH2_MSG_SERVICE_REQUEST &&
1290 type != SSH2_MSG_SERVICE_ACCEPT &&
1291 type != SSH2_MSG_EXT_INFO;
1292}
1293
markus@openbsd.org091c3022015-01-19 19:52:16 +00001294int
1295ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001296{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001297 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001298 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001299 u_char type;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001300 int r, need_rekey;
Damien Millera5539d22003-04-09 20:50:06 +10001301
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001302 if (sshbuf_len(state->outgoing_packet) < 6)
1303 return SSH_ERR_INTERNAL_ERROR;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001304 type = sshbuf_ptr(state->outgoing_packet)[5];
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001305 need_rekey = !ssh_packet_type_is_kex(type) &&
1306 ssh_packet_need_rekeying(ssh, sshbuf_len(state->outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +10001307
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001308 /*
1309 * During rekeying we can only send key exchange messages.
1310 * Queue everything else.
1311 */
1312 if ((need_rekey || state->rekeying) && !ssh_packet_type_is_kex(type)) {
1313 if (need_rekey)
1314 debug3("%s: rekex triggered", __func__);
1315 debug("enqueue packet: %u", type);
1316 p = calloc(1, sizeof(*p));
1317 if (p == NULL)
1318 return SSH_ERR_ALLOC_FAIL;
1319 p->type = type;
1320 p->payload = state->outgoing_packet;
1321 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1322 state->outgoing_packet = sshbuf_new();
1323 if (state->outgoing_packet == NULL)
1324 return SSH_ERR_ALLOC_FAIL;
1325 if (need_rekey) {
1326 /*
1327 * This packet triggered a rekey, so send the
1328 * KEXINIT now.
1329 * NB. reenters this function via kex_start_rekex().
1330 */
1331 return kex_start_rekex(ssh);
Damien Millera5539d22003-04-09 20:50:06 +10001332 }
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001333 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001334 }
1335
1336 /* rekeying starts with sending KEXINIT */
1337 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001338 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001339
markus@openbsd.org091c3022015-01-19 19:52:16 +00001340 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1341 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001342
1343 /* after a NEWKEYS message we can send the complete queue */
1344 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001345 state->rekeying = 0;
1346 state->rekey_time = monotime();
1347 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001348 type = p->type;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001349 /*
1350 * If this packet triggers a rekex, then skip the
1351 * remaining packets in the queue for now.
1352 * NB. re-enters this function via kex_start_rekex.
1353 */
1354 if (ssh_packet_need_rekeying(ssh,
1355 sshbuf_len(p->payload))) {
1356 debug3("%s: queued packet triggered rekex",
1357 __func__);
1358 return kex_start_rekex(ssh);
1359 }
Damien Millera5539d22003-04-09 20:50:06 +10001360 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001361 sshbuf_free(state->outgoing_packet);
1362 state->outgoing_packet = p->payload;
1363 TAILQ_REMOVE(&state->outgoing, p, next);
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001364 memset(p, 0, sizeof(*p));
Darren Tuckera627d422013-06-02 07:31:17 +10001365 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001366 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1367 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001368 }
1369 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001370 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001371}
1372
Damien Miller33b13562000-04-04 14:38:59 +10001373/*
Damien Miller5428f641999-11-25 11:54:57 +11001374 * Waits until a packet has been received, and returns its type. Note that
1375 * no other data is processed until this returns, so this function should not
1376 * be used during the interactive session.
1377 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378
1379int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001380ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001381{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001382 struct session_state *state = ssh->state;
markus@openbsd.orga3068632016-01-14 16:17:39 +00001383 int len, r, ms_remain;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001384 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001385 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001386 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
Darren Tucker99bb7612008-06-13 22:02:50 +10001388 DBG(debug("packet_read()"));
1389
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001390 setp = calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001391 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001392 if (setp == NULL)
1393 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001394
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001395 /*
1396 * Since we are blocking, ensure that all written packets have
1397 * been sent.
1398 */
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001399 if ((r = ssh_packet_write_wait(ssh)) != 0)
1400 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001401
Damien Miller95def091999-11-25 00:26:21 +11001402 /* Stay in the loop until we have received a complete packet. */
1403 for (;;) {
1404 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001405 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1406 if (r != 0)
1407 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001408 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001409 *typep == SSH_SMSG_SUCCESS
1410 || *typep == SSH_SMSG_FAILURE
1411 || *typep == SSH_CMSG_EOF
1412 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1413 if ((r = sshpkt_get_end(ssh)) != 0)
1414 break;
Damien Miller95def091999-11-25 00:26:21 +11001415 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001416 if (*typep != SSH_MSG_NONE)
1417 break;
Damien Miller5428f641999-11-25 11:54:57 +11001418 /*
1419 * Otherwise, wait for some data to arrive, add it to the
1420 * buffer, and try again.
1421 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001422 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001423 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001424 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001425
markus@openbsd.org091c3022015-01-19 19:52:16 +00001426 if (state->packet_timeout_ms > 0) {
1427 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001428 timeoutp = &timeout;
1429 }
Damien Miller95def091999-11-25 00:26:21 +11001430 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001431 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001432 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001433 ms_to_timeval(&timeout, ms_remain);
1434 gettimeofday(&start, NULL);
1435 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001436 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001437 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001438 break;
Damien Millerea437422009-10-02 11:49:03 +10001439 if (errno != EAGAIN && errno != EINTR &&
1440 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001441 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001442 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001443 continue;
1444 ms_subtract_diff(&start, &ms_remain);
1445 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001446 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001447 break;
1448 }
1449 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001450 if (r == 0)
1451 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001452 /* Read data from the socket. */
markus@openbsd.orga3068632016-01-14 16:17:39 +00001453 len = read(state->connection_in, buf, sizeof(buf));
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001454 if (len == 0) {
1455 r = SSH_ERR_CONN_CLOSED;
1456 goto out;
1457 }
1458 if (len < 0) {
1459 r = SSH_ERR_SYSTEM_ERROR;
1460 goto out;
1461 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001462
Damien Miller95def091999-11-25 00:26:21 +11001463 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001464 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001465 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001466 }
markus@openbsd.org4daeb672015-03-24 20:10:08 +00001467 out:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001468 free(setp);
1469 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470}
1471
Damien Miller278f9072001-12-21 15:00:19 +11001472int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001473ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001474{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001475 u_char type;
1476 int r;
1477
1478 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1479 fatal("%s: %s", __func__, ssh_err(r));
1480 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001481}
1482
Damien Miller5428f641999-11-25 11:54:57 +11001483/*
1484 * Waits until a packet has been received, verifies that its type matches
1485 * that given, and gives a fatal error and exits if there is a mismatch.
1486 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001487
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001488int
1489ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490{
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001491 int r;
1492 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001494 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1495 return r;
1496 if (type != expected_type) {
1497 if ((r = sshpkt_disconnect(ssh,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001498 "Protocol error: expected packet type %d, got %d",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001499 expected_type, type)) != 0)
1500 return r;
1501 return SSH_ERR_PROTOCOL_ERROR;
1502 }
1503 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504}
1505
1506/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001507 * packet_process_incoming. If so, reads the packet; otherwise returns
1508 * SSH_MSG_NONE. This does not wait for data from the connection.
1509 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001510 * SSH_MSG_DISCONNECT is handled specially here. Also,
1511 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1512 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001513 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001514
markus@openbsd.org091c3022015-01-19 19:52:16 +00001515int
1516ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001518 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001519 u_int len, padded_len;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001520 const char *emsg;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001521 const u_char *cp;
1522 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001523 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001524 int r;
1525
1526 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001527
Damien Miller95def091999-11-25 00:26:21 +11001528 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001529 if (sshbuf_len(state->input) < 4 + 8)
1530 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001531 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001532 len = PEEK_U32(sshbuf_ptr(state->input));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001533 if (len < 1 + 2 + 2 || len > 256 * 1024) {
1534 if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
1535 len)) != 0)
1536 return r;
1537 return SSH_ERR_CONN_CORRUPT;
1538 }
Damien Miller95def091999-11-25 00:26:21 +11001539 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001540
Damien Miller95def091999-11-25 00:26:21 +11001541 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001542 if (sshbuf_len(state->input) < 4 + padded_len)
1543 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001544
Damien Miller95def091999-11-25 00:26:21 +11001545 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001546
Damien Miller95def091999-11-25 00:26:21 +11001547 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001548 if ((r = sshbuf_consume(state->input, 4)) != 0)
1549 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001550
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001551 /*
1552 * Cryptographic attack detector for ssh
1553 * (C)1998 CORE-SDI, Buenos Aires Argentina
1554 * Ariel Futoransky(futo@core-sdi.com)
1555 */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001556 if (!cipher_ctx_is_plaintext(state->receive_context)) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001557 emsg = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001558 switch (detect_attack(&state->deattack,
1559 sshbuf_ptr(state->input), padded_len)) {
1560 case DEATTACK_OK:
1561 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001562 case DEATTACK_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001563 emsg = "crc32 compensation attack detected";
1564 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001565 case DEATTACK_DOS_DETECTED:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001566 emsg = "deattack denial of service detected";
1567 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001568 default:
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001569 emsg = "deattack error";
1570 break;
1571 }
1572 if (emsg != NULL) {
1573 error("%s", emsg);
1574 if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
1575 (r = ssh_packet_write_wait(ssh)) != 0)
1576 return r;
1577 return SSH_ERR_CONN_CORRUPT;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001578 }
1579 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001580
1581 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001582 sshbuf_reset(state->incoming_packet);
1583 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1584 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001585 if ((r = cipher_crypt(state->receive_context, 0, p,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001586 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1587 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001588
markus@openbsd.org091c3022015-01-19 19:52:16 +00001589 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1590 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001591
1592#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001593 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001594 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001595#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001596
Damien Miller95def091999-11-25 00:26:21 +11001597 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001598 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1599 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001600
Damien Miller95def091999-11-25 00:26:21 +11001601 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001602 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1603 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001604
Damien Miller95def091999-11-25 00:26:21 +11001605 /* Test check bytes. */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001606 if (len != sshbuf_len(state->incoming_packet)) {
1607 error("%s: len %d != sshbuf_len %zd", __func__,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001608 len, sshbuf_len(state->incoming_packet));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001609 if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
1610 (r = ssh_packet_write_wait(ssh)) != 0)
1611 return r;
1612 return SSH_ERR_CONN_CORRUPT;
1613 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001614
markus@openbsd.org091c3022015-01-19 19:52:16 +00001615 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1616 stored_checksum = PEEK_U32(cp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001617 if (checksum != stored_checksum) {
1618 error("Corrupted check bytes on input");
1619 if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
1620 (r = ssh_packet_write_wait(ssh)) != 0)
1621 return r;
1622 return SSH_ERR_CONN_CORRUPT;
1623 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001624 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1625 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001626
markus@openbsd.org091c3022015-01-19 19:52:16 +00001627 if (state->packet_compression) {
1628 sshbuf_reset(state->compression_buffer);
1629 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1630 state->compression_buffer)) != 0)
1631 goto out;
1632 sshbuf_reset(state->incoming_packet);
1633 if ((r = sshbuf_putb(state->incoming_packet,
1634 state->compression_buffer)) != 0)
1635 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001636 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001637 state->p_read.packets++;
1638 state->p_read.bytes += padded_len + 4;
1639 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1640 goto out;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001641 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
1642 error("Invalid ssh1 packet type: %d", *typep);
1643 if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
1644 (r = ssh_packet_write_wait(ssh)) != 0)
1645 return r;
1646 return SSH_ERR_PROTOCOL_ERROR;
1647 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001648 r = 0;
1649 out:
1650 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001651}
Damien Miller95def091999-11-25 00:26:21 +11001652
markus@openbsd.org8d057842016-09-30 09:19:13 +00001653static int
1654ssh_packet_read_poll2_mux(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
1655{
1656 struct session_state *state = ssh->state;
1657 const u_char *cp;
1658 size_t need;
1659 int r;
1660
1661 if (ssh->kex)
1662 return SSH_ERR_INTERNAL_ERROR;
1663 *typep = SSH_MSG_NONE;
1664 cp = sshbuf_ptr(state->input);
1665 if (state->packlen == 0) {
1666 if (sshbuf_len(state->input) < 4 + 1)
1667 return 0; /* packet is incomplete */
1668 state->packlen = PEEK_U32(cp);
1669 if (state->packlen < 4 + 1 ||
1670 state->packlen > PACKET_MAX_SIZE)
1671 return SSH_ERR_MESSAGE_INCOMPLETE;
1672 }
1673 need = state->packlen + 4;
1674 if (sshbuf_len(state->input) < need)
1675 return 0; /* packet is incomplete */
1676 sshbuf_reset(state->incoming_packet);
1677 if ((r = sshbuf_put(state->incoming_packet, cp + 4,
1678 state->packlen)) != 0 ||
1679 (r = sshbuf_consume(state->input, need)) != 0 ||
1680 (r = sshbuf_get_u8(state->incoming_packet, NULL)) != 0 ||
1681 (r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1682 return r;
1683 if (ssh_packet_log_type(*typep))
1684 debug3("%s: type %u", __func__, *typep);
1685 /* sshbuf_dump(state->incoming_packet, stderr); */
1686 /* reset for next packet */
1687 state->packlen = 0;
1688 return r;
1689}
1690
markus@openbsd.org091c3022015-01-19 19:52:16 +00001691int
1692ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001693{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001694 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001695 u_int padlen, need;
djm@openbsd.org6d311932016-07-08 03:44:42 +00001696 u_char *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001697 u_int maclen, aadlen = 0, authlen = 0, block_size;
1698 struct sshenc *enc = NULL;
1699 struct sshmac *mac = NULL;
1700 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001701 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001702
markus@openbsd.org8d057842016-09-30 09:19:13 +00001703 if (state->mux)
1704 return ssh_packet_read_poll2_mux(ssh, typep, seqnr_p);
1705
markus@openbsd.org091c3022015-01-19 19:52:16 +00001706 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001707
markus@openbsd.org091c3022015-01-19 19:52:16 +00001708 if (state->packet_discard)
1709 return 0;
1710
1711 if (state->newkeys[MODE_IN] != NULL) {
1712 enc = &state->newkeys[MODE_IN]->enc;
1713 mac = &state->newkeys[MODE_IN]->mac;
1714 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001715 /* disable mac for authenticated encryption */
1716 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1717 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001718 }
1719 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001720 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001721 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001722
markus@openbsd.org091c3022015-01-19 19:52:16 +00001723 if (aadlen && state->packlen == 0) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001724 if (cipher_get_length(state->receive_context,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001725 &state->packlen, state->p_read.seqnr,
1726 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1727 return 0;
1728 if (state->packlen < 1 + 4 ||
1729 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001730#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001731 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001732#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001733 logit("Bad packet length %u.", state->packlen);
1734 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1735 return r;
djm@openbsd.org2fecfd42015-11-08 21:59:11 +00001736 return SSH_ERR_CONN_CORRUPT;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001737 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001738 sshbuf_reset(state->incoming_packet);
1739 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001740 /*
1741 * check if input size is less than the cipher block size,
1742 * decrypt first block and extract length of incoming packet
1743 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001744 if (sshbuf_len(state->input) < block_size)
1745 return 0;
1746 sshbuf_reset(state->incoming_packet);
1747 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1748 &cp)) != 0)
1749 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001750 if ((r = cipher_crypt(state->receive_context,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001751 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1752 block_size, 0, 0)) != 0)
1753 goto out;
1754 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1755 if (state->packlen < 1 + 4 ||
1756 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001757#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001758 fprintf(stderr, "input: \n");
1759 sshbuf_dump(state->input, stderr);
1760 fprintf(stderr, "incoming_packet: \n");
1761 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001762#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001763 logit("Bad packet length %u.", state->packlen);
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +00001764 return ssh_packet_start_discard(ssh, enc, mac, 0,
1765 PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001766 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001767 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1768 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001769 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001770 DBG(debug("input: packet len %u", state->packlen+4));
1771
Damien Milleraf43a7a2012-12-12 10:46:31 +11001772 if (aadlen) {
1773 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001774 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001775 } else {
1776 /*
1777 * the payload size and the payload are encrypted, but we
1778 * have a partial packet of block_size bytes
1779 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001780 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001781 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001782 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1783 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001784 if (need % block_size != 0) {
1785 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001786 need, block_size, need % block_size);
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +00001787 return ssh_packet_start_discard(ssh, enc, mac, 0,
1788 PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001789 }
Damien Miller33b13562000-04-04 14:38:59 +10001790 /*
1791 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001792 * decrypt into incoming_packet:
1793 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001794 * 'need' bytes are encrypted, followed by either
1795 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001796 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001797 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001798 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
djm@openbsd.org6d311932016-07-08 03:44:42 +00001799 return 0; /* packet is incomplete */
Damien Miller33b13562000-04-04 14:38:59 +10001800#ifdef PACKET_DEBUG
1801 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001802 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001803#endif
djm@openbsd.org6d311932016-07-08 03:44:42 +00001804 /* EtM: check mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001805 if (mac && mac->enabled && mac->etm) {
djm@openbsd.org6d311932016-07-08 03:44:42 +00001806 if ((r = mac_check(mac, state->p_read.seqnr,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001807 sshbuf_ptr(state->input), aadlen + need,
djm@openbsd.org6d311932016-07-08 03:44:42 +00001808 sshbuf_ptr(state->input) + aadlen + need + authlen,
1809 maclen)) != 0) {
1810 if (r == SSH_ERR_MAC_INVALID)
1811 logit("Corrupted MAC on input.");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001812 goto out;
djm@openbsd.org6d311932016-07-08 03:44:42 +00001813 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001814 }
1815 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1816 &cp)) != 0)
1817 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001818 if ((r = cipher_crypt(state->receive_context, state->p_read.seqnr, cp,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001819 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1820 goto out;
1821 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1822 goto out;
Damien Miller4af51302000-04-16 11:18:38 +10001823 if (mac && mac->enabled) {
djm@openbsd.org6d311932016-07-08 03:44:42 +00001824 /* Not EtM: check MAC over cleartext */
1825 if (!mac->etm && (r = mac_check(mac, state->p_read.seqnr,
1826 sshbuf_ptr(state->incoming_packet),
1827 sshbuf_len(state->incoming_packet),
1828 sshbuf_ptr(state->input), maclen)) != 0) {
1829 if (r != SSH_ERR_MAC_INVALID)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001830 goto out;
Damien Miller13ae44c2009-01-28 16:38:41 +11001831 logit("Corrupted MAC on input.");
1832 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001833 return SSH_ERR_INTERNAL_ERROR;
1834 return ssh_packet_start_discard(ssh, enc, mac,
markus@openbsd.orgb98a2a82016-07-18 11:35:33 +00001835 sshbuf_len(state->incoming_packet),
1836 PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001837 }
djm@openbsd.org6d311932016-07-08 03:44:42 +00001838 /* Remove MAC from input buffer */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001839 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1840 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1841 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001842 }
Damien Miller278f9072001-12-21 15:00:19 +11001843 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001844 *seqnr_p = state->p_read.seqnr;
1845 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001846 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001847 if (++state->p_read.packets == 0)
1848 if (!(ssh->compat & SSH_BUG_NOREKEY))
1849 return SSH_ERR_NEED_REKEY;
1850 state->p_read.blocks += (state->packlen + 4) / block_size;
1851 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001852
1853 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001854 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001855 DBG(debug("input: padlen %d", padlen));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001856 if (padlen < 4) {
1857 if ((r = sshpkt_disconnect(ssh,
1858 "Corrupted padlen %d on input.", padlen)) != 0 ||
1859 (r = ssh_packet_write_wait(ssh)) != 0)
1860 return r;
1861 return SSH_ERR_CONN_CORRUPT;
1862 }
Damien Miller33b13562000-04-04 14:38:59 +10001863
1864 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001865 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1866 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1867 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001868
markus@openbsd.org091c3022015-01-19 19:52:16 +00001869 DBG(debug("input: len before de-compress %zd",
1870 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001871 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001872 sshbuf_reset(state->compression_buffer);
1873 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1874 state->compression_buffer)) != 0)
1875 goto out;
1876 sshbuf_reset(state->incoming_packet);
1877 if ((r = sshbuf_putb(state->incoming_packet,
1878 state->compression_buffer)) != 0)
1879 goto out;
1880 DBG(debug("input: len after de-compress %zd",
1881 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001882 }
1883 /*
1884 * get packet type, implies consume.
1885 * return length of payload (without type field)
1886 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001887 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1888 goto out;
djm@openbsd.org28136472016-01-29 05:46:01 +00001889 if (ssh_packet_log_type(*typep))
1890 debug3("receive packet: type %u", *typep);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00001891 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
1892 if ((r = sshpkt_disconnect(ssh,
1893 "Invalid ssh2 packet type: %d", *typep)) != 0 ||
1894 (r = ssh_packet_write_wait(ssh)) != 0)
1895 return r;
1896 return SSH_ERR_PROTOCOL_ERROR;
1897 }
djm@openbsd.org39af7b42016-10-11 21:47:45 +00001898 if (state->hook_in != NULL &&
1899 (r = state->hook_in(ssh, state->incoming_packet, typep,
1900 state->hook_in_ctx)) != 0)
1901 return r;
markus@openbsd.org28652bc2016-09-19 19:02:19 +00001902 if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001903 r = ssh_packet_enable_delayed_compress(ssh);
1904 else
1905 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001906#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001907 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1908 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001909#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001910 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001911 state->packlen = 0;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001912
1913 /* do we need to rekey? */
1914 if (ssh_packet_need_rekeying(ssh, 0)) {
1915 debug3("%s: rekex triggered", __func__);
1916 if ((r = kex_start_rekex(ssh)) != 0)
1917 return r;
1918 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001919 out:
1920 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001921}
1922
1923int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001924ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001925{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001926 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001927 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001928 int r;
1929 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001930
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001931 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001932 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001933 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001934 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1935 if (r != 0)
1936 return r;
1937 if (*typep) {
1938 state->keep_alive_timeouts = 0;
1939 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001940 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001941 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001942 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001943 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001944 break;
Damien Miller33b13562000-04-04 14:38:59 +10001945 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001946 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1947 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1948 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001949 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001950 return r;
1951 }
Damien Miller33b13562000-04-04 14:38:59 +10001952 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001953 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001954 break;
1955 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001956 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1957 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1958 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001959 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001960 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001961 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1962 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001963 "Received disconnect from %s port %d:"
1964 "%u: %.400s", ssh_remote_ipaddr(ssh),
1965 ssh_remote_port(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001966 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001967 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001968 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001969 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1970 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001971 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1972 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001973 break;
Damien Miller33b13562000-04-04 14:38:59 +10001974 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001975 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001976 }
Damien Miller33b13562000-04-04 14:38:59 +10001977 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001978 r = ssh_packet_read_poll1(ssh, typep);
1979 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001980 case SSH_MSG_NONE:
1981 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001982 case SSH_MSG_IGNORE:
1983 break;
1984 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001985 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1986 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001987 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001988 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001989 break;
1990 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001991 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1992 return r;
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00001993 error("Received disconnect from %s port %d: "
1994 "%.400s", ssh_remote_ipaddr(ssh),
1995 ssh_remote_port(ssh), msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001996 free(msg);
1997 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001998 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001999 DBG(debug("received packet type %d", *typep));
2000 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00002001 }
Damien Miller33b13562000-04-04 14:38:59 +10002002 }
2003 }
2004}
2005
Damien Miller5428f641999-11-25 11:54:57 +11002006/*
2007 * Buffers the given amount of input characters. This is intended to be used
2008 * together with packet_read_poll.
2009 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002010
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002011int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002012ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002013{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002014 struct session_state *state = ssh->state;
2015 int r;
2016
2017 if (state->packet_discard) {
2018 state->keep_alive_timeouts = 0; /* ?? */
2019 if (len >= state->packet_discard) {
2020 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002021 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002022 }
2023 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002024 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11002025 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002026 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00002027 return r;
2028
2029 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002030}
2031
Damien Miller4af51302000-04-16 11:18:38 +10002032int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002033ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10002034{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002035 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10002036}
2037
Damien Miller5428f641999-11-25 11:54:57 +11002038/*
2039 * Sends a diagnostic message from the server to the client. This message
2040 * can be sent at any time (but not while constructing another message). The
2041 * message is printed immediately, but only if the client is being executed
2042 * in verbose mode. These messages are primarily intended to ease debugging
2043 * authentication problems. The length of the formatted message must not
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002044 * exceed 1024 bytes. This will automatically call ssh_packet_write_wait.
Damien Miller5428f641999-11-25 11:54:57 +11002045 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002046void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002047ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002048{
Damien Miller95def091999-11-25 00:26:21 +11002049 char buf[1024];
2050 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002051 int r;
Damien Miller95def091999-11-25 00:26:21 +11002052
markus@openbsd.org091c3022015-01-19 19:52:16 +00002053 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00002054 return;
2055
Damien Miller95def091999-11-25 00:26:21 +11002056 va_start(args, fmt);
2057 vsnprintf(buf, sizeof(buf), fmt, args);
2058 va_end(args);
2059
Damien Miller7c8af4f2000-05-01 08:24:07 +10002060 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002061 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
2062 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
2063 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2064 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2065 (r = sshpkt_send(ssh)) != 0)
2066 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10002067 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002068 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
2069 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2070 (r = sshpkt_send(ssh)) != 0)
2071 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10002072 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002073 if ((r = ssh_packet_write_wait(ssh)) != 0)
2074 fatal("%s: %s", __func__, ssh_err(r));
2075}
2076
2077/*
2078 * Pretty-print connection-terminating errors and exit.
2079 */
2080void
2081sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
2082{
2083 switch (r) {
2084 case SSH_ERR_CONN_CLOSED:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002085 logdie("Connection closed by %.200s port %d",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002086 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002087 case SSH_ERR_CONN_TIMEOUT:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002088 logdie("Connection %s %.200s port %d timed out",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002089 ssh->state->server_side ? "from" : "to",
2090 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002091 case SSH_ERR_DISCONNECTED:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002092 logdie("Disconnected from %.200s port %d",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002093 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002094 case SSH_ERR_SYSTEM_ERROR:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002095 if (errno == ECONNRESET)
2096 logdie("Connection reset by %.200s port %d",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002097 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org639d6bc2015-05-01 07:10:01 +00002098 /* FALLTHROUGH */
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002099 case SSH_ERR_NO_CIPHER_ALG_MATCH:
2100 case SSH_ERR_NO_MAC_ALG_MATCH:
2101 case SSH_ERR_NO_COMPRESS_ALG_MATCH:
2102 case SSH_ERR_NO_KEX_ALG_MATCH:
2103 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
2104 if (ssh && ssh->kex && ssh->kex->failed_choice) {
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002105 logdie("Unable to negotiate with %.200s port %d: %s. "
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002106 "Their offer: %s", ssh_remote_ipaddr(ssh),
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002107 ssh_remote_port(ssh), ssh_err(r),
2108 ssh->kex->failed_choice);
djm@openbsd.orgf3199122015-07-29 04:43:06 +00002109 }
2110 /* FALLTHROUGH */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002111 default:
dtucker@openbsd.orgaf1f0842016-07-15 05:01:58 +00002112 logdie("%s%sConnection %s %.200s port %d: %s",
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002113 tag != NULL ? tag : "", tag != NULL ? ": " : "",
djm@openbsd.orga4b9e0f2015-12-11 03:24:25 +00002114 ssh->state->server_side ? "from" : "to",
2115 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002116 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002117}
2118
Damien Miller5428f641999-11-25 11:54:57 +11002119/*
2120 * Logs the error plus constructs and sends a disconnect packet, closes the
2121 * connection, and exits. This function never returns. The error message
2122 * should not contain a newline. The length of the formatted message must
2123 * not exceed 1024 bytes.
2124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002125void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002126ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002127{
Damien Miller95def091999-11-25 00:26:21 +11002128 char buf[1024];
2129 va_list args;
2130 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002131 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002132
Damien Miller95def091999-11-25 00:26:21 +11002133 if (disconnecting) /* Guard against recursive invocations. */
2134 fatal("packet_disconnect called recursively.");
2135 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002136
Damien Miller5428f641999-11-25 11:54:57 +11002137 /*
2138 * Format the message. Note that the caller must make sure the
2139 * message is of limited size.
2140 */
Damien Miller95def091999-11-25 00:26:21 +11002141 va_start(args, fmt);
2142 vsnprintf(buf, sizeof(buf), fmt, args);
2143 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002144
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002145 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10002146 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00002147
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002148 /*
2149 * Send the disconnect message to the other side, and wait
2150 * for it to get sent.
2151 */
2152 if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
2153 sshpkt_fatal(ssh, __func__, r);
2154
2155 if ((r = ssh_packet_write_wait(ssh)) != 0)
2156 sshpkt_fatal(ssh, __func__, r);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002157
Damien Miller95def091999-11-25 00:26:21 +11002158 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002159 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002160 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002161}
2162
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002163/*
2164 * Checks if there is any buffered output, and tries to write some of
2165 * the output.
2166 */
2167int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002168ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002169{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002170 struct session_state *state = ssh->state;
2171 int len = sshbuf_len(state->output);
markus@openbsd.orga3068632016-01-14 16:17:39 +00002172 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002173
Damien Miller95def091999-11-25 00:26:21 +11002174 if (len > 0) {
markus@openbsd.orga3068632016-01-14 16:17:39 +00002175 len = write(state->connection_out,
2176 sshbuf_ptr(state->output), len);
Damien Millerd874fa52008-07-05 09:40:56 +10002177 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10002178 if (errno == EINTR || errno == EAGAIN ||
2179 errno == EWOULDBLOCK)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002180 return 0;
2181 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +11002182 }
markus@openbsd.orga3068632016-01-14 16:17:39 +00002183 if (len == 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002184 return SSH_ERR_CONN_CLOSED;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002185 if ((r = sshbuf_consume(state->output, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002186 return r;
Damien Miller95def091999-11-25 00:26:21 +11002187 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002188 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002189}
2190
Damien Miller5428f641999-11-25 11:54:57 +11002191/*
2192 * Calls packet_write_poll repeatedly until all pending output data has been
2193 * written.
2194 */
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002195int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002196ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002197{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002198 fd_set *setp;
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002199 int ret, r, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002200 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002201 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00002202
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00002203 setp = calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002204 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002205 if (setp == NULL)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002206 return SSH_ERR_ALLOC_FAIL;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002207 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2208 free(setp);
djm@openbsd.org84082182015-09-21 04:31:00 +00002209 return r;
gsoares@openbsd.org66d2e222015-10-21 11:33:03 +00002210 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002211 while (ssh_packet_have_data_to_write(ssh)) {
2212 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002213 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00002214 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10002215
markus@openbsd.org091c3022015-01-19 19:52:16 +00002216 if (state->packet_timeout_ms > 0) {
2217 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002218 timeoutp = &timeout;
2219 }
2220 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002221 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10002222 ms_to_timeval(&timeout, ms_remain);
2223 gettimeofday(&start, NULL);
2224 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002225 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10002226 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002227 break;
Damien Millerea437422009-10-02 11:49:03 +10002228 if (errno != EAGAIN && errno != EINTR &&
2229 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002230 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002231 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10002232 continue;
2233 ms_subtract_diff(&start, &ms_remain);
2234 if (ms_remain <= 0) {
2235 ret = 0;
2236 break;
2237 }
2238 }
2239 if (ret == 0) {
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002240 free(setp);
2241 return SSH_ERR_CONN_TIMEOUT;
Darren Tucker3fc464e2008-06-13 06:42:45 +10002242 }
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002243 if ((r = ssh_packet_write_poll(ssh)) != 0) {
2244 free(setp);
2245 return r;
2246 }
Damien Miller95def091999-11-25 00:26:21 +11002247 }
Darren Tuckera627d422013-06-02 07:31:17 +10002248 free(setp);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +00002249 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002250}
2251
2252/* Returns true if there is buffered data to write to the connection. */
2253
2254int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002255ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002256{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002257 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002258}
2259
2260/* Returns true if there is not too much data to write to the connection. */
2261
2262int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002263ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002264{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002265 if (ssh->state->interactive_mode)
2266 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002267 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002268 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002269}
2270
markus@openbsd.org091c3022015-01-19 19:52:16 +00002271void
2272ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002273{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002274#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002275 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002276 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002277 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002278# ifdef IP_TOS
2279 case AF_INET:
2280 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002281 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002282 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2283 error("setsockopt IP_TOS %d: %.100s:",
2284 tos, strerror(errno));
2285 break;
2286# endif /* IP_TOS */
2287# ifdef IPV6_TCLASS
2288 case AF_INET6:
2289 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002290 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002291 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2292 error("setsockopt IPV6_TCLASS %d: %.100s:",
2293 tos, strerror(errno));
2294 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002295# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002296 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002297#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002298}
Ben Lindstroma7433982002-12-23 02:41:41 +00002299
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002300/* Informs that the current session is interactive. Sets IP flags for that. */
2301
2302void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002303ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002304{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002305 struct session_state *state = ssh->state;
2306
2307 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002308 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002309 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002310
Damien Miller95def091999-11-25 00:26:21 +11002311 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002312 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002313
Damien Miller34132e52000-01-14 15:45:46 +11002314 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002315 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002316 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002317 set_nodelay(state->connection_in);
2318 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2319 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002320}
2321
2322/* Returns true if the current connection is interactive. */
2323
2324int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002325ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002326{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002327 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002328}
Damien Miller6162d121999-11-21 13:23:52 +11002329
Darren Tucker1f8311c2004-05-13 16:39:33 +10002330int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002331ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002332{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002333 struct session_state *state = ssh->state;
2334
2335 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002336 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002337 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002338 return -1;
2339 }
2340 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002341 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002342 return -1;
2343 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002344 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002345 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002346 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002347 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002348}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002349
Darren Tuckerf7288d72009-06-21 18:12:20 +10002350int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002351ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002352{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002353 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002354}
2355
2356void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002357ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002358{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002359 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002360}
2361
2362u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002363ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002364{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002365 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002366}
2367
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002368/*
2369 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002370 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002371 * byte SSH_MSG_IGNORE
2372 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002373 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002374 * All implementations MUST understand (and ignore) this message at any
2375 * time (after receiving the protocol version). No implementation is
2376 * required to send them. This message can be used as an additional
2377 * protection measure against advanced traffic analysis techniques.
2378 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002379void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002380ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002381{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002382 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002383 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002384
markus@openbsd.org091c3022015-01-19 19:52:16 +00002385 if ((r = sshpkt_start(ssh, compat20 ?
2386 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2387 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2388 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002389 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002390 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002391 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002392 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2393 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002394 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002395 }
2396}
Damien Millera5539d22003-04-09 20:50:06 +10002397
Damien Millera5539d22003-04-09 20:50:06 +10002398void
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002399ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002400{
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002401 debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes,
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002402 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002403 ssh->state->rekey_limit = bytes;
2404 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002405}
2406
2407time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002408ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002409{
2410 time_t seconds;
2411
markus@openbsd.org091c3022015-01-19 19:52:16 +00002412 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002413 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002414 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002415}
Damien Miller9786e6e2005-07-26 21:54:56 +10002416
2417void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002418ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002419{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002420 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002421}
2422
2423void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002424ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002425{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002426 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002427}
2428
2429void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002430ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002431{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002432 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002433}
2434
2435void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002436ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002437{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002438 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002439}
2440
Damien Millerc31a0cd2014-05-15 14:37:39 +10002441/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002442static int
2443ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002444{
djm@openbsd.org0082fba2016-09-28 16:33:06 +00002445 int r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002446
2447 debug("%s: called", __func__);
2448 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002449 ssh->state->after_authentication = 1;
2450 ssh->state->rekeying = 0;
djm@openbsd.org0082fba2016-09-28 16:33:06 +00002451 if ((r = ssh_packet_enable_delayed_compress(ssh)) != 0)
2452 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002453 return 0;
2454}
2455
2456/* Packet state (de-)serialization for privsep */
2457
2458/* turn kex into a blob for packet state serialization */
2459static int
2460kex_to_blob(struct sshbuf *m, struct kex *kex)
2461{
2462 int r;
2463
2464 if ((r = sshbuf_put_string(m, kex->session_id,
2465 kex->session_id_len)) != 0 ||
2466 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2467 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2468 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2469 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2470 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2471 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2472 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2473 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2474 return r;
2475 return 0;
2476}
2477
2478/* turn key exchange results into a blob for packet state serialization */
2479static int
2480newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2481{
2482 struct sshbuf *b;
2483 struct sshcipher_ctx *cc;
2484 struct sshcomp *comp;
2485 struct sshenc *enc;
2486 struct sshmac *mac;
2487 struct newkeys *newkey;
2488 int r;
2489
2490 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2491 return SSH_ERR_INTERNAL_ERROR;
2492 enc = &newkey->enc;
2493 mac = &newkey->mac;
2494 comp = &newkey->comp;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002495 cc = (mode == MODE_OUT) ? ssh->state->send_context :
2496 ssh->state->receive_context;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002497 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2498 return r;
2499 if ((b = sshbuf_new()) == NULL)
2500 return SSH_ERR_ALLOC_FAIL;
2501 /* The cipher struct is constant and shared, you export pointer */
2502 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2503 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2504 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2505 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2506 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2507 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2508 goto out;
2509 if (cipher_authlen(enc->cipher) == 0) {
2510 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2511 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2512 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2513 goto out;
2514 }
2515 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002516 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2517 goto out;
2518 r = sshbuf_put_stringb(m, b);
2519 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002520 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002521 return r;
2522}
2523
2524/* serialize packet state into a blob */
2525int
2526ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2527{
2528 struct session_state *state = ssh->state;
2529 u_char *p;
2530 size_t slen, rlen;
2531 int r, ssh1cipher;
2532
2533 if (!compat20) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002534 ssh1cipher = cipher_ctx_get_number(state->receive_context);
2535 slen = cipher_get_keyiv_len(state->send_context);
2536 rlen = cipher_get_keyiv_len(state->receive_context);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002537 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2538 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2539 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2540 (r = sshbuf_put_u32(m, slen)) != 0 ||
2541 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002542 (r = cipher_get_keyiv(state->send_context, p, slen)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002543 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2544 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002545 (r = cipher_get_keyiv(state->receive_context, p, rlen)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002546 return r;
2547 } else {
2548 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2549 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2550 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002551 (r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002552 (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002553 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2554 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2555 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2556 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2557 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2558 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2559 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2560 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2561 return r;
2562 }
2563
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002564 slen = cipher_get_keycontext(state->send_context, NULL);
2565 rlen = cipher_get_keycontext(state->receive_context, NULL);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002566 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2567 (r = sshbuf_reserve(m, slen, &p)) != 0)
2568 return r;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002569 if (cipher_get_keycontext(state->send_context, p) != (int)slen)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002570 return SSH_ERR_INTERNAL_ERROR;
2571 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2572 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2573 return r;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002574 if (cipher_get_keycontext(state->receive_context, p) != (int)rlen)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002575 return SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org0082fba2016-09-28 16:33:06 +00002576 if ((r = sshbuf_put_stringb(m, state->input)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002577 (r = sshbuf_put_stringb(m, state->output)) != 0)
2578 return r;
2579
markus@openbsd.org091c3022015-01-19 19:52:16 +00002580 return 0;
2581}
2582
2583/* restore key exchange results from blob for packet state de-serialization */
2584static int
2585newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2586{
2587 struct sshbuf *b = NULL;
2588 struct sshcomp *comp;
2589 struct sshenc *enc;
2590 struct sshmac *mac;
2591 struct newkeys *newkey = NULL;
2592 size_t keylen, ivlen, maclen;
2593 int r;
2594
2595 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2596 r = SSH_ERR_ALLOC_FAIL;
2597 goto out;
2598 }
2599 if ((r = sshbuf_froms(m, &b)) != 0)
2600 goto out;
2601#ifdef DEBUG_PK
2602 sshbuf_dump(b, stderr);
2603#endif
2604 enc = &newkey->enc;
2605 mac = &newkey->mac;
2606 comp = &newkey->comp;
2607
2608 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2609 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2610 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2611 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2612 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2613 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2614 goto out;
2615 if (cipher_authlen(enc->cipher) == 0) {
2616 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2617 goto out;
2618 if ((r = mac_setup(mac, mac->name)) != 0)
2619 goto out;
2620 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2621 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2622 goto out;
2623 if (maclen > mac->key_len) {
2624 r = SSH_ERR_INVALID_FORMAT;
2625 goto out;
2626 }
2627 mac->key_len = maclen;
2628 }
2629 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002630 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2631 goto out;
2632 if (enc->name == NULL ||
2633 cipher_by_name(enc->name) != enc->cipher) {
2634 r = SSH_ERR_INVALID_FORMAT;
2635 goto out;
2636 }
2637 if (sshbuf_len(b) != 0) {
2638 r = SSH_ERR_INVALID_FORMAT;
2639 goto out;
2640 }
2641 enc->key_len = keylen;
2642 enc->iv_len = ivlen;
2643 ssh->kex->newkeys[mode] = newkey;
2644 newkey = NULL;
2645 r = 0;
2646 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002647 free(newkey);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002648 sshbuf_free(b);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002649 return r;
2650}
2651
2652/* restore kex from blob for packet state de-serialization */
2653static int
2654kex_from_blob(struct sshbuf *m, struct kex **kexp)
2655{
2656 struct kex *kex;
2657 int r;
2658
2659 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2660 (kex->my = sshbuf_new()) == NULL ||
2661 (kex->peer = sshbuf_new()) == NULL) {
2662 r = SSH_ERR_ALLOC_FAIL;
2663 goto out;
2664 }
2665 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2666 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2667 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2668 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2669 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2670 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2671 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2672 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2673 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2674 goto out;
2675 kex->server = 1;
2676 kex->done = 1;
2677 r = 0;
2678 out:
2679 if (r != 0 || kexp == NULL) {
2680 if (kex != NULL) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002681 sshbuf_free(kex->my);
2682 sshbuf_free(kex->peer);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002683 free(kex);
2684 }
2685 if (kexp != NULL)
2686 *kexp = NULL;
2687 } else {
2688 *kexp = kex;
2689 }
2690 return r;
2691}
2692
2693/*
2694 * Restore packet state from content of blob 'm' (de-serialization).
2695 * Note that 'm' will be partially consumed on parsing or any other errors.
2696 */
2697int
2698ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2699{
2700 struct session_state *state = ssh->state;
2701 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2702 size_t ssh1keylen, rlen, slen, ilen, olen;
2703 int r;
2704 u_int ssh1cipher = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002705
2706 if (!compat20) {
2707 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2708 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2709 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2710 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2711 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2712 return r;
2713 if (ssh1cipher > INT_MAX)
2714 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2715 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2716 (int)ssh1cipher);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002717 if (cipher_get_keyiv_len(state->send_context) != (int)slen ||
2718 cipher_get_keyiv_len(state->receive_context) != (int)rlen)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002719 return SSH_ERR_INVALID_FORMAT;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002720 if ((r = cipher_set_keyiv(state->send_context, ivout)) != 0 ||
2721 (r = cipher_set_keyiv(state->receive_context, ivin)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002722 return r;
2723 } else {
2724 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2725 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2726 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002727 (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
markus@openbsd.org02db4682015-02-13 18:57:00 +00002728 (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +00002729 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2730 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2731 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2732 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2733 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2734 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2735 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2736 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2737 return r;
markus@openbsd.org02db4682015-02-13 18:57:00 +00002738 /*
2739 * We set the time here so that in post-auth privsep slave we
2740 * count from the completion of the authentication.
2741 */
2742 state->rekey_time = monotime();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002743 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2744 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2745 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2746 return r;
2747 }
2748 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2749 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2750 return r;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002751 if (cipher_get_keycontext(state->send_context, NULL) != (int)slen ||
2752 cipher_get_keycontext(state->receive_context, NULL) != (int)rlen)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002753 return SSH_ERR_INVALID_FORMAT;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00002754 cipher_set_keycontext(state->send_context, keyout);
2755 cipher_set_keycontext(state->receive_context, keyin);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002756
djm@openbsd.org0082fba2016-09-28 16:33:06 +00002757 if ((r = ssh_packet_set_postauth(ssh)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00002758 return r;
2759
2760 sshbuf_reset(state->input);
2761 sshbuf_reset(state->output);
2762 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2763 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2764 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2765 (r = sshbuf_put(state->output, output, olen)) != 0)
2766 return r;
2767
markus@openbsd.org091c3022015-01-19 19:52:16 +00002768 if (sshbuf_len(m))
2769 return SSH_ERR_INVALID_FORMAT;
2770 debug3("%s: done", __func__);
2771 return 0;
2772}
2773
2774/* NEW API */
2775
2776/* put data to the outgoing packet */
2777
2778int
2779sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2780{
2781 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2782}
2783
2784int
2785sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2786{
2787 return sshbuf_putb(ssh->state->outgoing_packet, b);
2788}
2789
2790int
2791sshpkt_put_u8(struct ssh *ssh, u_char val)
2792{
2793 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2794}
2795
2796int
2797sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2798{
2799 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2800}
2801
2802int
2803sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2804{
2805 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2806}
2807
2808int
2809sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2810{
2811 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2812}
2813
2814int
2815sshpkt_put_cstring(struct ssh *ssh, const void *v)
2816{
2817 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2818}
2819
2820int
2821sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2822{
2823 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2824}
2825
djm@openbsd.org734226b2015-04-27 01:52:30 +00002826#ifdef WITH_OPENSSL
2827#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002828int
2829sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2830{
2831 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2832}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002833#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002834
Damien Miller773dda22015-01-30 23:10:17 +11002835#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002836int
2837sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2838{
2839 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2840}
Damien Miller773dda22015-01-30 23:10:17 +11002841#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002842
2843int
2844sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2845{
2846 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2847}
Damien Miller773dda22015-01-30 23:10:17 +11002848#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002849
2850/* fetch data from the incoming packet */
2851
2852int
2853sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2854{
2855 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2856}
2857
2858int
2859sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2860{
2861 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2862}
2863
2864int
2865sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2866{
2867 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2868}
2869
2870int
2871sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2872{
2873 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2874}
2875
2876int
2877sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2878{
2879 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2880}
2881
2882int
2883sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2884{
2885 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2886}
2887
2888int
2889sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2890{
2891 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2892}
2893
djm@openbsd.org734226b2015-04-27 01:52:30 +00002894#ifdef WITH_OPENSSL
2895#ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00002896int
2897sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2898{
2899 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2900}
djm@openbsd.org734226b2015-04-27 01:52:30 +00002901#endif /* OPENSSL_HAS_ECC */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002902
Damien Miller773dda22015-01-30 23:10:17 +11002903#ifdef WITH_SSH1
markus@openbsd.org091c3022015-01-19 19:52:16 +00002904int
2905sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2906{
2907 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2908}
Damien Miller773dda22015-01-30 23:10:17 +11002909#endif /* WITH_SSH1 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002910
2911int
2912sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2913{
2914 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2915}
Damien Miller773dda22015-01-30 23:10:17 +11002916#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002917
2918int
2919sshpkt_get_end(struct ssh *ssh)
2920{
2921 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2922 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2923 return 0;
2924}
2925
2926const u_char *
2927sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2928{
2929 if (lenp != NULL)
2930 *lenp = sshbuf_len(ssh->state->incoming_packet);
2931 return sshbuf_ptr(ssh->state->incoming_packet);
2932}
2933
2934/* start a new packet */
2935
2936int
2937sshpkt_start(struct ssh *ssh, u_char type)
2938{
2939 u_char buf[9];
2940 int len;
2941
2942 DBG(debug("packet_start[%d]", type));
2943 len = compat20 ? 6 : 9;
2944 memset(buf, 0, len - 1);
2945 buf[len - 1] = type;
2946 sshbuf_reset(ssh->state->outgoing_packet);
2947 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2948}
2949
markus@openbsd.org8d057842016-09-30 09:19:13 +00002950static int
2951ssh_packet_send_mux(struct ssh *ssh)
2952{
2953 struct session_state *state = ssh->state;
2954 u_char type, *cp;
2955 size_t len;
2956 int r;
2957
2958 if (ssh->kex)
2959 return SSH_ERR_INTERNAL_ERROR;
2960 len = sshbuf_len(state->outgoing_packet);
2961 if (len < 6)
2962 return SSH_ERR_INTERNAL_ERROR;
2963 cp = sshbuf_mutable_ptr(state->outgoing_packet);
2964 type = cp[5];
2965 if (ssh_packet_log_type(type))
2966 debug3("%s: type %u", __func__, type);
2967 /* drop everything, but the connection protocol */
2968 if (type >= SSH2_MSG_CONNECTION_MIN &&
2969 type <= SSH2_MSG_CONNECTION_MAX) {
2970 POKE_U32(cp, len - 4);
2971 if ((r = sshbuf_putb(state->output,
2972 state->outgoing_packet)) != 0)
2973 return r;
2974 /* sshbuf_dump(state->output, stderr); */
2975 }
2976 sshbuf_reset(state->outgoing_packet);
2977 return 0;
2978}
2979
markus@openbsd.org091c3022015-01-19 19:52:16 +00002980/* send it */
2981
2982int
2983sshpkt_send(struct ssh *ssh)
2984{
markus@openbsd.org8d057842016-09-30 09:19:13 +00002985 if (ssh->state && ssh->state->mux)
2986 return ssh_packet_send_mux(ssh);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002987 if (compat20)
2988 return ssh_packet_send2(ssh);
2989 else
2990 return ssh_packet_send1(ssh);
2991}
2992
2993int
2994sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2995{
2996 char buf[1024];
2997 va_list args;
2998 int r;
2999
3000 va_start(args, fmt);
3001 vsnprintf(buf, sizeof(buf), fmt, args);
3002 va_end(args);
3003
3004 if (compat20) {
3005 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
3006 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
3007 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
3008 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
3009 (r = sshpkt_send(ssh)) != 0)
3010 return r;
3011 } else {
3012 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
3013 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
3014 (r = sshpkt_send(ssh)) != 0)
3015 return r;
3016 }
3017 return 0;
3018}
3019
3020/* roundup current message to pad bytes */
3021int
3022sshpkt_add_padding(struct ssh *ssh, u_char pad)
3023{
3024 ssh->state->extra_pad = pad;
3025 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10003026}