blob: 9326ddea6d64ae4377e2a9af1036513607c90c29 [file] [log] [blame]
Damien Miller894926e2013-02-12 11:03:58 +11001/* $OpenBSD: packet.c,v 1.181 2013/02/10 23:35:24 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
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 Tucker5d196262006-07-12 22:15:16 +100061
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "xmalloc.h"
63#include "buffer.h"
64#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "compress.h"
67#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000068#include "channels.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"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000075#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
77#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110078#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000079#include "ssh.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100080#include "roaming.h"
Damien Miller33b13562000-04-04 14:38:59 +100081
82#ifdef PACKET_DEBUG
83#define DBG(x) x
84#else
85#define DBG(x)
86#endif
87
Damien Miller13ae44c2009-01-28 16:38:41 +110088#define PACKET_MAX_SIZE (256 * 1024)
89
Darren Tuckerf7288d72009-06-21 18:12:20 +100090struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100091 u_int32_t seqnr;
92 u_int32_t packets;
93 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +100094 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +100095};
Damien Miller13ae44c2009-01-28 16:38:41 +110096
Damien Millera5539d22003-04-09 20:50:06 +100097struct packet {
98 TAILQ_ENTRY(packet) next;
99 u_char type;
100 Buffer payload;
101};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000102
103struct session_state {
104 /*
105 * This variable contains the file descriptors used for
106 * communicating with the other side. connection_in is used for
107 * reading; connection_out for writing. These can be the same
108 * descriptor, in which case it is assumed to be a socket.
109 */
110 int connection_in;
111 int connection_out;
112
113 /* Protocol flags for the remote side. */
114 u_int remote_protocol_flags;
115
116 /* Encryption context for receiving data. Only used for decryption. */
117 CipherContext receive_context;
118
119 /* Encryption context for sending data. Only used for encryption. */
120 CipherContext send_context;
121
122 /* Buffer for raw input data from the socket. */
123 Buffer input;
124
125 /* Buffer for raw output data going to the socket. */
126 Buffer output;
127
128 /* Buffer for the partial outgoing packet being constructed. */
129 Buffer outgoing_packet;
130
131 /* Buffer for the incoming packet currently being processed. */
132 Buffer incoming_packet;
133
134 /* Scratch buffer for packet compression/decompression. */
135 Buffer compression_buffer;
136 int compression_buffer_ready;
137
138 /*
139 * Flag indicating whether packet compression/decompression is
140 * enabled.
141 */
142 int packet_compression;
143
144 /* default maximum packet size */
145 u_int max_packet_size;
146
147 /* Flag indicating whether this module has been initialized. */
148 int initialized;
149
150 /* Set to true if the connection is interactive. */
151 int interactive_mode;
152
153 /* Set to true if we are the server side. */
154 int server_side;
155
156 /* Set to true if we are authenticated. */
157 int after_authentication;
158
159 int keep_alive_timeouts;
160
161 /* The maximum time that we will wait to send or receive a packet */
162 int packet_timeout_ms;
163
164 /* Session key information for Encryption and MAC */
165 Newkeys *newkeys[MODE_MAX];
166 struct packet_state p_read, p_send;
167
168 u_int64_t max_blocks_in, max_blocks_out;
169 u_int32_t rekey_limit;
170
171 /* Session key for protocol v1 */
172 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
173 u_int ssh1_keylen;
174
175 /* roundup current message to extra_pad bytes */
176 u_char extra_pad;
177
178 /* XXX discard incoming data after MAC error */
179 u_int packet_discard;
180 Mac *packet_discard_mac;
181
182 /* Used in packet_read_poll2() */
183 u_int packlen;
184
Darren Tucker7b935c72009-06-21 18:59:36 +1000185 /* Used in packet_send2 */
186 int rekeying;
187
188 /* Used in packet_set_interactive */
189 int set_interactive_called;
190
191 /* Used in packet_set_maxsize */
192 int set_maxsize_called;
193
Darren Tuckerf7288d72009-06-21 18:12:20 +1000194 TAILQ_HEAD(, packet) outgoing;
195};
196
Darren Tuckere841eb02009-07-06 07:11:13 +1000197static struct session_state *active_state, *backup_state;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000198
199static struct session_state *
Darren Tuckerb422afa2009-06-21 18:58:46 +1000200alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000201{
Damien Miller7a221a12010-11-20 15:14:29 +1100202 struct session_state *s = xcalloc(1, sizeof(*s));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000203
Damien Miller7a221a12010-11-20 15:14:29 +1100204 s->connection_in = -1;
205 s->connection_out = -1;
206 s->max_packet_size = 32768;
207 s->packet_timeout_ms = -1;
208 return s;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000209}
Damien Millera5539d22003-04-09 20:50:06 +1000210
Damien Miller5428f641999-11-25 11:54:57 +1100211/*
212 * Sets the descriptors used for communication. Disables encryption until
213 * packet_set_encryption_key is called.
214 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215void
216packet_set_connection(int fd_in, int fd_out)
217{
Damien Miller874d77b2000-10-14 16:23:11 +1100218 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000219
Damien Miller874d77b2000-10-14 16:23:11 +1100220 if (none == NULL)
221 fatal("packet_set_connection: cannot load cipher 'none'");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000222 if (active_state == NULL)
223 active_state = alloc_session_state();
224 active_state->connection_in = fd_in;
225 active_state->connection_out = fd_out;
226 cipher_init(&active_state->send_context, none, (const u_char *)"",
Darren Tucker1f8311c2004-05-13 16:39:33 +1000227 0, NULL, 0, CIPHER_ENCRYPT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000228 cipher_init(&active_state->receive_context, none, (const u_char *)"",
Darren Tucker1f8311c2004-05-13 16:39:33 +1000229 0, NULL, 0, CIPHER_DECRYPT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000230 active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL;
231 if (!active_state->initialized) {
232 active_state->initialized = 1;
233 buffer_init(&active_state->input);
234 buffer_init(&active_state->output);
235 buffer_init(&active_state->outgoing_packet);
236 buffer_init(&active_state->incoming_packet);
237 TAILQ_INIT(&active_state->outgoing);
238 active_state->p_send.packets = active_state->p_read.packets = 0;
Damien Miller95def091999-11-25 00:26:21 +1100239 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
Darren Tucker3fc464e2008-06-13 06:42:45 +1000242void
243packet_set_timeout(int timeout, int count)
244{
Damien Miller8ed4de82011-12-19 10:52:50 +1100245 if (timeout <= 0 || count <= 0) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000246 active_state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000247 return;
248 }
249 if ((INT_MAX / 1000) / count < timeout)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000250 active_state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000251 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000252 active_state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000253}
254
Damien Miller13ae44c2009-01-28 16:38:41 +1100255static void
256packet_stop_discard(void)
257{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000258 if (active_state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100259 char buf[1024];
260
261 memset(buf, 'a', sizeof(buf));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000262 while (buffer_len(&active_state->incoming_packet) <
263 PACKET_MAX_SIZE)
264 buffer_append(&active_state->incoming_packet, buf,
265 sizeof(buf));
266 (void) mac_compute(active_state->packet_discard_mac,
267 active_state->p_read.seqnr,
268 buffer_ptr(&active_state->incoming_packet),
Damien Miller13ae44c2009-01-28 16:38:41 +1100269 PACKET_MAX_SIZE);
270 }
271 logit("Finished discarding for %.200s", get_remote_ipaddr());
272 cleanup_exit(255);
273}
274
275static void
276packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
277{
Damien Milleraf43a7a2012-12-12 10:46:31 +1100278 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm))
Damien Miller13ae44c2009-01-28 16:38:41 +1100279 packet_disconnect("Packet corrupt");
280 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000281 active_state->packet_discard_mac = mac;
282 if (buffer_len(&active_state->input) >= discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100283 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +1000284 active_state->packet_discard = discard -
285 buffer_len(&active_state->input);
Damien Miller13ae44c2009-01-28 16:38:41 +1100286}
287
Damien Miller34132e52000-01-14 15:45:46 +1100288/* Returns 1 if remote host is connected via socket, 0 if not. */
289
290int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000291packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100292{
293 struct sockaddr_storage from, to;
294 socklen_t fromlen, tolen;
295
296 /* filedescriptors in and out are the same, so it's a socket */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000297 if (active_state->connection_in == active_state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100298 return 1;
299 fromlen = sizeof(from);
300 memset(&from, 0, sizeof(from));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000301 if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
302 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100303 return 0;
304 tolen = sizeof(to);
305 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000306 if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
307 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100308 return 0;
309 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
310 return 0;
311 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
312 return 0;
313 return 1;
314}
315
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000316/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000317 * Exports an IV from the CipherContext required to export the key
318 * state back from the unprivileged child to the privileged parent
319 * process.
320 */
321
322void
323packet_get_keyiv(int mode, u_char *iv, u_int len)
324{
325 CipherContext *cc;
326
327 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000328 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000329 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000330 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000331
332 cipher_get_keyiv(cc, iv, len);
333}
334
335int
336packet_get_keycontext(int mode, u_char *dat)
337{
338 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000339
Ben Lindstromf6027d32002-03-22 01:42:04 +0000340 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000341 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000342 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000343 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000344
345 return (cipher_get_keycontext(cc, dat));
346}
347
348void
349packet_set_keycontext(int mode, u_char *dat)
350{
351 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000352
Ben Lindstromf6027d32002-03-22 01:42:04 +0000353 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000354 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000355 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000356 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000357
358 cipher_set_keycontext(cc, dat);
359}
360
361int
362packet_get_keyiv_len(int mode)
363{
364 CipherContext *cc;
365
366 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000367 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000368 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000369 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000370
371 return (cipher_get_keyiv_len(cc));
372}
Damien Miller4f7becb2006-03-26 14:10:14 +1100373
Ben Lindstromf6027d32002-03-22 01:42:04 +0000374void
375packet_set_iv(int mode, u_char *dat)
376{
377 CipherContext *cc;
378
379 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000380 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000381 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000382 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000383
384 cipher_set_keyiv(cc, dat);
385}
Damien Miller4f7becb2006-03-26 14:10:14 +1100386
Ben Lindstromf6027d32002-03-22 01:42:04 +0000387int
Damien Miller2b92d322003-06-11 22:05:06 +1000388packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000389{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000390 return (cipher_get_number(active_state->receive_context.cipher));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000391}
392
Damien Millera5539d22003-04-09 20:50:06 +1000393void
Damien Miller7a221a12010-11-20 15:14:29 +1100394packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks,
395 u_int32_t *packets, u_int64_t *bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000396{
Damien Millera5539d22003-04-09 20:50:06 +1000397 struct packet_state *state;
398
Darren Tuckerf7288d72009-06-21 18:12:20 +1000399 state = (mode == MODE_IN) ?
400 &active_state->p_read : &active_state->p_send;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000401 if (seqnr)
402 *seqnr = state->seqnr;
403 if (blocks)
404 *blocks = state->blocks;
405 if (packets)
406 *packets = state->packets;
407 if (bytes)
408 *bytes = state->bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000409}
410
411void
Damien Millerb61f3fc2008-07-11 17:36:48 +1000412packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
413 u_int64_t bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000414{
Damien Millera5539d22003-04-09 20:50:06 +1000415 struct packet_state *state;
416
Darren Tuckerf7288d72009-06-21 18:12:20 +1000417 state = (mode == MODE_IN) ?
418 &active_state->p_read : &active_state->p_send;
Damien Millera5539d22003-04-09 20:50:06 +1000419 state->seqnr = seqnr;
420 state->blocks = blocks;
421 state->packets = packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000422 state->bytes = bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000423}
424
Damien Millerd2ac5d72011-05-15 08:43:13 +1000425static int
426packet_connection_af(void)
Damien Miller34132e52000-01-14 15:45:46 +1100427{
428 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100429 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100430
431 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000432 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
433 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100434 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000435#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100436 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000437 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000438 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000439#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000440 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100441}
442
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443/* Sets the connection into non-blocking mode. */
444
445void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000446packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447{
Damien Miller95def091999-11-25 00:26:21 +1100448 /* Set the socket into non-blocking mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000449 set_nonblock(active_state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450
Darren Tuckerf7288d72009-06-21 18:12:20 +1000451 if (active_state->connection_out != active_state->connection_in)
452 set_nonblock(active_state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453}
454
455/* Returns the socket used for reading. */
456
457int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000458packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000460 return active_state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461}
462
463/* Returns the descriptor used for writing. */
464
465int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000466packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000468 return active_state->connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469}
470
471/* Closes the connection and clears and frees internal data structures. */
472
473void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000474packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000476 if (!active_state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100477 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000478 active_state->initialized = 0;
479 if (active_state->connection_in == active_state->connection_out) {
480 shutdown(active_state->connection_out, SHUT_RDWR);
481 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100482 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000483 close(active_state->connection_in);
484 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100485 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000486 buffer_free(&active_state->input);
487 buffer_free(&active_state->output);
488 buffer_free(&active_state->outgoing_packet);
489 buffer_free(&active_state->incoming_packet);
490 if (active_state->compression_buffer_ready) {
491 buffer_free(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100492 buffer_compress_uninit();
493 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000494 cipher_cleanup(&active_state->send_context);
495 cipher_cleanup(&active_state->receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496}
497
498/* Sets remote side protocol flags. */
499
500void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000501packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000503 active_state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504}
505
506/* Returns the remote protocol flags set earlier by the above function. */
507
Ben Lindstrom46c16222000-12-22 01:43:59 +0000508u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000509packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000511 return active_state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512}
513
Damien Miller5428f641999-11-25 11:54:57 +1100514/*
515 * Starts packet compression from the next packet on in both directions.
516 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
517 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Ben Lindstrombba81212001-06-25 05:01:22 +0000519static void
520packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000521{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000522 if (active_state->compression_buffer_ready == 1)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000523 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000524 active_state->compression_buffer_ready = 1;
525 buffer_init(&active_state->compression_buffer);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000526}
527
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528void
529packet_start_compression(int level)
530{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000531 if (active_state->packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100532 fatal("Compression already enabled.");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000533 active_state->packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000534 packet_init_compression();
535 buffer_compress_init_send(level);
536 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537}
538
Damien Miller5428f641999-11-25 11:54:57 +1100539/*
Damien Miller5428f641999-11-25 11:54:57 +1100540 * Causes any further packets to be encrypted using the given key. The same
541 * key is used for both sending and reception. However, both directions are
542 * encrypted independently of each other.
543 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000544
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545void
Damien Miller7a221a12010-11-20 15:14:29 +1100546packet_set_encryption_key(const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547{
Damien Miller874d77b2000-10-14 16:23:11 +1100548 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000549
Damien Miller874d77b2000-10-14 16:23:11 +1100550 if (cipher == NULL)
551 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000552 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100553 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000554 if (keylen > SSH_SESSION_KEY_LENGTH)
555 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000556 memcpy(active_state->ssh1_key, key, keylen);
557 active_state->ssh1_keylen = keylen;
558 cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
559 0, CIPHER_ENCRYPT);
560 cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
561 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562}
563
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000564u_int
565packet_get_encryption_key(u_char *key)
566{
567 if (key == NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000568 return (active_state->ssh1_keylen);
569 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
570 return (active_state->ssh1_keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000571}
572
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000573/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000575packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000577 u_char buf[9];
578 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579
Ben Lindstrom204e4882001-03-05 06:47:00 +0000580 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000581 len = compat20 ? 6 : 9;
582 memset(buf, 0, len - 1);
583 buf[len - 1] = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000584 buffer_clear(&active_state->outgoing_packet);
585 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000586}
587
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000588/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589void
590packet_put_char(int value)
591{
Damien Miller95def091999-11-25 00:26:21 +1100592 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000593
Darren Tuckerf7288d72009-06-21 18:12:20 +1000594 buffer_append(&active_state->outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595}
Damien Miller4f7becb2006-03-26 14:10:14 +1100596
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000598packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000600 buffer_put_int(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000601}
Damien Miller4f7becb2006-03-26 14:10:14 +1100602
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603void
Darren Tucker761c3892009-06-21 18:16:26 +1000604packet_put_int64(u_int64_t value)
605{
606 buffer_put_int64(&active_state->outgoing_packet, value);
607}
608
609void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100610packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000612 buffer_put_string(&active_state->outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613}
Damien Miller4f7becb2006-03-26 14:10:14 +1100614
Damien Miller33b13562000-04-04 14:38:59 +1000615void
616packet_put_cstring(const char *str)
617{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000618 buffer_put_cstring(&active_state->outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000619}
Damien Miller4f7becb2006-03-26 14:10:14 +1100620
Damien Miller33b13562000-04-04 14:38:59 +1000621void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100622packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000623{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000624 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000625}
Damien Miller4f7becb2006-03-26 14:10:14 +1100626
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627void
Damien Miller95def091999-11-25 00:26:21 +1100628packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000630 buffer_put_bignum(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631}
Damien Miller4f7becb2006-03-26 14:10:14 +1100632
Damien Miller33b13562000-04-04 14:38:59 +1000633void
634packet_put_bignum2(BIGNUM * value)
635{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000636 buffer_put_bignum2(&active_state->outgoing_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +1000637}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638
Damien Miller6af914a2010-09-10 11:39:26 +1000639#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000640void
641packet_put_ecpoint(const EC_GROUP *curve, const EC_POINT *point)
642{
643 buffer_put_ecpoint(&active_state->outgoing_packet, curve, point);
644}
Damien Miller6af914a2010-09-10 11:39:26 +1000645#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000646
Damien Miller5428f641999-11-25 11:54:57 +1100647/*
648 * Finalizes and sends the packet. If the encryption key has been set,
649 * encrypts the packet before sending.
650 */
Damien Miller95def091999-11-25 00:26:21 +1100651
Ben Lindstrombba81212001-06-25 05:01:22 +0000652static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000653packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000655 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100656 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000657 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000658 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659
Damien Miller5428f641999-11-25 11:54:57 +1100660 /*
661 * If using packet compression, compress the payload of the outgoing
662 * packet.
663 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000664 if (active_state->packet_compression) {
665 buffer_clear(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100666 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000667 buffer_consume(&active_state->outgoing_packet, 8);
Damien Miller95def091999-11-25 00:26:21 +1100668 /* padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000669 buffer_append(&active_state->compression_buffer,
670 "\0\0\0\0\0\0\0\0", 8);
671 buffer_compress(&active_state->outgoing_packet,
672 &active_state->compression_buffer);
673 buffer_clear(&active_state->outgoing_packet);
674 buffer_append(&active_state->outgoing_packet,
675 buffer_ptr(&active_state->compression_buffer),
676 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100677 }
678 /* Compute packet length without padding (add checksum, remove padding). */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000679 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000680
Damien Millere247cc42000-05-07 12:03:14 +1000681 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100682 padding = 8 - len % 8;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000683 if (!active_state->send_context.plaintext) {
684 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller95def091999-11-25 00:26:21 +1100685 for (i = 0; i < padding; i++) {
686 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000687 rnd = arc4random();
688 cp[7 - i] = rnd & 0xff;
689 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100690 }
691 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000692 buffer_consume(&active_state->outgoing_packet, 8 - padding);
Damien Miller95def091999-11-25 00:26:21 +1100693
694 /* Add check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000695 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
696 buffer_len(&active_state->outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100697 put_u32(buf, checksum);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000698 buffer_append(&active_state->outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000699
700#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100701 fprintf(stderr, "packet_send plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000702 buffer_dump(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000703#endif
704
Damien Miller95def091999-11-25 00:26:21 +1100705 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100706 put_u32(buf, len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000707 buffer_append(&active_state->output, buf, 4);
708 cp = buffer_append_space(&active_state->output,
709 buffer_len(&active_state->outgoing_packet));
710 cipher_crypt(&active_state->send_context, cp,
711 buffer_ptr(&active_state->outgoing_packet),
Damien Miller1d75abf2013-01-09 16:12:19 +1100712 buffer_len(&active_state->outgoing_packet), 0, 0);
Damien Miller95def091999-11-25 00:26:21 +1100713
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100715 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000716 buffer_dump(&active_state->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +1000718 active_state->p_send.packets++;
719 active_state->p_send.bytes += len +
720 buffer_len(&active_state->outgoing_packet);
721 buffer_clear(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722
Damien Miller5428f641999-11-25 11:54:57 +1100723 /*
Damien Miller788f2122005-11-05 15:14:59 +1100724 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100725 * actually sent until packet_write_wait or packet_write_poll is
726 * called.
727 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728}
729
Ben Lindstromf6027d32002-03-22 01:42:04 +0000730void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000731set_newkeys(int mode)
732{
733 Enc *enc;
734 Mac *mac;
735 Comp *comp;
736 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000737 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000738 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000739
Ben Lindstrom064496f2002-12-23 02:04:22 +0000740 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000741
Damien Miller963f6b22002-02-19 15:21:23 +1100742 if (mode == MODE_OUT) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000743 cc = &active_state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000744 crypt_type = CIPHER_ENCRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000745 active_state->p_send.packets = active_state->p_send.blocks = 0;
746 max_blocks = &active_state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100747 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000748 cc = &active_state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000749 crypt_type = CIPHER_DECRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000750 active_state->p_read.packets = active_state->p_read.blocks = 0;
751 max_blocks = &active_state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100752 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000753 if (active_state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000754 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100755 cipher_cleanup(cc);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000756 enc = &active_state->newkeys[mode]->enc;
757 mac = &active_state->newkeys[mode]->mac;
758 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000759 mac_clear(mac);
Damien Miller1d75abf2013-01-09 16:12:19 +1100760 memset(enc->iv, 0, enc->iv_len);
Darren Tucker302889a2012-10-05 10:42:53 +1000761 memset(enc->key, 0, enc->key_len);
762 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000763 xfree(enc->name);
764 xfree(enc->iv);
765 xfree(enc->key);
766 xfree(mac->name);
767 xfree(mac->key);
768 xfree(comp->name);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000769 xfree(active_state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000770 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000771 active_state->newkeys[mode] = kex_get_newkeys(mode);
772 if (active_state->newkeys[mode] == NULL)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000773 fatal("newkeys: no keys for mode %d", mode);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000774 enc = &active_state->newkeys[mode]->enc;
775 mac = &active_state->newkeys[mode]->mac;
776 comp = &active_state->newkeys[mode]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100777 if (cipher_authlen(enc->cipher) == 0 && mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000778 mac->enabled = 1;
779 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100780 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Damien Miller1d75abf2013-01-09 16:12:19 +1100781 enc->iv, enc->iv_len, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000782 /* Deleting the keys does not gain extra security */
783 /* memset(enc->iv, 0, enc->block_size);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000784 memset(enc->key, 0, enc->key_len);
785 memset(mac->key, 0, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000786 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000787 (comp->type == COMP_DELAYED &&
788 active_state->after_authentication)) && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000789 packet_init_compression();
790 if (mode == MODE_OUT)
791 buffer_compress_init_send(6);
792 else
793 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000794 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000795 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000796 /*
797 * The 2^(blocksize*2) limit is too expensive for 3DES,
798 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
799 */
800 if (enc->block_size >= 16)
801 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
802 else
803 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000804 if (active_state->rekey_limit)
805 *max_blocks = MIN(*max_blocks,
806 active_state->rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000807}
808
Damien Miller5428f641999-11-25 11:54:57 +1100809/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000810 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000811 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000812 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
813 */
814static void
815packet_enable_delayed_compress(void)
816{
817 Comp *comp = NULL;
818 int mode;
819
820 /*
821 * Remember that we are past the authentication step, so rekeying
822 * with COMP_DELAYED will turn on compression immediately.
823 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000824 active_state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +1000825 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000826 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000827 if (active_state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +1000828 continue;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000829 comp = &active_state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +1000830 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000831 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000832 if (mode == MODE_OUT)
833 buffer_compress_init_send(6);
834 else
835 buffer_compress_init_recv();
836 comp->enabled = 1;
837 }
838 }
839}
840
841/*
Damien Miller33b13562000-04-04 14:38:59 +1000842 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
843 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000844static void
Damien Millera5539d22003-04-09 20:50:06 +1000845packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000846{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000847 u_char type, *cp, *macbuf = NULL;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100848 u_char padlen, pad = 0;
Damien Miller1d75abf2013-01-09 16:12:19 +1100849 u_int i, len, authlen = 0, aadlen = 0;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000850 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000851 Enc *enc = NULL;
852 Mac *mac = NULL;
853 Comp *comp = NULL;
854 int block_size;
855
Darren Tuckerf7288d72009-06-21 18:12:20 +1000856 if (active_state->newkeys[MODE_OUT] != NULL) {
857 enc = &active_state->newkeys[MODE_OUT]->enc;
858 mac = &active_state->newkeys[MODE_OUT]->mac;
859 comp = &active_state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100860 /* disable mac for authenticated encryption */
861 if ((authlen = cipher_authlen(enc->cipher)) != 0)
862 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +1000863 }
Damien Miller963f6b22002-02-19 15:21:23 +1100864 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +1100865 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +1000866
Darren Tuckerf7288d72009-06-21 18:12:20 +1000867 cp = buffer_ptr(&active_state->outgoing_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000868 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000869
870#ifdef PACKET_DEBUG
871 fprintf(stderr, "plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000872 buffer_dump(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000873#endif
874
875 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000876 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000877 /* skip header, compress only payload */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000878 buffer_consume(&active_state->outgoing_packet, 5);
879 buffer_clear(&active_state->compression_buffer);
880 buffer_compress(&active_state->outgoing_packet,
881 &active_state->compression_buffer);
882 buffer_clear(&active_state->outgoing_packet);
883 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
884 buffer_append(&active_state->outgoing_packet,
885 buffer_ptr(&active_state->compression_buffer),
886 buffer_len(&active_state->compression_buffer));
Damien Miller33b13562000-04-04 14:38:59 +1000887 DBG(debug("compression: raw %d compressed %d", len,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000888 buffer_len(&active_state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +1000889 }
890
891 /* sizeof (packet_len + pad_len + payload) */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000892 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000893
894 /*
895 * calc size of padding, alloc space, get random data,
896 * minimum padding is 4 bytes
897 */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100898 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +1000899 padlen = block_size - (len % block_size);
900 if (padlen < 4)
901 padlen += block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000902 if (active_state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +1100903 /* will wrap if extra_pad+padlen > 255 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000904 active_state->extra_pad =
905 roundup(active_state->extra_pad, block_size);
906 pad = active_state->extra_pad -
907 ((len + padlen) % active_state->extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000908 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000909 pad, len, padlen, active_state->extra_pad);
Damien Miller9f643902001-11-12 11:02:52 +1100910 padlen += pad;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000911 active_state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100912 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000913 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
914 if (enc && !active_state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000915 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000916 for (i = 0; i < padlen; i++) {
917 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000918 rnd = arc4random();
919 cp[i] = rnd & 0xff;
920 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000921 }
Damien Millere247cc42000-05-07 12:03:14 +1000922 } else {
923 /* clear padding */
924 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000925 }
Damien Milleraf43a7a2012-12-12 10:46:31 +1100926 /* sizeof (packet_len + pad_len + payload + padding) */
927 len = buffer_len(&active_state->outgoing_packet);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000928 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Milleraf43a7a2012-12-12 10:46:31 +1100929 /* packet_length includes payload, padding and padding length field */
930 put_u32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000931 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100932 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
933 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +1000934
935 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100936 if (mac && mac->enabled && !mac->etm) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000937 macbuf = mac_compute(mac, active_state->p_send.seqnr,
Damien Milleraf43a7a2012-12-12 10:46:31 +1100938 buffer_ptr(&active_state->outgoing_packet), len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000939 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000940 }
941 /* encrypt packet and append to output buffer. */
Damien Miller1d75abf2013-01-09 16:12:19 +1100942 cp = buffer_append_space(&active_state->output, len + authlen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000943 cipher_crypt(&active_state->send_context, cp,
944 buffer_ptr(&active_state->outgoing_packet),
Damien Miller1d75abf2013-01-09 16:12:19 +1100945 len - aadlen, aadlen, authlen);
Damien Miller33b13562000-04-04 14:38:59 +1000946 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100947 if (mac && mac->enabled) {
948 if (mac->etm) {
949 /* EtM: compute mac over aadlen + cipher text */
950 macbuf = mac_compute(mac,
951 active_state->p_send.seqnr, cp, len);
952 DBG(debug("done calc MAC(EtM) out #%d",
953 active_state->p_send.seqnr));
954 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000955 buffer_append(&active_state->output, macbuf, mac->mac_len);
Damien Milleraf43a7a2012-12-12 10:46:31 +1100956 }
Damien Miller33b13562000-04-04 14:38:59 +1000957#ifdef PACKET_DEBUG
958 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000959 buffer_dump(&active_state->output);
Damien Miller33b13562000-04-04 14:38:59 +1000960#endif
Damien Miller4af51302000-04-16 11:18:38 +1000961 /* increment sequence number for outgoing packets */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000962 if (++active_state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000963 logit("outgoing seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000964 if (++active_state->p_send.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +1000965 if (!(datafellows & SSH_BUG_NOREKEY))
966 fatal("XXX too many packets with same key");
Damien Milleraf43a7a2012-12-12 10:46:31 +1100967 active_state->p_send.blocks += len / block_size;
968 active_state->p_send.bytes += len;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000969 buffer_clear(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000970
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000971 if (type == SSH2_MSG_NEWKEYS)
972 set_newkeys(MODE_OUT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000973 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +1000974 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000975}
976
Damien Millera5539d22003-04-09 20:50:06 +1000977static void
978packet_send2(void)
979{
Damien Millera5539d22003-04-09 20:50:06 +1000980 struct packet *p;
981 u_char type, *cp;
982
Darren Tuckerf7288d72009-06-21 18:12:20 +1000983 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000984 type = cp[5];
985
986 /* during rekeying we can only send key exchange messages */
Darren Tucker7b935c72009-06-21 18:59:36 +1000987 if (active_state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +1100988 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
989 (type > SSH2_MSG_TRANSPORT_MAX) ||
990 (type == SSH2_MSG_SERVICE_REQUEST) ||
991 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +1000992 debug("enqueue packet: %u", type);
993 p = xmalloc(sizeof(*p));
994 p->type = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000995 memcpy(&p->payload, &active_state->outgoing_packet,
996 sizeof(Buffer));
997 buffer_init(&active_state->outgoing_packet);
998 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +1000999 return;
1000 }
1001 }
1002
1003 /* rekeying starts with sending KEXINIT */
1004 if (type == SSH2_MSG_KEXINIT)
Darren Tucker7b935c72009-06-21 18:59:36 +10001005 active_state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001006
1007 packet_send2_wrapped();
1008
1009 /* after a NEWKEYS message we can send the complete queue */
1010 if (type == SSH2_MSG_NEWKEYS) {
Darren Tucker7b935c72009-06-21 18:59:36 +10001011 active_state->rekeying = 0;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001012 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001013 type = p->type;
1014 debug("dequeue packet: %u", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001015 buffer_free(&active_state->outgoing_packet);
1016 memcpy(&active_state->outgoing_packet, &p->payload,
Damien Millera5539d22003-04-09 20:50:06 +10001017 sizeof(Buffer));
Darren Tuckerf7288d72009-06-21 18:12:20 +10001018 TAILQ_REMOVE(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +10001019 xfree(p);
1020 packet_send2_wrapped();
1021 }
1022 }
1023}
1024
Damien Miller33b13562000-04-04 14:38:59 +10001025void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001026packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +10001027{
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001028 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +10001029 packet_send2();
1030 else
1031 packet_send1();
1032 DBG(debug("packet_send done"));
1033}
1034
Damien Miller33b13562000-04-04 14:38:59 +10001035/*
Damien Miller5428f641999-11-25 11:54:57 +11001036 * Waits until a packet has been received, and returns its type. Note that
1037 * no other data is processed until this returns, so this function should not
1038 * be used during the interactive session.
1039 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040
1041int
Damien Millerdff50992002-01-22 23:16:32 +11001042packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043{
Darren Tuckerc5564e12009-06-21 18:53:53 +10001044 int type, len, ret, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001045 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001046 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001047 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048
Darren Tucker99bb7612008-06-13 22:02:50 +10001049 DBG(debug("packet_read()"));
1050
Darren Tuckerf7288d72009-06-21 18:12:20 +10001051 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1052 NFDBITS), sizeof(fd_mask));
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001053
Damien Miller95def091999-11-25 00:26:21 +11001054 /* Since we are blocking, ensure that all written packets have been sent. */
1055 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001056
Damien Miller95def091999-11-25 00:26:21 +11001057 /* Stay in the loop until we have received a complete packet. */
1058 for (;;) {
1059 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +11001060 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001061 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +10001062 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +11001063 || type == SSH_SMSG_FAILURE
1064 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +10001065 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +11001066 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001067 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001068 if (type != SSH_MSG_NONE) {
1069 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +11001070 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001071 }
Damien Miller5428f641999-11-25 11:54:57 +11001072 /*
1073 * Otherwise, wait for some data to arrive, add it to the
1074 * buffer, and try again.
1075 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001076 memset(setp, 0, howmany(active_state->connection_in + 1,
1077 NFDBITS) * sizeof(fd_mask));
1078 FD_SET(active_state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001079
Darren Tuckerf7288d72009-06-21 18:12:20 +10001080 if (active_state->packet_timeout_ms > 0) {
1081 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001082 timeoutp = &timeout;
1083 }
Damien Miller95def091999-11-25 00:26:21 +11001084 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001085 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001086 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001087 ms_to_timeval(&timeout, ms_remain);
1088 gettimeofday(&start, NULL);
1089 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001090 if ((ret = select(active_state->connection_in + 1, setp,
1091 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001092 break;
Damien Millerea437422009-10-02 11:49:03 +10001093 if (errno != EAGAIN && errno != EINTR &&
1094 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001095 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001096 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001097 continue;
1098 ms_subtract_diff(&start, &ms_remain);
1099 if (ms_remain <= 0) {
1100 ret = 0;
1101 break;
1102 }
1103 }
1104 if (ret == 0) {
1105 logit("Connection to %.200s timed out while "
1106 "waiting to read", get_remote_ipaddr());
1107 cleanup_exit(255);
1108 }
Damien Miller95def091999-11-25 00:26:21 +11001109 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001110 do {
1111 cont = 0;
1112 len = roaming_read(active_state->connection_in, buf,
1113 sizeof(buf), &cont);
1114 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001115 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001116 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +10001117 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001118 }
Damien Miller95def091999-11-25 00:26:21 +11001119 if (len < 0)
1120 fatal("Read from socket failed: %.100s", strerror(errno));
1121 /* Append it to the buffer. */
1122 packet_process_incoming(buf, len);
1123 }
1124 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001125}
1126
Damien Miller278f9072001-12-21 15:00:19 +11001127int
Damien Millerdff50992002-01-22 23:16:32 +11001128packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +11001129{
Damien Millerdff50992002-01-22 23:16:32 +11001130 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001131}
1132
Damien Miller5428f641999-11-25 11:54:57 +11001133/*
1134 * Waits until a packet has been received, verifies that its type matches
1135 * that given, and gives a fatal error and exits if there is a mismatch.
1136 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137
1138void
Damien Millerdff50992002-01-22 23:16:32 +11001139packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140{
Damien Miller95def091999-11-25 00:26:21 +11001141 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001142
Damien Millerdff50992002-01-22 23:16:32 +11001143 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001144 if (type != expected_type)
1145 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001146 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147}
1148
1149/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001150 * packet_process_incoming. If so, reads the packet; otherwise returns
1151 * SSH_MSG_NONE. This does not wait for data from the connection.
1152 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001153 * SSH_MSG_DISCONNECT is handled specially here. Also,
1154 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1155 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001156 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157
Ben Lindstrombba81212001-06-25 05:01:22 +00001158static int
Damien Millerdff50992002-01-22 23:16:32 +11001159packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001161 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001162 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001163 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164
Damien Miller95def091999-11-25 00:26:21 +11001165 /* Check if input size is less than minimum packet size. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001166 if (buffer_len(&active_state->input) < 4 + 8)
Damien Miller95def091999-11-25 00:26:21 +11001167 return SSH_MSG_NONE;
1168 /* Get length of incoming packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001169 cp = buffer_ptr(&active_state->input);
Damien Miller3f941882006-03-31 23:13:02 +11001170 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001171 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001172 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +11001173 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174
Damien Miller95def091999-11-25 00:26:21 +11001175 /* Check if the packet has been entirely received. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001176 if (buffer_len(&active_state->input) < 4 + padded_len)
Damien Miller95def091999-11-25 00:26:21 +11001177 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178
Damien Miller95def091999-11-25 00:26:21 +11001179 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001180
Damien Miller95def091999-11-25 00:26:21 +11001181 /* Consume packet length. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001182 buffer_consume(&active_state->input, 4);
Damien Miller95def091999-11-25 00:26:21 +11001183
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001184 /*
1185 * Cryptographic attack detector for ssh
1186 * (C)1998 CORE-SDI, Buenos Aires Argentina
1187 * Ariel Futoransky(futo@core-sdi.com)
1188 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001189 if (!active_state->receive_context.plaintext) {
1190 switch (detect_attack(buffer_ptr(&active_state->input),
1191 padded_len)) {
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001192 case DEATTACK_DETECTED:
1193 packet_disconnect("crc32 compensation attack: "
1194 "network attack detected");
1195 case DEATTACK_DOS_DETECTED:
1196 packet_disconnect("deattack denial of "
1197 "service detected");
1198 }
1199 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001200
1201 /* Decrypt data to incoming_packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001202 buffer_clear(&active_state->incoming_packet);
1203 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1204 cipher_crypt(&active_state->receive_context, cp,
Damien Miller1d75abf2013-01-09 16:12:19 +11001205 buffer_ptr(&active_state->input), padded_len, 0, 0);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001206
Darren Tuckerf7288d72009-06-21 18:12:20 +10001207 buffer_consume(&active_state->input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001208
1209#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001210 fprintf(stderr, "read_poll plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001211 buffer_dump(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213
Damien Miller95def091999-11-25 00:26:21 +11001214 /* Compute packet checksum. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001215 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1216 buffer_len(&active_state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001217
Damien Miller95def091999-11-25 00:26:21 +11001218 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001219 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001220
Damien Miller95def091999-11-25 00:26:21 +11001221 /* Test check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001222 if (len != buffer_len(&active_state->incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001223 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001224 len, buffer_len(&active_state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225
Darren Tuckerf7288d72009-06-21 18:12:20 +10001226 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001227 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001228 if (checksum != stored_checksum)
1229 packet_disconnect("Corrupted check bytes on input.");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001230 buffer_consume_end(&active_state->incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001231
Darren Tuckerf7288d72009-06-21 18:12:20 +10001232 if (active_state->packet_compression) {
1233 buffer_clear(&active_state->compression_buffer);
1234 buffer_uncompress(&active_state->incoming_packet,
1235 &active_state->compression_buffer);
1236 buffer_clear(&active_state->incoming_packet);
1237 buffer_append(&active_state->incoming_packet,
1238 buffer_ptr(&active_state->compression_buffer),
1239 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001240 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001241 active_state->p_read.packets++;
1242 active_state->p_read.bytes += padded_len + 4;
1243 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001244 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1245 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001246 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247}
Damien Miller95def091999-11-25 00:26:21 +11001248
Ben Lindstrombba81212001-06-25 05:01:22 +00001249static int
Damien Millerdff50992002-01-22 23:16:32 +11001250packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001251{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001252 u_int padlen, need;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001253 u_char *macbuf = NULL, *cp, type;
Damien Miller1d75abf2013-01-09 16:12:19 +11001254 u_int maclen, authlen = 0, aadlen = 0, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001255 Enc *enc = NULL;
1256 Mac *mac = NULL;
1257 Comp *comp = NULL;
1258
Darren Tuckerf7288d72009-06-21 18:12:20 +10001259 if (active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001260 return SSH_MSG_NONE;
1261
Darren Tuckerf7288d72009-06-21 18:12:20 +10001262 if (active_state->newkeys[MODE_IN] != NULL) {
1263 enc = &active_state->newkeys[MODE_IN]->enc;
1264 mac = &active_state->newkeys[MODE_IN]->mac;
1265 comp = &active_state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001266 /* disable mac for authenticated encryption */
1267 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1268 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001269 }
1270 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001271 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001272 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001273
Damien Milleraf43a7a2012-12-12 10:46:31 +11001274 if (aadlen && active_state->packlen == 0) {
1275 if (buffer_len(&active_state->input) < 4)
1276 return SSH_MSG_NONE;
1277 cp = buffer_ptr(&active_state->input);
1278 active_state->packlen = get_u32(cp);
1279 if (active_state->packlen < 1 + 4 ||
1280 active_state->packlen > PACKET_MAX_SIZE) {
1281#ifdef PACKET_DEBUG
1282 buffer_dump(&active_state->input);
1283#endif
1284 logit("Bad packet length %u.", active_state->packlen);
1285 packet_disconnect("Packet corrupt");
1286 }
Damien Miller8c05da32012-12-13 07:18:59 +11001287 buffer_clear(&active_state->incoming_packet);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001288 } else if (active_state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001289 /*
1290 * check if input size is less than the cipher block size,
1291 * decrypt first block and extract length of incoming packet
1292 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001293 if (buffer_len(&active_state->input) < block_size)
Damien Miller33b13562000-04-04 14:38:59 +10001294 return SSH_MSG_NONE;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001295 buffer_clear(&active_state->incoming_packet);
1296 cp = buffer_append_space(&active_state->incoming_packet,
Damien Miller33b13562000-04-04 14:38:59 +10001297 block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001298 cipher_crypt(&active_state->receive_context, cp,
Damien Miller1d75abf2013-01-09 16:12:19 +11001299 buffer_ptr(&active_state->input), block_size, 0, 0);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001300 cp = buffer_ptr(&active_state->incoming_packet);
1301 active_state->packlen = get_u32(cp);
1302 if (active_state->packlen < 1 + 4 ||
1303 active_state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001304#ifdef PACKET_DEBUG
Darren Tuckerf7288d72009-06-21 18:12:20 +10001305 buffer_dump(&active_state->incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001306#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001307 logit("Bad packet length %u.", active_state->packlen);
1308 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001309 PACKET_MAX_SIZE);
1310 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001311 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001312 buffer_consume(&active_state->input, block_size);
Damien Miller33b13562000-04-04 14:38:59 +10001313 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001314 DBG(debug("input: packet len %u", active_state->packlen+4));
1315 if (aadlen) {
1316 /* only the payload is encrypted */
1317 need = active_state->packlen;
1318 } else {
1319 /*
1320 * the payload size and the payload are encrypted, but we
1321 * have a partial packet of block_size bytes
1322 */
1323 need = 4 + active_state->packlen - block_size;
1324 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001325 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1326 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001327 if (need % block_size != 0) {
1328 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001329 need, block_size, need % block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001330 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001331 PACKET_MAX_SIZE - block_size);
1332 return SSH_MSG_NONE;
Darren Tucker99d11a32008-12-01 21:40:48 +11001333 }
Damien Miller33b13562000-04-04 14:38:59 +10001334 /*
1335 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001336 * decrypt into incoming_packet:
1337 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001338 * 'need' bytes are encrypted, followed by either
1339 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001340 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001341 */
Damien Miller1d75abf2013-01-09 16:12:19 +11001342 if (buffer_len(&active_state->input) < aadlen + need + authlen + maclen)
Damien Miller33b13562000-04-04 14:38:59 +10001343 return SSH_MSG_NONE;
1344#ifdef PACKET_DEBUG
1345 fprintf(stderr, "read_poll enc/full: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001346 buffer_dump(&active_state->input);
Damien Miller33b13562000-04-04 14:38:59 +10001347#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001348 /* EtM: compute mac over encrypted input */
1349 if (mac && mac->enabled && mac->etm)
1350 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1351 buffer_ptr(&active_state->input), aadlen + need);
1352 cp = buffer_append_space(&active_state->incoming_packet, aadlen + need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001353 cipher_crypt(&active_state->receive_context, cp,
Damien Miller1d75abf2013-01-09 16:12:19 +11001354 buffer_ptr(&active_state->input), need, aadlen, authlen);
1355 buffer_consume(&active_state->input, aadlen + need + authlen);
Damien Miller33b13562000-04-04 14:38:59 +10001356 /*
1357 * compute MAC over seqnr and packet,
1358 * increment sequence number for incoming packet
1359 */
Damien Miller4af51302000-04-16 11:18:38 +10001360 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001361 if (!mac->etm)
1362 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1363 buffer_ptr(&active_state->incoming_packet),
1364 buffer_len(&active_state->incoming_packet));
Damien Millerea1651c2010-07-16 13:58:37 +10001365 if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001366 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001367 logit("Corrupted MAC on input.");
1368 if (need > PACKET_MAX_SIZE)
1369 fatal("internal error need %d", need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001370 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001371 PACKET_MAX_SIZE - need);
1372 return SSH_MSG_NONE;
1373 }
1374
Darren Tuckerf7288d72009-06-21 18:12:20 +10001375 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1376 buffer_consume(&active_state->input, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +10001377 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001378 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001379 if (seqnr_p != NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001380 *seqnr_p = active_state->p_read.seqnr;
1381 if (++active_state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001382 logit("incoming seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001383 if (++active_state->p_read.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +10001384 if (!(datafellows & SSH_BUG_NOREKEY))
1385 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001386 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1387 active_state->p_read.bytes += active_state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001388
1389 /* get padlen */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001390 cp = buffer_ptr(&active_state->incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001391 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001392 DBG(debug("input: padlen %d", padlen));
1393 if (padlen < 4)
1394 packet_disconnect("Corrupted padlen %d on input.", padlen);
1395
1396 /* skip packet size + padlen, discard padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001397 buffer_consume(&active_state->incoming_packet, 4 + 1);
1398 buffer_consume_end(&active_state->incoming_packet, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001399
Darren Tuckerf7288d72009-06-21 18:12:20 +10001400 DBG(debug("input: len before de-compress %d",
1401 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001402 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001403 buffer_clear(&active_state->compression_buffer);
1404 buffer_uncompress(&active_state->incoming_packet,
1405 &active_state->compression_buffer);
1406 buffer_clear(&active_state->incoming_packet);
1407 buffer_append(&active_state->incoming_packet,
1408 buffer_ptr(&active_state->compression_buffer),
1409 buffer_len(&active_state->compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001410 DBG(debug("input: len after de-compress %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001411 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001412 }
1413 /*
1414 * get packet type, implies consume.
1415 * return length of payload (without type field)
1416 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001417 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001418 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1419 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001420 if (type == SSH2_MSG_NEWKEYS)
1421 set_newkeys(MODE_IN);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001422 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1423 !active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +10001424 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001425#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001426 fprintf(stderr, "read/plain[%d]:\r\n", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001427 buffer_dump(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001428#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001429 /* reset for next packet */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001430 active_state->packlen = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001431 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001432}
1433
1434int
Damien Millerdff50992002-01-22 23:16:32 +11001435packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001436{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001437 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001438 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001439 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001440
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001441 for (;;) {
1442 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001443 type = packet_read_poll2(seqnr_p);
Darren Tucker136e56f2008-06-08 12:49:30 +10001444 if (type) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001445 active_state->keep_alive_timeouts = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001446 DBG(debug("received packet type %d", type));
Darren Tucker136e56f2008-06-08 12:49:30 +10001447 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001448 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001449 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001450 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001451 break;
Damien Miller33b13562000-04-04 14:38:59 +10001452 case SSH2_MSG_DEBUG:
1453 packet_get_char();
1454 msg = packet_get_string(NULL);
1455 debug("Remote: %.900s", msg);
1456 xfree(msg);
1457 msg = packet_get_string(NULL);
1458 xfree(msg);
1459 break;
1460 case SSH2_MSG_DISCONNECT:
1461 reason = packet_get_int();
1462 msg = packet_get_string(NULL);
Damien Miller894926e2013-02-12 11:03:58 +11001463 error("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001464 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001465 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001466 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001467 break;
Damien Miller659811f2002-01-22 23:23:11 +11001468 case SSH2_MSG_UNIMPLEMENTED:
1469 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001470 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1471 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001472 break;
Damien Miller33b13562000-04-04 14:38:59 +10001473 default:
1474 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001475 }
Damien Miller33b13562000-04-04 14:38:59 +10001476 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001477 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001478 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001479 case SSH_MSG_IGNORE:
1480 break;
1481 case SSH_MSG_DEBUG:
1482 msg = packet_get_string(NULL);
1483 debug("Remote: %.900s", msg);
1484 xfree(msg);
1485 break;
1486 case SSH_MSG_DISCONNECT:
1487 msg = packet_get_string(NULL);
Damien Miller894926e2013-02-12 11:03:58 +11001488 error("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001489 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001490 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001491 break;
1492 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001493 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001494 DBG(debug("received packet type %d", type));
1495 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001496 }
Damien Miller33b13562000-04-04 14:38:59 +10001497 }
1498 }
1499}
1500
Damien Miller5428f641999-11-25 11:54:57 +11001501/*
1502 * Buffers the given amount of input characters. This is intended to be used
1503 * together with packet_read_poll.
1504 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001505
1506void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001507packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001508{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001509 if (active_state->packet_discard) {
1510 active_state->keep_alive_timeouts = 0; /* ?? */
1511 if (len >= active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001512 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001513 active_state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001514 return;
1515 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001516 buffer_append(&active_state->input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517}
1518
1519/* Returns a character from the packet. */
1520
Ben Lindstrom46c16222000-12-22 01:43:59 +00001521u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001522packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001523{
Damien Miller95def091999-11-25 00:26:21 +11001524 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001525
Darren Tuckerf7288d72009-06-21 18:12:20 +10001526 buffer_get(&active_state->incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001527 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001528}
1529
1530/* Returns an integer from the packet data. */
1531
Ben Lindstrom46c16222000-12-22 01:43:59 +00001532u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001533packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001534{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001535 return buffer_get_int(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001536}
1537
Darren Tucker761c3892009-06-21 18:16:26 +10001538/* Returns an 64 bit integer from the packet data. */
1539
1540u_int64_t
1541packet_get_int64(void)
1542{
1543 return buffer_get_int64(&active_state->incoming_packet);
1544}
1545
Damien Miller5428f641999-11-25 11:54:57 +11001546/*
1547 * Returns an arbitrary precision integer from the packet data. The integer
1548 * must have been initialized before this call.
1549 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001550
1551void
Damien Millerd432ccf2002-01-22 23:14:44 +11001552packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001553{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001554 buffer_get_bignum(&active_state->incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001555}
1556
Damien Miller33b13562000-04-04 14:38:59 +10001557void
Damien Millerd432ccf2002-01-22 23:14:44 +11001558packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001559{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001560 buffer_get_bignum2(&active_state->incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001561}
1562
Damien Miller6af914a2010-09-10 11:39:26 +10001563#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001564void
1565packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point)
1566{
1567 buffer_get_ecpoint(&active_state->incoming_packet, curve, point);
1568}
Damien Miller6af914a2010-09-10 11:39:26 +10001569#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001570
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001571void *
Damien Millereccb9de2005-06-17 12:59:34 +10001572packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001573{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001574 u_int bytes = buffer_len(&active_state->incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001575
Damien Miller33b13562000-04-04 14:38:59 +10001576 if (length_ptr != NULL)
1577 *length_ptr = bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001578 return buffer_ptr(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001579}
1580
Damien Miller4af51302000-04-16 11:18:38 +10001581int
1582packet_remaining(void)
1583{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001584 return buffer_len(&active_state->incoming_packet);
Damien Miller4af51302000-04-16 11:18:38 +10001585}
1586
Damien Miller5428f641999-11-25 11:54:57 +11001587/*
1588 * Returns a string from the packet data. The string is allocated using
1589 * xmalloc; it is the responsibility of the calling program to free it when
1590 * no longer needed. The length_ptr argument may be NULL, or point to an
1591 * integer into which the length of the string is stored.
1592 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001593
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001594void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001595packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001596{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001597 return buffer_get_string(&active_state->incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001598}
1599
Damien Millerdb255ca2008-05-19 14:59:37 +10001600void *
1601packet_get_string_ptr(u_int *length_ptr)
1602{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001603 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
Damien Millerdb255ca2008-05-19 14:59:37 +10001604}
1605
Damien Millerda108ec2010-08-31 22:36:39 +10001606/* Ensures the returned string has no embedded \0 characters in it. */
1607char *
1608packet_get_cstring(u_int *length_ptr)
1609{
1610 return buffer_get_cstring(&active_state->incoming_packet, length_ptr);
1611}
1612
Damien Miller5428f641999-11-25 11:54:57 +11001613/*
1614 * Sends a diagnostic message from the server to the client. This message
1615 * can be sent at any time (but not while constructing another message). The
1616 * message is printed immediately, but only if the client is being executed
1617 * in verbose mode. These messages are primarily intended to ease debugging
1618 * authentication problems. The length of the formatted message must not
1619 * exceed 1024 bytes. This will automatically call packet_write_wait.
1620 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621
1622void
Damien Miller95def091999-11-25 00:26:21 +11001623packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001624{
Damien Miller95def091999-11-25 00:26:21 +11001625 char buf[1024];
1626 va_list args;
1627
Ben Lindstroma14ee472000-12-07 01:24:58 +00001628 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1629 return;
1630
Damien Miller95def091999-11-25 00:26:21 +11001631 va_start(args, fmt);
1632 vsnprintf(buf, sizeof(buf), fmt, args);
1633 va_end(args);
1634
Damien Miller7c8af4f2000-05-01 08:24:07 +10001635 if (compat20) {
1636 packet_start(SSH2_MSG_DEBUG);
1637 packet_put_char(0); /* bool: always display */
1638 packet_put_cstring(buf);
1639 packet_put_cstring("");
1640 } else {
1641 packet_start(SSH_MSG_DEBUG);
1642 packet_put_cstring(buf);
1643 }
Damien Miller95def091999-11-25 00:26:21 +11001644 packet_send();
1645 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001646}
1647
Damien Miller5428f641999-11-25 11:54:57 +11001648/*
1649 * Logs the error plus constructs and sends a disconnect packet, closes the
1650 * connection, and exits. This function never returns. The error message
1651 * should not contain a newline. The length of the formatted message must
1652 * not exceed 1024 bytes.
1653 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001654
1655void
Damien Miller95def091999-11-25 00:26:21 +11001656packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001657{
Damien Miller95def091999-11-25 00:26:21 +11001658 char buf[1024];
1659 va_list args;
1660 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001661
Damien Miller95def091999-11-25 00:26:21 +11001662 if (disconnecting) /* Guard against recursive invocations. */
1663 fatal("packet_disconnect called recursively.");
1664 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001665
Damien Miller5428f641999-11-25 11:54:57 +11001666 /*
1667 * Format the message. Note that the caller must make sure the
1668 * message is of limited size.
1669 */
Damien Miller95def091999-11-25 00:26:21 +11001670 va_start(args, fmt);
1671 vsnprintf(buf, sizeof(buf), fmt, args);
1672 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001673
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001674 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001675 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001676
Damien Miller95def091999-11-25 00:26:21 +11001677 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001678 if (compat20) {
1679 packet_start(SSH2_MSG_DISCONNECT);
1680 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1681 packet_put_cstring(buf);
1682 packet_put_cstring("");
1683 } else {
1684 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001685 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001686 }
Damien Miller95def091999-11-25 00:26:21 +11001687 packet_send();
1688 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001689
Damien Miller95def091999-11-25 00:26:21 +11001690 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001691 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001692
Damien Miller95def091999-11-25 00:26:21 +11001693 /* Close the connection. */
1694 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001695 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001696}
1697
Damien Miller5428f641999-11-25 11:54:57 +11001698/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001699
1700void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001701packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001702{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001703 int len = buffer_len(&active_state->output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001704 int cont;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001705
Damien Miller95def091999-11-25 00:26:21 +11001706 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001707 cont = 0;
1708 len = roaming_write(active_state->connection_out,
1709 buffer_ptr(&active_state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001710 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001711 if (errno == EINTR || errno == EAGAIN ||
1712 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001713 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001714 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001715 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001716 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001717 fatal("Write connection closed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001718 buffer_consume(&active_state->output, len);
Damien Miller95def091999-11-25 00:26:21 +11001719 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001720}
1721
Damien Miller5428f641999-11-25 11:54:57 +11001722/*
1723 * Calls packet_write_poll repeatedly until all pending output data has been
1724 * written.
1725 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001726
1727void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001728packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001729{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001730 fd_set *setp;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001731 int ret, ms_remain;
1732 struct timeval start, timeout, *timeoutp = NULL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001733
Darren Tuckerf7288d72009-06-21 18:12:20 +10001734 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1735 NFDBITS), sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001736 packet_write_poll();
1737 while (packet_have_data_to_write()) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001738 memset(setp, 0, howmany(active_state->connection_out + 1,
1739 NFDBITS) * sizeof(fd_mask));
1740 FD_SET(active_state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001741
Darren Tuckerf7288d72009-06-21 18:12:20 +10001742 if (active_state->packet_timeout_ms > 0) {
1743 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001744 timeoutp = &timeout;
1745 }
1746 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001747 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001748 ms_to_timeval(&timeout, ms_remain);
1749 gettimeofday(&start, NULL);
1750 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001751 if ((ret = select(active_state->connection_out + 1,
1752 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001753 break;
Damien Millerea437422009-10-02 11:49:03 +10001754 if (errno != EAGAIN && errno != EINTR &&
1755 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001756 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001757 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001758 continue;
1759 ms_subtract_diff(&start, &ms_remain);
1760 if (ms_remain <= 0) {
1761 ret = 0;
1762 break;
1763 }
1764 }
1765 if (ret == 0) {
1766 logit("Connection to %.200s timed out while "
1767 "waiting to write", get_remote_ipaddr());
1768 cleanup_exit(255);
1769 }
Damien Miller95def091999-11-25 00:26:21 +11001770 packet_write_poll();
1771 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001772 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001773}
1774
1775/* Returns true if there is buffered data to write to the connection. */
1776
1777int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001778packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001779{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001780 return buffer_len(&active_state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001781}
1782
1783/* Returns true if there is not too much data to write to the connection. */
1784
1785int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001786packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001787{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001788 if (active_state->interactive_mode)
1789 return buffer_len(&active_state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11001790 else
Darren Tuckerf7288d72009-06-21 18:12:20 +10001791 return buffer_len(&active_state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001792}
1793
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001794static void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001795packet_set_tos(int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00001796{
Damien Millerd2ac5d72011-05-15 08:43:13 +10001797#ifndef IP_TOS_IS_BROKEN
1798 if (!packet_connection_is_on_socket())
Ben Lindstroma7433982002-12-23 02:41:41 +00001799 return;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001800 switch (packet_connection_af()) {
1801# ifdef IP_TOS
1802 case AF_INET:
1803 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
1804 if (setsockopt(active_state->connection_in,
1805 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
1806 error("setsockopt IP_TOS %d: %.100s:",
1807 tos, strerror(errno));
1808 break;
1809# endif /* IP_TOS */
1810# ifdef IPV6_TCLASS
1811 case AF_INET6:
1812 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
1813 if (setsockopt(active_state->connection_in,
1814 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
1815 error("setsockopt IPV6_TCLASS %d: %.100s:",
1816 tos, strerror(errno));
1817 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001818# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10001819 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10001820#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11001821}
Ben Lindstroma7433982002-12-23 02:41:41 +00001822
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001823/* Informs that the current session is interactive. Sets IP flags for that. */
1824
1825void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001826packet_set_interactive(int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001827{
Darren Tucker7b935c72009-06-21 18:59:36 +10001828 if (active_state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001829 return;
Darren Tucker7b935c72009-06-21 18:59:36 +10001830 active_state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001831
Damien Miller95def091999-11-25 00:26:21 +11001832 /* Record that we are in interactive mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001833 active_state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001834
Damien Miller34132e52000-01-14 15:45:46 +11001835 /* Only set socket options if using a socket. */
1836 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001837 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001838 set_nodelay(active_state->connection_in);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001839 packet_set_tos(interactive ? qos_interactive : qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001840}
1841
1842/* Returns true if the current connection is interactive. */
1843
1844int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001845packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001846{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001847 return active_state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001848}
Damien Miller6162d121999-11-21 13:23:52 +11001849
Darren Tucker1f8311c2004-05-13 16:39:33 +10001850int
Darren Tucker502d3842003-06-28 12:38:01 +10001851packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001852{
Darren Tucker7b935c72009-06-21 18:59:36 +10001853 if (active_state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10001854 logit("packet_set_maxsize: called twice: old %d new %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001855 active_state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001856 return -1;
1857 }
1858 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001859 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001860 return -1;
1861 }
Darren Tucker7b935c72009-06-21 18:59:36 +10001862 active_state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001863 debug("packet_set_maxsize: setting to %d", s);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001864 active_state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11001865 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001866}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001867
Darren Tuckerf7288d72009-06-21 18:12:20 +10001868int
1869packet_inc_alive_timeouts(void)
1870{
1871 return ++active_state->keep_alive_timeouts;
1872}
1873
1874void
1875packet_set_alive_timeouts(int ka)
1876{
1877 active_state->keep_alive_timeouts = ka;
1878}
1879
1880u_int
1881packet_get_maxsize(void)
1882{
1883 return active_state->max_packet_size;
1884}
1885
Damien Miller9f643902001-11-12 11:02:52 +11001886/* roundup current message to pad bytes */
1887void
1888packet_add_padding(u_char pad)
1889{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001890 active_state->extra_pad = pad;
Damien Miller9f643902001-11-12 11:02:52 +11001891}
1892
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001893/*
1894 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001895 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001896 * byte SSH_MSG_IGNORE
1897 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001898 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001899 * All implementations MUST understand (and ignore) this message at any
1900 * time (after receiving the protocol version). No implementation is
1901 * required to send them. This message can be used as an additional
1902 * protection measure against advanced traffic analysis techniques.
1903 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001904void
1905packet_send_ignore(int nbytes)
1906{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001907 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001908 int i;
1909
1910 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001911 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001912 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001913 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001914 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001915 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001916 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001917 }
1918}
Damien Millera5539d22003-04-09 20:50:06 +10001919
Darren Tucker1f8311c2004-05-13 16:39:33 +10001920#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001921int
1922packet_need_rekeying(void)
1923{
1924 if (datafellows & SSH_BUG_NOREKEY)
1925 return 0;
1926 return
Darren Tuckerf7288d72009-06-21 18:12:20 +10001927 (active_state->p_send.packets > MAX_PACKETS) ||
1928 (active_state->p_read.packets > MAX_PACKETS) ||
1929 (active_state->max_blocks_out &&
1930 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1931 (active_state->max_blocks_in &&
1932 (active_state->p_read.blocks > active_state->max_blocks_in));
Damien Millera5539d22003-04-09 20:50:06 +10001933}
1934
1935void
1936packet_set_rekey_limit(u_int32_t bytes)
1937{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001938 active_state->rekey_limit = bytes;
Damien Millera5539d22003-04-09 20:50:06 +10001939}
Damien Miller9786e6e2005-07-26 21:54:56 +10001940
1941void
1942packet_set_server(void)
1943{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001944 active_state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001945}
1946
1947void
1948packet_set_authenticated(void)
1949{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001950 active_state->after_authentication = 1;
1951}
1952
1953void *
1954packet_get_input(void)
1955{
1956 return (void *)&active_state->input;
1957}
1958
1959void *
1960packet_get_output(void)
1961{
1962 return (void *)&active_state->output;
1963}
1964
1965void *
1966packet_get_newkeys(int mode)
1967{
1968 return (void *)active_state->newkeys[mode];
Damien Miller9786e6e2005-07-26 21:54:56 +10001969}
Darren Tuckere841eb02009-07-06 07:11:13 +10001970
1971/*
1972 * Save the state for the real connection, and use a separate state when
1973 * resuming a suspended connection.
1974 */
1975void
1976packet_backup_state(void)
1977{
1978 struct session_state *tmp;
1979
1980 close(active_state->connection_in);
1981 active_state->connection_in = -1;
1982 close(active_state->connection_out);
1983 active_state->connection_out = -1;
1984 if (backup_state)
1985 tmp = backup_state;
1986 else
1987 tmp = alloc_session_state();
1988 backup_state = active_state;
1989 active_state = tmp;
1990}
1991
1992/*
1993 * Swap in the old state when resuming a connecion.
1994 */
1995void
1996packet_restore_state(void)
1997{
1998 struct session_state *tmp;
1999 void *buf;
2000 u_int len;
2001
2002 tmp = backup_state;
2003 backup_state = active_state;
2004 active_state = tmp;
2005 active_state->connection_in = backup_state->connection_in;
2006 backup_state->connection_in = -1;
2007 active_state->connection_out = backup_state->connection_out;
2008 backup_state->connection_out = -1;
2009 len = buffer_len(&backup_state->input);
2010 if (len > 0) {
2011 buf = buffer_ptr(&backup_state->input);
2012 buffer_append(&active_state->input, buf, len);
2013 buffer_clear(&backup_state->input);
2014 add_recv_bytes(len);
2015 }
2016}