blob: 5e82fe7537c1587a4f900d447d0e2277ad4dd0f0 [file] [log] [blame]
Damien Miller8ed4de82011-12-19 10:52:50 +11001/* $OpenBSD: packet.c,v 1.174 2011/12/07 05:44:38 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 Miller61433be2009-02-14 16:35:01 +1100278 if (enc == NULL || !cipher_is_cbc(enc->cipher))
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 if (to.ss_family == AF_INET)
436 return 1;
437#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100438 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000439 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000440 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000441#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000442 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100443}
444
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445/* Sets the connection into non-blocking mode. */
446
447void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000448packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449{
Damien Miller95def091999-11-25 00:26:21 +1100450 /* Set the socket into non-blocking mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000451 set_nonblock(active_state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452
Darren Tuckerf7288d72009-06-21 18:12:20 +1000453 if (active_state->connection_out != active_state->connection_in)
454 set_nonblock(active_state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455}
456
457/* Returns the socket used for reading. */
458
459int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000460packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000462 return active_state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463}
464
465/* Returns the descriptor used for writing. */
466
467int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000468packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000470 return active_state->connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471}
472
473/* Closes the connection and clears and frees internal data structures. */
474
475void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000476packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000478 if (!active_state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100479 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000480 active_state->initialized = 0;
481 if (active_state->connection_in == active_state->connection_out) {
482 shutdown(active_state->connection_out, SHUT_RDWR);
483 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100484 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000485 close(active_state->connection_in);
486 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100487 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000488 buffer_free(&active_state->input);
489 buffer_free(&active_state->output);
490 buffer_free(&active_state->outgoing_packet);
491 buffer_free(&active_state->incoming_packet);
492 if (active_state->compression_buffer_ready) {
493 buffer_free(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100494 buffer_compress_uninit();
495 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000496 cipher_cleanup(&active_state->send_context);
497 cipher_cleanup(&active_state->receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498}
499
500/* Sets remote side protocol flags. */
501
502void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000503packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000505 active_state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506}
507
508/* Returns the remote protocol flags set earlier by the above function. */
509
Ben Lindstrom46c16222000-12-22 01:43:59 +0000510u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000511packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000513 return active_state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514}
515
Damien Miller5428f641999-11-25 11:54:57 +1100516/*
517 * Starts packet compression from the next packet on in both directions.
518 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
519 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520
Ben Lindstrombba81212001-06-25 05:01:22 +0000521static void
522packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000523{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000524 if (active_state->compression_buffer_ready == 1)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000525 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000526 active_state->compression_buffer_ready = 1;
527 buffer_init(&active_state->compression_buffer);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000528}
529
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530void
531packet_start_compression(int level)
532{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000533 if (active_state->packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100534 fatal("Compression already enabled.");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000535 active_state->packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000536 packet_init_compression();
537 buffer_compress_init_send(level);
538 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539}
540
Damien Miller5428f641999-11-25 11:54:57 +1100541/*
Damien Miller5428f641999-11-25 11:54:57 +1100542 * Causes any further packets to be encrypted using the given key. The same
543 * key is used for both sending and reception. However, both directions are
544 * encrypted independently of each other.
545 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000546
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547void
Damien Miller7a221a12010-11-20 15:14:29 +1100548packet_set_encryption_key(const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549{
Damien Miller874d77b2000-10-14 16:23:11 +1100550 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000551
Damien Miller874d77b2000-10-14 16:23:11 +1100552 if (cipher == NULL)
553 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000554 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100555 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000556 if (keylen > SSH_SESSION_KEY_LENGTH)
557 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000558 memcpy(active_state->ssh1_key, key, keylen);
559 active_state->ssh1_keylen = keylen;
560 cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
561 0, CIPHER_ENCRYPT);
562 cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
563 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564}
565
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000566u_int
567packet_get_encryption_key(u_char *key)
568{
569 if (key == NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000570 return (active_state->ssh1_keylen);
571 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
572 return (active_state->ssh1_keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000573}
574
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000575/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000577packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000579 u_char buf[9];
580 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581
Ben Lindstrom204e4882001-03-05 06:47:00 +0000582 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000583 len = compat20 ? 6 : 9;
584 memset(buf, 0, len - 1);
585 buf[len - 1] = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000586 buffer_clear(&active_state->outgoing_packet);
587 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000588}
589
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000590/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591void
592packet_put_char(int value)
593{
Damien Miller95def091999-11-25 00:26:21 +1100594 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000595
Darren Tuckerf7288d72009-06-21 18:12:20 +1000596 buffer_append(&active_state->outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597}
Damien Miller4f7becb2006-03-26 14:10:14 +1100598
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000600packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000601{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000602 buffer_put_int(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603}
Damien Miller4f7becb2006-03-26 14:10:14 +1100604
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605void
Darren Tucker761c3892009-06-21 18:16:26 +1000606packet_put_int64(u_int64_t value)
607{
608 buffer_put_int64(&active_state->outgoing_packet, value);
609}
610
611void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100612packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000614 buffer_put_string(&active_state->outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615}
Damien Miller4f7becb2006-03-26 14:10:14 +1100616
Damien Miller33b13562000-04-04 14:38:59 +1000617void
618packet_put_cstring(const char *str)
619{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000620 buffer_put_cstring(&active_state->outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000621}
Damien Miller4f7becb2006-03-26 14:10:14 +1100622
Damien Miller33b13562000-04-04 14:38:59 +1000623void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100624packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000625{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000626 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000627}
Damien Miller4f7becb2006-03-26 14:10:14 +1100628
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629void
Damien Miller95def091999-11-25 00:26:21 +1100630packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000632 buffer_put_bignum(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633}
Damien Miller4f7becb2006-03-26 14:10:14 +1100634
Damien Miller33b13562000-04-04 14:38:59 +1000635void
636packet_put_bignum2(BIGNUM * value)
637{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000638 buffer_put_bignum2(&active_state->outgoing_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +1000639}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640
Damien Miller6af914a2010-09-10 11:39:26 +1000641#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000642void
643packet_put_ecpoint(const EC_GROUP *curve, const EC_POINT *point)
644{
645 buffer_put_ecpoint(&active_state->outgoing_packet, curve, point);
646}
Damien Miller6af914a2010-09-10 11:39:26 +1000647#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000648
Damien Miller5428f641999-11-25 11:54:57 +1100649/*
650 * Finalizes and sends the packet. If the encryption key has been set,
651 * encrypts the packet before sending.
652 */
Damien Miller95def091999-11-25 00:26:21 +1100653
Ben Lindstrombba81212001-06-25 05:01:22 +0000654static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000655packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000657 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100658 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000659 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000660 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661
Damien Miller5428f641999-11-25 11:54:57 +1100662 /*
663 * If using packet compression, compress the payload of the outgoing
664 * packet.
665 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000666 if (active_state->packet_compression) {
667 buffer_clear(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100668 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000669 buffer_consume(&active_state->outgoing_packet, 8);
Damien Miller95def091999-11-25 00:26:21 +1100670 /* padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000671 buffer_append(&active_state->compression_buffer,
672 "\0\0\0\0\0\0\0\0", 8);
673 buffer_compress(&active_state->outgoing_packet,
674 &active_state->compression_buffer);
675 buffer_clear(&active_state->outgoing_packet);
676 buffer_append(&active_state->outgoing_packet,
677 buffer_ptr(&active_state->compression_buffer),
678 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100679 }
680 /* Compute packet length without padding (add checksum, remove padding). */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000681 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682
Damien Millere247cc42000-05-07 12:03:14 +1000683 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100684 padding = 8 - len % 8;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000685 if (!active_state->send_context.plaintext) {
686 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller95def091999-11-25 00:26:21 +1100687 for (i = 0; i < padding; i++) {
688 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000689 rnd = arc4random();
690 cp[7 - i] = rnd & 0xff;
691 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100692 }
693 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000694 buffer_consume(&active_state->outgoing_packet, 8 - padding);
Damien Miller95def091999-11-25 00:26:21 +1100695
696 /* Add check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000697 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
698 buffer_len(&active_state->outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100699 put_u32(buf, checksum);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000700 buffer_append(&active_state->outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701
702#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100703 fprintf(stderr, "packet_send plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000704 buffer_dump(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705#endif
706
Damien Miller95def091999-11-25 00:26:21 +1100707 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100708 put_u32(buf, len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000709 buffer_append(&active_state->output, buf, 4);
710 cp = buffer_append_space(&active_state->output,
711 buffer_len(&active_state->outgoing_packet));
712 cipher_crypt(&active_state->send_context, cp,
713 buffer_ptr(&active_state->outgoing_packet),
714 buffer_len(&active_state->outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100715
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100717 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000718 buffer_dump(&active_state->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +1000720 active_state->p_send.packets++;
721 active_state->p_send.bytes += len +
722 buffer_len(&active_state->outgoing_packet);
723 buffer_clear(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724
Damien Miller5428f641999-11-25 11:54:57 +1100725 /*
Damien Miller788f2122005-11-05 15:14:59 +1100726 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100727 * actually sent until packet_write_wait or packet_write_poll is
728 * called.
729 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000730}
731
Ben Lindstromf6027d32002-03-22 01:42:04 +0000732void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000733set_newkeys(int mode)
734{
735 Enc *enc;
736 Mac *mac;
737 Comp *comp;
738 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000739 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000740 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000741
Ben Lindstrom064496f2002-12-23 02:04:22 +0000742 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000743
Damien Miller963f6b22002-02-19 15:21:23 +1100744 if (mode == MODE_OUT) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000745 cc = &active_state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000746 crypt_type = CIPHER_ENCRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000747 active_state->p_send.packets = active_state->p_send.blocks = 0;
748 max_blocks = &active_state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100749 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000750 cc = &active_state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000751 crypt_type = CIPHER_DECRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000752 active_state->p_read.packets = active_state->p_read.blocks = 0;
753 max_blocks = &active_state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100754 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000755 if (active_state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000756 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100757 cipher_cleanup(cc);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000758 enc = &active_state->newkeys[mode]->enc;
759 mac = &active_state->newkeys[mode]->mac;
760 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000761 mac_clear(mac);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000762 xfree(enc->name);
763 xfree(enc->iv);
764 xfree(enc->key);
765 xfree(mac->name);
766 xfree(mac->key);
767 xfree(comp->name);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000768 xfree(active_state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000769 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000770 active_state->newkeys[mode] = kex_get_newkeys(mode);
771 if (active_state->newkeys[mode] == NULL)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000772 fatal("newkeys: no keys for mode %d", mode);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000773 enc = &active_state->newkeys[mode]->enc;
774 mac = &active_state->newkeys[mode]->mac;
775 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000776 if (mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000777 mac->enabled = 1;
778 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100779 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000780 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000781 /* Deleting the keys does not gain extra security */
782 /* memset(enc->iv, 0, enc->block_size);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000783 memset(enc->key, 0, enc->key_len);
784 memset(mac->key, 0, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000785 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000786 (comp->type == COMP_DELAYED &&
787 active_state->after_authentication)) && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000788 packet_init_compression();
789 if (mode == MODE_OUT)
790 buffer_compress_init_send(6);
791 else
792 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000793 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000794 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000795 /*
796 * The 2^(blocksize*2) limit is too expensive for 3DES,
797 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
798 */
799 if (enc->block_size >= 16)
800 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
801 else
802 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000803 if (active_state->rekey_limit)
804 *max_blocks = MIN(*max_blocks,
805 active_state->rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000806}
807
Damien Miller5428f641999-11-25 11:54:57 +1100808/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000809 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000810 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000811 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
812 */
813static void
814packet_enable_delayed_compress(void)
815{
816 Comp *comp = NULL;
817 int mode;
818
819 /*
820 * Remember that we are past the authentication step, so rekeying
821 * with COMP_DELAYED will turn on compression immediately.
822 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000823 active_state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +1000824 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000825 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000826 if (active_state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +1000827 continue;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000828 comp = &active_state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +1000829 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000830 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000831 if (mode == MODE_OUT)
832 buffer_compress_init_send(6);
833 else
834 buffer_compress_init_recv();
835 comp->enabled = 1;
836 }
837 }
838}
839
840/*
Damien Miller33b13562000-04-04 14:38:59 +1000841 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
842 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000843static void
Damien Millera5539d22003-04-09 20:50:06 +1000844packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000845{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000846 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100847 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000848 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100849 u_int i, len;
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 Miller33b13562000-04-04 14:38:59 +1000860 }
Damien Miller963f6b22002-02-19 15:21:23 +1100861 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000862
Darren Tuckerf7288d72009-06-21 18:12:20 +1000863 cp = buffer_ptr(&active_state->outgoing_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000864 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000865
866#ifdef PACKET_DEBUG
867 fprintf(stderr, "plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000868 buffer_dump(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000869#endif
870
871 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000872 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000873 /* skip header, compress only payload */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000874 buffer_consume(&active_state->outgoing_packet, 5);
875 buffer_clear(&active_state->compression_buffer);
876 buffer_compress(&active_state->outgoing_packet,
877 &active_state->compression_buffer);
878 buffer_clear(&active_state->outgoing_packet);
879 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
880 buffer_append(&active_state->outgoing_packet,
881 buffer_ptr(&active_state->compression_buffer),
882 buffer_len(&active_state->compression_buffer));
Damien Miller33b13562000-04-04 14:38:59 +1000883 DBG(debug("compression: raw %d compressed %d", len,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000884 buffer_len(&active_state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +1000885 }
886
887 /* sizeof (packet_len + pad_len + payload) */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000888 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000889
890 /*
891 * calc size of padding, alloc space, get random data,
892 * minimum padding is 4 bytes
893 */
894 padlen = block_size - (len % block_size);
895 if (padlen < 4)
896 padlen += block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000897 if (active_state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +1100898 /* will wrap if extra_pad+padlen > 255 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000899 active_state->extra_pad =
900 roundup(active_state->extra_pad, block_size);
901 pad = active_state->extra_pad -
902 ((len + padlen) % active_state->extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000903 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000904 pad, len, padlen, active_state->extra_pad);
Damien Miller9f643902001-11-12 11:02:52 +1100905 padlen += pad;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000906 active_state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100907 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000908 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
909 if (enc && !active_state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000910 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000911 for (i = 0; i < padlen; i++) {
912 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000913 rnd = arc4random();
914 cp[i] = rnd & 0xff;
915 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000916 }
Damien Millere247cc42000-05-07 12:03:14 +1000917 } else {
918 /* clear padding */
919 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000920 }
921 /* packet_length includes payload, padding and padding length field */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000922 packet_length = buffer_len(&active_state->outgoing_packet) - 4;
923 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100924 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000925 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000926 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
927
928 /* compute MAC over seqnr and packet(length fields, payload, padding) */
929 if (mac && mac->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000930 macbuf = mac_compute(mac, active_state->p_send.seqnr,
931 buffer_ptr(&active_state->outgoing_packet),
932 buffer_len(&active_state->outgoing_packet));
933 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000934 }
935 /* encrypt packet and append to output buffer. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000936 cp = buffer_append_space(&active_state->output,
937 buffer_len(&active_state->outgoing_packet));
938 cipher_crypt(&active_state->send_context, cp,
939 buffer_ptr(&active_state->outgoing_packet),
940 buffer_len(&active_state->outgoing_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000941 /* append unencrypted MAC */
942 if (mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000943 buffer_append(&active_state->output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000944#ifdef PACKET_DEBUG
945 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000946 buffer_dump(&active_state->output);
Damien Miller33b13562000-04-04 14:38:59 +1000947#endif
Damien Miller4af51302000-04-16 11:18:38 +1000948 /* increment sequence number for outgoing packets */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000949 if (++active_state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000950 logit("outgoing seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000951 if (++active_state->p_send.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +1000952 if (!(datafellows & SSH_BUG_NOREKEY))
953 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000954 active_state->p_send.blocks += (packet_length + 4) / block_size;
955 active_state->p_send.bytes += packet_length + 4;
956 buffer_clear(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000957
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000958 if (type == SSH2_MSG_NEWKEYS)
959 set_newkeys(MODE_OUT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000960 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +1000961 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000962}
963
Damien Millera5539d22003-04-09 20:50:06 +1000964static void
965packet_send2(void)
966{
Damien Millera5539d22003-04-09 20:50:06 +1000967 struct packet *p;
968 u_char type, *cp;
969
Darren Tuckerf7288d72009-06-21 18:12:20 +1000970 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000971 type = cp[5];
972
973 /* during rekeying we can only send key exchange messages */
Darren Tucker7b935c72009-06-21 18:59:36 +1000974 if (active_state->rekeying) {
Damien Millera5539d22003-04-09 20:50:06 +1000975 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
976 (type <= SSH2_MSG_TRANSPORT_MAX))) {
977 debug("enqueue packet: %u", type);
978 p = xmalloc(sizeof(*p));
979 p->type = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000980 memcpy(&p->payload, &active_state->outgoing_packet,
981 sizeof(Buffer));
982 buffer_init(&active_state->outgoing_packet);
983 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +1000984 return;
985 }
986 }
987
988 /* rekeying starts with sending KEXINIT */
989 if (type == SSH2_MSG_KEXINIT)
Darren Tucker7b935c72009-06-21 18:59:36 +1000990 active_state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +1000991
992 packet_send2_wrapped();
993
994 /* after a NEWKEYS message we can send the complete queue */
995 if (type == SSH2_MSG_NEWKEYS) {
Darren Tucker7b935c72009-06-21 18:59:36 +1000996 active_state->rekeying = 0;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000997 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +1000998 type = p->type;
999 debug("dequeue packet: %u", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001000 buffer_free(&active_state->outgoing_packet);
1001 memcpy(&active_state->outgoing_packet, &p->payload,
Damien Millera5539d22003-04-09 20:50:06 +10001002 sizeof(Buffer));
Darren Tuckerf7288d72009-06-21 18:12:20 +10001003 TAILQ_REMOVE(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +10001004 xfree(p);
1005 packet_send2_wrapped();
1006 }
1007 }
1008}
1009
Damien Miller33b13562000-04-04 14:38:59 +10001010void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001011packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +10001012{
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001013 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +10001014 packet_send2();
1015 else
1016 packet_send1();
1017 DBG(debug("packet_send done"));
1018}
1019
Damien Miller33b13562000-04-04 14:38:59 +10001020/*
Damien Miller5428f641999-11-25 11:54:57 +11001021 * Waits until a packet has been received, and returns its type. Note that
1022 * no other data is processed until this returns, so this function should not
1023 * be used during the interactive session.
1024 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001025
1026int
Damien Millerdff50992002-01-22 23:16:32 +11001027packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028{
Darren Tuckerc5564e12009-06-21 18:53:53 +10001029 int type, len, ret, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001030 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001031 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001032 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001033
Darren Tucker99bb7612008-06-13 22:02:50 +10001034 DBG(debug("packet_read()"));
1035
Darren Tuckerf7288d72009-06-21 18:12:20 +10001036 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1037 NFDBITS), sizeof(fd_mask));
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001038
Damien Miller95def091999-11-25 00:26:21 +11001039 /* Since we are blocking, ensure that all written packets have been sent. */
1040 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041
Damien Miller95def091999-11-25 00:26:21 +11001042 /* Stay in the loop until we have received a complete packet. */
1043 for (;;) {
1044 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +11001045 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001046 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +10001047 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +11001048 || type == SSH_SMSG_FAILURE
1049 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +10001050 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +11001051 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001052 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001053 if (type != SSH_MSG_NONE) {
1054 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +11001055 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001056 }
Damien Miller5428f641999-11-25 11:54:57 +11001057 /*
1058 * Otherwise, wait for some data to arrive, add it to the
1059 * buffer, and try again.
1060 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001061 memset(setp, 0, howmany(active_state->connection_in + 1,
1062 NFDBITS) * sizeof(fd_mask));
1063 FD_SET(active_state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001064
Darren Tuckerf7288d72009-06-21 18:12:20 +10001065 if (active_state->packet_timeout_ms > 0) {
1066 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001067 timeoutp = &timeout;
1068 }
Damien Miller95def091999-11-25 00:26:21 +11001069 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001070 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001071 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001072 ms_to_timeval(&timeout, ms_remain);
1073 gettimeofday(&start, NULL);
1074 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001075 if ((ret = select(active_state->connection_in + 1, setp,
1076 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001077 break;
Damien Millerea437422009-10-02 11:49:03 +10001078 if (errno != EAGAIN && errno != EINTR &&
1079 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001080 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001081 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001082 continue;
1083 ms_subtract_diff(&start, &ms_remain);
1084 if (ms_remain <= 0) {
1085 ret = 0;
1086 break;
1087 }
1088 }
1089 if (ret == 0) {
1090 logit("Connection to %.200s timed out while "
1091 "waiting to read", get_remote_ipaddr());
1092 cleanup_exit(255);
1093 }
Damien Miller95def091999-11-25 00:26:21 +11001094 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001095 do {
1096 cont = 0;
1097 len = roaming_read(active_state->connection_in, buf,
1098 sizeof(buf), &cont);
1099 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001100 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001101 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +10001102 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001103 }
Damien Miller95def091999-11-25 00:26:21 +11001104 if (len < 0)
1105 fatal("Read from socket failed: %.100s", strerror(errno));
1106 /* Append it to the buffer. */
1107 packet_process_incoming(buf, len);
1108 }
1109 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001110}
1111
Damien Miller278f9072001-12-21 15:00:19 +11001112int
Damien Millerdff50992002-01-22 23:16:32 +11001113packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +11001114{
Damien Millerdff50992002-01-22 23:16:32 +11001115 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001116}
1117
Damien Miller5428f641999-11-25 11:54:57 +11001118/*
1119 * Waits until a packet has been received, verifies that its type matches
1120 * that given, and gives a fatal error and exits if there is a mismatch.
1121 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001122
1123void
Damien Millerdff50992002-01-22 23:16:32 +11001124packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001125{
Damien Miller95def091999-11-25 00:26:21 +11001126 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127
Damien Millerdff50992002-01-22 23:16:32 +11001128 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001129 if (type != expected_type)
1130 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001131 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001132}
1133
1134/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001135 * packet_process_incoming. If so, reads the packet; otherwise returns
1136 * SSH_MSG_NONE. This does not wait for data from the connection.
1137 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001138 * SSH_MSG_DISCONNECT is handled specially here. Also,
1139 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1140 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001141 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001142
Ben Lindstrombba81212001-06-25 05:01:22 +00001143static int
Damien Millerdff50992002-01-22 23:16:32 +11001144packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001146 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001147 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001148 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149
Damien Miller95def091999-11-25 00:26:21 +11001150 /* Check if input size is less than minimum packet size. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001151 if (buffer_len(&active_state->input) < 4 + 8)
Damien Miller95def091999-11-25 00:26:21 +11001152 return SSH_MSG_NONE;
1153 /* Get length of incoming packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001154 cp = buffer_ptr(&active_state->input);
Damien Miller3f941882006-03-31 23:13:02 +11001155 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001156 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001157 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +11001158 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001159
Damien Miller95def091999-11-25 00:26:21 +11001160 /* Check if the packet has been entirely received. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001161 if (buffer_len(&active_state->input) < 4 + padded_len)
Damien Miller95def091999-11-25 00:26:21 +11001162 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001163
Damien Miller95def091999-11-25 00:26:21 +11001164 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165
Damien Miller95def091999-11-25 00:26:21 +11001166 /* Consume packet length. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001167 buffer_consume(&active_state->input, 4);
Damien Miller95def091999-11-25 00:26:21 +11001168
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001169 /*
1170 * Cryptographic attack detector for ssh
1171 * (C)1998 CORE-SDI, Buenos Aires Argentina
1172 * Ariel Futoransky(futo@core-sdi.com)
1173 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001174 if (!active_state->receive_context.plaintext) {
1175 switch (detect_attack(buffer_ptr(&active_state->input),
1176 padded_len)) {
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001177 case DEATTACK_DETECTED:
1178 packet_disconnect("crc32 compensation attack: "
1179 "network attack detected");
1180 case DEATTACK_DOS_DETECTED:
1181 packet_disconnect("deattack denial of "
1182 "service detected");
1183 }
1184 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001185
1186 /* Decrypt data to incoming_packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001187 buffer_clear(&active_state->incoming_packet);
1188 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1189 cipher_crypt(&active_state->receive_context, cp,
1190 buffer_ptr(&active_state->input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001191
Darren Tuckerf7288d72009-06-21 18:12:20 +10001192 buffer_consume(&active_state->input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193
1194#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001195 fprintf(stderr, "read_poll plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001196 buffer_dump(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198
Damien Miller95def091999-11-25 00:26:21 +11001199 /* Compute packet checksum. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001200 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1201 buffer_len(&active_state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001202
Damien Miller95def091999-11-25 00:26:21 +11001203 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001204 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001205
Damien Miller95def091999-11-25 00:26:21 +11001206 /* Test check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001207 if (len != buffer_len(&active_state->incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001208 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001209 len, buffer_len(&active_state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001210
Darren Tuckerf7288d72009-06-21 18:12:20 +10001211 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001212 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001213 if (checksum != stored_checksum)
1214 packet_disconnect("Corrupted check bytes on input.");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001215 buffer_consume_end(&active_state->incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001216
Darren Tuckerf7288d72009-06-21 18:12:20 +10001217 if (active_state->packet_compression) {
1218 buffer_clear(&active_state->compression_buffer);
1219 buffer_uncompress(&active_state->incoming_packet,
1220 &active_state->compression_buffer);
1221 buffer_clear(&active_state->incoming_packet);
1222 buffer_append(&active_state->incoming_packet,
1223 buffer_ptr(&active_state->compression_buffer),
1224 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001225 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001226 active_state->p_read.packets++;
1227 active_state->p_read.bytes += padded_len + 4;
1228 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001229 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1230 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001231 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001232}
Damien Miller95def091999-11-25 00:26:21 +11001233
Ben Lindstrombba81212001-06-25 05:01:22 +00001234static int
Damien Millerdff50992002-01-22 23:16:32 +11001235packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001236{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001237 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001238 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001239 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001240 Enc *enc = NULL;
1241 Mac *mac = NULL;
1242 Comp *comp = NULL;
1243
Darren Tuckerf7288d72009-06-21 18:12:20 +10001244 if (active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001245 return SSH_MSG_NONE;
1246
Darren Tuckerf7288d72009-06-21 18:12:20 +10001247 if (active_state->newkeys[MODE_IN] != NULL) {
1248 enc = &active_state->newkeys[MODE_IN]->enc;
1249 mac = &active_state->newkeys[MODE_IN]->mac;
1250 comp = &active_state->newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001251 }
1252 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001253 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001254
Darren Tuckerf7288d72009-06-21 18:12:20 +10001255 if (active_state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001256 /*
1257 * check if input size is less than the cipher block size,
1258 * decrypt first block and extract length of incoming packet
1259 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001260 if (buffer_len(&active_state->input) < block_size)
Damien Miller33b13562000-04-04 14:38:59 +10001261 return SSH_MSG_NONE;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001262 buffer_clear(&active_state->incoming_packet);
1263 cp = buffer_append_space(&active_state->incoming_packet,
Damien Miller33b13562000-04-04 14:38:59 +10001264 block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001265 cipher_crypt(&active_state->receive_context, cp,
1266 buffer_ptr(&active_state->input), block_size);
1267 cp = buffer_ptr(&active_state->incoming_packet);
1268 active_state->packlen = get_u32(cp);
1269 if (active_state->packlen < 1 + 4 ||
1270 active_state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001271#ifdef PACKET_DEBUG
Darren Tuckerf7288d72009-06-21 18:12:20 +10001272 buffer_dump(&active_state->incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001273#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001274 logit("Bad packet length %u.", active_state->packlen);
1275 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001276 PACKET_MAX_SIZE);
1277 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001278 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001279 DBG(debug("input: packet len %u", active_state->packlen+4));
1280 buffer_consume(&active_state->input, block_size);
Damien Miller33b13562000-04-04 14:38:59 +10001281 }
1282 /* we have a partial packet of block_size bytes */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001283 need = 4 + active_state->packlen - block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001284 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1285 need, maclen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001286 if (need % block_size != 0) {
1287 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001288 need, block_size, need % block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001289 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001290 PACKET_MAX_SIZE - block_size);
1291 return SSH_MSG_NONE;
Darren Tucker99d11a32008-12-01 21:40:48 +11001292 }
Damien Miller33b13562000-04-04 14:38:59 +10001293 /*
1294 * check if the entire packet has been received and
1295 * decrypt into incoming_packet
1296 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001297 if (buffer_len(&active_state->input) < need + maclen)
Damien Miller33b13562000-04-04 14:38:59 +10001298 return SSH_MSG_NONE;
1299#ifdef PACKET_DEBUG
1300 fprintf(stderr, "read_poll enc/full: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001301 buffer_dump(&active_state->input);
Damien Miller33b13562000-04-04 14:38:59 +10001302#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001303 cp = buffer_append_space(&active_state->incoming_packet, need);
1304 cipher_crypt(&active_state->receive_context, cp,
1305 buffer_ptr(&active_state->input), need);
1306 buffer_consume(&active_state->input, need);
Damien Miller33b13562000-04-04 14:38:59 +10001307 /*
1308 * compute MAC over seqnr and packet,
1309 * increment sequence number for incoming packet
1310 */
Damien Miller4af51302000-04-16 11:18:38 +10001311 if (mac && mac->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001312 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1313 buffer_ptr(&active_state->incoming_packet),
1314 buffer_len(&active_state->incoming_packet));
Damien Millerea1651c2010-07-16 13:58:37 +10001315 if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001316 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001317 logit("Corrupted MAC on input.");
1318 if (need > PACKET_MAX_SIZE)
1319 fatal("internal error need %d", need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001320 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001321 PACKET_MAX_SIZE - need);
1322 return SSH_MSG_NONE;
1323 }
1324
Darren Tuckerf7288d72009-06-21 18:12:20 +10001325 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1326 buffer_consume(&active_state->input, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +10001327 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001328 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001329 if (seqnr_p != NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001330 *seqnr_p = active_state->p_read.seqnr;
1331 if (++active_state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001332 logit("incoming seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001333 if (++active_state->p_read.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +10001334 if (!(datafellows & SSH_BUG_NOREKEY))
1335 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001336 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1337 active_state->p_read.bytes += active_state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001338
1339 /* get padlen */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001340 cp = buffer_ptr(&active_state->incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001341 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001342 DBG(debug("input: padlen %d", padlen));
1343 if (padlen < 4)
1344 packet_disconnect("Corrupted padlen %d on input.", padlen);
1345
1346 /* skip packet size + padlen, discard padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001347 buffer_consume(&active_state->incoming_packet, 4 + 1);
1348 buffer_consume_end(&active_state->incoming_packet, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001349
Darren Tuckerf7288d72009-06-21 18:12:20 +10001350 DBG(debug("input: len before de-compress %d",
1351 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001352 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001353 buffer_clear(&active_state->compression_buffer);
1354 buffer_uncompress(&active_state->incoming_packet,
1355 &active_state->compression_buffer);
1356 buffer_clear(&active_state->incoming_packet);
1357 buffer_append(&active_state->incoming_packet,
1358 buffer_ptr(&active_state->compression_buffer),
1359 buffer_len(&active_state->compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001360 DBG(debug("input: len after de-compress %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001361 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001362 }
1363 /*
1364 * get packet type, implies consume.
1365 * return length of payload (without type field)
1366 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001367 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001368 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1369 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001370 if (type == SSH2_MSG_NEWKEYS)
1371 set_newkeys(MODE_IN);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001372 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1373 !active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +10001374 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001375#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001376 fprintf(stderr, "read/plain[%d]:\r\n", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001377 buffer_dump(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001378#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001379 /* reset for next packet */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001380 active_state->packlen = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001381 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001382}
1383
1384int
Damien Millerdff50992002-01-22 23:16:32 +11001385packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001386{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001387 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001388 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001389 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001390
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001391 for (;;) {
1392 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001393 type = packet_read_poll2(seqnr_p);
Darren Tucker136e56f2008-06-08 12:49:30 +10001394 if (type) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001395 active_state->keep_alive_timeouts = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001396 DBG(debug("received packet type %d", type));
Darren Tucker136e56f2008-06-08 12:49:30 +10001397 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001398 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001399 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001400 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001401 break;
Damien Miller33b13562000-04-04 14:38:59 +10001402 case SSH2_MSG_DEBUG:
1403 packet_get_char();
1404 msg = packet_get_string(NULL);
1405 debug("Remote: %.900s", msg);
1406 xfree(msg);
1407 msg = packet_get_string(NULL);
1408 xfree(msg);
1409 break;
1410 case SSH2_MSG_DISCONNECT:
1411 reason = packet_get_int();
1412 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001413 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001414 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001415 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001416 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001417 break;
Damien Miller659811f2002-01-22 23:23:11 +11001418 case SSH2_MSG_UNIMPLEMENTED:
1419 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001420 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1421 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001422 break;
Damien Miller33b13562000-04-04 14:38:59 +10001423 default:
1424 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001425 }
Damien Miller33b13562000-04-04 14:38:59 +10001426 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001427 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001428 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001429 case SSH_MSG_IGNORE:
1430 break;
1431 case SSH_MSG_DEBUG:
1432 msg = packet_get_string(NULL);
1433 debug("Remote: %.900s", msg);
1434 xfree(msg);
1435 break;
1436 case SSH_MSG_DISCONNECT:
1437 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001438 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001439 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001440 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001441 break;
1442 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001443 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001444 DBG(debug("received packet type %d", type));
1445 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001446 }
Damien Miller33b13562000-04-04 14:38:59 +10001447 }
1448 }
1449}
1450
Damien Miller278f9072001-12-21 15:00:19 +11001451int
Damien Millerdff50992002-01-22 23:16:32 +11001452packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001453{
Damien Millerdff50992002-01-22 23:16:32 +11001454 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001455}
1456
Damien Miller5428f641999-11-25 11:54:57 +11001457/*
1458 * Buffers the given amount of input characters. This is intended to be used
1459 * together with packet_read_poll.
1460 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461
1462void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001463packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001464{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001465 if (active_state->packet_discard) {
1466 active_state->keep_alive_timeouts = 0; /* ?? */
1467 if (len >= active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001468 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001469 active_state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001470 return;
1471 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001472 buffer_append(&active_state->input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001473}
1474
1475/* Returns a character from the packet. */
1476
Ben Lindstrom46c16222000-12-22 01:43:59 +00001477u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001478packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479{
Damien Miller95def091999-11-25 00:26:21 +11001480 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001481
Darren Tuckerf7288d72009-06-21 18:12:20 +10001482 buffer_get(&active_state->incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001483 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001484}
1485
1486/* Returns an integer from the packet data. */
1487
Ben Lindstrom46c16222000-12-22 01:43:59 +00001488u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001489packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001491 return buffer_get_int(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492}
1493
Darren Tucker761c3892009-06-21 18:16:26 +10001494/* Returns an 64 bit integer from the packet data. */
1495
1496u_int64_t
1497packet_get_int64(void)
1498{
1499 return buffer_get_int64(&active_state->incoming_packet);
1500}
1501
Damien Miller5428f641999-11-25 11:54:57 +11001502/*
1503 * Returns an arbitrary precision integer from the packet data. The integer
1504 * must have been initialized before this call.
1505 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506
1507void
Damien Millerd432ccf2002-01-22 23:14:44 +11001508packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001509{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001510 buffer_get_bignum(&active_state->incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001511}
1512
Damien Miller33b13562000-04-04 14:38:59 +10001513void
Damien Millerd432ccf2002-01-22 23:14:44 +11001514packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001515{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001516 buffer_get_bignum2(&active_state->incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001517}
1518
Damien Miller6af914a2010-09-10 11:39:26 +10001519#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001520void
1521packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point)
1522{
1523 buffer_get_ecpoint(&active_state->incoming_packet, curve, point);
1524}
Damien Miller6af914a2010-09-10 11:39:26 +10001525#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001526
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001527void *
Damien Millereccb9de2005-06-17 12:59:34 +10001528packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001529{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001530 u_int bytes = buffer_len(&active_state->incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001531
Damien Miller33b13562000-04-04 14:38:59 +10001532 if (length_ptr != NULL)
1533 *length_ptr = bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001534 return buffer_ptr(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001535}
1536
Damien Miller4af51302000-04-16 11:18:38 +10001537int
1538packet_remaining(void)
1539{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001540 return buffer_len(&active_state->incoming_packet);
Damien Miller4af51302000-04-16 11:18:38 +10001541}
1542
Damien Miller5428f641999-11-25 11:54:57 +11001543/*
1544 * Returns a string from the packet data. The string is allocated using
1545 * xmalloc; it is the responsibility of the calling program to free it when
1546 * no longer needed. The length_ptr argument may be NULL, or point to an
1547 * integer into which the length of the string is stored.
1548 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001550void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001551packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001553 return buffer_get_string(&active_state->incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001554}
1555
Damien Millerdb255ca2008-05-19 14:59:37 +10001556void *
1557packet_get_string_ptr(u_int *length_ptr)
1558{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001559 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
Damien Millerdb255ca2008-05-19 14:59:37 +10001560}
1561
Damien Millerda108ec2010-08-31 22:36:39 +10001562/* Ensures the returned string has no embedded \0 characters in it. */
1563char *
1564packet_get_cstring(u_int *length_ptr)
1565{
1566 return buffer_get_cstring(&active_state->incoming_packet, length_ptr);
1567}
1568
Damien Miller5428f641999-11-25 11:54:57 +11001569/*
1570 * Sends a diagnostic message from the server to the client. This message
1571 * can be sent at any time (but not while constructing another message). The
1572 * message is printed immediately, but only if the client is being executed
1573 * in verbose mode. These messages are primarily intended to ease debugging
1574 * authentication problems. The length of the formatted message must not
1575 * exceed 1024 bytes. This will automatically call packet_write_wait.
1576 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001577
1578void
Damien Miller95def091999-11-25 00:26:21 +11001579packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001580{
Damien Miller95def091999-11-25 00:26:21 +11001581 char buf[1024];
1582 va_list args;
1583
Ben Lindstroma14ee472000-12-07 01:24:58 +00001584 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1585 return;
1586
Damien Miller95def091999-11-25 00:26:21 +11001587 va_start(args, fmt);
1588 vsnprintf(buf, sizeof(buf), fmt, args);
1589 va_end(args);
1590
Damien Miller7c8af4f2000-05-01 08:24:07 +10001591 if (compat20) {
1592 packet_start(SSH2_MSG_DEBUG);
1593 packet_put_char(0); /* bool: always display */
1594 packet_put_cstring(buf);
1595 packet_put_cstring("");
1596 } else {
1597 packet_start(SSH_MSG_DEBUG);
1598 packet_put_cstring(buf);
1599 }
Damien Miller95def091999-11-25 00:26:21 +11001600 packet_send();
1601 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001602}
1603
Damien Miller5428f641999-11-25 11:54:57 +11001604/*
1605 * Logs the error plus constructs and sends a disconnect packet, closes the
1606 * connection, and exits. This function never returns. The error message
1607 * should not contain a newline. The length of the formatted message must
1608 * not exceed 1024 bytes.
1609 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001610
1611void
Damien Miller95def091999-11-25 00:26:21 +11001612packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001613{
Damien Miller95def091999-11-25 00:26:21 +11001614 char buf[1024];
1615 va_list args;
1616 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001617
Damien Miller95def091999-11-25 00:26:21 +11001618 if (disconnecting) /* Guard against recursive invocations. */
1619 fatal("packet_disconnect called recursively.");
1620 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621
Damien Miller5428f641999-11-25 11:54:57 +11001622 /*
1623 * Format the message. Note that the caller must make sure the
1624 * message is of limited size.
1625 */
Damien Miller95def091999-11-25 00:26:21 +11001626 va_start(args, fmt);
1627 vsnprintf(buf, sizeof(buf), fmt, args);
1628 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001629
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001630 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001631 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001632
Damien Miller95def091999-11-25 00:26:21 +11001633 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001634 if (compat20) {
1635 packet_start(SSH2_MSG_DISCONNECT);
1636 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1637 packet_put_cstring(buf);
1638 packet_put_cstring("");
1639 } else {
1640 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001641 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001642 }
Damien Miller95def091999-11-25 00:26:21 +11001643 packet_send();
1644 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001645
Damien Miller95def091999-11-25 00:26:21 +11001646 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001647 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001648
Damien Miller95def091999-11-25 00:26:21 +11001649 /* Close the connection. */
1650 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001651 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001652}
1653
Damien Miller5428f641999-11-25 11:54:57 +11001654/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001655
1656void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001657packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001658{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001659 int len = buffer_len(&active_state->output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001660 int cont;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001661
Damien Miller95def091999-11-25 00:26:21 +11001662 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001663 cont = 0;
1664 len = roaming_write(active_state->connection_out,
1665 buffer_ptr(&active_state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001666 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001667 if (errno == EINTR || errno == EAGAIN ||
1668 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001669 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001670 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001671 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001672 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001673 fatal("Write connection closed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001674 buffer_consume(&active_state->output, len);
Damien Miller95def091999-11-25 00:26:21 +11001675 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001676}
1677
Damien Miller5428f641999-11-25 11:54:57 +11001678/*
1679 * Calls packet_write_poll repeatedly until all pending output data has been
1680 * written.
1681 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001682
1683void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001684packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001685{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001686 fd_set *setp;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001687 int ret, ms_remain;
1688 struct timeval start, timeout, *timeoutp = NULL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001689
Darren Tuckerf7288d72009-06-21 18:12:20 +10001690 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1691 NFDBITS), sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001692 packet_write_poll();
1693 while (packet_have_data_to_write()) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001694 memset(setp, 0, howmany(active_state->connection_out + 1,
1695 NFDBITS) * sizeof(fd_mask));
1696 FD_SET(active_state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001697
Darren Tuckerf7288d72009-06-21 18:12:20 +10001698 if (active_state->packet_timeout_ms > 0) {
1699 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001700 timeoutp = &timeout;
1701 }
1702 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001703 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001704 ms_to_timeval(&timeout, ms_remain);
1705 gettimeofday(&start, NULL);
1706 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001707 if ((ret = select(active_state->connection_out + 1,
1708 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001709 break;
Damien Millerea437422009-10-02 11:49:03 +10001710 if (errno != EAGAIN && errno != EINTR &&
1711 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001712 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001713 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001714 continue;
1715 ms_subtract_diff(&start, &ms_remain);
1716 if (ms_remain <= 0) {
1717 ret = 0;
1718 break;
1719 }
1720 }
1721 if (ret == 0) {
1722 logit("Connection to %.200s timed out while "
1723 "waiting to write", get_remote_ipaddr());
1724 cleanup_exit(255);
1725 }
Damien Miller95def091999-11-25 00:26:21 +11001726 packet_write_poll();
1727 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001728 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001729}
1730
1731/* Returns true if there is buffered data to write to the connection. */
1732
1733int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001734packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001735{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001736 return buffer_len(&active_state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001737}
1738
1739/* Returns true if there is not too much data to write to the connection. */
1740
1741int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001742packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001743{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001744 if (active_state->interactive_mode)
1745 return buffer_len(&active_state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11001746 else
Darren Tuckerf7288d72009-06-21 18:12:20 +10001747 return buffer_len(&active_state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001748}
1749
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001750static void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001751packet_set_tos(int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00001752{
Damien Millerd2ac5d72011-05-15 08:43:13 +10001753#ifndef IP_TOS_IS_BROKEN
1754 if (!packet_connection_is_on_socket())
Ben Lindstroma7433982002-12-23 02:41:41 +00001755 return;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001756 switch (packet_connection_af()) {
1757# ifdef IP_TOS
1758 case AF_INET:
1759 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
1760 if (setsockopt(active_state->connection_in,
1761 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
1762 error("setsockopt IP_TOS %d: %.100s:",
1763 tos, strerror(errno));
1764 break;
1765# endif /* IP_TOS */
1766# ifdef IPV6_TCLASS
1767 case AF_INET6:
1768 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
1769 if (setsockopt(active_state->connection_in,
1770 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
1771 error("setsockopt IPV6_TCLASS %d: %.100s:",
1772 tos, strerror(errno));
1773 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001774# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10001775 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10001776#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11001777}
Ben Lindstroma7433982002-12-23 02:41:41 +00001778
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001779/* Informs that the current session is interactive. Sets IP flags for that. */
1780
1781void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001782packet_set_interactive(int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001783{
Darren Tucker7b935c72009-06-21 18:59:36 +10001784 if (active_state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001785 return;
Darren Tucker7b935c72009-06-21 18:59:36 +10001786 active_state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001787
Damien Miller95def091999-11-25 00:26:21 +11001788 /* Record that we are in interactive mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001789 active_state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001790
Damien Miller34132e52000-01-14 15:45:46 +11001791 /* Only set socket options if using a socket. */
1792 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001793 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001794 set_nodelay(active_state->connection_in);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001795 packet_set_tos(interactive ? qos_interactive : qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001796}
1797
1798/* Returns true if the current connection is interactive. */
1799
1800int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001801packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001802{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001803 return active_state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001804}
Damien Miller6162d121999-11-21 13:23:52 +11001805
Darren Tucker1f8311c2004-05-13 16:39:33 +10001806int
Darren Tucker502d3842003-06-28 12:38:01 +10001807packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001808{
Darren Tucker7b935c72009-06-21 18:59:36 +10001809 if (active_state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10001810 logit("packet_set_maxsize: called twice: old %d new %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001811 active_state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001812 return -1;
1813 }
1814 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001815 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001816 return -1;
1817 }
Darren Tucker7b935c72009-06-21 18:59:36 +10001818 active_state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001819 debug("packet_set_maxsize: setting to %d", s);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001820 active_state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11001821 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001822}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001823
Darren Tuckerf7288d72009-06-21 18:12:20 +10001824int
1825packet_inc_alive_timeouts(void)
1826{
1827 return ++active_state->keep_alive_timeouts;
1828}
1829
1830void
1831packet_set_alive_timeouts(int ka)
1832{
1833 active_state->keep_alive_timeouts = ka;
1834}
1835
1836u_int
1837packet_get_maxsize(void)
1838{
1839 return active_state->max_packet_size;
1840}
1841
Damien Miller9f643902001-11-12 11:02:52 +11001842/* roundup current message to pad bytes */
1843void
1844packet_add_padding(u_char pad)
1845{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001846 active_state->extra_pad = pad;
Damien Miller9f643902001-11-12 11:02:52 +11001847}
1848
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001849/*
1850 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001851 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001852 * byte SSH_MSG_IGNORE
1853 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001854 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001855 * All implementations MUST understand (and ignore) this message at any
1856 * time (after receiving the protocol version). No implementation is
1857 * required to send them. This message can be used as an additional
1858 * protection measure against advanced traffic analysis techniques.
1859 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001860void
1861packet_send_ignore(int nbytes)
1862{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001863 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001864 int i;
1865
1866 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001867 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001868 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001869 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001870 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001871 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001872 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001873 }
1874}
Damien Millera5539d22003-04-09 20:50:06 +10001875
Darren Tucker1f8311c2004-05-13 16:39:33 +10001876#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001877int
1878packet_need_rekeying(void)
1879{
1880 if (datafellows & SSH_BUG_NOREKEY)
1881 return 0;
1882 return
Darren Tuckerf7288d72009-06-21 18:12:20 +10001883 (active_state->p_send.packets > MAX_PACKETS) ||
1884 (active_state->p_read.packets > MAX_PACKETS) ||
1885 (active_state->max_blocks_out &&
1886 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1887 (active_state->max_blocks_in &&
1888 (active_state->p_read.blocks > active_state->max_blocks_in));
Damien Millera5539d22003-04-09 20:50:06 +10001889}
1890
1891void
1892packet_set_rekey_limit(u_int32_t bytes)
1893{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001894 active_state->rekey_limit = bytes;
Damien Millera5539d22003-04-09 20:50:06 +10001895}
Damien Miller9786e6e2005-07-26 21:54:56 +10001896
1897void
1898packet_set_server(void)
1899{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001900 active_state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001901}
1902
1903void
1904packet_set_authenticated(void)
1905{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001906 active_state->after_authentication = 1;
1907}
1908
1909void *
1910packet_get_input(void)
1911{
1912 return (void *)&active_state->input;
1913}
1914
1915void *
1916packet_get_output(void)
1917{
1918 return (void *)&active_state->output;
1919}
1920
1921void *
1922packet_get_newkeys(int mode)
1923{
1924 return (void *)active_state->newkeys[mode];
Damien Miller9786e6e2005-07-26 21:54:56 +10001925}
Darren Tuckere841eb02009-07-06 07:11:13 +10001926
1927/*
1928 * Save the state for the real connection, and use a separate state when
1929 * resuming a suspended connection.
1930 */
1931void
1932packet_backup_state(void)
1933{
1934 struct session_state *tmp;
1935
1936 close(active_state->connection_in);
1937 active_state->connection_in = -1;
1938 close(active_state->connection_out);
1939 active_state->connection_out = -1;
1940 if (backup_state)
1941 tmp = backup_state;
1942 else
1943 tmp = alloc_session_state();
1944 backup_state = active_state;
1945 active_state = tmp;
1946}
1947
1948/*
1949 * Swap in the old state when resuming a connecion.
1950 */
1951void
1952packet_restore_state(void)
1953{
1954 struct session_state *tmp;
1955 void *buf;
1956 u_int len;
1957
1958 tmp = backup_state;
1959 backup_state = active_state;
1960 active_state = tmp;
1961 active_state->connection_in = backup_state->connection_in;
1962 backup_state->connection_in = -1;
1963 active_state->connection_out = backup_state->connection_out;
1964 backup_state->connection_out = -1;
1965 len = buffer_len(&backup_state->input);
1966 if (len > 0) {
1967 buf = buffer_ptr(&backup_state->input);
1968 buffer_append(&active_state->input, buf, len);
1969 buffer_clear(&backup_state->input);
1970 add_recv_bytes(len);
1971 }
1972}