blob: 6b326f3676a36001e4329c540df9d435f2e65667 [file] [log] [blame]
markus@openbsd.org128343b2015-01-13 19:31:40 +00001/* $OpenBSD: packet.c,v 1.200 2015/01/13 19:31:40 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
Damien Millere3b60b52006-07-10 21:08:03 +100042#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100043#include "openbsd-compat/sys-queue.h"
Damien Miller8dbffe72006-08-05 11:02:17 +100044#include <sys/param.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>
Damien Millerd7834352006-08-05 12:39:39 +100060#include <signal.h>
Darren Tuckerc53c2af2013-05-16 20:28:16 +100061#include <time.h>
Darren Tucker5d196262006-07-12 22:15:16 +100062
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063#include "xmalloc.h"
64#include "buffer.h"
65#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067#include "compress.h"
68#include "deattack.h"
Damien Miller33b13562000-04-04 14:38:59 +100069#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100071#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110072#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100073#include "key.h"
Damien Miller33b13562000-04-04 14:38:59 +100074#include "kex.h"
markus@openbsd.org128343b2015-01-13 19:31:40 +000075#include "digest.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000076#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
78#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110079#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100080#include "channels.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000081#include "ssh.h"
Damien Miller86687062014-07-02 15:28:02 +100082#include "ssherr.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100083#include "roaming.h"
Damien Miller33b13562000-04-04 14:38:59 +100084
85#ifdef PACKET_DEBUG
86#define DBG(x) x
87#else
88#define DBG(x)
89#endif
90
Damien Miller13ae44c2009-01-28 16:38:41 +110091#define PACKET_MAX_SIZE (256 * 1024)
92
Darren Tuckerf7288d72009-06-21 18:12:20 +100093struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100094 u_int32_t seqnr;
95 u_int32_t packets;
96 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +100097 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +100098};
Damien Miller13ae44c2009-01-28 16:38:41 +110099
Damien Millera5539d22003-04-09 20:50:06 +1000100struct packet {
101 TAILQ_ENTRY(packet) next;
102 u_char type;
103 Buffer payload;
104};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000105
106struct session_state {
107 /*
108 * This variable contains the file descriptors used for
109 * communicating with the other side. connection_in is used for
110 * reading; connection_out for writing. These can be the same
111 * descriptor, in which case it is assumed to be a socket.
112 */
113 int connection_in;
114 int connection_out;
115
116 /* Protocol flags for the remote side. */
117 u_int remote_protocol_flags;
118
119 /* Encryption context for receiving data. Only used for decryption. */
120 CipherContext receive_context;
121
122 /* Encryption context for sending data. Only used for encryption. */
123 CipherContext send_context;
124
125 /* Buffer for raw input data from the socket. */
126 Buffer input;
127
128 /* Buffer for raw output data going to the socket. */
129 Buffer output;
130
131 /* Buffer for the partial outgoing packet being constructed. */
132 Buffer outgoing_packet;
133
134 /* Buffer for the incoming packet currently being processed. */
135 Buffer incoming_packet;
136
137 /* Scratch buffer for packet compression/decompression. */
138 Buffer compression_buffer;
139 int compression_buffer_ready;
140
141 /*
142 * Flag indicating whether packet compression/decompression is
143 * enabled.
144 */
145 int packet_compression;
146
147 /* default maximum packet size */
148 u_int max_packet_size;
149
150 /* Flag indicating whether this module has been initialized. */
151 int initialized;
152
153 /* Set to true if the connection is interactive. */
154 int interactive_mode;
155
156 /* Set to true if we are the server side. */
157 int server_side;
158
159 /* Set to true if we are authenticated. */
160 int after_authentication;
161
162 int keep_alive_timeouts;
163
164 /* The maximum time that we will wait to send or receive a packet */
165 int packet_timeout_ms;
166
167 /* Session key information for Encryption and MAC */
168 Newkeys *newkeys[MODE_MAX];
169 struct packet_state p_read, p_send;
170
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000171 /* Volume-based rekeying */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000172 u_int64_t max_blocks_in, max_blocks_out;
173 u_int32_t rekey_limit;
174
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000175 /* Time-based rekeying */
176 time_t rekey_interval; /* how often in seconds */
177 time_t rekey_time; /* time of last rekeying */
178
Darren Tuckerf7288d72009-06-21 18:12:20 +1000179 /* Session key for protocol v1 */
180 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
181 u_int ssh1_keylen;
182
183 /* roundup current message to extra_pad bytes */
184 u_char extra_pad;
185
186 /* XXX discard incoming data after MAC error */
187 u_int packet_discard;
188 Mac *packet_discard_mac;
189
190 /* Used in packet_read_poll2() */
191 u_int packlen;
192
Darren Tucker7b935c72009-06-21 18:59:36 +1000193 /* Used in packet_send2 */
194 int rekeying;
195
196 /* Used in packet_set_interactive */
197 int set_interactive_called;
198
199 /* Used in packet_set_maxsize */
200 int set_maxsize_called;
201
Darren Tuckerf7288d72009-06-21 18:12:20 +1000202 TAILQ_HEAD(, packet) outgoing;
203};
204
Darren Tuckere841eb02009-07-06 07:11:13 +1000205static struct session_state *active_state, *backup_state;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000206
207static struct session_state *
Darren Tuckerb422afa2009-06-21 18:58:46 +1000208alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000209{
Damien Miller7a221a12010-11-20 15:14:29 +1100210 struct session_state *s = xcalloc(1, sizeof(*s));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000211
Damien Miller7a221a12010-11-20 15:14:29 +1100212 s->connection_in = -1;
213 s->connection_out = -1;
214 s->max_packet_size = 32768;
215 s->packet_timeout_ms = -1;
216 return s;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000217}
Damien Millera5539d22003-04-09 20:50:06 +1000218
Damien Miller5428f641999-11-25 11:54:57 +1100219/*
220 * Sets the descriptors used for communication. Disables encryption until
221 * packet_set_encryption_key is called.
222 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223void
224packet_set_connection(int fd_in, int fd_out)
225{
Damien Millerea111192013-04-23 19:24:32 +1000226 const Cipher *none = cipher_by_name("none");
Damien Miller86687062014-07-02 15:28:02 +1000227 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000228
Damien Miller874d77b2000-10-14 16:23:11 +1100229 if (none == NULL)
230 fatal("packet_set_connection: cannot load cipher 'none'");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000231 if (active_state == NULL)
232 active_state = alloc_session_state();
233 active_state->connection_in = fd_in;
234 active_state->connection_out = fd_out;
Damien Miller86687062014-07-02 15:28:02 +1000235 if ((r = cipher_init(&active_state->send_context, none,
236 (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
237 (r = cipher_init(&active_state->receive_context, none,
238 (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0)
239 fatal("%s: cipher_init: %s", __func__, ssh_err(r));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000240 active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL;
241 if (!active_state->initialized) {
242 active_state->initialized = 1;
243 buffer_init(&active_state->input);
244 buffer_init(&active_state->output);
245 buffer_init(&active_state->outgoing_packet);
246 buffer_init(&active_state->incoming_packet);
247 TAILQ_INIT(&active_state->outgoing);
248 active_state->p_send.packets = active_state->p_read.packets = 0;
Damien Miller95def091999-11-25 00:26:21 +1100249 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250}
251
Darren Tucker3fc464e2008-06-13 06:42:45 +1000252void
253packet_set_timeout(int timeout, int count)
254{
Damien Miller8ed4de82011-12-19 10:52:50 +1100255 if (timeout <= 0 || count <= 0) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000256 active_state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000257 return;
258 }
259 if ((INT_MAX / 1000) / count < timeout)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000260 active_state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000261 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000262 active_state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000263}
264
Damien Miller13ae44c2009-01-28 16:38:41 +1100265static void
266packet_stop_discard(void)
267{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000268 if (active_state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100269 char buf[1024];
270
271 memset(buf, 'a', sizeof(buf));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000272 while (buffer_len(&active_state->incoming_packet) <
273 PACKET_MAX_SIZE)
274 buffer_append(&active_state->incoming_packet, buf,
275 sizeof(buf));
276 (void) mac_compute(active_state->packet_discard_mac,
277 active_state->p_read.seqnr,
278 buffer_ptr(&active_state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +0000279 PACKET_MAX_SIZE, NULL, 0);
Damien Miller13ae44c2009-01-28 16:38:41 +1100280 }
281 logit("Finished discarding for %.200s", get_remote_ipaddr());
282 cleanup_exit(255);
283}
284
285static void
286packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
287{
Damien Milleraf43a7a2012-12-12 10:46:31 +1100288 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm))
Damien Miller13ae44c2009-01-28 16:38:41 +1100289 packet_disconnect("Packet corrupt");
290 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000291 active_state->packet_discard_mac = mac;
292 if (buffer_len(&active_state->input) >= discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100293 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +1000294 active_state->packet_discard = discard -
295 buffer_len(&active_state->input);
Damien Miller13ae44c2009-01-28 16:38:41 +1100296}
297
Damien Miller34132e52000-01-14 15:45:46 +1100298/* Returns 1 if remote host is connected via socket, 0 if not. */
299
300int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000301packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100302{
303 struct sockaddr_storage from, to;
304 socklen_t fromlen, tolen;
305
306 /* filedescriptors in and out are the same, so it's a socket */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000307 if (active_state->connection_in == active_state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100308 return 1;
309 fromlen = sizeof(from);
310 memset(&from, 0, sizeof(from));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000311 if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
312 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100313 return 0;
314 tolen = sizeof(to);
315 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000316 if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
317 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100318 return 0;
319 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
320 return 0;
321 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
322 return 0;
323 return 1;
324}
325
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000326/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000327 * Exports an IV from the CipherContext required to export the key
328 * state back from the unprivileged child to the privileged parent
329 * process.
330 */
331
332void
333packet_get_keyiv(int mode, u_char *iv, u_int len)
334{
335 CipherContext *cc;
Damien Miller86687062014-07-02 15:28:02 +1000336 int r;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000337
338 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000339 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000340 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000341 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000342
Damien Miller86687062014-07-02 15:28:02 +1000343 if ((r = cipher_get_keyiv(cc, iv, len)) != 0)
344 fatal("%s: cipher_get_keyiv: %s", __func__, ssh_err(r));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000345}
346
347int
348packet_get_keycontext(int mode, u_char *dat)
349{
350 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000351
Ben Lindstromf6027d32002-03-22 01:42:04 +0000352 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000353 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000354 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000355 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000356
357 return (cipher_get_keycontext(cc, dat));
358}
359
360void
361packet_set_keycontext(int mode, u_char *dat)
362{
363 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000364
Ben Lindstromf6027d32002-03-22 01:42:04 +0000365 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000366 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000367 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000368 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000369
370 cipher_set_keycontext(cc, dat);
371}
372
373int
374packet_get_keyiv_len(int mode)
375{
376 CipherContext *cc;
377
378 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000379 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000380 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000381 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000382
383 return (cipher_get_keyiv_len(cc));
384}
Damien Miller4f7becb2006-03-26 14:10:14 +1100385
Ben Lindstromf6027d32002-03-22 01:42:04 +0000386void
387packet_set_iv(int mode, u_char *dat)
388{
389 CipherContext *cc;
Damien Miller86687062014-07-02 15:28:02 +1000390 int r;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000391
392 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000393 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000394 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000395 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000396
Damien Miller86687062014-07-02 15:28:02 +1000397 if ((r = cipher_set_keyiv(cc, dat)) != 0)
398 fatal("%s: cipher_set_keyiv: %s", __func__, ssh_err(r));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000399}
Damien Miller4f7becb2006-03-26 14:10:14 +1100400
Ben Lindstromf6027d32002-03-22 01:42:04 +0000401int
Damien Miller2b92d322003-06-11 22:05:06 +1000402packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000403{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000404 return (cipher_get_number(active_state->receive_context.cipher));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000405}
406
Damien Millera5539d22003-04-09 20:50:06 +1000407void
Damien Miller7a221a12010-11-20 15:14:29 +1100408packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks,
409 u_int32_t *packets, u_int64_t *bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000410{
Damien Millera5539d22003-04-09 20:50:06 +1000411 struct packet_state *state;
412
Darren Tuckerf7288d72009-06-21 18:12:20 +1000413 state = (mode == MODE_IN) ?
414 &active_state->p_read : &active_state->p_send;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000415 if (seqnr)
416 *seqnr = state->seqnr;
417 if (blocks)
418 *blocks = state->blocks;
419 if (packets)
420 *packets = state->packets;
421 if (bytes)
422 *bytes = state->bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000423}
424
425void
Damien Millerb61f3fc2008-07-11 17:36:48 +1000426packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
427 u_int64_t bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000428{
Damien Millera5539d22003-04-09 20:50:06 +1000429 struct packet_state *state;
430
Darren Tuckerf7288d72009-06-21 18:12:20 +1000431 state = (mode == MODE_IN) ?
432 &active_state->p_read : &active_state->p_send;
Damien Millera5539d22003-04-09 20:50:06 +1000433 state->seqnr = seqnr;
434 state->blocks = blocks;
435 state->packets = packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000436 state->bytes = bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000437}
438
Damien Millerd2ac5d72011-05-15 08:43:13 +1000439static int
440packet_connection_af(void)
Damien Miller34132e52000-01-14 15:45:46 +1100441{
442 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100443 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100444
445 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000446 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
447 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100448 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000449#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100450 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000451 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000452 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000453#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000454 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100455}
456
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457/* Sets the connection into non-blocking mode. */
458
459void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000460packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461{
Damien Miller95def091999-11-25 00:26:21 +1100462 /* Set the socket into non-blocking mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000463 set_nonblock(active_state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Darren Tuckerf7288d72009-06-21 18:12:20 +1000465 if (active_state->connection_out != active_state->connection_in)
466 set_nonblock(active_state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467}
468
469/* Returns the socket used for reading. */
470
471int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000472packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000474 return active_state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
476
477/* Returns the descriptor used for writing. */
478
479int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000480packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000482 return active_state->connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483}
484
485/* Closes the connection and clears and frees internal data structures. */
486
487void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000488packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000490 if (!active_state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100491 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000492 active_state->initialized = 0;
493 if (active_state->connection_in == active_state->connection_out) {
494 shutdown(active_state->connection_out, SHUT_RDWR);
495 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100496 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000497 close(active_state->connection_in);
498 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100499 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000500 buffer_free(&active_state->input);
501 buffer_free(&active_state->output);
502 buffer_free(&active_state->outgoing_packet);
503 buffer_free(&active_state->incoming_packet);
504 if (active_state->compression_buffer_ready) {
505 buffer_free(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100506 buffer_compress_uninit();
507 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000508 cipher_cleanup(&active_state->send_context);
509 cipher_cleanup(&active_state->receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510}
511
512/* Sets remote side protocol flags. */
513
514void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000515packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000517 active_state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518}
519
520/* Returns the remote protocol flags set earlier by the above function. */
521
Ben Lindstrom46c16222000-12-22 01:43:59 +0000522u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000523packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000525 return active_state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526}
527
Damien Miller5428f641999-11-25 11:54:57 +1100528/*
529 * Starts packet compression from the next packet on in both directions.
530 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
531 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532
Ben Lindstrombba81212001-06-25 05:01:22 +0000533static void
534packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000535{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000536 if (active_state->compression_buffer_ready == 1)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000537 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000538 active_state->compression_buffer_ready = 1;
539 buffer_init(&active_state->compression_buffer);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000540}
541
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542void
543packet_start_compression(int level)
544{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000545 if (active_state->packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100546 fatal("Compression already enabled.");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000547 active_state->packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000548 packet_init_compression();
549 buffer_compress_init_send(level);
550 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551}
552
Damien Miller5428f641999-11-25 11:54:57 +1100553/*
Damien Miller5428f641999-11-25 11:54:57 +1100554 * Causes any further packets to be encrypted using the given key. The same
555 * key is used for both sending and reception. However, both directions are
556 * encrypted independently of each other.
557 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000558
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559void
Damien Miller7a221a12010-11-20 15:14:29 +1100560packet_set_encryption_key(const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561{
Damien Millerea111192013-04-23 19:24:32 +1000562 const Cipher *cipher = cipher_by_number(number);
Damien Miller86687062014-07-02 15:28:02 +1000563 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000564
Damien Miller874d77b2000-10-14 16:23:11 +1100565 if (cipher == NULL)
566 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000567 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100568 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000569 if (keylen > SSH_SESSION_KEY_LENGTH)
570 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000571 memcpy(active_state->ssh1_key, key, keylen);
572 active_state->ssh1_keylen = keylen;
Damien Miller86687062014-07-02 15:28:02 +1000573 if ((r = cipher_init(&active_state->send_context, cipher,
574 key, keylen, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
575 (r = cipher_init(&active_state->receive_context, cipher,
576 key, keylen, NULL, 0, CIPHER_DECRYPT)) != 0)
577 fatal("%s: cipher_init: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578}
579
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000580u_int
581packet_get_encryption_key(u_char *key)
582{
583 if (key == NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000584 return (active_state->ssh1_keylen);
585 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
586 return (active_state->ssh1_keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000587}
588
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000589/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000591packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000593 u_char buf[9];
594 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595
Ben Lindstrom204e4882001-03-05 06:47:00 +0000596 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000597 len = compat20 ? 6 : 9;
598 memset(buf, 0, len - 1);
599 buf[len - 1] = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000600 buffer_clear(&active_state->outgoing_packet);
601 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000602}
603
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000604/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605void
606packet_put_char(int value)
607{
Damien Miller95def091999-11-25 00:26:21 +1100608 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000609
Darren Tuckerf7288d72009-06-21 18:12:20 +1000610 buffer_append(&active_state->outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611}
Damien Miller4f7becb2006-03-26 14:10:14 +1100612
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000614packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000616 buffer_put_int(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617}
Damien Miller4f7becb2006-03-26 14:10:14 +1100618
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619void
Darren Tucker761c3892009-06-21 18:16:26 +1000620packet_put_int64(u_int64_t value)
621{
622 buffer_put_int64(&active_state->outgoing_packet, value);
623}
624
625void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100626packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000628 buffer_put_string(&active_state->outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629}
Damien Miller4f7becb2006-03-26 14:10:14 +1100630
Damien Miller33b13562000-04-04 14:38:59 +1000631void
632packet_put_cstring(const char *str)
633{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000634 buffer_put_cstring(&active_state->outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000635}
Damien Miller4f7becb2006-03-26 14:10:14 +1100636
Damien Miller33b13562000-04-04 14:38:59 +1000637void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100638packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000639{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000640 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000641}
Damien Miller4f7becb2006-03-26 14:10:14 +1100642
Damien Miller1f0311c2014-05-15 14:24:09 +1000643#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644void
Damien Miller95def091999-11-25 00:26:21 +1100645packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000646{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000647 buffer_put_bignum(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648}
Damien Miller4f7becb2006-03-26 14:10:14 +1100649
Damien Miller33b13562000-04-04 14:38:59 +1000650void
651packet_put_bignum2(BIGNUM * value)
652{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000653 buffer_put_bignum2(&active_state->outgoing_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +1000654}
Damien Miller1f0311c2014-05-15 14:24:09 +1000655#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656
Damien Miller6af914a2010-09-10 11:39:26 +1000657#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000658void
659packet_put_ecpoint(const EC_GROUP *curve, const EC_POINT *point)
660{
661 buffer_put_ecpoint(&active_state->outgoing_packet, curve, point);
662}
Damien Miller6af914a2010-09-10 11:39:26 +1000663#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000664
Damien Miller5428f641999-11-25 11:54:57 +1100665/*
666 * Finalizes and sends the packet. If the encryption key has been set,
667 * encrypts the packet before sending.
668 */
Damien Miller95def091999-11-25 00:26:21 +1100669
Ben Lindstrombba81212001-06-25 05:01:22 +0000670static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000671packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000673 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100674 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000675 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000676 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677
Damien Miller5428f641999-11-25 11:54:57 +1100678 /*
679 * If using packet compression, compress the payload of the outgoing
680 * packet.
681 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000682 if (active_state->packet_compression) {
683 buffer_clear(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100684 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000685 buffer_consume(&active_state->outgoing_packet, 8);
Damien Miller95def091999-11-25 00:26:21 +1100686 /* padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000687 buffer_append(&active_state->compression_buffer,
688 "\0\0\0\0\0\0\0\0", 8);
689 buffer_compress(&active_state->outgoing_packet,
690 &active_state->compression_buffer);
691 buffer_clear(&active_state->outgoing_packet);
692 buffer_append(&active_state->outgoing_packet,
693 buffer_ptr(&active_state->compression_buffer),
694 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100695 }
696 /* Compute packet length without padding (add checksum, remove padding). */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000697 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698
Damien Millere247cc42000-05-07 12:03:14 +1000699 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100700 padding = 8 - len % 8;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000701 if (!active_state->send_context.plaintext) {
702 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller95def091999-11-25 00:26:21 +1100703 for (i = 0; i < padding; i++) {
704 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000705 rnd = arc4random();
706 cp[7 - i] = rnd & 0xff;
707 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100708 }
709 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000710 buffer_consume(&active_state->outgoing_packet, 8 - padding);
Damien Miller95def091999-11-25 00:26:21 +1100711
712 /* Add check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000713 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
714 buffer_len(&active_state->outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100715 put_u32(buf, checksum);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000716 buffer_append(&active_state->outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717
718#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100719 fprintf(stderr, "packet_send plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000720 buffer_dump(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721#endif
722
Damien Miller95def091999-11-25 00:26:21 +1100723 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100724 put_u32(buf, len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000725 buffer_append(&active_state->output, buf, 4);
726 cp = buffer_append_space(&active_state->output,
727 buffer_len(&active_state->outgoing_packet));
Damien Millerbcd00ab2013-12-07 10:41:55 +1100728 if (cipher_crypt(&active_state->send_context, 0, cp,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000729 buffer_ptr(&active_state->outgoing_packet),
Damien Millerbcd00ab2013-12-07 10:41:55 +1100730 buffer_len(&active_state->outgoing_packet), 0, 0) != 0)
731 fatal("%s: cipher_crypt failed", __func__);
Damien Miller95def091999-11-25 00:26:21 +1100732
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100734 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000735 buffer_dump(&active_state->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000736#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +1000737 active_state->p_send.packets++;
738 active_state->p_send.bytes += len +
739 buffer_len(&active_state->outgoing_packet);
740 buffer_clear(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741
Damien Miller5428f641999-11-25 11:54:57 +1100742 /*
Damien Miller788f2122005-11-05 15:14:59 +1100743 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100744 * actually sent until packet_write_wait or packet_write_poll is
745 * called.
746 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747}
748
Ben Lindstromf6027d32002-03-22 01:42:04 +0000749void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000750set_newkeys(int mode)
751{
752 Enc *enc;
753 Mac *mac;
754 Comp *comp;
755 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000756 u_int64_t *max_blocks;
Damien Miller86687062014-07-02 15:28:02 +1000757 int r, crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000758
Ben Lindstrom064496f2002-12-23 02:04:22 +0000759 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000760
Damien Miller963f6b22002-02-19 15:21:23 +1100761 if (mode == MODE_OUT) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000762 cc = &active_state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000763 crypt_type = CIPHER_ENCRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000764 active_state->p_send.packets = active_state->p_send.blocks = 0;
765 max_blocks = &active_state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100766 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000767 cc = &active_state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000768 crypt_type = CIPHER_DECRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000769 active_state->p_read.packets = active_state->p_read.blocks = 0;
770 max_blocks = &active_state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100771 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000772 if (active_state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000773 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100774 cipher_cleanup(cc);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000775 enc = &active_state->newkeys[mode]->enc;
776 mac = &active_state->newkeys[mode]->mac;
777 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000778 mac_clear(mac);
Damien Millera5103f42014-02-04 11:20:14 +1100779 explicit_bzero(enc->iv, enc->iv_len);
780 explicit_bzero(enc->key, enc->key_len);
781 explicit_bzero(mac->key, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000782 free(enc->name);
783 free(enc->iv);
784 free(enc->key);
785 free(mac->name);
786 free(mac->key);
787 free(comp->name);
788 free(active_state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000789 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000790 active_state->newkeys[mode] = kex_get_newkeys(mode);
791 if (active_state->newkeys[mode] == NULL)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000792 fatal("newkeys: no keys for mode %d", mode);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000793 enc = &active_state->newkeys[mode]->enc;
794 mac = &active_state->newkeys[mode]->mac;
795 comp = &active_state->newkeys[mode]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100796 if (cipher_authlen(enc->cipher) == 0 && mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000797 mac->enabled = 1;
798 DBG(debug("cipher_init_context: %d", mode));
Damien Miller86687062014-07-02 15:28:02 +1000799 if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
800 enc->iv, enc->iv_len, crypt_type)) != 0)
801 fatal("%s: cipher_init: %s", __func__, ssh_err(r));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000802 /* Deleting the keys does not gain extra security */
Damien Millera5103f42014-02-04 11:20:14 +1100803 /* explicit_bzero(enc->iv, enc->block_size);
804 explicit_bzero(enc->key, enc->key_len);
805 explicit_bzero(mac->key, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000806 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000807 (comp->type == COMP_DELAYED &&
808 active_state->after_authentication)) && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000809 packet_init_compression();
810 if (mode == MODE_OUT)
811 buffer_compress_init_send(6);
812 else
813 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000814 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000815 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000816 /*
817 * The 2^(blocksize*2) limit is too expensive for 3DES,
818 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
819 */
820 if (enc->block_size >= 16)
821 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
822 else
823 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000824 if (active_state->rekey_limit)
825 *max_blocks = MIN(*max_blocks,
826 active_state->rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000827}
828
Damien Miller5428f641999-11-25 11:54:57 +1100829/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000830 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000831 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000832 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
833 */
834static void
835packet_enable_delayed_compress(void)
836{
837 Comp *comp = NULL;
838 int mode;
839
840 /*
841 * Remember that we are past the authentication step, so rekeying
842 * with COMP_DELAYED will turn on compression immediately.
843 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000844 active_state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +1000845 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000846 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000847 if (active_state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +1000848 continue;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000849 comp = &active_state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +1000850 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000851 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000852 if (mode == MODE_OUT)
853 buffer_compress_init_send(6);
854 else
855 buffer_compress_init_recv();
856 comp->enabled = 1;
857 }
858 }
859}
860
861/*
Damien Miller33b13562000-04-04 14:38:59 +1000862 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
863 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000864static void
Damien Millera5539d22003-04-09 20:50:06 +1000865packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000866{
markus@openbsd.org128343b2015-01-13 19:31:40 +0000867 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Milleraf43a7a2012-12-12 10:46:31 +1100868 u_char padlen, pad = 0;
Damien Miller1d75abf2013-01-09 16:12:19 +1100869 u_int i, len, authlen = 0, aadlen = 0;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000870 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000871 Enc *enc = NULL;
872 Mac *mac = NULL;
873 Comp *comp = NULL;
874 int block_size;
markus@openbsd.org128343b2015-01-13 19:31:40 +0000875 int r;
Damien Miller33b13562000-04-04 14:38:59 +1000876
Darren Tuckerf7288d72009-06-21 18:12:20 +1000877 if (active_state->newkeys[MODE_OUT] != NULL) {
878 enc = &active_state->newkeys[MODE_OUT]->enc;
879 mac = &active_state->newkeys[MODE_OUT]->mac;
880 comp = &active_state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100881 /* disable mac for authenticated encryption */
882 if ((authlen = cipher_authlen(enc->cipher)) != 0)
883 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +1000884 }
Damien Miller963f6b22002-02-19 15:21:23 +1100885 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +1100886 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +1000887
Darren Tuckerf7288d72009-06-21 18:12:20 +1000888 cp = buffer_ptr(&active_state->outgoing_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000889 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000890
891#ifdef PACKET_DEBUG
892 fprintf(stderr, "plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000893 buffer_dump(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000894#endif
895
896 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000897 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000898 /* skip header, compress only payload */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000899 buffer_consume(&active_state->outgoing_packet, 5);
900 buffer_clear(&active_state->compression_buffer);
901 buffer_compress(&active_state->outgoing_packet,
902 &active_state->compression_buffer);
903 buffer_clear(&active_state->outgoing_packet);
904 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
905 buffer_append(&active_state->outgoing_packet,
906 buffer_ptr(&active_state->compression_buffer),
907 buffer_len(&active_state->compression_buffer));
Damien Miller33b13562000-04-04 14:38:59 +1000908 DBG(debug("compression: raw %d compressed %d", len,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000909 buffer_len(&active_state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +1000910 }
911
912 /* sizeof (packet_len + pad_len + payload) */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000913 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000914
915 /*
916 * calc size of padding, alloc space, get random data,
917 * minimum padding is 4 bytes
918 */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100919 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +1000920 padlen = block_size - (len % block_size);
921 if (padlen < 4)
922 padlen += block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000923 if (active_state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +1100924 /* will wrap if extra_pad+padlen > 255 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000925 active_state->extra_pad =
926 roundup(active_state->extra_pad, block_size);
927 pad = active_state->extra_pad -
928 ((len + padlen) % active_state->extra_pad);
Damien Miller2a328432014-04-20 13:24:01 +1000929 DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
930 __func__, pad, len, padlen, active_state->extra_pad));
Damien Miller9f643902001-11-12 11:02:52 +1100931 padlen += pad;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000932 active_state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100933 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000934 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
935 if (enc && !active_state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000936 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000937 for (i = 0; i < padlen; i++) {
938 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000939 rnd = arc4random();
940 cp[i] = rnd & 0xff;
941 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000942 }
Damien Millere247cc42000-05-07 12:03:14 +1000943 } else {
944 /* clear padding */
Damien Millera5103f42014-02-04 11:20:14 +1100945 explicit_bzero(cp, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000946 }
Damien Milleraf43a7a2012-12-12 10:46:31 +1100947 /* sizeof (packet_len + pad_len + payload + padding) */
948 len = buffer_len(&active_state->outgoing_packet);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000949 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Milleraf43a7a2012-12-12 10:46:31 +1100950 /* packet_length includes payload, padding and padding length field */
951 put_u32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000952 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100953 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
954 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +1000955
956 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100957 if (mac && mac->enabled && !mac->etm) {
markus@openbsd.org128343b2015-01-13 19:31:40 +0000958 if ((r = mac_compute(mac, active_state->p_send.seqnr,
959 buffer_ptr(&active_state->outgoing_packet), len,
960 macbuf, sizeof(macbuf))) != 0)
961 fatal("%s: mac_compute: %s", __func__, ssh_err(r));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000962 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000963 }
964 /* encrypt packet and append to output buffer. */
Damien Miller1d75abf2013-01-09 16:12:19 +1100965 cp = buffer_append_space(&active_state->output, len + authlen);
Damien Millerbcd00ab2013-12-07 10:41:55 +1100966 if (cipher_crypt(&active_state->send_context, active_state->p_send.seqnr,
Damien Miller0fde8ac2013-11-21 14:12:23 +1100967 cp, buffer_ptr(&active_state->outgoing_packet),
Damien Millerbcd00ab2013-12-07 10:41:55 +1100968 len - aadlen, aadlen, authlen) != 0)
969 fatal("%s: cipher_crypt failed", __func__);
Damien Miller33b13562000-04-04 14:38:59 +1000970 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100971 if (mac && mac->enabled) {
972 if (mac->etm) {
973 /* EtM: compute mac over aadlen + cipher text */
markus@openbsd.org128343b2015-01-13 19:31:40 +0000974 if ((r = mac_compute(mac,
975 active_state->p_send.seqnr, cp, len,
976 macbuf, sizeof(macbuf))) != 0)
977 fatal("%s: mac_compute: %s", __func__, ssh_err(r));
Damien Milleraf43a7a2012-12-12 10:46:31 +1100978 DBG(debug("done calc MAC(EtM) out #%d",
979 active_state->p_send.seqnr));
980 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000981 buffer_append(&active_state->output, macbuf, mac->mac_len);
Damien Milleraf43a7a2012-12-12 10:46:31 +1100982 }
Damien Miller33b13562000-04-04 14:38:59 +1000983#ifdef PACKET_DEBUG
984 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000985 buffer_dump(&active_state->output);
Damien Miller33b13562000-04-04 14:38:59 +1000986#endif
Damien Miller4af51302000-04-16 11:18:38 +1000987 /* increment sequence number for outgoing packets */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000988 if (++active_state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000989 logit("outgoing seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000990 if (++active_state->p_send.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +1000991 if (!(datafellows & SSH_BUG_NOREKEY))
992 fatal("XXX too many packets with same key");
Damien Milleraf43a7a2012-12-12 10:46:31 +1100993 active_state->p_send.blocks += len / block_size;
994 active_state->p_send.bytes += len;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000995 buffer_clear(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000996
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000997 if (type == SSH2_MSG_NEWKEYS)
998 set_newkeys(MODE_OUT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000999 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +10001000 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001001}
1002
Damien Millera5539d22003-04-09 20:50:06 +10001003static void
1004packet_send2(void)
1005{
Damien Millera5539d22003-04-09 20:50:06 +10001006 struct packet *p;
1007 u_char type, *cp;
1008
Darren Tuckerf7288d72009-06-21 18:12:20 +10001009 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Millera5539d22003-04-09 20:50:06 +10001010 type = cp[5];
1011
1012 /* during rekeying we can only send key exchange messages */
Darren Tucker7b935c72009-06-21 18:59:36 +10001013 if (active_state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +11001014 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
1015 (type > SSH2_MSG_TRANSPORT_MAX) ||
1016 (type == SSH2_MSG_SERVICE_REQUEST) ||
1017 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +10001018 debug("enqueue packet: %u", type);
Damien Miller6c81fee2013-11-08 12:19:55 +11001019 p = xcalloc(1, sizeof(*p));
Damien Millera5539d22003-04-09 20:50:06 +10001020 p->type = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001021 memcpy(&p->payload, &active_state->outgoing_packet,
1022 sizeof(Buffer));
1023 buffer_init(&active_state->outgoing_packet);
1024 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +10001025 return;
1026 }
1027 }
1028
1029 /* rekeying starts with sending KEXINIT */
1030 if (type == SSH2_MSG_KEXINIT)
Darren Tucker7b935c72009-06-21 18:59:36 +10001031 active_state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001032
1033 packet_send2_wrapped();
1034
1035 /* after a NEWKEYS message we can send the complete queue */
1036 if (type == SSH2_MSG_NEWKEYS) {
Darren Tucker7b935c72009-06-21 18:59:36 +10001037 active_state->rekeying = 0;
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001038 active_state->rekey_time = monotime();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001039 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001040 type = p->type;
1041 debug("dequeue packet: %u", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001042 buffer_free(&active_state->outgoing_packet);
1043 memcpy(&active_state->outgoing_packet, &p->payload,
Damien Millera5539d22003-04-09 20:50:06 +10001044 sizeof(Buffer));
Darren Tuckerf7288d72009-06-21 18:12:20 +10001045 TAILQ_REMOVE(&active_state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001046 free(p);
Damien Millera5539d22003-04-09 20:50:06 +10001047 packet_send2_wrapped();
1048 }
1049 }
1050}
1051
Damien Miller33b13562000-04-04 14:38:59 +10001052void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001053packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +10001054{
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001055 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +10001056 packet_send2();
1057 else
1058 packet_send1();
1059 DBG(debug("packet_send done"));
1060}
1061
Damien Miller33b13562000-04-04 14:38:59 +10001062/*
Damien Miller5428f641999-11-25 11:54:57 +11001063 * Waits until a packet has been received, and returns its type. Note that
1064 * no other data is processed until this returns, so this function should not
1065 * be used during the interactive session.
1066 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
1068int
Damien Millerdff50992002-01-22 23:16:32 +11001069packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070{
Damien Millerce986542013-07-18 16:12:44 +10001071 int type, len, ret, cont, ms_remain = 0;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001072 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001073 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001074 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001075
Darren Tucker99bb7612008-06-13 22:02:50 +10001076 DBG(debug("packet_read()"));
1077
Darren Tuckerf7288d72009-06-21 18:12:20 +10001078 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1079 NFDBITS), sizeof(fd_mask));
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001080
Damien Miller95def091999-11-25 00:26:21 +11001081 /* Since we are blocking, ensure that all written packets have been sent. */
1082 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001083
Damien Miller95def091999-11-25 00:26:21 +11001084 /* Stay in the loop until we have received a complete packet. */
1085 for (;;) {
1086 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +11001087 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001088 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +10001089 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +11001090 || type == SSH_SMSG_FAILURE
1091 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +10001092 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +11001093 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001094 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001095 if (type != SSH_MSG_NONE) {
Darren Tuckera627d422013-06-02 07:31:17 +10001096 free(setp);
Damien Miller95def091999-11-25 00:26:21 +11001097 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001098 }
Damien Miller5428f641999-11-25 11:54:57 +11001099 /*
1100 * Otherwise, wait for some data to arrive, add it to the
1101 * buffer, and try again.
1102 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001103 memset(setp, 0, howmany(active_state->connection_in + 1,
1104 NFDBITS) * sizeof(fd_mask));
1105 FD_SET(active_state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001106
Darren Tuckerf7288d72009-06-21 18:12:20 +10001107 if (active_state->packet_timeout_ms > 0) {
1108 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001109 timeoutp = &timeout;
1110 }
Damien Miller95def091999-11-25 00:26:21 +11001111 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001112 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001113 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001114 ms_to_timeval(&timeout, ms_remain);
1115 gettimeofday(&start, NULL);
1116 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001117 if ((ret = select(active_state->connection_in + 1, setp,
1118 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001119 break;
Damien Millerea437422009-10-02 11:49:03 +10001120 if (errno != EAGAIN && errno != EINTR &&
1121 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001122 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001123 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001124 continue;
1125 ms_subtract_diff(&start, &ms_remain);
1126 if (ms_remain <= 0) {
1127 ret = 0;
1128 break;
1129 }
1130 }
1131 if (ret == 0) {
1132 logit("Connection to %.200s timed out while "
1133 "waiting to read", get_remote_ipaddr());
1134 cleanup_exit(255);
1135 }
Damien Miller95def091999-11-25 00:26:21 +11001136 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001137 do {
1138 cont = 0;
1139 len = roaming_read(active_state->connection_in, buf,
1140 sizeof(buf), &cont);
1141 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001142 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001143 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +10001144 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001145 }
Damien Miller95def091999-11-25 00:26:21 +11001146 if (len < 0)
1147 fatal("Read from socket failed: %.100s", strerror(errno));
1148 /* Append it to the buffer. */
1149 packet_process_incoming(buf, len);
1150 }
1151 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001152}
1153
Damien Miller278f9072001-12-21 15:00:19 +11001154int
Damien Millerdff50992002-01-22 23:16:32 +11001155packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +11001156{
Damien Millerdff50992002-01-22 23:16:32 +11001157 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001158}
1159
Damien Miller5428f641999-11-25 11:54:57 +11001160/*
1161 * Waits until a packet has been received, verifies that its type matches
1162 * that given, and gives a fatal error and exits if there is a mismatch.
1163 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164
1165void
Damien Millerdff50992002-01-22 23:16:32 +11001166packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167{
Damien Miller95def091999-11-25 00:26:21 +11001168 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169
Damien Millerdff50992002-01-22 23:16:32 +11001170 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001171 if (type != expected_type)
1172 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001173 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174}
1175
1176/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001177 * packet_process_incoming. If so, reads the packet; otherwise returns
1178 * SSH_MSG_NONE. This does not wait for data from the connection.
1179 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001180 * SSH_MSG_DISCONNECT is handled specially here. Also,
1181 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1182 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001183 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184
Ben Lindstrombba81212001-06-25 05:01:22 +00001185static int
Damien Millerdff50992002-01-22 23:16:32 +11001186packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001188 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001189 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001190 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001191
Damien Miller95def091999-11-25 00:26:21 +11001192 /* Check if input size is less than minimum packet size. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001193 if (buffer_len(&active_state->input) < 4 + 8)
Damien Miller95def091999-11-25 00:26:21 +11001194 return SSH_MSG_NONE;
1195 /* Get length of incoming packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001196 cp = buffer_ptr(&active_state->input);
Damien Miller3f941882006-03-31 23:13:02 +11001197 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001198 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001199 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +11001200 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201
Damien Miller95def091999-11-25 00:26:21 +11001202 /* Check if the packet has been entirely received. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001203 if (buffer_len(&active_state->input) < 4 + padded_len)
Damien Miller95def091999-11-25 00:26:21 +11001204 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001205
Damien Miller95def091999-11-25 00:26:21 +11001206 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207
Damien Miller95def091999-11-25 00:26:21 +11001208 /* Consume packet length. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001209 buffer_consume(&active_state->input, 4);
Damien Miller95def091999-11-25 00:26:21 +11001210
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001211 /*
1212 * Cryptographic attack detector for ssh
1213 * (C)1998 CORE-SDI, Buenos Aires Argentina
1214 * Ariel Futoransky(futo@core-sdi.com)
1215 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001216 if (!active_state->receive_context.plaintext) {
1217 switch (detect_attack(buffer_ptr(&active_state->input),
1218 padded_len)) {
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001219 case DEATTACK_DETECTED:
1220 packet_disconnect("crc32 compensation attack: "
1221 "network attack detected");
1222 case DEATTACK_DOS_DETECTED:
1223 packet_disconnect("deattack denial of "
1224 "service detected");
1225 }
1226 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001227
1228 /* Decrypt data to incoming_packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001229 buffer_clear(&active_state->incoming_packet);
1230 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
Damien Millerbcd00ab2013-12-07 10:41:55 +11001231 if (cipher_crypt(&active_state->receive_context, 0, cp,
1232 buffer_ptr(&active_state->input), padded_len, 0, 0) != 0)
1233 fatal("%s: cipher_crypt failed", __func__);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001234
Darren Tuckerf7288d72009-06-21 18:12:20 +10001235 buffer_consume(&active_state->input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001236
1237#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001238 fprintf(stderr, "read_poll plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001239 buffer_dump(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241
Damien Miller95def091999-11-25 00:26:21 +11001242 /* Compute packet checksum. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001243 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1244 buffer_len(&active_state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245
Damien Miller95def091999-11-25 00:26:21 +11001246 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001247 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001248
Damien Miller95def091999-11-25 00:26:21 +11001249 /* Test check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001250 if (len != buffer_len(&active_state->incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001251 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001252 len, buffer_len(&active_state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253
Darren Tuckerf7288d72009-06-21 18:12:20 +10001254 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001255 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001256 if (checksum != stored_checksum)
1257 packet_disconnect("Corrupted check bytes on input.");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001258 buffer_consume_end(&active_state->incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259
Darren Tuckerf7288d72009-06-21 18:12:20 +10001260 if (active_state->packet_compression) {
1261 buffer_clear(&active_state->compression_buffer);
1262 buffer_uncompress(&active_state->incoming_packet,
1263 &active_state->compression_buffer);
1264 buffer_clear(&active_state->incoming_packet);
1265 buffer_append(&active_state->incoming_packet,
1266 buffer_ptr(&active_state->compression_buffer),
1267 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001268 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001269 active_state->p_read.packets++;
1270 active_state->p_read.bytes += padded_len + 4;
1271 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001272 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1273 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001274 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001275}
Damien Miller95def091999-11-25 00:26:21 +11001276
Ben Lindstrombba81212001-06-25 05:01:22 +00001277static int
Damien Millerdff50992002-01-22 23:16:32 +11001278packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001279{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001280 u_int padlen, need;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001281 u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
Damien Miller1d75abf2013-01-09 16:12:19 +11001282 u_int maclen, authlen = 0, aadlen = 0, block_size;
markus@openbsd.org128343b2015-01-13 19:31:40 +00001283 int r;
Damien Miller33b13562000-04-04 14:38:59 +10001284 Enc *enc = NULL;
1285 Mac *mac = NULL;
1286 Comp *comp = NULL;
1287
Darren Tuckerf7288d72009-06-21 18:12:20 +10001288 if (active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001289 return SSH_MSG_NONE;
1290
Darren Tuckerf7288d72009-06-21 18:12:20 +10001291 if (active_state->newkeys[MODE_IN] != NULL) {
1292 enc = &active_state->newkeys[MODE_IN]->enc;
1293 mac = &active_state->newkeys[MODE_IN]->mac;
1294 comp = &active_state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001295 /* disable mac for authenticated encryption */
1296 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1297 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001298 }
1299 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001300 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001301 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001302
Damien Milleraf43a7a2012-12-12 10:46:31 +11001303 if (aadlen && active_state->packlen == 0) {
Damien Miller0fde8ac2013-11-21 14:12:23 +11001304 if (cipher_get_length(&active_state->receive_context,
1305 &active_state->packlen,
1306 active_state->p_read.seqnr,
1307 buffer_ptr(&active_state->input),
1308 buffer_len(&active_state->input)) != 0)
Damien Milleraf43a7a2012-12-12 10:46:31 +11001309 return SSH_MSG_NONE;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001310 if (active_state->packlen < 1 + 4 ||
1311 active_state->packlen > PACKET_MAX_SIZE) {
1312#ifdef PACKET_DEBUG
1313 buffer_dump(&active_state->input);
1314#endif
1315 logit("Bad packet length %u.", active_state->packlen);
1316 packet_disconnect("Packet corrupt");
1317 }
Damien Miller8c05da32012-12-13 07:18:59 +11001318 buffer_clear(&active_state->incoming_packet);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001319 } else if (active_state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001320 /*
1321 * check if input size is less than the cipher block size,
1322 * decrypt first block and extract length of incoming packet
1323 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001324 if (buffer_len(&active_state->input) < block_size)
Damien Miller33b13562000-04-04 14:38:59 +10001325 return SSH_MSG_NONE;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001326 buffer_clear(&active_state->incoming_packet);
1327 cp = buffer_append_space(&active_state->incoming_packet,
Damien Miller33b13562000-04-04 14:38:59 +10001328 block_size);
Damien Millerbcd00ab2013-12-07 10:41:55 +11001329 if (cipher_crypt(&active_state->receive_context,
Damien Miller0fde8ac2013-11-21 14:12:23 +11001330 active_state->p_read.seqnr, cp,
Damien Millerbcd00ab2013-12-07 10:41:55 +11001331 buffer_ptr(&active_state->input), block_size, 0, 0) != 0)
1332 fatal("Decryption integrity check failed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001333 cp = buffer_ptr(&active_state->incoming_packet);
1334 active_state->packlen = get_u32(cp);
1335 if (active_state->packlen < 1 + 4 ||
1336 active_state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001337#ifdef PACKET_DEBUG
Darren Tuckerf7288d72009-06-21 18:12:20 +10001338 buffer_dump(&active_state->incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001339#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001340 logit("Bad packet length %u.", active_state->packlen);
1341 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001342 PACKET_MAX_SIZE);
1343 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001344 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001345 buffer_consume(&active_state->input, block_size);
Damien Miller33b13562000-04-04 14:38:59 +10001346 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001347 DBG(debug("input: packet len %u", active_state->packlen+4));
1348 if (aadlen) {
1349 /* only the payload is encrypted */
1350 need = active_state->packlen;
1351 } else {
1352 /*
1353 * the payload size and the payload are encrypted, but we
1354 * have a partial packet of block_size bytes
1355 */
1356 need = 4 + active_state->packlen - block_size;
1357 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001358 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1359 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001360 if (need % block_size != 0) {
1361 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001362 need, block_size, need % block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001363 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001364 PACKET_MAX_SIZE - block_size);
1365 return SSH_MSG_NONE;
Darren Tucker99d11a32008-12-01 21:40:48 +11001366 }
Damien Miller33b13562000-04-04 14:38:59 +10001367 /*
1368 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001369 * decrypt into incoming_packet:
1370 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001371 * 'need' bytes are encrypted, followed by either
1372 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001373 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001374 */
Damien Miller1d75abf2013-01-09 16:12:19 +11001375 if (buffer_len(&active_state->input) < aadlen + need + authlen + maclen)
Damien Miller33b13562000-04-04 14:38:59 +10001376 return SSH_MSG_NONE;
1377#ifdef PACKET_DEBUG
1378 fprintf(stderr, "read_poll enc/full: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001379 buffer_dump(&active_state->input);
Damien Miller33b13562000-04-04 14:38:59 +10001380#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001381 /* EtM: compute mac over encrypted input */
1382 if (mac && mac->enabled && mac->etm)
markus@openbsd.org128343b2015-01-13 19:31:40 +00001383 if ((r = mac_compute(mac, active_state->p_read.seqnr,
1384 buffer_ptr(&active_state->input), aadlen + need,
1385 macbuf, sizeof(macbuf))) != 0)
1386 fatal("%s: mac_compute: %s", __func__, ssh_err(r));
Damien Milleraf43a7a2012-12-12 10:46:31 +11001387 cp = buffer_append_space(&active_state->incoming_packet, aadlen + need);
Damien Millerbcd00ab2013-12-07 10:41:55 +11001388 if (cipher_crypt(&active_state->receive_context,
Damien Miller0fde8ac2013-11-21 14:12:23 +11001389 active_state->p_read.seqnr, cp,
Damien Millerbcd00ab2013-12-07 10:41:55 +11001390 buffer_ptr(&active_state->input), need, aadlen, authlen) != 0)
1391 fatal("Decryption integrity check failed");
Damien Miller1d75abf2013-01-09 16:12:19 +11001392 buffer_consume(&active_state->input, aadlen + need + authlen);
Damien Miller33b13562000-04-04 14:38:59 +10001393 /*
1394 * compute MAC over seqnr and packet,
1395 * increment sequence number for incoming packet
1396 */
Damien Miller4af51302000-04-16 11:18:38 +10001397 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001398 if (!mac->etm)
markus@openbsd.org128343b2015-01-13 19:31:40 +00001399 if ((r = mac_compute(mac, active_state->p_read.seqnr,
Damien Milleraf43a7a2012-12-12 10:46:31 +11001400 buffer_ptr(&active_state->incoming_packet),
markus@openbsd.org128343b2015-01-13 19:31:40 +00001401 buffer_len(&active_state->incoming_packet),
1402 macbuf, sizeof(macbuf))) != 0)
1403 fatal("%s: mac_compute: %s", __func__, ssh_err(r));
Damien Millerea1651c2010-07-16 13:58:37 +10001404 if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001405 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001406 logit("Corrupted MAC on input.");
1407 if (need > PACKET_MAX_SIZE)
1408 fatal("internal error need %d", need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001409 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001410 PACKET_MAX_SIZE - need);
1411 return SSH_MSG_NONE;
1412 }
1413
Darren Tuckerf7288d72009-06-21 18:12:20 +10001414 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1415 buffer_consume(&active_state->input, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +10001416 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001417 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001418 if (seqnr_p != NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001419 *seqnr_p = active_state->p_read.seqnr;
1420 if (++active_state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001421 logit("incoming seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001422 if (++active_state->p_read.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +10001423 if (!(datafellows & SSH_BUG_NOREKEY))
1424 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001425 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1426 active_state->p_read.bytes += active_state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001427
1428 /* get padlen */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001429 cp = buffer_ptr(&active_state->incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001430 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001431 DBG(debug("input: padlen %d", padlen));
1432 if (padlen < 4)
1433 packet_disconnect("Corrupted padlen %d on input.", padlen);
1434
1435 /* skip packet size + padlen, discard padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001436 buffer_consume(&active_state->incoming_packet, 4 + 1);
1437 buffer_consume_end(&active_state->incoming_packet, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001438
Darren Tuckerf7288d72009-06-21 18:12:20 +10001439 DBG(debug("input: len before de-compress %d",
1440 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001441 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001442 buffer_clear(&active_state->compression_buffer);
1443 buffer_uncompress(&active_state->incoming_packet,
1444 &active_state->compression_buffer);
1445 buffer_clear(&active_state->incoming_packet);
1446 buffer_append(&active_state->incoming_packet,
1447 buffer_ptr(&active_state->compression_buffer),
1448 buffer_len(&active_state->compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001449 DBG(debug("input: len after de-compress %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001450 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001451 }
1452 /*
1453 * get packet type, implies consume.
1454 * return length of payload (without type field)
1455 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001456 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001457 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1458 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001459 if (type == SSH2_MSG_NEWKEYS)
1460 set_newkeys(MODE_IN);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001461 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1462 !active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +10001463 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001464#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001465 fprintf(stderr, "read/plain[%d]:\r\n", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001466 buffer_dump(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001467#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001468 /* reset for next packet */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001469 active_state->packlen = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001470 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001471}
1472
1473int
Damien Millerdff50992002-01-22 23:16:32 +11001474packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001475{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001476 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001477 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001478 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001479
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001480 for (;;) {
1481 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001482 type = packet_read_poll2(seqnr_p);
Darren Tucker136e56f2008-06-08 12:49:30 +10001483 if (type) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001484 active_state->keep_alive_timeouts = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001485 DBG(debug("received packet type %d", type));
Darren Tucker136e56f2008-06-08 12:49:30 +10001486 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001487 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001488 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001489 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001490 break;
Damien Miller33b13562000-04-04 14:38:59 +10001491 case SSH2_MSG_DEBUG:
1492 packet_get_char();
1493 msg = packet_get_string(NULL);
1494 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001495 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001496 msg = packet_get_string(NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001497 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001498 break;
1499 case SSH2_MSG_DISCONNECT:
1500 reason = packet_get_int();
1501 msg = packet_get_string(NULL);
Damien Millerd5edefd2013-04-23 15:21:39 +10001502 /* Ignore normal client exit notifications */
1503 do_log2(active_state->server_side &&
1504 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1505 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1506 "Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001507 get_remote_ipaddr(), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001508 free(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001509 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001510 break;
Damien Miller659811f2002-01-22 23:23:11 +11001511 case SSH2_MSG_UNIMPLEMENTED:
1512 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001513 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1514 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001515 break;
Damien Miller33b13562000-04-04 14:38:59 +10001516 default:
1517 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001518 }
Damien Miller33b13562000-04-04 14:38:59 +10001519 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001520 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001521 switch (type) {
Damien Millerce986542013-07-18 16:12:44 +10001522 case SSH_MSG_NONE:
1523 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001524 case SSH_MSG_IGNORE:
1525 break;
1526 case SSH_MSG_DEBUG:
1527 msg = packet_get_string(NULL);
1528 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001529 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001530 break;
1531 case SSH_MSG_DISCONNECT:
1532 msg = packet_get_string(NULL);
Damien Miller894926e2013-02-12 11:03:58 +11001533 error("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001534 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001535 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001536 break;
1537 default:
Damien Millerce986542013-07-18 16:12:44 +10001538 DBG(debug("received packet type %d", type));
Damien Miller33b13562000-04-04 14:38:59 +10001539 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001540 }
Damien Miller33b13562000-04-04 14:38:59 +10001541 }
1542 }
1543}
1544
Damien Miller5428f641999-11-25 11:54:57 +11001545/*
1546 * Buffers the given amount of input characters. This is intended to be used
1547 * together with packet_read_poll.
1548 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549
1550void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001551packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001553 if (active_state->packet_discard) {
1554 active_state->keep_alive_timeouts = 0; /* ?? */
1555 if (len >= active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001556 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001557 active_state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001558 return;
1559 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001560 buffer_append(&active_state->input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001561}
1562
1563/* Returns a character from the packet. */
1564
Ben Lindstrom46c16222000-12-22 01:43:59 +00001565u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001566packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567{
Damien Miller95def091999-11-25 00:26:21 +11001568 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001569
Darren Tuckerf7288d72009-06-21 18:12:20 +10001570 buffer_get(&active_state->incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001571 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001572}
1573
1574/* Returns an integer from the packet data. */
1575
Ben Lindstrom46c16222000-12-22 01:43:59 +00001576u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001577packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001578{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001579 return buffer_get_int(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001580}
1581
Darren Tucker761c3892009-06-21 18:16:26 +10001582/* Returns an 64 bit integer from the packet data. */
1583
1584u_int64_t
1585packet_get_int64(void)
1586{
1587 return buffer_get_int64(&active_state->incoming_packet);
1588}
1589
Damien Miller5428f641999-11-25 11:54:57 +11001590/*
1591 * Returns an arbitrary precision integer from the packet data. The integer
1592 * must have been initialized before this call.
1593 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001594
Damien Miller1f0311c2014-05-15 14:24:09 +10001595#ifdef WITH_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001596void
Damien Millerd432ccf2002-01-22 23:14:44 +11001597packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001598{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001599 buffer_get_bignum(&active_state->incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001600}
1601
Damien Miller33b13562000-04-04 14:38:59 +10001602void
Damien Millerd432ccf2002-01-22 23:14:44 +11001603packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001604{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001605 buffer_get_bignum2(&active_state->incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001606}
1607
Damien Miller6af914a2010-09-10 11:39:26 +10001608#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001609void
1610packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point)
1611{
1612 buffer_get_ecpoint(&active_state->incoming_packet, curve, point);
1613}
Damien Miller6af914a2010-09-10 11:39:26 +10001614#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001615
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001616void *
Damien Millereccb9de2005-06-17 12:59:34 +10001617packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001618{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001619 u_int bytes = buffer_len(&active_state->incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001620
Damien Miller33b13562000-04-04 14:38:59 +10001621 if (length_ptr != NULL)
1622 *length_ptr = bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001623 return buffer_ptr(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001624}
Damien Miller1f0311c2014-05-15 14:24:09 +10001625#endif
Damien Miller33b13562000-04-04 14:38:59 +10001626
Damien Miller4af51302000-04-16 11:18:38 +10001627int
1628packet_remaining(void)
1629{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001630 return buffer_len(&active_state->incoming_packet);
Damien Miller4af51302000-04-16 11:18:38 +10001631}
1632
Damien Miller5428f641999-11-25 11:54:57 +11001633/*
1634 * Returns a string from the packet data. The string is allocated using
1635 * xmalloc; it is the responsibility of the calling program to free it when
1636 * no longer needed. The length_ptr argument may be NULL, or point to an
1637 * integer into which the length of the string is stored.
1638 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001639
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001640void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001641packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001642{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001643 return buffer_get_string(&active_state->incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001644}
1645
Damien Miller633de332014-05-15 13:48:26 +10001646const void *
Damien Millerdb255ca2008-05-19 14:59:37 +10001647packet_get_string_ptr(u_int *length_ptr)
1648{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001649 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
Damien Millerdb255ca2008-05-19 14:59:37 +10001650}
1651
Damien Millerda108ec2010-08-31 22:36:39 +10001652/* Ensures the returned string has no embedded \0 characters in it. */
1653char *
1654packet_get_cstring(u_int *length_ptr)
1655{
1656 return buffer_get_cstring(&active_state->incoming_packet, length_ptr);
1657}
1658
Damien Miller5428f641999-11-25 11:54:57 +11001659/*
1660 * Sends a diagnostic message from the server to the client. This message
1661 * can be sent at any time (but not while constructing another message). The
1662 * message is printed immediately, but only if the client is being executed
1663 * in verbose mode. These messages are primarily intended to ease debugging
1664 * authentication problems. The length of the formatted message must not
1665 * exceed 1024 bytes. This will automatically call packet_write_wait.
1666 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001667
1668void
Damien Miller95def091999-11-25 00:26:21 +11001669packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001670{
Damien Miller95def091999-11-25 00:26:21 +11001671 char buf[1024];
1672 va_list args;
1673
Ben Lindstroma14ee472000-12-07 01:24:58 +00001674 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1675 return;
1676
Damien Miller95def091999-11-25 00:26:21 +11001677 va_start(args, fmt);
1678 vsnprintf(buf, sizeof(buf), fmt, args);
1679 va_end(args);
1680
Damien Miller7c8af4f2000-05-01 08:24:07 +10001681 if (compat20) {
1682 packet_start(SSH2_MSG_DEBUG);
1683 packet_put_char(0); /* bool: always display */
1684 packet_put_cstring(buf);
1685 packet_put_cstring("");
1686 } else {
1687 packet_start(SSH_MSG_DEBUG);
1688 packet_put_cstring(buf);
1689 }
Damien Miller95def091999-11-25 00:26:21 +11001690 packet_send();
1691 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001692}
1693
Damien Miller5428f641999-11-25 11:54:57 +11001694/*
1695 * Logs the error plus constructs and sends a disconnect packet, closes the
1696 * connection, and exits. This function never returns. The error message
1697 * should not contain a newline. The length of the formatted message must
1698 * not exceed 1024 bytes.
1699 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001700
1701void
Damien Miller95def091999-11-25 00:26:21 +11001702packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001703{
Damien Miller95def091999-11-25 00:26:21 +11001704 char buf[1024];
1705 va_list args;
1706 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001707
Damien Miller95def091999-11-25 00:26:21 +11001708 if (disconnecting) /* Guard against recursive invocations. */
1709 fatal("packet_disconnect called recursively.");
1710 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001711
Damien Miller5428f641999-11-25 11:54:57 +11001712 /*
1713 * Format the message. Note that the caller must make sure the
1714 * message is of limited size.
1715 */
Damien Miller95def091999-11-25 00:26:21 +11001716 va_start(args, fmt);
1717 vsnprintf(buf, sizeof(buf), fmt, args);
1718 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001719
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001720 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001721 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001722
Damien Miller95def091999-11-25 00:26:21 +11001723 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001724 if (compat20) {
1725 packet_start(SSH2_MSG_DISCONNECT);
1726 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1727 packet_put_cstring(buf);
1728 packet_put_cstring("");
1729 } else {
1730 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001731 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001732 }
Damien Miller95def091999-11-25 00:26:21 +11001733 packet_send();
1734 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001735
Damien Miller95def091999-11-25 00:26:21 +11001736 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001737 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001738
Damien Miller95def091999-11-25 00:26:21 +11001739 /* Close the connection. */
1740 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001741 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001742}
1743
Damien Miller5428f641999-11-25 11:54:57 +11001744/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001745
1746void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001747packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001748{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001749 int len = buffer_len(&active_state->output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001750 int cont;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001751
Damien Miller95def091999-11-25 00:26:21 +11001752 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001753 cont = 0;
1754 len = roaming_write(active_state->connection_out,
1755 buffer_ptr(&active_state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001756 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001757 if (errno == EINTR || errno == EAGAIN ||
1758 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001759 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001760 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001761 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001762 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001763 fatal("Write connection closed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001764 buffer_consume(&active_state->output, len);
Damien Miller95def091999-11-25 00:26:21 +11001765 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001766}
1767
Damien Miller5428f641999-11-25 11:54:57 +11001768/*
1769 * Calls packet_write_poll repeatedly until all pending output data has been
1770 * written.
1771 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001772
1773void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001774packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001775{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001776 fd_set *setp;
Damien Millerce986542013-07-18 16:12:44 +10001777 int ret, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001778 struct timeval start, timeout, *timeoutp = NULL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001779
Darren Tuckerf7288d72009-06-21 18:12:20 +10001780 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1781 NFDBITS), sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001782 packet_write_poll();
1783 while (packet_have_data_to_write()) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001784 memset(setp, 0, howmany(active_state->connection_out + 1,
1785 NFDBITS) * sizeof(fd_mask));
1786 FD_SET(active_state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001787
Darren Tuckerf7288d72009-06-21 18:12:20 +10001788 if (active_state->packet_timeout_ms > 0) {
1789 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001790 timeoutp = &timeout;
1791 }
1792 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001793 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001794 ms_to_timeval(&timeout, ms_remain);
1795 gettimeofday(&start, NULL);
1796 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001797 if ((ret = select(active_state->connection_out + 1,
1798 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001799 break;
Damien Millerea437422009-10-02 11:49:03 +10001800 if (errno != EAGAIN && errno != EINTR &&
1801 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001802 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001803 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001804 continue;
1805 ms_subtract_diff(&start, &ms_remain);
1806 if (ms_remain <= 0) {
1807 ret = 0;
1808 break;
1809 }
1810 }
1811 if (ret == 0) {
1812 logit("Connection to %.200s timed out while "
1813 "waiting to write", get_remote_ipaddr());
1814 cleanup_exit(255);
1815 }
Damien Miller95def091999-11-25 00:26:21 +11001816 packet_write_poll();
1817 }
Darren Tuckera627d422013-06-02 07:31:17 +10001818 free(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001819}
1820
1821/* Returns true if there is buffered data to write to the connection. */
1822
1823int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001824packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001825{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001826 return buffer_len(&active_state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001827}
1828
1829/* Returns true if there is not too much data to write to the connection. */
1830
1831int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001832packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001833{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001834 if (active_state->interactive_mode)
1835 return buffer_len(&active_state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11001836 else
Darren Tuckerf7288d72009-06-21 18:12:20 +10001837 return buffer_len(&active_state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001838}
1839
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001840static void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001841packet_set_tos(int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00001842{
Damien Millerd2ac5d72011-05-15 08:43:13 +10001843#ifndef IP_TOS_IS_BROKEN
1844 if (!packet_connection_is_on_socket())
Ben Lindstroma7433982002-12-23 02:41:41 +00001845 return;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001846 switch (packet_connection_af()) {
1847# ifdef IP_TOS
1848 case AF_INET:
1849 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
1850 if (setsockopt(active_state->connection_in,
1851 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
1852 error("setsockopt IP_TOS %d: %.100s:",
1853 tos, strerror(errno));
1854 break;
1855# endif /* IP_TOS */
1856# ifdef IPV6_TCLASS
1857 case AF_INET6:
1858 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
1859 if (setsockopt(active_state->connection_in,
1860 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
1861 error("setsockopt IPV6_TCLASS %d: %.100s:",
1862 tos, strerror(errno));
1863 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001864# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10001865 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10001866#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11001867}
Ben Lindstroma7433982002-12-23 02:41:41 +00001868
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001869/* Informs that the current session is interactive. Sets IP flags for that. */
1870
1871void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001872packet_set_interactive(int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001873{
Darren Tucker7b935c72009-06-21 18:59:36 +10001874 if (active_state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001875 return;
Darren Tucker7b935c72009-06-21 18:59:36 +10001876 active_state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001877
Damien Miller95def091999-11-25 00:26:21 +11001878 /* Record that we are in interactive mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001879 active_state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001880
Damien Miller34132e52000-01-14 15:45:46 +11001881 /* Only set socket options if using a socket. */
1882 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001883 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001884 set_nodelay(active_state->connection_in);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001885 packet_set_tos(interactive ? qos_interactive : qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001886}
1887
1888/* Returns true if the current connection is interactive. */
1889
1890int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001891packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001892{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001893 return active_state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001894}
Damien Miller6162d121999-11-21 13:23:52 +11001895
Darren Tucker1f8311c2004-05-13 16:39:33 +10001896int
Darren Tucker502d3842003-06-28 12:38:01 +10001897packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001898{
Darren Tucker7b935c72009-06-21 18:59:36 +10001899 if (active_state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10001900 logit("packet_set_maxsize: called twice: old %d new %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001901 active_state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001902 return -1;
1903 }
1904 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001905 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001906 return -1;
1907 }
Darren Tucker7b935c72009-06-21 18:59:36 +10001908 active_state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001909 debug("packet_set_maxsize: setting to %d", s);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001910 active_state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11001911 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001912}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001913
Darren Tuckerf7288d72009-06-21 18:12:20 +10001914int
1915packet_inc_alive_timeouts(void)
1916{
1917 return ++active_state->keep_alive_timeouts;
1918}
1919
1920void
1921packet_set_alive_timeouts(int ka)
1922{
1923 active_state->keep_alive_timeouts = ka;
1924}
1925
1926u_int
1927packet_get_maxsize(void)
1928{
1929 return active_state->max_packet_size;
1930}
1931
Damien Miller9f643902001-11-12 11:02:52 +11001932/* roundup current message to pad bytes */
1933void
1934packet_add_padding(u_char pad)
1935{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001936 active_state->extra_pad = pad;
Damien Miller9f643902001-11-12 11:02:52 +11001937}
1938
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001939/*
1940 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001941 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001942 * byte SSH_MSG_IGNORE
1943 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001944 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001945 * All implementations MUST understand (and ignore) this message at any
1946 * time (after receiving the protocol version). No implementation is
1947 * required to send them. This message can be used as an additional
1948 * protection measure against advanced traffic analysis techniques.
1949 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001950void
1951packet_send_ignore(int nbytes)
1952{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001953 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001954 int i;
1955
1956 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001957 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001958 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001959 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001960 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001961 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001962 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001963 }
1964}
Damien Millera5539d22003-04-09 20:50:06 +10001965
Darren Tucker1f8311c2004-05-13 16:39:33 +10001966#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001967int
1968packet_need_rekeying(void)
1969{
1970 if (datafellows & SSH_BUG_NOREKEY)
1971 return 0;
1972 return
Darren Tuckerf7288d72009-06-21 18:12:20 +10001973 (active_state->p_send.packets > MAX_PACKETS) ||
1974 (active_state->p_read.packets > MAX_PACKETS) ||
1975 (active_state->max_blocks_out &&
1976 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1977 (active_state->max_blocks_in &&
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001978 (active_state->p_read.blocks > active_state->max_blocks_in)) ||
1979 (active_state->rekey_interval != 0 && active_state->rekey_time +
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001980 active_state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10001981}
1982
1983void
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001984packet_set_rekey_limits(u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10001985{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001986 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
1987 (int)seconds);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001988 active_state->rekey_limit = bytes;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001989 active_state->rekey_interval = seconds;
1990 /*
1991 * We set the time here so that in post-auth privsep slave we count
1992 * from the completion of the authentication.
1993 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001994 active_state->rekey_time = monotime();
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001995}
1996
1997time_t
1998packet_get_rekey_timeout(void)
1999{
2000 time_t seconds;
2001
2002 seconds = active_state->rekey_time + active_state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002003 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002004 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10002005}
Damien Miller9786e6e2005-07-26 21:54:56 +10002006
2007void
2008packet_set_server(void)
2009{
Darren Tuckerf7288d72009-06-21 18:12:20 +10002010 active_state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10002011}
2012
2013void
2014packet_set_authenticated(void)
2015{
Darren Tuckerf7288d72009-06-21 18:12:20 +10002016 active_state->after_authentication = 1;
2017}
2018
2019void *
2020packet_get_input(void)
2021{
2022 return (void *)&active_state->input;
2023}
2024
2025void *
2026packet_get_output(void)
2027{
2028 return (void *)&active_state->output;
2029}
2030
2031void *
2032packet_get_newkeys(int mode)
2033{
2034 return (void *)active_state->newkeys[mode];
Damien Miller9786e6e2005-07-26 21:54:56 +10002035}
Darren Tuckere841eb02009-07-06 07:11:13 +10002036
2037/*
2038 * Save the state for the real connection, and use a separate state when
2039 * resuming a suspended connection.
2040 */
2041void
2042packet_backup_state(void)
2043{
2044 struct session_state *tmp;
2045
2046 close(active_state->connection_in);
2047 active_state->connection_in = -1;
2048 close(active_state->connection_out);
2049 active_state->connection_out = -1;
2050 if (backup_state)
2051 tmp = backup_state;
2052 else
2053 tmp = alloc_session_state();
2054 backup_state = active_state;
2055 active_state = tmp;
2056}
2057
2058/*
2059 * Swap in the old state when resuming a connecion.
2060 */
2061void
2062packet_restore_state(void)
2063{
2064 struct session_state *tmp;
2065 void *buf;
2066 u_int len;
2067
2068 tmp = backup_state;
2069 backup_state = active_state;
2070 active_state = tmp;
2071 active_state->connection_in = backup_state->connection_in;
2072 backup_state->connection_in = -1;
2073 active_state->connection_out = backup_state->connection_out;
2074 backup_state->connection_out = -1;
2075 len = buffer_len(&backup_state->input);
2076 if (len > 0) {
2077 buf = buffer_ptr(&backup_state->input);
2078 buffer_append(&active_state->input, buf, len);
2079 buffer_clear(&backup_state->input);
2080 add_recv_bytes(len);
2081 }
2082}
Damien Millerc31a0cd2014-05-15 14:37:39 +10002083
2084/* Reset after_authentication and reset compression in post-auth privsep */
2085void
2086packet_set_postauth(void)
2087{
2088 Comp *comp;
2089 int mode;
2090
2091 debug("%s: called", __func__);
2092 /* This was set in net child, but is not visible in user child */
2093 active_state->after_authentication = 1;
2094 active_state->rekeying = 0;
2095 for (mode = 0; mode < MODE_MAX; mode++) {
2096 if (active_state->newkeys[mode] == NULL)
2097 continue;
2098 comp = &active_state->newkeys[mode]->comp;
2099 if (comp && comp->enabled)
2100 packet_init_compression();
2101 }
2102}