blob: eb178f149c600e6206c3f164f2ace29b24378791 [file] [log] [blame]
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001/* $OpenBSD: packet.c,v 1.204 2015/01/28 21:15:47 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"
Damien Miller68f8e992006-03-15 11:24:12 +110041
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000042#include <sys/param.h> /* MIN roundup */
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100044#include "openbsd-compat/sys-queue.h"
Damien Miller8ec8c3e2006-07-10 20:35:38 +100045#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100046#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100049
Damien Miller8ec8c3e2006-07-10 20:35:38 +100050#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110051#include <netinet/ip.h>
Darren Tuckerdace2332006-09-22 19:22:17 +100052#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Darren Tucker39972492006-07-12 22:22:46 +100054#include <errno.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"
Darren Tuckerc5564e12009-06-21 18:53:53 +100086#include "roaming.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000087#include "ssherr.h"
88#include "sshbuf.h"
Damien Miller33b13562000-04-04 14:38:59 +100089
90#ifdef PACKET_DEBUG
91#define DBG(x) x
92#else
93#define DBG(x)
94#endif
95
Damien Miller13ae44c2009-01-28 16:38:41 +110096#define PACKET_MAX_SIZE (256 * 1024)
97
Darren Tuckerf7288d72009-06-21 18:12:20 +100098struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100099 u_int32_t seqnr;
100 u_int32_t packets;
101 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000102 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000103};
Damien Miller13ae44c2009-01-28 16:38:41 +1100104
Damien Millera5539d22003-04-09 20:50:06 +1000105struct packet {
106 TAILQ_ENTRY(packet) next;
107 u_char type;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000108 struct sshbuf *payload;
Damien Millera5539d22003-04-09 20:50:06 +1000109};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000110
111struct session_state {
112 /*
113 * This variable contains the file descriptors used for
114 * communicating with the other side. connection_in is used for
115 * reading; connection_out for writing. These can be the same
116 * descriptor, in which case it is assumed to be a socket.
117 */
118 int connection_in;
119 int connection_out;
120
121 /* Protocol flags for the remote side. */
122 u_int remote_protocol_flags;
123
124 /* Encryption context for receiving data. Only used for decryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000125 struct sshcipher_ctx receive_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000126
127 /* Encryption context for sending data. Only used for encryption. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000128 struct sshcipher_ctx send_context;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000129
130 /* Buffer for raw input data from the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000131 struct sshbuf *input;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000132
133 /* Buffer for raw output data going to the socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000134 struct sshbuf *output;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000135
136 /* Buffer for the partial outgoing packet being constructed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000137 struct sshbuf *outgoing_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000138
139 /* Buffer for the incoming packet currently being processed. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000140 struct sshbuf *incoming_packet;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000141
142 /* Scratch buffer for packet compression/decompression. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000143 struct sshbuf *compression_buffer;
144
145 /* Incoming/outgoing compression dictionaries */
146 z_stream compression_in_stream;
147 z_stream compression_out_stream;
148 int compression_in_started;
149 int compression_out_started;
150 int compression_in_failures;
151 int compression_out_failures;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000152
153 /*
154 * Flag indicating whether packet compression/decompression is
155 * enabled.
156 */
157 int packet_compression;
158
159 /* default maximum packet size */
160 u_int max_packet_size;
161
162 /* Flag indicating whether this module has been initialized. */
163 int initialized;
164
165 /* Set to true if the connection is interactive. */
166 int interactive_mode;
167
168 /* Set to true if we are the server side. */
169 int server_side;
170
171 /* Set to true if we are authenticated. */
172 int after_authentication;
173
174 int keep_alive_timeouts;
175
176 /* The maximum time that we will wait to send or receive a packet */
177 int packet_timeout_ms;
178
179 /* Session key information for Encryption and MAC */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000180 struct newkeys *newkeys[MODE_MAX];
Darren Tuckerf7288d72009-06-21 18:12:20 +1000181 struct packet_state p_read, p_send;
182
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000183 /* Volume-based rekeying */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000184 u_int64_t max_blocks_in, max_blocks_out;
185 u_int32_t rekey_limit;
186
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000187 /* Time-based rekeying */
188 time_t rekey_interval; /* how often in seconds */
189 time_t rekey_time; /* time of last rekeying */
190
Darren Tuckerf7288d72009-06-21 18:12:20 +1000191 /* Session key for protocol v1 */
192 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
193 u_int ssh1_keylen;
194
195 /* roundup current message to extra_pad bytes */
196 u_char extra_pad;
197
198 /* XXX discard incoming data after MAC error */
199 u_int packet_discard;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000200 struct sshmac *packet_discard_mac;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000201
202 /* Used in packet_read_poll2() */
203 u_int packlen;
204
Darren Tucker7b935c72009-06-21 18:59:36 +1000205 /* Used in packet_send2 */
206 int rekeying;
207
208 /* Used in packet_set_interactive */
209 int set_interactive_called;
210
211 /* Used in packet_set_maxsize */
212 int set_maxsize_called;
213
markus@openbsd.org091c3022015-01-19 19:52:16 +0000214 /* One-off warning about weak ciphers */
215 int cipher_warning_done;
216
217 /* SSH1 CRC compensation attack detector */
218 struct deattack_ctx deattack;
219
Darren Tuckerf7288d72009-06-21 18:12:20 +1000220 TAILQ_HEAD(, packet) outgoing;
221};
222
markus@openbsd.org091c3022015-01-19 19:52:16 +0000223struct ssh *
224ssh_alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000225{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000226 struct ssh *ssh = NULL;
227 struct session_state *state = NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000228
markus@openbsd.org091c3022015-01-19 19:52:16 +0000229 if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
230 (state = calloc(1, sizeof(*state))) == NULL ||
231 (state->input = sshbuf_new()) == NULL ||
232 (state->output = sshbuf_new()) == NULL ||
233 (state->outgoing_packet = sshbuf_new()) == NULL ||
234 (state->incoming_packet = sshbuf_new()) == NULL)
235 goto fail;
236 TAILQ_INIT(&state->outgoing);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000237 TAILQ_INIT(&ssh->private_keys);
238 TAILQ_INIT(&ssh->public_keys);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000239 state->connection_in = -1;
240 state->connection_out = -1;
241 state->max_packet_size = 32768;
242 state->packet_timeout_ms = -1;
243 state->p_send.packets = state->p_read.packets = 0;
244 state->initialized = 1;
245 /*
246 * ssh_packet_send2() needs to queue packets until
247 * we've done the initial key exchange.
248 */
249 state->rekeying = 1;
250 ssh->state = state;
251 return ssh;
252 fail:
253 if (state) {
254 sshbuf_free(state->input);
255 sshbuf_free(state->output);
256 sshbuf_free(state->incoming_packet);
257 sshbuf_free(state->outgoing_packet);
258 free(state);
259 }
260 free(ssh);
261 return NULL;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000262}
Damien Millera5539d22003-04-09 20:50:06 +1000263
Damien Miller5428f641999-11-25 11:54:57 +1100264/*
265 * Sets the descriptors used for communication. Disables encryption until
266 * packet_set_encryption_key is called.
267 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000268struct ssh *
269ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000271 struct session_state *state;
272 const struct sshcipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000273 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000274
Damien Miller874d77b2000-10-14 16:23:11 +1100275 if (none == NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000276 fatal("%s: cannot load cipher 'none'", __func__);
277 if (ssh == NULL)
278 ssh = ssh_alloc_session_state();
279 if (ssh == NULL)
280 fatal("%s: cound not allocate state", __func__);
281 state = ssh->state;
282 state->connection_in = fd_in;
283 state->connection_out = fd_out;
284 if ((r = cipher_init(&state->send_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000285 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000286 (r = cipher_init(&state->receive_context, none,
Damien Miller86687062014-07-02 15:28:02 +1000287 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000288 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
289 state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
290 deattack_init(&state->deattack);
291 return ssh;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292}
293
Darren Tucker3fc464e2008-06-13 06:42:45 +1000294void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000295ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
Darren Tucker3fc464e2008-06-13 06:42:45 +1000296{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000297 struct session_state *state = ssh->state;
298
Damien Miller8ed4de82011-12-19 10:52:50 +1100299 if (timeout <= 0 || count <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000300 state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000301 return;
302 }
303 if ((INT_MAX / 1000) / count < timeout)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000304 state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000305 else
markus@openbsd.org091c3022015-01-19 19:52:16 +0000306 state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000307}
308
markus@openbsd.org091c3022015-01-19 19:52:16 +0000309int
310ssh_packet_stop_discard(struct ssh *ssh)
Damien Miller13ae44c2009-01-28 16:38:41 +1100311{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000312 struct session_state *state = ssh->state;
313 int r;
314
315 if (state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100316 char buf[1024];
markus@openbsd.org091c3022015-01-19 19:52:16 +0000317
Damien Miller13ae44c2009-01-28 16:38:41 +1100318 memset(buf, 'a', sizeof(buf));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000319 while (sshbuf_len(state->incoming_packet) <
Darren Tuckerf7288d72009-06-21 18:12:20 +1000320 PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000321 if ((r = sshbuf_put(state->incoming_packet, buf,
322 sizeof(buf))) != 0)
323 return r;
324 (void) mac_compute(state->packet_discard_mac,
325 state->p_read.seqnr,
326 sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
327 NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100328 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000329 logit("Finished discarding for %.200s", ssh_remote_ipaddr(ssh));
330 return SSH_ERR_MAC_INVALID;
Damien Miller13ae44c2009-01-28 16:38:41 +1100331}
332
markus@openbsd.org091c3022015-01-19 19:52:16 +0000333static int
334ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
335 struct sshmac *mac, u_int packet_length, u_int discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100336{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000337 struct session_state *state = ssh->state;
338 int r;
339
340 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
341 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
342 return r;
343 return SSH_ERR_MAC_INVALID;
344 }
Damien Miller13ae44c2009-01-28 16:38:41 +1100345 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000346 state->packet_discard_mac = mac;
347 if (sshbuf_len(state->input) >= discard &&
348 (r = ssh_packet_stop_discard(ssh)) != 0)
349 return r;
350 state->packet_discard = discard - sshbuf_len(state->input);
351 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100352}
353
Damien Miller34132e52000-01-14 15:45:46 +1100354/* Returns 1 if remote host is connected via socket, 0 if not. */
355
356int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000357ssh_packet_connection_is_on_socket(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100358{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000359 struct session_state *state = ssh->state;
Damien Miller34132e52000-01-14 15:45:46 +1100360 struct sockaddr_storage from, to;
361 socklen_t fromlen, tolen;
362
363 /* filedescriptors in and out are the same, so it's a socket */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000364 if (state->connection_in == state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100365 return 1;
366 fromlen = sizeof(from);
367 memset(&from, 0, sizeof(from));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000368 if (getpeername(state->connection_in, (struct sockaddr *)&from,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000369 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100370 return 0;
371 tolen = sizeof(to);
372 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000373 if (getpeername(state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000374 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100375 return 0;
376 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
377 return 0;
378 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
379 return 0;
380 return 1;
381}
382
Ben Lindstromf6027d32002-03-22 01:42:04 +0000383void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000384ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000385{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000386 if (ibytes)
387 *ibytes = ssh->state->p_read.bytes;
388 if (obytes)
389 *obytes = ssh->state->p_send.bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000390}
391
392int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000393ssh_packet_connection_af(struct ssh *ssh)
Damien Miller34132e52000-01-14 15:45:46 +1100394{
395 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100396 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100397
398 memset(&to, 0, sizeof(to));
markus@openbsd.org091c3022015-01-19 19:52:16 +0000399 if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000400 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100401 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000402#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100403 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000404 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000405 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000406#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000407 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100408}
409
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410/* Sets the connection into non-blocking mode. */
411
412void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000413ssh_packet_set_nonblocking(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414{
Damien Miller95def091999-11-25 00:26:21 +1100415 /* Set the socket into non-blocking mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000416 set_nonblock(ssh->state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
markus@openbsd.org091c3022015-01-19 19:52:16 +0000418 if (ssh->state->connection_out != ssh->state->connection_in)
419 set_nonblock(ssh->state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420}
421
422/* Returns the socket used for reading. */
423
424int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000425ssh_packet_get_connection_in(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000427 return ssh->state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428}
429
430/* Returns the descriptor used for writing. */
431
432int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000433ssh_packet_get_connection_out(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000435 return ssh->state->connection_out;
436}
437
438/*
439 * Returns the IP-address of the remote host as a string. The returned
440 * string must not be freed.
441 */
442
443const char *
444ssh_remote_ipaddr(struct ssh *ssh)
445{
446 /* Check whether we have cached the ipaddr. */
447 if (ssh->remote_ipaddr == NULL)
448 ssh->remote_ipaddr = ssh_packet_connection_is_on_socket(ssh) ?
449 get_peer_ipaddr(ssh->state->connection_in) :
450 strdup("UNKNOWN");
451 if (ssh->remote_ipaddr == NULL)
452 return "UNKNOWN";
453 return ssh->remote_ipaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454}
455
456/* Closes the connection and clears and frees internal data structures. */
457
458void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000459ssh_packet_close(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000461 struct session_state *state = ssh->state;
462 int r;
463 u_int mode;
464
465 if (!state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100466 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000467 state->initialized = 0;
468 if (state->connection_in == state->connection_out) {
469 shutdown(state->connection_out, SHUT_RDWR);
470 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100471 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000472 close(state->connection_in);
473 close(state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100474 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000475 sshbuf_free(state->input);
476 sshbuf_free(state->output);
477 sshbuf_free(state->outgoing_packet);
478 sshbuf_free(state->incoming_packet);
479 for (mode = 0; mode < MODE_MAX; mode++)
480 kex_free_newkeys(state->newkeys[mode]);
481 if (state->compression_buffer) {
482 sshbuf_free(state->compression_buffer);
483 if (state->compression_out_started) {
484 z_streamp stream = &state->compression_out_stream;
485 debug("compress outgoing: "
486 "raw data %llu, compressed %llu, factor %.2f",
487 (unsigned long long)stream->total_in,
488 (unsigned long long)stream->total_out,
489 stream->total_in == 0 ? 0.0 :
490 (double) stream->total_out / stream->total_in);
491 if (state->compression_out_failures == 0)
492 deflateEnd(stream);
493 }
494 if (state->compression_in_started) {
495 z_streamp stream = &state->compression_out_stream;
496 debug("compress incoming: "
497 "raw data %llu, compressed %llu, factor %.2f",
498 (unsigned long long)stream->total_out,
499 (unsigned long long)stream->total_in,
500 stream->total_out == 0 ? 0.0 :
501 (double) stream->total_in / stream->total_out);
502 if (state->compression_in_failures == 0)
503 inflateEnd(stream);
504 }
Damien Miller95def091999-11-25 00:26:21 +1100505 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000506 if ((r = cipher_cleanup(&state->send_context)) != 0)
507 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
508 if ((r = cipher_cleanup(&state->receive_context)) != 0)
509 error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
510 if (ssh->remote_ipaddr) {
511 free(ssh->remote_ipaddr);
512 ssh->remote_ipaddr = NULL;
513 }
514 free(ssh->state);
515 ssh->state = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516}
517
518/* Sets remote side protocol flags. */
519
520void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000521ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000523 ssh->state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524}
525
526/* Returns the remote protocol flags set earlier by the above function. */
527
Ben Lindstrom46c16222000-12-22 01:43:59 +0000528u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000529ssh_packet_get_protocol_flags(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000531 return ssh->state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532}
533
Damien Miller5428f641999-11-25 11:54:57 +1100534/*
535 * Starts packet compression from the next packet on in both directions.
536 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
537 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538
markus@openbsd.org091c3022015-01-19 19:52:16 +0000539static int
540ssh_packet_init_compression(struct ssh *ssh)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000541{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000542 if (!ssh->state->compression_buffer &&
543 ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
544 return SSH_ERR_ALLOC_FAIL;
545 return 0;
546}
547
548static int
549start_compression_out(struct ssh *ssh, int level)
550{
551 if (level < 1 || level > 9)
552 return SSH_ERR_INVALID_ARGUMENT;
553 debug("Enabling compression at level %d.", level);
554 if (ssh->state->compression_out_started == 1)
555 deflateEnd(&ssh->state->compression_out_stream);
556 switch (deflateInit(&ssh->state->compression_out_stream, level)) {
557 case Z_OK:
558 ssh->state->compression_out_started = 1;
559 break;
560 case Z_MEM_ERROR:
561 return SSH_ERR_ALLOC_FAIL;
562 default:
563 return SSH_ERR_INTERNAL_ERROR;
564 }
565 return 0;
566}
567
568static int
569start_compression_in(struct ssh *ssh)
570{
571 if (ssh->state->compression_in_started == 1)
572 inflateEnd(&ssh->state->compression_in_stream);
573 switch (inflateInit(&ssh->state->compression_in_stream)) {
574 case Z_OK:
575 ssh->state->compression_in_started = 1;
576 break;
577 case Z_MEM_ERROR:
578 return SSH_ERR_ALLOC_FAIL;
579 default:
580 return SSH_ERR_INTERNAL_ERROR;
581 }
582 return 0;
583}
584
585int
586ssh_packet_start_compression(struct ssh *ssh, int level)
587{
588 int r;
589
590 if (ssh->state->packet_compression && !compat20)
591 return SSH_ERR_INTERNAL_ERROR;
592 ssh->state->packet_compression = 1;
593 if ((r = ssh_packet_init_compression(ssh)) != 0 ||
594 (r = start_compression_in(ssh)) != 0 ||
595 (r = start_compression_out(ssh, level)) != 0)
596 return r;
597 return 0;
598}
599
600/* XXX remove need for separate compression buffer */
601static int
602compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
603{
604 u_char buf[4096];
605 int r, status;
606
607 if (ssh->state->compression_out_started != 1)
608 return SSH_ERR_INTERNAL_ERROR;
609
610 /* This case is not handled below. */
611 if (sshbuf_len(in) == 0)
612 return 0;
613
614 /* Input is the contents of the input buffer. */
615 if ((ssh->state->compression_out_stream.next_in =
616 sshbuf_mutable_ptr(in)) == NULL)
617 return SSH_ERR_INTERNAL_ERROR;
618 ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
619
620 /* Loop compressing until deflate() returns with avail_out != 0. */
621 do {
622 /* Set up fixed-size output buffer. */
623 ssh->state->compression_out_stream.next_out = buf;
624 ssh->state->compression_out_stream.avail_out = sizeof(buf);
625
626 /* Compress as much data into the buffer as possible. */
627 status = deflate(&ssh->state->compression_out_stream,
628 Z_PARTIAL_FLUSH);
629 switch (status) {
630 case Z_MEM_ERROR:
631 return SSH_ERR_ALLOC_FAIL;
632 case Z_OK:
633 /* Append compressed data to output_buffer. */
634 if ((r = sshbuf_put(out, buf, sizeof(buf) -
635 ssh->state->compression_out_stream.avail_out)) != 0)
636 return r;
637 break;
638 case Z_STREAM_ERROR:
639 default:
640 ssh->state->compression_out_failures++;
641 return SSH_ERR_INVALID_FORMAT;
642 }
643 } while (ssh->state->compression_out_stream.avail_out == 0);
644 return 0;
645}
646
647static int
648uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
649{
650 u_char buf[4096];
651 int r, status;
652
653 if (ssh->state->compression_in_started != 1)
654 return SSH_ERR_INTERNAL_ERROR;
655
656 if ((ssh->state->compression_in_stream.next_in =
657 sshbuf_mutable_ptr(in)) == NULL)
658 return SSH_ERR_INTERNAL_ERROR;
659 ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
660
661 for (;;) {
662 /* Set up fixed-size output buffer. */
663 ssh->state->compression_in_stream.next_out = buf;
664 ssh->state->compression_in_stream.avail_out = sizeof(buf);
665
666 status = inflate(&ssh->state->compression_in_stream,
667 Z_PARTIAL_FLUSH);
668 switch (status) {
669 case Z_OK:
670 if ((r = sshbuf_put(out, buf, sizeof(buf) -
671 ssh->state->compression_in_stream.avail_out)) != 0)
672 return r;
673 break;
674 case Z_BUF_ERROR:
675 /*
676 * Comments in zlib.h say that we should keep calling
677 * inflate() until we get an error. This appears to
678 * be the error that we get.
679 */
680 return 0;
681 case Z_DATA_ERROR:
682 return SSH_ERR_INVALID_FORMAT;
683 case Z_MEM_ERROR:
684 return SSH_ERR_ALLOC_FAIL;
685 case Z_STREAM_ERROR:
686 default:
687 ssh->state->compression_in_failures++;
688 return SSH_ERR_INTERNAL_ERROR;
689 }
690 }
691 /* NOTREACHED */
692}
693
694/* Serialise compression state into a blob for privsep */
695static int
696ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
697{
698 struct session_state *state = ssh->state;
699 struct sshbuf *b;
700 int r;
701
702 if ((b = sshbuf_new()) == NULL)
703 return SSH_ERR_ALLOC_FAIL;
704 if (state->compression_in_started) {
705 if ((r = sshbuf_put_string(b, &state->compression_in_stream,
706 sizeof(state->compression_in_stream))) != 0)
707 goto out;
708 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
709 goto out;
710 if (state->compression_out_started) {
711 if ((r = sshbuf_put_string(b, &state->compression_out_stream,
712 sizeof(state->compression_out_stream))) != 0)
713 goto out;
714 } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
715 goto out;
716 r = sshbuf_put_stringb(m, b);
717 out:
718 sshbuf_free(b);
719 return r;
720}
721
722/* Deserialise compression state from a blob for privsep */
723static int
724ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
725{
726 struct session_state *state = ssh->state;
727 struct sshbuf *b = NULL;
728 int r;
729 const u_char *inblob, *outblob;
730 size_t inl, outl;
731
732 if ((r = sshbuf_froms(m, &b)) != 0)
733 goto out;
734 if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
735 (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
736 goto out;
737 if (inl == 0)
738 state->compression_in_started = 0;
739 else if (inl != sizeof(state->compression_in_stream)) {
740 r = SSH_ERR_INTERNAL_ERROR;
741 goto out;
742 } else {
743 state->compression_in_started = 1;
744 memcpy(&state->compression_in_stream, inblob, inl);
745 }
746 if (outl == 0)
747 state->compression_out_started = 0;
748 else if (outl != sizeof(state->compression_out_stream)) {
749 r = SSH_ERR_INTERNAL_ERROR;
750 goto out;
751 } else {
752 state->compression_out_started = 1;
753 memcpy(&state->compression_out_stream, outblob, outl);
754 }
755 r = 0;
756 out:
757 sshbuf_free(b);
758 return r;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000759}
760
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000761void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000762ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
763 void *(*allocfunc)(void *, u_int, u_int),
764 void (*freefunc)(void *, void *))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000766 ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
767 ssh->state->compression_out_stream.zfree = (free_func)freefunc;
768 ssh->state->compression_out_stream.opaque = ctx;
769 ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
770 ssh->state->compression_in_stream.zfree = (free_func)freefunc;
771 ssh->state->compression_in_stream.opaque = ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000772}
773
Damien Miller5428f641999-11-25 11:54:57 +1100774/*
Damien Miller5428f641999-11-25 11:54:57 +1100775 * Causes any further packets to be encrypted using the given key. The same
776 * key is used for both sending and reception. However, both directions are
777 * encrypted independently of each other.
778 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000779
Damien Miller1f0311c2014-05-15 14:24:09 +1000780#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781void
markus@openbsd.org091c3022015-01-19 19:52:16 +0000782ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000784 struct session_state *state = ssh->state;
785 const struct sshcipher *cipher = cipher_by_number(number);
786 int r;
787 const char *wmsg;
Damien Miller4f7becb2006-03-26 14:10:14 +1100788
markus@openbsd.org091c3022015-01-19 19:52:16 +0000789 if (cipher == NULL)
790 fatal("%s: unknown cipher number %d", __func__, number);
791 if (keylen < 20)
792 fatal("%s: keylen too small: %d", __func__, keylen);
793 if (keylen > SSH_SESSION_KEY_LENGTH)
794 fatal("%s: keylen too big: %d", __func__, keylen);
795 memcpy(state->ssh1_key, key, keylen);
796 state->ssh1_keylen = keylen;
797 if ((r = cipher_init(&state->send_context, cipher, key, keylen,
798 NULL, 0, CIPHER_ENCRYPT)) != 0 ||
799 (r = cipher_init(&state->receive_context, cipher, key, keylen,
800 NULL, 0, CIPHER_DECRYPT) != 0))
801 fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
802 if (!state->cipher_warning_done &&
803 ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
804 (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
805 error("Warning: %s", wmsg);
806 state->cipher_warning_done = 1;
807 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000808}
Damien Miller6af914a2010-09-10 11:39:26 +1000809#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000810
Damien Miller5428f641999-11-25 11:54:57 +1100811/*
812 * Finalizes and sends the packet. If the encryption key has been set,
813 * encrypts the packet before sending.
814 */
Damien Miller95def091999-11-25 00:26:21 +1100815
markus@openbsd.org091c3022015-01-19 19:52:16 +0000816int
817ssh_packet_send1(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000819 struct session_state *state = ssh->state;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000820 u_char buf[8], *cp;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000821 int r, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000822 u_int checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823
Damien Miller5428f641999-11-25 11:54:57 +1100824 /*
825 * If using packet compression, compress the payload of the outgoing
826 * packet.
827 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000828 if (state->packet_compression) {
829 sshbuf_reset(state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100830 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000831 if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
832 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100833 /* padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000834 if ((r = sshbuf_put(state->compression_buffer,
835 "\0\0\0\0\0\0\0\0", 8)) != 0)
836 goto out;
837 if ((r = compress_buffer(ssh, state->outgoing_packet,
838 state->compression_buffer)) != 0)
839 goto out;
840 sshbuf_reset(state->outgoing_packet);
841 if ((r = sshbuf_putb(state->outgoing_packet,
842 state->compression_buffer)) != 0)
843 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100844 }
845 /* Compute packet length without padding (add checksum, remove padding). */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000846 len = sshbuf_len(state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847
Damien Millere247cc42000-05-07 12:03:14 +1000848 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100849 padding = 8 - len % 8;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000850 if (!state->send_context.plaintext) {
851 cp = sshbuf_mutable_ptr(state->outgoing_packet);
852 if (cp == NULL) {
853 r = SSH_ERR_INTERNAL_ERROR;
854 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100855 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000856 arc4random_buf(cp + 8 - padding, padding);
Damien Miller95def091999-11-25 00:26:21 +1100857 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000858 if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
859 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100860
861 /* Add check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000862 checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
863 sshbuf_len(state->outgoing_packet));
864 POKE_U32(buf, checksum);
865 if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
866 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867
868#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100869 fprintf(stderr, "packet_send plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000870 sshbuf_dump(state->outgoing_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871#endif
872
Damien Miller95def091999-11-25 00:26:21 +1100873 /* Append to output. */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000874 POKE_U32(buf, len);
875 if ((r = sshbuf_put(state->output, buf, 4)) != 0)
876 goto out;
877 if ((r = sshbuf_reserve(state->output,
878 sshbuf_len(state->outgoing_packet), &cp)) != 0)
879 goto out;
880 if ((r = cipher_crypt(&state->send_context, 0, cp,
881 sshbuf_ptr(state->outgoing_packet),
882 sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
883 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100884
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100886 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000887 sshbuf_dump(state->output, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000889 state->p_send.packets++;
890 state->p_send.bytes += len +
891 sshbuf_len(state->outgoing_packet);
892 sshbuf_reset(state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Miller5428f641999-11-25 11:54:57 +1100894 /*
Damien Miller788f2122005-11-05 15:14:59 +1100895 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100896 * actually sent until packet_write_wait or packet_write_poll is
897 * called.
898 */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000899 r = 0;
900 out:
901 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902}
903
markus@openbsd.org091c3022015-01-19 19:52:16 +0000904int
905ssh_set_newkeys(struct ssh *ssh, int mode)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000906{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000907 struct session_state *state = ssh->state;
908 struct sshenc *enc;
909 struct sshmac *mac;
910 struct sshcomp *comp;
911 struct sshcipher_ctx *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000912 u_int64_t *max_blocks;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000913 const char *wmsg;
Damien Miller86687062014-07-02 15:28:02 +1000914 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000915
Ben Lindstrom064496f2002-12-23 02:04:22 +0000916 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000917
Damien Miller963f6b22002-02-19 15:21:23 +1100918 if (mode == MODE_OUT) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000919 cc = &state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000920 crypt_type = CIPHER_ENCRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000921 state->p_send.packets = state->p_send.blocks = 0;
922 max_blocks = &state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100923 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000924 cc = &state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000925 crypt_type = CIPHER_DECRYPT;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000926 state->p_read.packets = state->p_read.blocks = 0;
927 max_blocks = &state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100928 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000929 if (state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000930 debug("set_newkeys: rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +0000931 if ((r = cipher_cleanup(cc)) != 0)
932 return r;
933 enc = &state->newkeys[mode]->enc;
934 mac = &state->newkeys[mode]->mac;
935 comp = &state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000936 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100937 explicit_bzero(enc->iv, enc->iv_len);
938 explicit_bzero(enc->key, enc->key_len);
939 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000940 free(enc->name);
941 free(enc->iv);
942 free(enc->key);
943 free(mac->name);
944 free(mac->key);
945 free(comp->name);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000946 free(state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000947 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000948 /* move newkeys from kex to state */
949 if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
950 return SSH_ERR_INTERNAL_ERROR;
951 ssh->kex->newkeys[mode] = NULL;
952 enc = &state->newkeys[mode]->enc;
953 mac = &state->newkeys[mode]->mac;
954 comp = &state->newkeys[mode]->comp;
955 if (cipher_authlen(enc->cipher) == 0) {
956 if ((r = mac_init(mac)) != 0)
957 return r;
958 }
959 mac->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000960 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000961 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
962 enc->iv, enc->iv_len, crypt_type)) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000963 return r;
964 if (!state->cipher_warning_done &&
965 (wmsg = cipher_warning_message(cc)) != NULL) {
966 error("Warning: %s", wmsg);
967 state->cipher_warning_done = 1;
968 }
Ben Lindstromf6027d32002-03-22 01:42:04 +0000969 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100970 /* explicit_bzero(enc->iv, enc->block_size);
971 explicit_bzero(enc->key, enc->key_len);
972 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000973 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000974 (comp->type == COMP_DELAYED &&
markus@openbsd.org091c3022015-01-19 19:52:16 +0000975 state->after_authentication)) && comp->enabled == 0) {
976 if ((r = ssh_packet_init_compression(ssh)) < 0)
977 return r;
978 if (mode == MODE_OUT) {
979 if ((r = start_compression_out(ssh, 6)) != 0)
980 return r;
981 } else {
982 if ((r = start_compression_in(ssh)) != 0)
983 return r;
984 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000985 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000986 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000987 /*
988 * The 2^(blocksize*2) limit is too expensive for 3DES,
989 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
990 */
991 if (enc->block_size >= 16)
992 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
993 else
994 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000995 if (state->rekey_limit)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000996 *max_blocks = MIN(*max_blocks,
markus@openbsd.org091c3022015-01-19 19:52:16 +0000997 state->rekey_limit / enc->block_size);
998 return 0;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000999}
1000
Damien Miller5428f641999-11-25 11:54:57 +11001001/*
Damien Miller9786e6e2005-07-26 21:54:56 +10001002 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +10001003 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +10001004 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
1005 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001006static int
1007ssh_packet_enable_delayed_compress(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10001008{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001009 struct session_state *state = ssh->state;
1010 struct sshcomp *comp = NULL;
1011 int r, mode;
Damien Miller9786e6e2005-07-26 21:54:56 +10001012
1013 /*
1014 * Remember that we are past the authentication step, so rekeying
1015 * with COMP_DELAYED will turn on compression immediately.
1016 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001017 state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001018 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +10001019 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001020 if (state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +10001021 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001022 comp = &state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +10001023 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001024 if ((r = ssh_packet_init_compression(ssh)) != 0)
1025 return r;
1026 if (mode == MODE_OUT) {
1027 if ((r = start_compression_out(ssh, 6)) != 0)
1028 return r;
1029 } else {
1030 if ((r = start_compression_in(ssh)) != 0)
1031 return r;
1032 }
Damien Miller9786e6e2005-07-26 21:54:56 +10001033 comp->enabled = 1;
1034 }
1035 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001036 return 0;
Damien Miller9786e6e2005-07-26 21:54:56 +10001037}
1038
1039/*
Damien Miller33b13562000-04-04 14:38:59 +10001040 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
1041 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001042int
1043ssh_packet_send2_wrapped(struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10001044{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001045 struct session_state *state = ssh->state;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001046 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +11001047 u_char padlen, pad = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001048 u_int authlen = 0, aadlen = 0;
1049 u_int len;
1050 struct sshenc *enc = NULL;
1051 struct sshmac *mac = NULL;
1052 struct sshcomp *comp = NULL;
1053 int r, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001054
markus@openbsd.org091c3022015-01-19 19:52:16 +00001055 if (state->newkeys[MODE_OUT] != NULL) {
1056 enc = &state->newkeys[MODE_OUT]->enc;
1057 mac = &state->newkeys[MODE_OUT]->mac;
1058 comp = &state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001059 /* disable mac for authenticated encryption */
1060 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1061 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001062 }
Damien Miller963f6b22002-02-19 15:21:23 +11001063 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001064 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001065
markus@openbsd.org091c3022015-01-19 19:52:16 +00001066 type = (sshbuf_ptr(state->outgoing_packet))[5];
Damien Miller33b13562000-04-04 14:38:59 +10001067
1068#ifdef PACKET_DEBUG
1069 fprintf(stderr, "plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001070 sshbuf_dump(state->outgoing_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001071#endif
1072
1073 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001074 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001075 /* skip header, compress only payload */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001076 if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
1077 goto out;
1078 sshbuf_reset(state->compression_buffer);
1079 if ((r = compress_buffer(ssh, state->outgoing_packet,
1080 state->compression_buffer)) != 0)
1081 goto out;
1082 sshbuf_reset(state->outgoing_packet);
1083 if ((r = sshbuf_put(state->outgoing_packet,
1084 "\0\0\0\0\0", 5)) != 0 ||
1085 (r = sshbuf_putb(state->outgoing_packet,
1086 state->compression_buffer)) != 0)
1087 goto out;
1088 DBG(debug("compression: raw %d compressed %zd", len,
1089 sshbuf_len(state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001090 }
1091
1092 /* sizeof (packet_len + pad_len + payload) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001093 len = sshbuf_len(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001094
1095 /*
1096 * calc size of padding, alloc space, get random data,
1097 * minimum padding is 4 bytes
1098 */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001099 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +10001100 padlen = block_size - (len % block_size);
1101 if (padlen < 4)
1102 padlen += block_size;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001103 if (state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +11001104 /* will wrap if extra_pad+padlen > 255 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001105 state->extra_pad =
1106 roundup(state->extra_pad, block_size);
1107 pad = state->extra_pad -
1108 ((len + padlen) % state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +10001109 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001110 __func__, pad, len, padlen, state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +11001111 padlen += pad;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001112 state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +11001113 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001114 if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
1115 goto out;
1116 if (enc && !state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +10001117 /* random padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001118 arc4random_buf(cp, padlen);
Damien Millere247cc42000-05-07 12:03:14 +10001119 } else {
1120 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +11001121 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001122 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001123 /* sizeof (packet_len + pad_len + payload + padding) */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001124 len = sshbuf_len(state->outgoing_packet);
1125 cp = sshbuf_mutable_ptr(state->outgoing_packet);
1126 if (cp == NULL) {
1127 r = SSH_ERR_INTERNAL_ERROR;
1128 goto out;
1129 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001130 /* packet_length includes payload, padding and padding length field */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001131 POKE_U32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001132 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001133 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
1134 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +10001135
1136 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001137 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001138 if ((r = mac_compute(mac, state->p_send.seqnr,
1139 sshbuf_ptr(state->outgoing_packet), len,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001140 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001141 goto out;
1142 DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001143 }
1144 /* encrypt packet and append to output buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001145 if ((r = sshbuf_reserve(state->output,
1146 sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
1147 goto out;
1148 if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
1149 sshbuf_ptr(state->outgoing_packet),
1150 len - aadlen, aadlen, authlen)) != 0)
1151 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001152 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +11001153 if (mac && mac->enabled) {
1154 if (mac->etm) {
1155 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001156 if ((r = mac_compute(mac, state->p_send.seqnr,
1157 cp, len, macbuf, sizeof(macbuf))) != 0)
1158 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001159 DBG(debug("done calc MAC(EtM) out #%d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001160 state->p_send.seqnr));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001161 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001162 if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
1163 goto out;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001164 }
Damien Miller33b13562000-04-04 14:38:59 +10001165#ifdef PACKET_DEBUG
1166 fprintf(stderr, "encrypted: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001167 sshbuf_dump(state->output, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001168#endif
Damien Miller4af51302000-04-16 11:18:38 +10001169 /* increment sequence number for outgoing packets */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001170 if (++state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001171 logit("outgoing seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001172 if (++state->p_send.packets == 0)
1173 if (!(ssh->compat & SSH_BUG_NOREKEY))
1174 return SSH_ERR_NEED_REKEY;
1175 state->p_send.blocks += len / block_size;
1176 state->p_send.bytes += len;
1177 sshbuf_reset(state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001178
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001179 if (type == SSH2_MSG_NEWKEYS)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001180 r = ssh_set_newkeys(ssh, MODE_OUT);
1181 else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
1182 r = ssh_packet_enable_delayed_compress(ssh);
1183 else
1184 r = 0;
1185 out:
1186 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001187}
1188
markus@openbsd.org091c3022015-01-19 19:52:16 +00001189int
1190ssh_packet_send2(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10001191{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001192 struct session_state *state = ssh->state;
Damien Millera5539d22003-04-09 20:50:06 +10001193 struct packet *p;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001194 u_char type;
1195 int r;
Damien Millera5539d22003-04-09 20:50:06 +10001196
markus@openbsd.org091c3022015-01-19 19:52:16 +00001197 type = sshbuf_ptr(state->outgoing_packet)[5];
Damien Millera5539d22003-04-09 20:50:06 +10001198
1199 /* during rekeying we can only send key exchange messages */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001200 if (state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001201 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1202 (type > SSH2_MSG_TRANSPORT_MAX) ||
1203 (type == SSH2_MSG_SERVICE_REQUEST) ||
1204 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +10001205 debug("enqueue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001206 p = calloc(1, sizeof(*p));
1207 if (p == NULL)
1208 return SSH_ERR_ALLOC_FAIL;
Damien Millera5539d22003-04-09 20:50:06 +10001209 p->type = type;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001210 p->payload = state->outgoing_packet;
1211 TAILQ_INSERT_TAIL(&state->outgoing, p, next);
1212 state->outgoing_packet = sshbuf_new();
1213 if (state->outgoing_packet == NULL)
1214 return SSH_ERR_ALLOC_FAIL;
1215 return 0;
Damien Millera5539d22003-04-09 20:50:06 +10001216 }
1217 }
1218
1219 /* rekeying starts with sending KEXINIT */
1220 if (type == SSH2_MSG_KEXINIT)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001221 state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001222
markus@openbsd.org091c3022015-01-19 19:52:16 +00001223 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1224 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001225
1226 /* after a NEWKEYS message we can send the complete queue */
1227 if (type == SSH2_MSG_NEWKEYS) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001228 state->rekeying = 0;
1229 state->rekey_time = monotime();
1230 while ((p = TAILQ_FIRST(&state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001231 type = p->type;
1232 debug("dequeue packet: %u", type);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001233 sshbuf_free(state->outgoing_packet);
1234 state->outgoing_packet = p->payload;
1235 TAILQ_REMOVE(&state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001236 free(p);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001237 if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
1238 return r;
Damien Millera5539d22003-04-09 20:50:06 +10001239 }
1240 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001241 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001242}
1243
Damien Miller33b13562000-04-04 14:38:59 +10001244/*
Damien Miller5428f641999-11-25 11:54:57 +11001245 * Waits until a packet has been received, and returns its type. Note that
1246 * no other data is processed until this returns, so this function should not
1247 * be used during the interactive session.
1248 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249
1250int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001251ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001253 struct session_state *state = ssh->state;
1254 int len, r, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001255 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001256 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001257 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258
Darren Tucker99bb7612008-06-13 22:02:50 +10001259 DBG(debug("packet_read()"));
1260
markus@openbsd.org091c3022015-01-19 19:52:16 +00001261 setp = (fd_set *)calloc(howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001262 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001263 if (setp == NULL)
1264 return SSH_ERR_ALLOC_FAIL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001265
Damien Miller95def091999-11-25 00:26:21 +11001266 /* Since we are blocking, ensure that all written packets have been sent. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001267 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268
Damien Miller95def091999-11-25 00:26:21 +11001269 /* Stay in the loop until we have received a complete packet. */
1270 for (;;) {
1271 /* Try to read a packet from the buffer. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001272 r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
1273 if (r != 0)
1274 break;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001275 if (!compat20 && (
markus@openbsd.org091c3022015-01-19 19:52:16 +00001276 *typep == SSH_SMSG_SUCCESS
1277 || *typep == SSH_SMSG_FAILURE
1278 || *typep == SSH_CMSG_EOF
1279 || *typep == SSH_CMSG_EXIT_CONFIRMATION))
1280 if ((r = sshpkt_get_end(ssh)) != 0)
1281 break;
Damien Miller95def091999-11-25 00:26:21 +11001282 /* If we got a packet, return it. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001283 if (*typep != SSH_MSG_NONE)
1284 break;
Damien Miller5428f641999-11-25 11:54:57 +11001285 /*
1286 * Otherwise, wait for some data to arrive, add it to the
1287 * buffer, and try again.
1288 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001289 memset(setp, 0, howmany(state->connection_in + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001290 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001291 FD_SET(state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001292
markus@openbsd.org091c3022015-01-19 19:52:16 +00001293 if (state->packet_timeout_ms > 0) {
1294 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001295 timeoutp = &timeout;
1296 }
Damien Miller95def091999-11-25 00:26:21 +11001297 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001298 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001299 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001300 ms_to_timeval(&timeout, ms_remain);
1301 gettimeofday(&start, NULL);
1302 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001303 if ((r = select(state->connection_in + 1, setp,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001304 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001305 break;
Damien Millerea437422009-10-02 11:49:03 +10001306 if (errno != EAGAIN && errno != EINTR &&
1307 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001308 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001309 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001310 continue;
1311 ms_subtract_diff(&start, &ms_remain);
1312 if (ms_remain <= 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001313 r = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001314 break;
1315 }
1316 }
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001317 if (r == 0)
1318 return SSH_ERR_CONN_TIMEOUT;
Damien Miller95def091999-11-25 00:26:21 +11001319 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001320 do {
1321 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001322 len = roaming_read(state->connection_in, buf,
Darren Tuckerc5564e12009-06-21 18:53:53 +10001323 sizeof(buf), &cont);
1324 } while (len == 0 && cont);
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001325 if (len == 0)
1326 return SSH_ERR_CONN_CLOSED;
Damien Miller95def091999-11-25 00:26:21 +11001327 if (len < 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001328 return SSH_ERR_SYSTEM_ERROR;
1329
Damien Miller95def091999-11-25 00:26:21 +11001330 /* Append it to the buffer. */
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001331 if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
1332 return r;
Damien Miller95def091999-11-25 00:26:21 +11001333 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001334 free(setp);
1335 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001336}
1337
Damien Miller278f9072001-12-21 15:00:19 +11001338int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001339ssh_packet_read(struct ssh *ssh)
Damien Miller278f9072001-12-21 15:00:19 +11001340{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001341 u_char type;
1342 int r;
1343
1344 if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
1345 fatal("%s: %s", __func__, ssh_err(r));
1346 return type;
Damien Miller278f9072001-12-21 15:00:19 +11001347}
1348
Damien Miller5428f641999-11-25 11:54:57 +11001349/*
1350 * Waits until a packet has been received, verifies that its type matches
1351 * that given, and gives a fatal error and exits if there is a mismatch.
1352 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001353
1354void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001355ssh_packet_read_expect(struct ssh *ssh, int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001356{
Damien Miller95def091999-11-25 00:26:21 +11001357 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001358
markus@openbsd.org091c3022015-01-19 19:52:16 +00001359 type = ssh_packet_read(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001360 if (type != expected_type)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001361 ssh_packet_disconnect(ssh,
1362 "Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001363 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364}
1365
1366/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001367 * packet_process_incoming. If so, reads the packet; otherwise returns
1368 * SSH_MSG_NONE. This does not wait for data from the connection.
1369 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001370 * SSH_MSG_DISCONNECT is handled specially here. Also,
1371 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1372 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001373 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001374
markus@openbsd.org091c3022015-01-19 19:52:16 +00001375int
1376ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001377{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001378 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001379 u_int len, padded_len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001380 const u_char *cp;
1381 u_char *p;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001382 u_int checksum, stored_checksum;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001383 int r;
1384
1385 *typep = SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001386
Damien Miller95def091999-11-25 00:26:21 +11001387 /* Check if input size is less than minimum packet size. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001388 if (sshbuf_len(state->input) < 4 + 8)
1389 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001390 /* Get length of incoming packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001391 len = PEEK_U32(sshbuf_ptr(state->input));
Damien Miller95def091999-11-25 00:26:21 +11001392 if (len < 1 + 2 + 2 || len > 256 * 1024)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001393 ssh_packet_disconnect(ssh, "Bad packet length %u.",
1394 len);
Damien Miller95def091999-11-25 00:26:21 +11001395 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396
Damien Miller95def091999-11-25 00:26:21 +11001397 /* Check if the packet has been entirely received. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001398 if (sshbuf_len(state->input) < 4 + padded_len)
1399 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400
Damien Miller95def091999-11-25 00:26:21 +11001401 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402
Damien Miller95def091999-11-25 00:26:21 +11001403 /* Consume packet length. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001404 if ((r = sshbuf_consume(state->input, 4)) != 0)
1405 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001406
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001407 /*
1408 * Cryptographic attack detector for ssh
1409 * (C)1998 CORE-SDI, Buenos Aires Argentina
1410 * Ariel Futoransky(futo@core-sdi.com)
1411 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001412 if (!state->receive_context.plaintext) {
1413 switch (detect_attack(&state->deattack,
1414 sshbuf_ptr(state->input), padded_len)) {
1415 case DEATTACK_OK:
1416 break;
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001417 case DEATTACK_DETECTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001418 ssh_packet_disconnect(ssh,
1419 "crc32 compensation attack: network attack detected"
1420 );
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001421 case DEATTACK_DOS_DETECTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001422 ssh_packet_disconnect(ssh,
1423 "deattack denial of service detected");
1424 default:
1425 ssh_packet_disconnect(ssh, "deattack error");
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001426 }
1427 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001428
1429 /* Decrypt data to incoming_packet. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001430 sshbuf_reset(state->incoming_packet);
1431 if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
1432 goto out;
1433 if ((r = cipher_crypt(&state->receive_context, 0, p,
1434 sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
1435 goto out;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001436
markus@openbsd.org091c3022015-01-19 19:52:16 +00001437 if ((r = sshbuf_consume(state->input, padded_len)) != 0)
1438 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001439
1440#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001441 fprintf(stderr, "read_poll plain: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001442 sshbuf_dump(state->incoming_packet, stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444
Damien Miller95def091999-11-25 00:26:21 +11001445 /* Compute packet checksum. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001446 checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
1447 sshbuf_len(state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001448
Damien Miller95def091999-11-25 00:26:21 +11001449 /* Skip padding. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001450 if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
1451 goto out;
Damien Millerfd7c9111999-11-08 16:15:55 +11001452
Damien Miller95def091999-11-25 00:26:21 +11001453 /* Test check bytes. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001454 if (len != sshbuf_len(state->incoming_packet))
1455 ssh_packet_disconnect(ssh,
1456 "packet_read_poll1: len %d != sshbuf_len %zd.",
1457 len, sshbuf_len(state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001458
markus@openbsd.org091c3022015-01-19 19:52:16 +00001459 cp = sshbuf_ptr(state->incoming_packet) + len - 4;
1460 stored_checksum = PEEK_U32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001461 if (checksum != stored_checksum)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001462 ssh_packet_disconnect(ssh,
1463 "Corrupted check bytes on input.");
1464 if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
1465 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466
markus@openbsd.org091c3022015-01-19 19:52:16 +00001467 if (state->packet_compression) {
1468 sshbuf_reset(state->compression_buffer);
1469 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1470 state->compression_buffer)) != 0)
1471 goto out;
1472 sshbuf_reset(state->incoming_packet);
1473 if ((r = sshbuf_putb(state->incoming_packet,
1474 state->compression_buffer)) != 0)
1475 goto out;
Damien Miller95def091999-11-25 00:26:21 +11001476 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001477 state->p_read.packets++;
1478 state->p_read.bytes += padded_len + 4;
1479 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1480 goto out;
1481 if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX)
1482 ssh_packet_disconnect(ssh,
1483 "Invalid ssh1 packet type: %d", *typep);
1484 r = 0;
1485 out:
1486 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001487}
Damien Miller95def091999-11-25 00:26:21 +11001488
markus@openbsd.org091c3022015-01-19 19:52:16 +00001489int
1490ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001491{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001492 struct session_state *state = ssh->state;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001493 u_int padlen, need;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001494 u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1495 u_int maclen, aadlen = 0, authlen = 0, block_size;
1496 struct sshenc *enc = NULL;
1497 struct sshmac *mac = NULL;
1498 struct sshcomp *comp = NULL;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001499 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001500
markus@openbsd.org091c3022015-01-19 19:52:16 +00001501 *typep = SSH_MSG_NONE;
Damien Miller13ae44c2009-01-28 16:38:41 +11001502
markus@openbsd.org091c3022015-01-19 19:52:16 +00001503 if (state->packet_discard)
1504 return 0;
1505
1506 if (state->newkeys[MODE_IN] != NULL) {
1507 enc = &state->newkeys[MODE_IN]->enc;
1508 mac = &state->newkeys[MODE_IN]->mac;
1509 comp = &state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001510 /* disable mac for authenticated encryption */
1511 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1512 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001513 }
1514 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001515 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001516 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001517
markus@openbsd.org091c3022015-01-19 19:52:16 +00001518 if (aadlen && state->packlen == 0) {
1519 if (cipher_get_length(&state->receive_context,
1520 &state->packlen, state->p_read.seqnr,
1521 sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
1522 return 0;
1523 if (state->packlen < 1 + 4 ||
1524 state->packlen > PACKET_MAX_SIZE) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001525#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001526 sshbuf_dump(state->input, stderr);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001527#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001528 logit("Bad packet length %u.", state->packlen);
1529 if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
1530 return r;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001531 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001532 sshbuf_reset(state->incoming_packet);
1533 } else if (state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001534 /*
1535 * check if input size is less than the cipher block size,
1536 * decrypt first block and extract length of incoming packet
1537 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001538 if (sshbuf_len(state->input) < block_size)
1539 return 0;
1540 sshbuf_reset(state->incoming_packet);
1541 if ((r = sshbuf_reserve(state->incoming_packet, block_size,
1542 &cp)) != 0)
1543 goto out;
1544 if ((r = cipher_crypt(&state->receive_context,
1545 state->p_send.seqnr, cp, sshbuf_ptr(state->input),
1546 block_size, 0, 0)) != 0)
1547 goto out;
1548 state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
1549 if (state->packlen < 1 + 4 ||
1550 state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001551#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001552 fprintf(stderr, "input: \n");
1553 sshbuf_dump(state->input, stderr);
1554 fprintf(stderr, "incoming_packet: \n");
1555 sshbuf_dump(state->incoming_packet, stderr);
Darren Tuckera8151da2003-09-22 21:06:46 +10001556#endif
markus@openbsd.org091c3022015-01-19 19:52:16 +00001557 logit("Bad packet length %u.", state->packlen);
1558 return ssh_packet_start_discard(ssh, enc, mac,
1559 state->packlen, PACKET_MAX_SIZE);
Damien Miller33b13562000-04-04 14:38:59 +10001560 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001561 if ((r = sshbuf_consume(state->input, block_size)) != 0)
1562 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001563 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001564 DBG(debug("input: packet len %u", state->packlen+4));
1565
Damien Milleraf43a7a2012-12-12 10:46:31 +11001566 if (aadlen) {
1567 /* only the payload is encrypted */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001568 need = state->packlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001569 } else {
1570 /*
1571 * the payload size and the payload are encrypted, but we
1572 * have a partial packet of block_size bytes
1573 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001574 need = 4 + state->packlen - block_size;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001575 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001576 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1577 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001578 if (need % block_size != 0) {
1579 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001580 need, block_size, need % block_size);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001581 return ssh_packet_start_discard(ssh, enc, mac,
1582 state->packlen, PACKET_MAX_SIZE - block_size);
Darren Tucker99d11a32008-12-01 21:40:48 +11001583 }
Damien Miller33b13562000-04-04 14:38:59 +10001584 /*
1585 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001586 * decrypt into incoming_packet:
1587 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001588 * 'need' bytes are encrypted, followed by either
1589 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001590 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001591 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001592 if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
1593 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001594#ifdef PACKET_DEBUG
1595 fprintf(stderr, "read_poll enc/full: ");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001596 sshbuf_dump(state->input, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001597#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001598 /* EtM: compute mac over encrypted input */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001599 if (mac && mac->enabled && mac->etm) {
1600 if ((r = mac_compute(mac, state->p_read.seqnr,
1601 sshbuf_ptr(state->input), aadlen + need,
markus@openbsd.org128343b2015-01-13 19:31:40 +00001602 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001603 goto out;
1604 }
1605 if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
1606 &cp)) != 0)
1607 goto out;
1608 if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
1609 sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
1610 goto out;
1611 if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
1612 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001613 /*
1614 * compute MAC over seqnr and packet,
1615 * increment sequence number for incoming packet
1616 */
Damien Miller4af51302000-04-16 11:18:38 +10001617 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001618 if (!mac->etm)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001619 if ((r = mac_compute(mac, state->p_read.seqnr,
1620 sshbuf_ptr(state->incoming_packet),
1621 sshbuf_len(state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001622 macbuf, sizeof(macbuf))) != 0)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001623 goto out;
1624 if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001625 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001626 logit("Corrupted MAC on input.");
1627 if (need > PACKET_MAX_SIZE)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001628 return SSH_ERR_INTERNAL_ERROR;
1629 return ssh_packet_start_discard(ssh, enc, mac,
1630 state->packlen, PACKET_MAX_SIZE - need);
Damien Miller13ae44c2009-01-28 16:38:41 +11001631 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001632
1633 DBG(debug("MAC #%d ok", state->p_read.seqnr));
1634 if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
1635 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001636 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001637 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001638 if (seqnr_p != NULL)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001639 *seqnr_p = state->p_read.seqnr;
1640 if (++state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001641 logit("incoming seqnr wraps around");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001642 if (++state->p_read.packets == 0)
1643 if (!(ssh->compat & SSH_BUG_NOREKEY))
1644 return SSH_ERR_NEED_REKEY;
1645 state->p_read.blocks += (state->packlen + 4) / block_size;
1646 state->p_read.bytes += state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001647
1648 /* get padlen */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001649 padlen = sshbuf_ptr(state->incoming_packet)[4];
Damien Miller33b13562000-04-04 14:38:59 +10001650 DBG(debug("input: padlen %d", padlen));
1651 if (padlen < 4)
markus@openbsd.org091c3022015-01-19 19:52:16 +00001652 ssh_packet_disconnect(ssh,
1653 "Corrupted padlen %d on input.", padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001654
1655 /* skip packet size + padlen, discard padding */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001656 if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
1657 ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
1658 goto out;
Damien Miller33b13562000-04-04 14:38:59 +10001659
markus@openbsd.org091c3022015-01-19 19:52:16 +00001660 DBG(debug("input: len before de-compress %zd",
1661 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001662 if (comp && comp->enabled) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001663 sshbuf_reset(state->compression_buffer);
1664 if ((r = uncompress_buffer(ssh, state->incoming_packet,
1665 state->compression_buffer)) != 0)
1666 goto out;
1667 sshbuf_reset(state->incoming_packet);
1668 if ((r = sshbuf_putb(state->incoming_packet,
1669 state->compression_buffer)) != 0)
1670 goto out;
1671 DBG(debug("input: len after de-compress %zd",
1672 sshbuf_len(state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001673 }
1674 /*
1675 * get packet type, implies consume.
1676 * return length of payload (without type field)
1677 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001678 if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
1679 goto out;
1680 if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN)
1681 ssh_packet_disconnect(ssh,
1682 "Invalid ssh2 packet type: %d", *typep);
1683 if (*typep == SSH2_MSG_NEWKEYS)
1684 r = ssh_set_newkeys(ssh, MODE_IN);
1685 else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
1686 r = ssh_packet_enable_delayed_compress(ssh);
1687 else
1688 r = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001689#ifdef PACKET_DEBUG
markus@openbsd.org091c3022015-01-19 19:52:16 +00001690 fprintf(stderr, "read/plain[%d]:\r\n", *typep);
1691 sshbuf_dump(state->incoming_packet, stderr);
Damien Miller33b13562000-04-04 14:38:59 +10001692#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001693 /* reset for next packet */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001694 state->packlen = 0;
1695 out:
1696 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001697}
1698
1699int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001700ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001701{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001702 struct session_state *state = ssh->state;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001703 u_int reason, seqnr;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001704 int r;
1705 u_char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001706
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001707 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001708 msg = NULL;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001709 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001710 r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
1711 if (r != 0)
1712 return r;
1713 if (*typep) {
1714 state->keep_alive_timeouts = 0;
1715 DBG(debug("received packet type %d", *typep));
Darren Tucker136e56f2008-06-08 12:49:30 +10001716 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001717 switch (*typep) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001718 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001719 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001720 break;
Damien Miller33b13562000-04-04 14:38:59 +10001721 case SSH2_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001722 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
1723 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1724 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
1725 if (msg)
1726 free(msg);
1727 return r;
1728 }
Damien Miller33b13562000-04-04 14:38:59 +10001729 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001730 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001731 break;
1732 case SSH2_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001733 if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
1734 (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1735 return r;
Damien Millerd5edefd2013-04-23 15:21:39 +10001736 /* Ignore normal client exit notifications */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001737 do_log2(ssh->state->server_side &&
Damien Millerd5edefd2013-04-23 15:21:39 +10001738 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1739 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1740 "Received disconnect from %s: %u: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001741 ssh_remote_ipaddr(ssh), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001742 free(msg);
markus@openbsd.org091c3022015-01-19 19:52:16 +00001743 return SSH_ERR_DISCONNECTED;
Damien Miller659811f2002-01-22 23:23:11 +11001744 case SSH2_MSG_UNIMPLEMENTED:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001745 if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
1746 return r;
Ben Lindstrom3f584742002-06-23 21:49:25 +00001747 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1748 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001749 break;
Damien Miller33b13562000-04-04 14:38:59 +10001750 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001751 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001752 }
Damien Miller33b13562000-04-04 14:38:59 +10001753 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001754 r = ssh_packet_read_poll1(ssh, typep);
1755 switch (*typep) {
Damien Millerce986542013-07-18 16:12:44 +10001756 case SSH_MSG_NONE:
1757 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001758 case SSH_MSG_IGNORE:
1759 break;
1760 case SSH_MSG_DEBUG:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001761 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1762 return r;
Damien Miller33b13562000-04-04 14:38:59 +10001763 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001764 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001765 break;
1766 case SSH_MSG_DISCONNECT:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001767 if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
1768 return r;
Damien Miller894926e2013-02-12 11:03:58 +11001769 error("Received disconnect from %s: %.400s",
markus@openbsd.org091c3022015-01-19 19:52:16 +00001770 ssh_remote_ipaddr(ssh), msg);
1771 free(msg);
1772 return SSH_ERR_DISCONNECTED;
Damien Miller33b13562000-04-04 14:38:59 +10001773 default:
markus@openbsd.org091c3022015-01-19 19:52:16 +00001774 DBG(debug("received packet type %d", *typep));
1775 return 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001776 }
Damien Miller33b13562000-04-04 14:38:59 +10001777 }
1778 }
1779}
1780
Damien Miller5428f641999-11-25 11:54:57 +11001781/*
1782 * Buffers the given amount of input characters. This is intended to be used
1783 * together with packet_read_poll.
1784 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001785
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001786int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001787ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001788{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001789 struct session_state *state = ssh->state;
1790 int r;
1791
1792 if (state->packet_discard) {
1793 state->keep_alive_timeouts = 0; /* ?? */
1794 if (len >= state->packet_discard) {
1795 if ((r = ssh_packet_stop_discard(ssh)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001796 return r;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001797 }
1798 state->packet_discard -= len;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001799 return 0;
Damien Miller13ae44c2009-01-28 16:38:41 +11001800 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001801 if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +00001802 return r;
1803
1804 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10001805}
1806
Damien Miller4af51302000-04-16 11:18:38 +10001807int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001808ssh_packet_remaining(struct ssh *ssh)
Damien Miller4af51302000-04-16 11:18:38 +10001809{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001810 return sshbuf_len(ssh->state->incoming_packet);
Damien Millerda108ec2010-08-31 22:36:39 +10001811}
1812
Damien Miller5428f641999-11-25 11:54:57 +11001813/*
1814 * Sends a diagnostic message from the server to the client. This message
1815 * can be sent at any time (but not while constructing another message). The
1816 * message is printed immediately, but only if the client is being executed
1817 * in verbose mode. These messages are primarily intended to ease debugging
1818 * authentication problems. The length of the formatted message must not
1819 * exceed 1024 bytes. This will automatically call packet_write_wait.
1820 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001821
1822void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001823ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001824{
Damien Miller95def091999-11-25 00:26:21 +11001825 char buf[1024];
1826 va_list args;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001827 int r;
Damien Miller95def091999-11-25 00:26:21 +11001828
markus@openbsd.org091c3022015-01-19 19:52:16 +00001829 if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
Ben Lindstroma14ee472000-12-07 01:24:58 +00001830 return;
1831
Damien Miller95def091999-11-25 00:26:21 +11001832 va_start(args, fmt);
1833 vsnprintf(buf, sizeof(buf), fmt, args);
1834 va_end(args);
1835
Damien Miller7c8af4f2000-05-01 08:24:07 +10001836 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001837 if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
1838 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
1839 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1840 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1841 (r = sshpkt_send(ssh)) != 0)
1842 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001843 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001844 if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
1845 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1846 (r = sshpkt_send(ssh)) != 0)
1847 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7c8af4f2000-05-01 08:24:07 +10001848 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001849 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001850}
1851
Damien Miller5428f641999-11-25 11:54:57 +11001852/*
1853 * Logs the error plus constructs and sends a disconnect packet, closes the
1854 * connection, and exits. This function never returns. The error message
1855 * should not contain a newline. The length of the formatted message must
1856 * not exceed 1024 bytes.
1857 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001858
1859void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001860ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001861{
Damien Miller95def091999-11-25 00:26:21 +11001862 char buf[1024];
1863 va_list args;
1864 static int disconnecting = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001865 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001866
Damien Miller95def091999-11-25 00:26:21 +11001867 if (disconnecting) /* Guard against recursive invocations. */
1868 fatal("packet_disconnect called recursively.");
1869 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001870
Damien Miller5428f641999-11-25 11:54:57 +11001871 /*
1872 * Format the message. Note that the caller must make sure the
1873 * message is of limited size.
1874 */
Damien Miller95def091999-11-25 00:26:21 +11001875 va_start(args, fmt);
1876 vsnprintf(buf, sizeof(buf), fmt, args);
1877 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001879 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001880 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001881
Damien Miller95def091999-11-25 00:26:21 +11001882 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001883 if (compat20) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001884 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
1885 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
1886 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1887 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1888 (r = sshpkt_send(ssh)) != 0)
1889 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001890 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001891 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
1892 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
1893 (r = sshpkt_send(ssh)) != 0)
1894 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001895 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001896 ssh_packet_write_wait(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001897
Damien Miller95def091999-11-25 00:26:21 +11001898 /* Close the connection. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001899 ssh_packet_close(ssh);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001900 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001901}
1902
Damien Miller5428f641999-11-25 11:54:57 +11001903/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001904
1905void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001906ssh_packet_write_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001907{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001908 struct session_state *state = ssh->state;
1909 int len = sshbuf_len(state->output);
1910 int cont, r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001911
Damien Miller95def091999-11-25 00:26:21 +11001912 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001913 cont = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001914 len = roaming_write(state->connection_out,
1915 sshbuf_ptr(state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001916 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001917 if (errno == EINTR || errno == EAGAIN ||
1918 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001919 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001920 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001921 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001922 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001923 fatal("Write connection closed");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001924 if ((r = sshbuf_consume(state->output, len)) != 0)
1925 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001926 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001927}
1928
Damien Miller5428f641999-11-25 11:54:57 +11001929/*
1930 * Calls packet_write_poll repeatedly until all pending output data has been
1931 * written.
1932 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001933
1934void
markus@openbsd.org091c3022015-01-19 19:52:16 +00001935ssh_packet_write_wait(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001936{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001937 fd_set *setp;
Damien Millerce986542013-07-18 16:12:44 +10001938 int ret, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001939 struct timeval start, timeout, *timeoutp = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001940 struct session_state *state = ssh->state;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001941
markus@openbsd.org091c3022015-01-19 19:52:16 +00001942 setp = (fd_set *)calloc(howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001943 NFDBITS), sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001944 if (setp == NULL)
1945 fatal("%s: calloc failed", __func__);
1946 ssh_packet_write_poll(ssh);
1947 while (ssh_packet_have_data_to_write(ssh)) {
1948 memset(setp, 0, howmany(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001949 NFDBITS) * sizeof(fd_mask));
markus@openbsd.org091c3022015-01-19 19:52:16 +00001950 FD_SET(state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001951
markus@openbsd.org091c3022015-01-19 19:52:16 +00001952 if (state->packet_timeout_ms > 0) {
1953 ms_remain = state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001954 timeoutp = &timeout;
1955 }
1956 for (;;) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001957 if (state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001958 ms_to_timeval(&timeout, ms_remain);
1959 gettimeofday(&start, NULL);
1960 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001961 if ((ret = select(state->connection_out + 1,
Darren Tuckerf7288d72009-06-21 18:12:20 +10001962 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001963 break;
Damien Millerea437422009-10-02 11:49:03 +10001964 if (errno != EAGAIN && errno != EINTR &&
1965 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001966 break;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001967 if (state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001968 continue;
1969 ms_subtract_diff(&start, &ms_remain);
1970 if (ms_remain <= 0) {
1971 ret = 0;
1972 break;
1973 }
1974 }
1975 if (ret == 0) {
1976 logit("Connection to %.200s timed out while "
markus@openbsd.org091c3022015-01-19 19:52:16 +00001977 "waiting to write", ssh_remote_ipaddr(ssh));
Darren Tucker3fc464e2008-06-13 06:42:45 +10001978 cleanup_exit(255);
1979 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00001980 ssh_packet_write_poll(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001981 }
Darren Tuckera627d422013-06-02 07:31:17 +10001982 free(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001983}
1984
1985/* Returns true if there is buffered data to write to the connection. */
1986
1987int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001988ssh_packet_have_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001989{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001990 return sshbuf_len(ssh->state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001991}
1992
1993/* Returns true if there is not too much data to write to the connection. */
1994
1995int
markus@openbsd.org091c3022015-01-19 19:52:16 +00001996ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001997{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001998 if (ssh->state->interactive_mode)
1999 return sshbuf_len(ssh->state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11002000 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002001 return sshbuf_len(ssh->state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002002}
2003
markus@openbsd.org091c3022015-01-19 19:52:16 +00002004void
2005ssh_packet_set_tos(struct ssh *ssh, int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00002006{
Damien Millerd2ac5d72011-05-15 08:43:13 +10002007#ifndef IP_TOS_IS_BROKEN
markus@openbsd.org091c3022015-01-19 19:52:16 +00002008 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstroma7433982002-12-23 02:41:41 +00002009 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002010 switch (ssh_packet_connection_af(ssh)) {
Damien Millerd2ac5d72011-05-15 08:43:13 +10002011# ifdef IP_TOS
2012 case AF_INET:
2013 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002014 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002015 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
2016 error("setsockopt IP_TOS %d: %.100s:",
2017 tos, strerror(errno));
2018 break;
2019# endif /* IP_TOS */
2020# ifdef IPV6_TCLASS
2021 case AF_INET6:
2022 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002023 if (setsockopt(ssh->state->connection_in,
Damien Millerd2ac5d72011-05-15 08:43:13 +10002024 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
2025 error("setsockopt IPV6_TCLASS %d: %.100s:",
2026 tos, strerror(errno));
2027 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10002028# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10002029 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10002030#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11002031}
Ben Lindstroma7433982002-12-23 02:41:41 +00002032
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002033/* Informs that the current session is interactive. Sets IP flags for that. */
2034
2035void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002036ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002037{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002038 struct session_state *state = ssh->state;
2039
2040 if (state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002041 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002042 state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00002043
Damien Miller95def091999-11-25 00:26:21 +11002044 /* Record that we are in interactive mode. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002045 state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002046
Damien Miller34132e52000-01-14 15:45:46 +11002047 /* Only set socket options if using a socket. */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002048 if (!ssh_packet_connection_is_on_socket(ssh))
Ben Lindstrom93b6b772003-04-27 17:55:33 +00002049 return;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002050 set_nodelay(state->connection_in);
2051 ssh_packet_set_tos(ssh, interactive ? qos_interactive :
2052 qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002053}
2054
2055/* Returns true if the current connection is interactive. */
2056
2057int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002058ssh_packet_is_interactive(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002059{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002060 return ssh->state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002061}
Damien Miller6162d121999-11-21 13:23:52 +11002062
Darren Tucker1f8311c2004-05-13 16:39:33 +10002063int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002064ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11002065{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002066 struct session_state *state = ssh->state;
2067
2068 if (state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10002069 logit("packet_set_maxsize: called twice: old %d new %d",
markus@openbsd.org091c3022015-01-19 19:52:16 +00002070 state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11002071 return -1;
2072 }
2073 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10002074 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11002075 return -1;
2076 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002077 state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00002078 debug("packet_set_maxsize: setting to %d", s);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002079 state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11002080 return s;
Damien Miller6162d121999-11-21 13:23:52 +11002081}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002082
Darren Tuckerf7288d72009-06-21 18:12:20 +10002083int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002084ssh_packet_inc_alive_timeouts(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002085{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002086 return ++ssh->state->keep_alive_timeouts;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002087}
2088
2089void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002090ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002091{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002092 ssh->state->keep_alive_timeouts = ka;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002093}
2094
2095u_int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002096ssh_packet_get_maxsize(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002097{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002098 return ssh->state->max_packet_size;
Damien Miller9f643902001-11-12 11:02:52 +11002099}
2100
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002101/*
2102 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00002103 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002104 * byte SSH_MSG_IGNORE
2105 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00002106 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002107 * All implementations MUST understand (and ignore) this message at any
2108 * time (after receiving the protocol version). No implementation is
2109 * required to send them. This message can be used as an additional
2110 * protection measure against advanced traffic analysis techniques.
2111 */
Ben Lindstrome229b252001-03-05 06:28:06 +00002112void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002113ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Ben Lindstrome229b252001-03-05 06:28:06 +00002114{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002115 u_int32_t rnd = 0;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002116 int r, i;
Ben Lindstrome229b252001-03-05 06:28:06 +00002117
markus@openbsd.org091c3022015-01-19 19:52:16 +00002118 if ((r = sshpkt_start(ssh, compat20 ?
2119 SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
2120 (r = sshpkt_put_u32(ssh, nbytes)) != 0)
2121 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller9f0f5c62001-12-21 14:45:46 +11002122 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002123 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002124 rnd = arc4random();
markus@openbsd.org091c3022015-01-19 19:52:16 +00002125 if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
2126 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002127 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00002128 }
2129}
Damien Millera5539d22003-04-09 20:50:06 +10002130
Darren Tucker1f8311c2004-05-13 16:39:33 +10002131#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10002132int
markus@openbsd.org091c3022015-01-19 19:52:16 +00002133ssh_packet_need_rekeying(struct ssh *ssh)
Damien Millera5539d22003-04-09 20:50:06 +10002134{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002135 struct session_state *state = ssh->state;
2136
2137 if (ssh->compat & SSH_BUG_NOREKEY)
Damien Millera5539d22003-04-09 20:50:06 +10002138 return 0;
2139 return
markus@openbsd.org091c3022015-01-19 19:52:16 +00002140 (state->p_send.packets > MAX_PACKETS) ||
2141 (state->p_read.packets > MAX_PACKETS) ||
2142 (state->max_blocks_out &&
2143 (state->p_send.blocks > state->max_blocks_out)) ||
2144 (state->max_blocks_in &&
2145 (state->p_read.blocks > state->max_blocks_in)) ||
2146 (state->rekey_interval != 0 && state->rekey_time +
2147 state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10002148}
2149
2150void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002151ssh_packet_set_rekey_limits(struct ssh *ssh, u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10002152{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002153 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
2154 (int)seconds);
markus@openbsd.org091c3022015-01-19 19:52:16 +00002155 ssh->state->rekey_limit = bytes;
2156 ssh->state->rekey_interval = seconds;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002157 /*
2158 * We set the time here so that in post-auth privsep slave we count
2159 * from the completion of the authentication.
2160 */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002161 ssh->state->rekey_time = monotime();
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002162}
2163
2164time_t
markus@openbsd.org091c3022015-01-19 19:52:16 +00002165ssh_packet_get_rekey_timeout(struct ssh *ssh)
Darren Tuckerc53c2af2013-05-16 20:28:16 +10002166{
2167 time_t seconds;
2168
markus@openbsd.org091c3022015-01-19 19:52:16 +00002169 seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002170 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002171 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002172}
Damien Miller9786e6e2005-07-26 21:54:56 +10002173
2174void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002175ssh_packet_set_server(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002176{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002177 ssh->state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002178}
2179
2180void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002181ssh_packet_set_authenticated(struct ssh *ssh)
Damien Miller9786e6e2005-07-26 21:54:56 +10002182{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002183 ssh->state->after_authentication = 1;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002184}
2185
2186void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002187ssh_packet_get_input(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002188{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002189 return (void *)ssh->state->input;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002190}
2191
2192void *
markus@openbsd.org091c3022015-01-19 19:52:16 +00002193ssh_packet_get_output(struct ssh *ssh)
Darren Tuckerf7288d72009-06-21 18:12:20 +10002194{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002195 return (void *)ssh->state->output;
Darren Tuckerf7288d72009-06-21 18:12:20 +10002196}
2197
markus@openbsd.org091c3022015-01-19 19:52:16 +00002198/* XXX TODO update roaming to new API (does not work anyway) */
Darren Tuckere841eb02009-07-06 07:11:13 +10002199/*
2200 * Save the state for the real connection, and use a separate state when
2201 * resuming a suspended connection.
2202 */
2203void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002204ssh_packet_backup_state(struct ssh *ssh,
2205 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002206{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002207 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002208
markus@openbsd.org091c3022015-01-19 19:52:16 +00002209 close(ssh->state->connection_in);
2210 ssh->state->connection_in = -1;
2211 close(ssh->state->connection_out);
2212 ssh->state->connection_out = -1;
Darren Tuckere841eb02009-07-06 07:11:13 +10002213 if (backup_state)
2214 tmp = backup_state;
2215 else
markus@openbsd.org091c3022015-01-19 19:52:16 +00002216 tmp = ssh_alloc_session_state();
2217 backup_state = ssh;
2218 ssh = tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002219}
2220
markus@openbsd.org091c3022015-01-19 19:52:16 +00002221/* XXX FIXME FIXME FIXME */
Darren Tuckere841eb02009-07-06 07:11:13 +10002222/*
2223 * Swap in the old state when resuming a connecion.
2224 */
2225void
markus@openbsd.org091c3022015-01-19 19:52:16 +00002226ssh_packet_restore_state(struct ssh *ssh,
2227 struct ssh *backup_state)
Darren Tuckere841eb02009-07-06 07:11:13 +10002228{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002229 struct ssh *tmp;
Darren Tuckere841eb02009-07-06 07:11:13 +10002230 u_int len;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002231 int r;
Darren Tuckere841eb02009-07-06 07:11:13 +10002232
2233 tmp = backup_state;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002234 backup_state = ssh;
2235 ssh = tmp;
2236 ssh->state->connection_in = backup_state->state->connection_in;
2237 backup_state->state->connection_in = -1;
2238 ssh->state->connection_out = backup_state->state->connection_out;
2239 backup_state->state->connection_out = -1;
2240 len = sshbuf_len(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002241 if (len > 0) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002242 if ((r = sshbuf_putb(ssh->state->input,
2243 backup_state->state->input)) != 0)
2244 fatal("%s: %s", __func__, ssh_err(r));
2245 sshbuf_reset(backup_state->state->input);
Darren Tuckere841eb02009-07-06 07:11:13 +10002246 add_recv_bytes(len);
2247 }
2248}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002249
2250/* Reset after_authentication and reset compression in post-auth privsep */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002251static int
2252ssh_packet_set_postauth(struct ssh *ssh)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002253{
markus@openbsd.org091c3022015-01-19 19:52:16 +00002254 struct sshcomp *comp;
2255 int r, mode;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002256
2257 debug("%s: called", __func__);
2258 /* This was set in net child, but is not visible in user child */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002259 ssh->state->after_authentication = 1;
2260 ssh->state->rekeying = 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002261 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00002262 if (ssh->state->newkeys[mode] == NULL)
Damien Millerc31a0cd2014-05-15 14:37:39 +10002263 continue;
markus@openbsd.org091c3022015-01-19 19:52:16 +00002264 comp = &ssh->state->newkeys[mode]->comp;
2265 if (comp && comp->enabled &&
2266 (r = ssh_packet_init_compression(ssh)) != 0)
2267 return r;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002268 }
markus@openbsd.org091c3022015-01-19 19:52:16 +00002269 return 0;
2270}
2271
2272/* Packet state (de-)serialization for privsep */
2273
2274/* turn kex into a blob for packet state serialization */
2275static int
2276kex_to_blob(struct sshbuf *m, struct kex *kex)
2277{
2278 int r;
2279
2280 if ((r = sshbuf_put_string(m, kex->session_id,
2281 kex->session_id_len)) != 0 ||
2282 (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
2283 (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
2284 (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
2285 (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
2286 (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
2287 (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
2288 (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
2289 (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
2290 return r;
2291 return 0;
2292}
2293
2294/* turn key exchange results into a blob for packet state serialization */
2295static int
2296newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2297{
2298 struct sshbuf *b;
2299 struct sshcipher_ctx *cc;
2300 struct sshcomp *comp;
2301 struct sshenc *enc;
2302 struct sshmac *mac;
2303 struct newkeys *newkey;
2304 int r;
2305
2306 if ((newkey = ssh->state->newkeys[mode]) == NULL)
2307 return SSH_ERR_INTERNAL_ERROR;
2308 enc = &newkey->enc;
2309 mac = &newkey->mac;
2310 comp = &newkey->comp;
2311 cc = (mode == MODE_OUT) ? &ssh->state->send_context :
2312 &ssh->state->receive_context;
2313 if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
2314 return r;
2315 if ((b = sshbuf_new()) == NULL)
2316 return SSH_ERR_ALLOC_FAIL;
2317 /* The cipher struct is constant and shared, you export pointer */
2318 if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
2319 (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2320 (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
2321 (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
2322 (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
2323 (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
2324 goto out;
2325 if (cipher_authlen(enc->cipher) == 0) {
2326 if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
2327 (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
2328 (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
2329 goto out;
2330 }
2331 if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
2332 (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
2333 (r = sshbuf_put_cstring(b, comp->name)) != 0)
2334 goto out;
2335 r = sshbuf_put_stringb(m, b);
2336 out:
2337 if (b != NULL)
2338 sshbuf_free(b);
2339 return r;
2340}
2341
2342/* serialize packet state into a blob */
2343int
2344ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
2345{
2346 struct session_state *state = ssh->state;
2347 u_char *p;
2348 size_t slen, rlen;
2349 int r, ssh1cipher;
2350
2351 if (!compat20) {
2352 ssh1cipher = cipher_get_number(state->receive_context.cipher);
2353 slen = cipher_get_keyiv_len(&state->send_context);
2354 rlen = cipher_get_keyiv_len(&state->receive_context);
2355 if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
2356 (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
2357 (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
2358 (r = sshbuf_put_u32(m, slen)) != 0 ||
2359 (r = sshbuf_reserve(m, slen, &p)) != 0 ||
2360 (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
2361 (r = sshbuf_put_u32(m, rlen)) != 0 ||
2362 (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
2363 (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
2364 return r;
2365 } else {
2366 if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
2367 (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
2368 (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
2369 (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
2370 (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
2371 (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
2372 (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
2373 (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
2374 (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
2375 (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
2376 (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
2377 return r;
2378 }
2379
2380 slen = cipher_get_keycontext(&state->send_context, NULL);
2381 rlen = cipher_get_keycontext(&state->receive_context, NULL);
2382 if ((r = sshbuf_put_u32(m, slen)) != 0 ||
2383 (r = sshbuf_reserve(m, slen, &p)) != 0)
2384 return r;
2385 if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
2386 return SSH_ERR_INTERNAL_ERROR;
2387 if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
2388 (r = sshbuf_reserve(m, rlen, &p)) != 0)
2389 return r;
2390 if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
2391 return SSH_ERR_INTERNAL_ERROR;
2392
2393 if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
2394 (r = sshbuf_put_stringb(m, state->input)) != 0 ||
2395 (r = sshbuf_put_stringb(m, state->output)) != 0)
2396 return r;
2397
2398 if (compat20) {
2399 if ((r = sshbuf_put_u64(m, get_sent_bytes())) != 0 ||
2400 (r = sshbuf_put_u64(m, get_recv_bytes())) != 0)
2401 return r;
2402 }
2403 return 0;
2404}
2405
2406/* restore key exchange results from blob for packet state de-serialization */
2407static int
2408newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
2409{
2410 struct sshbuf *b = NULL;
2411 struct sshcomp *comp;
2412 struct sshenc *enc;
2413 struct sshmac *mac;
2414 struct newkeys *newkey = NULL;
2415 size_t keylen, ivlen, maclen;
2416 int r;
2417
2418 if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
2419 r = SSH_ERR_ALLOC_FAIL;
2420 goto out;
2421 }
2422 if ((r = sshbuf_froms(m, &b)) != 0)
2423 goto out;
2424#ifdef DEBUG_PK
2425 sshbuf_dump(b, stderr);
2426#endif
2427 enc = &newkey->enc;
2428 mac = &newkey->mac;
2429 comp = &newkey->comp;
2430
2431 if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
2432 (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
2433 (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
2434 (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
2435 (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
2436 (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
2437 goto out;
2438 if (cipher_authlen(enc->cipher) == 0) {
2439 if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
2440 goto out;
2441 if ((r = mac_setup(mac, mac->name)) != 0)
2442 goto out;
2443 if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
2444 (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
2445 goto out;
2446 if (maclen > mac->key_len) {
2447 r = SSH_ERR_INVALID_FORMAT;
2448 goto out;
2449 }
2450 mac->key_len = maclen;
2451 }
2452 if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
2453 (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
2454 (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
2455 goto out;
2456 if (enc->name == NULL ||
2457 cipher_by_name(enc->name) != enc->cipher) {
2458 r = SSH_ERR_INVALID_FORMAT;
2459 goto out;
2460 }
2461 if (sshbuf_len(b) != 0) {
2462 r = SSH_ERR_INVALID_FORMAT;
2463 goto out;
2464 }
2465 enc->key_len = keylen;
2466 enc->iv_len = ivlen;
2467 ssh->kex->newkeys[mode] = newkey;
2468 newkey = NULL;
2469 r = 0;
2470 out:
2471 if (newkey != NULL)
2472 free(newkey);
2473 if (b != NULL)
2474 sshbuf_free(b);
2475 return r;
2476}
2477
2478/* restore kex from blob for packet state de-serialization */
2479static int
2480kex_from_blob(struct sshbuf *m, struct kex **kexp)
2481{
2482 struct kex *kex;
2483 int r;
2484
2485 if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
2486 (kex->my = sshbuf_new()) == NULL ||
2487 (kex->peer = sshbuf_new()) == NULL) {
2488 r = SSH_ERR_ALLOC_FAIL;
2489 goto out;
2490 }
2491 if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
2492 (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
2493 (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
2494 (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
2495 (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
2496 (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
2497 (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
2498 (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
2499 (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
2500 goto out;
2501 kex->server = 1;
2502 kex->done = 1;
2503 r = 0;
2504 out:
2505 if (r != 0 || kexp == NULL) {
2506 if (kex != NULL) {
2507 if (kex->my != NULL)
2508 sshbuf_free(kex->my);
2509 if (kex->peer != NULL)
2510 sshbuf_free(kex->peer);
2511 free(kex);
2512 }
2513 if (kexp != NULL)
2514 *kexp = NULL;
2515 } else {
2516 *kexp = kex;
2517 }
2518 return r;
2519}
2520
2521/*
2522 * Restore packet state from content of blob 'm' (de-serialization).
2523 * Note that 'm' will be partially consumed on parsing or any other errors.
2524 */
2525int
2526ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
2527{
2528 struct session_state *state = ssh->state;
2529 const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
2530 size_t ssh1keylen, rlen, slen, ilen, olen;
2531 int r;
2532 u_int ssh1cipher = 0;
2533 u_int64_t sent_bytes = 0, recv_bytes = 0;
2534
2535 if (!compat20) {
2536 if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
2537 (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
2538 (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
2539 (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
2540 (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
2541 return r;
2542 if (ssh1cipher > INT_MAX)
2543 return SSH_ERR_KEY_UNKNOWN_CIPHER;
2544 ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
2545 (int)ssh1cipher);
2546 if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
2547 cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
2548 return SSH_ERR_INVALID_FORMAT;
2549 if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
2550 (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
2551 return r;
2552 } else {
2553 if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
2554 (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
2555 (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
2556 (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
2557 (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
2558 (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
2559 (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
2560 (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
2561 (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
2562 (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
2563 (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
2564 return r;
2565 /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
2566 if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
2567 (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
2568 return r;
2569 }
2570 if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
2571 (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
2572 return r;
2573 if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
2574 cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
2575 return SSH_ERR_INVALID_FORMAT;
2576 cipher_set_keycontext(&state->send_context, keyout);
2577 cipher_set_keycontext(&state->receive_context, keyin);
2578
2579 if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
2580 (r = ssh_packet_set_postauth(ssh)) != 0)
2581 return r;
2582
2583 sshbuf_reset(state->input);
2584 sshbuf_reset(state->output);
2585 if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
2586 (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
2587 (r = sshbuf_put(state->input, input, ilen)) != 0 ||
2588 (r = sshbuf_put(state->output, output, olen)) != 0)
2589 return r;
2590
2591 if (compat20) {
2592 if ((r = sshbuf_get_u64(m, &sent_bytes)) != 0 ||
2593 (r = sshbuf_get_u64(m, &recv_bytes)) != 0)
2594 return r;
2595 roam_set_bytes(sent_bytes, recv_bytes);
2596 }
2597 if (sshbuf_len(m))
2598 return SSH_ERR_INVALID_FORMAT;
2599 debug3("%s: done", __func__);
2600 return 0;
2601}
2602
2603/* NEW API */
2604
2605/* put data to the outgoing packet */
2606
2607int
2608sshpkt_put(struct ssh *ssh, const void *v, size_t len)
2609{
2610 return sshbuf_put(ssh->state->outgoing_packet, v, len);
2611}
2612
2613int
2614sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
2615{
2616 return sshbuf_putb(ssh->state->outgoing_packet, b);
2617}
2618
2619int
2620sshpkt_put_u8(struct ssh *ssh, u_char val)
2621{
2622 return sshbuf_put_u8(ssh->state->outgoing_packet, val);
2623}
2624
2625int
2626sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
2627{
2628 return sshbuf_put_u32(ssh->state->outgoing_packet, val);
2629}
2630
2631int
2632sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
2633{
2634 return sshbuf_put_u64(ssh->state->outgoing_packet, val);
2635}
2636
2637int
2638sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
2639{
2640 return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
2641}
2642
2643int
2644sshpkt_put_cstring(struct ssh *ssh, const void *v)
2645{
2646 return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
2647}
2648
2649int
2650sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
2651{
2652 return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
2653}
2654
2655int
2656sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
2657{
2658 return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
2659}
2660
2661int
2662sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
2663{
2664 return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
2665}
2666
2667int
2668sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
2669{
2670 return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
2671}
2672
2673/* fetch data from the incoming packet */
2674
2675int
2676sshpkt_get(struct ssh *ssh, void *valp, size_t len)
2677{
2678 return sshbuf_get(ssh->state->incoming_packet, valp, len);
2679}
2680
2681int
2682sshpkt_get_u8(struct ssh *ssh, u_char *valp)
2683{
2684 return sshbuf_get_u8(ssh->state->incoming_packet, valp);
2685}
2686
2687int
2688sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
2689{
2690 return sshbuf_get_u32(ssh->state->incoming_packet, valp);
2691}
2692
2693int
2694sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
2695{
2696 return sshbuf_get_u64(ssh->state->incoming_packet, valp);
2697}
2698
2699int
2700sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
2701{
2702 return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
2703}
2704
2705int
2706sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
2707{
2708 return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
2709}
2710
2711int
2712sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
2713{
2714 return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
2715}
2716
2717int
2718sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
2719{
2720 return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
2721}
2722
2723int
2724sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
2725{
2726 return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
2727}
2728
2729int
2730sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
2731{
2732 return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
2733}
2734
2735int
2736sshpkt_get_end(struct ssh *ssh)
2737{
2738 if (sshbuf_len(ssh->state->incoming_packet) > 0)
2739 return SSH_ERR_UNEXPECTED_TRAILING_DATA;
2740 return 0;
2741}
2742
2743const u_char *
2744sshpkt_ptr(struct ssh *ssh, size_t *lenp)
2745{
2746 if (lenp != NULL)
2747 *lenp = sshbuf_len(ssh->state->incoming_packet);
2748 return sshbuf_ptr(ssh->state->incoming_packet);
2749}
2750
2751/* start a new packet */
2752
2753int
2754sshpkt_start(struct ssh *ssh, u_char type)
2755{
2756 u_char buf[9];
2757 int len;
2758
2759 DBG(debug("packet_start[%d]", type));
2760 len = compat20 ? 6 : 9;
2761 memset(buf, 0, len - 1);
2762 buf[len - 1] = type;
2763 sshbuf_reset(ssh->state->outgoing_packet);
2764 return sshbuf_put(ssh->state->outgoing_packet, buf, len);
2765}
2766
2767/* send it */
2768
2769int
2770sshpkt_send(struct ssh *ssh)
2771{
2772 if (compat20)
2773 return ssh_packet_send2(ssh);
2774 else
2775 return ssh_packet_send1(ssh);
2776}
2777
2778int
2779sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
2780{
2781 char buf[1024];
2782 va_list args;
2783 int r;
2784
2785 va_start(args, fmt);
2786 vsnprintf(buf, sizeof(buf), fmt, args);
2787 va_end(args);
2788
2789 if (compat20) {
2790 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
2791 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
2792 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2793 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2794 (r = sshpkt_send(ssh)) != 0)
2795 return r;
2796 } else {
2797 if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
2798 (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
2799 (r = sshpkt_send(ssh)) != 0)
2800 return r;
2801 }
2802 return 0;
2803}
2804
2805/* roundup current message to pad bytes */
2806int
2807sshpkt_add_padding(struct ssh *ssh, u_char pad)
2808{
2809 ssh->state->extra_pad = pad;
2810 return 0;
Damien Millerc31a0cd2014-05-15 14:37:39 +10002811}