blob: 994e35b6df50f926455475e6121fa6a16c252a6c [file] [log] [blame]
Darren Tuckere841eb02009-07-06 07:11:13 +10001/* $OpenBSD: packet.c,v 1.166 2009/06/27 09:29:06 andreas 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{
202 struct session_state *s = xcalloc(1, sizeof(*s));
203
204 s->connection_in = -1;
205 s->connection_out = -1;
206 s->max_packet_size = 32768;
207 s->packet_timeout_ms = -1;
208 return s;
209}
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{
245 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 Millerb61f3fc2008-07-11 17:36:48 +1000394packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets,
395 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 Miller34132e52000-01-14 15:45:46 +1100425/* returns 1 if connection is via ipv4 */
426
427int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000428packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100429{
430 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100431 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100432
433 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000434 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
435 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100436 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000437 if (to.ss_family == AF_INET)
438 return 1;
439#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100440 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000441 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
442 return 1;
443#endif
444 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100445}
446
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447/* Sets the connection into non-blocking mode. */
448
449void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000450packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451{
Damien Miller95def091999-11-25 00:26:21 +1100452 /* Set the socket into non-blocking mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000453 set_nonblock(active_state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454
Darren Tuckerf7288d72009-06-21 18:12:20 +1000455 if (active_state->connection_out != active_state->connection_in)
456 set_nonblock(active_state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457}
458
459/* Returns the socket used for reading. */
460
461int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000462packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000464 return active_state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465}
466
467/* Returns the descriptor used for writing. */
468
469int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000470packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000472 return active_state->connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473}
474
475/* Closes the connection and clears and frees internal data structures. */
476
477void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000478packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000480 if (!active_state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100481 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000482 active_state->initialized = 0;
483 if (active_state->connection_in == active_state->connection_out) {
484 shutdown(active_state->connection_out, SHUT_RDWR);
485 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100486 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000487 close(active_state->connection_in);
488 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100489 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000490 buffer_free(&active_state->input);
491 buffer_free(&active_state->output);
492 buffer_free(&active_state->outgoing_packet);
493 buffer_free(&active_state->incoming_packet);
494 if (active_state->compression_buffer_ready) {
495 buffer_free(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100496 buffer_compress_uninit();
497 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000498 cipher_cleanup(&active_state->send_context);
499 cipher_cleanup(&active_state->receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500}
501
502/* Sets remote side protocol flags. */
503
504void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000505packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000507 active_state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508}
509
510/* Returns the remote protocol flags set earlier by the above function. */
511
Ben Lindstrom46c16222000-12-22 01:43:59 +0000512u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000513packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000515 return active_state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516}
517
Damien Miller5428f641999-11-25 11:54:57 +1100518/*
519 * Starts packet compression from the next packet on in both directions.
520 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
521 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522
Ben Lindstrombba81212001-06-25 05:01:22 +0000523static void
524packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000525{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000526 if (active_state->compression_buffer_ready == 1)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000527 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000528 active_state->compression_buffer_ready = 1;
529 buffer_init(&active_state->compression_buffer);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000530}
531
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532void
533packet_start_compression(int level)
534{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000535 if (active_state->packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100536 fatal("Compression already enabled.");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000537 active_state->packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000538 packet_init_compression();
539 buffer_compress_init_send(level);
540 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541}
542
Damien Miller5428f641999-11-25 11:54:57 +1100543/*
Damien Miller5428f641999-11-25 11:54:57 +1100544 * Causes any further packets to be encrypted using the given key. The same
545 * key is used for both sending and reception. However, both directions are
546 * encrypted independently of each other.
547 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000548
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000550packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100551 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552{
Damien Miller874d77b2000-10-14 16:23:11 +1100553 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000554
Damien Miller874d77b2000-10-14 16:23:11 +1100555 if (cipher == NULL)
556 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000557 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100558 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000559 if (keylen > SSH_SESSION_KEY_LENGTH)
560 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000561 memcpy(active_state->ssh1_key, key, keylen);
562 active_state->ssh1_keylen = keylen;
563 cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
564 0, CIPHER_ENCRYPT);
565 cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
566 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567}
568
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000569u_int
570packet_get_encryption_key(u_char *key)
571{
572 if (key == NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000573 return (active_state->ssh1_keylen);
574 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
575 return (active_state->ssh1_keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000576}
577
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000578/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000580packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000582 u_char buf[9];
583 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584
Ben Lindstrom204e4882001-03-05 06:47:00 +0000585 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000586 len = compat20 ? 6 : 9;
587 memset(buf, 0, len - 1);
588 buf[len - 1] = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000589 buffer_clear(&active_state->outgoing_packet);
590 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000591}
592
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000593/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594void
595packet_put_char(int value)
596{
Damien Miller95def091999-11-25 00:26:21 +1100597 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000598
Darren Tuckerf7288d72009-06-21 18:12:20 +1000599 buffer_append(&active_state->outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600}
Damien Miller4f7becb2006-03-26 14:10:14 +1100601
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000602void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000603packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000605 buffer_put_int(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606}
Damien Miller4f7becb2006-03-26 14:10:14 +1100607
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608void
Darren Tucker761c3892009-06-21 18:16:26 +1000609packet_put_int64(u_int64_t value)
610{
611 buffer_put_int64(&active_state->outgoing_packet, value);
612}
613
614void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100615packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000617 buffer_put_string(&active_state->outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618}
Damien Miller4f7becb2006-03-26 14:10:14 +1100619
Damien Miller33b13562000-04-04 14:38:59 +1000620void
621packet_put_cstring(const char *str)
622{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000623 buffer_put_cstring(&active_state->outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000624}
Damien Miller4f7becb2006-03-26 14:10:14 +1100625
Damien Miller33b13562000-04-04 14:38:59 +1000626void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100627packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000628{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000629 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000630}
Damien Miller4f7becb2006-03-26 14:10:14 +1100631
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632void
Damien Miller95def091999-11-25 00:26:21 +1100633packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000635 buffer_put_bignum(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636}
Damien Miller4f7becb2006-03-26 14:10:14 +1100637
Damien Miller33b13562000-04-04 14:38:59 +1000638void
639packet_put_bignum2(BIGNUM * value)
640{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000641 buffer_put_bignum2(&active_state->outgoing_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +1000642}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643
Damien Miller5428f641999-11-25 11:54:57 +1100644/*
645 * Finalizes and sends the packet. If the encryption key has been set,
646 * encrypts the packet before sending.
647 */
Damien Miller95def091999-11-25 00:26:21 +1100648
Ben Lindstrombba81212001-06-25 05:01:22 +0000649static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000650packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000651{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000652 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100653 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000654 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000655 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656
Damien Miller5428f641999-11-25 11:54:57 +1100657 /*
658 * If using packet compression, compress the payload of the outgoing
659 * packet.
660 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000661 if (active_state->packet_compression) {
662 buffer_clear(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100663 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000664 buffer_consume(&active_state->outgoing_packet, 8);
Damien Miller95def091999-11-25 00:26:21 +1100665 /* padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000666 buffer_append(&active_state->compression_buffer,
667 "\0\0\0\0\0\0\0\0", 8);
668 buffer_compress(&active_state->outgoing_packet,
669 &active_state->compression_buffer);
670 buffer_clear(&active_state->outgoing_packet);
671 buffer_append(&active_state->outgoing_packet,
672 buffer_ptr(&active_state->compression_buffer),
673 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100674 }
675 /* Compute packet length without padding (add checksum, remove padding). */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000676 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677
Damien Millere247cc42000-05-07 12:03:14 +1000678 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100679 padding = 8 - len % 8;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000680 if (!active_state->send_context.plaintext) {
681 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller95def091999-11-25 00:26:21 +1100682 for (i = 0; i < padding; i++) {
683 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000684 rnd = arc4random();
685 cp[7 - i] = rnd & 0xff;
686 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100687 }
688 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000689 buffer_consume(&active_state->outgoing_packet, 8 - padding);
Damien Miller95def091999-11-25 00:26:21 +1100690
691 /* Add check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000692 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
693 buffer_len(&active_state->outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100694 put_u32(buf, checksum);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000695 buffer_append(&active_state->outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696
697#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100698 fprintf(stderr, "packet_send plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000699 buffer_dump(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700#endif
701
Damien Miller95def091999-11-25 00:26:21 +1100702 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100703 put_u32(buf, len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000704 buffer_append(&active_state->output, buf, 4);
705 cp = buffer_append_space(&active_state->output,
706 buffer_len(&active_state->outgoing_packet));
707 cipher_crypt(&active_state->send_context, cp,
708 buffer_ptr(&active_state->outgoing_packet),
709 buffer_len(&active_state->outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100710
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100712 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000713 buffer_dump(&active_state->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +1000715 active_state->p_send.packets++;
716 active_state->p_send.bytes += len +
717 buffer_len(&active_state->outgoing_packet);
718 buffer_clear(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719
Damien Miller5428f641999-11-25 11:54:57 +1100720 /*
Damien Miller788f2122005-11-05 15:14:59 +1100721 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100722 * actually sent until packet_write_wait or packet_write_poll is
723 * called.
724 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725}
726
Ben Lindstromf6027d32002-03-22 01:42:04 +0000727void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000728set_newkeys(int mode)
729{
730 Enc *enc;
731 Mac *mac;
732 Comp *comp;
733 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000734 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000735 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000736
Ben Lindstrom064496f2002-12-23 02:04:22 +0000737 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000738
Damien Miller963f6b22002-02-19 15:21:23 +1100739 if (mode == MODE_OUT) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000740 cc = &active_state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000741 crypt_type = CIPHER_ENCRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000742 active_state->p_send.packets = active_state->p_send.blocks = 0;
743 max_blocks = &active_state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100744 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000745 cc = &active_state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000746 crypt_type = CIPHER_DECRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000747 active_state->p_read.packets = active_state->p_read.blocks = 0;
748 max_blocks = &active_state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100749 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000750 if (active_state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000751 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100752 cipher_cleanup(cc);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000753 enc = &active_state->newkeys[mode]->enc;
754 mac = &active_state->newkeys[mode]->mac;
755 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000756 mac_clear(mac);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000757 xfree(enc->name);
758 xfree(enc->iv);
759 xfree(enc->key);
760 xfree(mac->name);
761 xfree(mac->key);
762 xfree(comp->name);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000763 xfree(active_state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000764 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000765 active_state->newkeys[mode] = kex_get_newkeys(mode);
766 if (active_state->newkeys[mode] == NULL)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000767 fatal("newkeys: no keys for mode %d", mode);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000768 enc = &active_state->newkeys[mode]->enc;
769 mac = &active_state->newkeys[mode]->mac;
770 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000771 if (mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000772 mac->enabled = 1;
773 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100774 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000775 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000776 /* Deleting the keys does not gain extra security */
777 /* memset(enc->iv, 0, enc->block_size);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000778 memset(enc->key, 0, enc->key_len);
779 memset(mac->key, 0, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000780 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000781 (comp->type == COMP_DELAYED &&
782 active_state->after_authentication)) && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000783 packet_init_compression();
784 if (mode == MODE_OUT)
785 buffer_compress_init_send(6);
786 else
787 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000788 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000789 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000790 /*
791 * The 2^(blocksize*2) limit is too expensive for 3DES,
792 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
793 */
794 if (enc->block_size >= 16)
795 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
796 else
797 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000798 if (active_state->rekey_limit)
799 *max_blocks = MIN(*max_blocks,
800 active_state->rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000801}
802
Damien Miller5428f641999-11-25 11:54:57 +1100803/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000804 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000805 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000806 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
807 */
808static void
809packet_enable_delayed_compress(void)
810{
811 Comp *comp = NULL;
812 int mode;
813
814 /*
815 * Remember that we are past the authentication step, so rekeying
816 * with COMP_DELAYED will turn on compression immediately.
817 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000818 active_state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +1000819 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000820 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000821 if (active_state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +1000822 continue;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000823 comp = &active_state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +1000824 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000825 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000826 if (mode == MODE_OUT)
827 buffer_compress_init_send(6);
828 else
829 buffer_compress_init_recv();
830 comp->enabled = 1;
831 }
832 }
833}
834
835/*
Damien Miller33b13562000-04-04 14:38:59 +1000836 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
837 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000838static void
Damien Millera5539d22003-04-09 20:50:06 +1000839packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000840{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000841 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100842 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000843 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100844 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000845 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000846 Enc *enc = NULL;
847 Mac *mac = NULL;
848 Comp *comp = NULL;
849 int block_size;
850
Darren Tuckerf7288d72009-06-21 18:12:20 +1000851 if (active_state->newkeys[MODE_OUT] != NULL) {
852 enc = &active_state->newkeys[MODE_OUT]->enc;
853 mac = &active_state->newkeys[MODE_OUT]->mac;
854 comp = &active_state->newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000855 }
Damien Miller963f6b22002-02-19 15:21:23 +1100856 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000857
Darren Tuckerf7288d72009-06-21 18:12:20 +1000858 cp = buffer_ptr(&active_state->outgoing_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000859 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000860
861#ifdef PACKET_DEBUG
862 fprintf(stderr, "plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000863 buffer_dump(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000864#endif
865
866 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000867 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000868 /* skip header, compress only payload */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000869 buffer_consume(&active_state->outgoing_packet, 5);
870 buffer_clear(&active_state->compression_buffer);
871 buffer_compress(&active_state->outgoing_packet,
872 &active_state->compression_buffer);
873 buffer_clear(&active_state->outgoing_packet);
874 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
875 buffer_append(&active_state->outgoing_packet,
876 buffer_ptr(&active_state->compression_buffer),
877 buffer_len(&active_state->compression_buffer));
Damien Miller33b13562000-04-04 14:38:59 +1000878 DBG(debug("compression: raw %d compressed %d", len,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000879 buffer_len(&active_state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +1000880 }
881
882 /* sizeof (packet_len + pad_len + payload) */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000883 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000884
885 /*
886 * calc size of padding, alloc space, get random data,
887 * minimum padding is 4 bytes
888 */
889 padlen = block_size - (len % block_size);
890 if (padlen < 4)
891 padlen += block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000892 if (active_state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +1100893 /* will wrap if extra_pad+padlen > 255 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000894 active_state->extra_pad =
895 roundup(active_state->extra_pad, block_size);
896 pad = active_state->extra_pad -
897 ((len + padlen) % active_state->extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000898 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000899 pad, len, padlen, active_state->extra_pad);
Damien Miller9f643902001-11-12 11:02:52 +1100900 padlen += pad;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000901 active_state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100902 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000903 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
904 if (enc && !active_state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000905 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000906 for (i = 0; i < padlen; i++) {
907 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000908 rnd = arc4random();
909 cp[i] = rnd & 0xff;
910 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000911 }
Damien Millere247cc42000-05-07 12:03:14 +1000912 } else {
913 /* clear padding */
914 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000915 }
916 /* packet_length includes payload, padding and padding length field */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000917 packet_length = buffer_len(&active_state->outgoing_packet) - 4;
918 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100919 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000920 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000921 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
922
923 /* compute MAC over seqnr and packet(length fields, payload, padding) */
924 if (mac && mac->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000925 macbuf = mac_compute(mac, active_state->p_send.seqnr,
926 buffer_ptr(&active_state->outgoing_packet),
927 buffer_len(&active_state->outgoing_packet));
928 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000929 }
930 /* encrypt packet and append to output buffer. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000931 cp = buffer_append_space(&active_state->output,
932 buffer_len(&active_state->outgoing_packet));
933 cipher_crypt(&active_state->send_context, cp,
934 buffer_ptr(&active_state->outgoing_packet),
935 buffer_len(&active_state->outgoing_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000936 /* append unencrypted MAC */
937 if (mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000938 buffer_append(&active_state->output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000939#ifdef PACKET_DEBUG
940 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000941 buffer_dump(&active_state->output);
Damien Miller33b13562000-04-04 14:38:59 +1000942#endif
Damien Miller4af51302000-04-16 11:18:38 +1000943 /* increment sequence number for outgoing packets */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000944 if (++active_state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000945 logit("outgoing seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000946 if (++active_state->p_send.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +1000947 if (!(datafellows & SSH_BUG_NOREKEY))
948 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000949 active_state->p_send.blocks += (packet_length + 4) / block_size;
950 active_state->p_send.bytes += packet_length + 4;
951 buffer_clear(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000952
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000953 if (type == SSH2_MSG_NEWKEYS)
954 set_newkeys(MODE_OUT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000955 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +1000956 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000957}
958
Damien Millera5539d22003-04-09 20:50:06 +1000959static void
960packet_send2(void)
961{
Damien Millera5539d22003-04-09 20:50:06 +1000962 struct packet *p;
963 u_char type, *cp;
964
Darren Tuckerf7288d72009-06-21 18:12:20 +1000965 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000966 type = cp[5];
967
968 /* during rekeying we can only send key exchange messages */
Darren Tucker7b935c72009-06-21 18:59:36 +1000969 if (active_state->rekeying) {
Damien Millera5539d22003-04-09 20:50:06 +1000970 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
971 (type <= SSH2_MSG_TRANSPORT_MAX))) {
972 debug("enqueue packet: %u", type);
973 p = xmalloc(sizeof(*p));
974 p->type = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000975 memcpy(&p->payload, &active_state->outgoing_packet,
976 sizeof(Buffer));
977 buffer_init(&active_state->outgoing_packet);
978 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +1000979 return;
980 }
981 }
982
983 /* rekeying starts with sending KEXINIT */
984 if (type == SSH2_MSG_KEXINIT)
Darren Tucker7b935c72009-06-21 18:59:36 +1000985 active_state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +1000986
987 packet_send2_wrapped();
988
989 /* after a NEWKEYS message we can send the complete queue */
990 if (type == SSH2_MSG_NEWKEYS) {
Darren Tucker7b935c72009-06-21 18:59:36 +1000991 active_state->rekeying = 0;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000992 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +1000993 type = p->type;
994 debug("dequeue packet: %u", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000995 buffer_free(&active_state->outgoing_packet);
996 memcpy(&active_state->outgoing_packet, &p->payload,
Damien Millera5539d22003-04-09 20:50:06 +1000997 sizeof(Buffer));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000998 TAILQ_REMOVE(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +1000999 xfree(p);
1000 packet_send2_wrapped();
1001 }
1002 }
1003}
1004
Damien Miller33b13562000-04-04 14:38:59 +10001005void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001006packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +10001007{
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001008 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +10001009 packet_send2();
1010 else
1011 packet_send1();
1012 DBG(debug("packet_send done"));
1013}
1014
Damien Miller33b13562000-04-04 14:38:59 +10001015/*
Damien Miller5428f641999-11-25 11:54:57 +11001016 * Waits until a packet has been received, and returns its type. Note that
1017 * no other data is processed until this returns, so this function should not
1018 * be used during the interactive session.
1019 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020
1021int
Damien Millerdff50992002-01-22 23:16:32 +11001022packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023{
Darren Tuckerc5564e12009-06-21 18:53:53 +10001024 int type, len, ret, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001025 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001026 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001027 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028
Darren Tucker99bb7612008-06-13 22:02:50 +10001029 DBG(debug("packet_read()"));
1030
Darren Tuckerf7288d72009-06-21 18:12:20 +10001031 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1032 NFDBITS), sizeof(fd_mask));
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001033
Damien Miller95def091999-11-25 00:26:21 +11001034 /* Since we are blocking, ensure that all written packets have been sent. */
1035 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036
Damien Miller95def091999-11-25 00:26:21 +11001037 /* Stay in the loop until we have received a complete packet. */
1038 for (;;) {
1039 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +11001040 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001041 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +10001042 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +11001043 || type == SSH_SMSG_FAILURE
1044 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +10001045 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +11001046 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001047 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001048 if (type != SSH_MSG_NONE) {
1049 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +11001050 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001051 }
Damien Miller5428f641999-11-25 11:54:57 +11001052 /*
1053 * Otherwise, wait for some data to arrive, add it to the
1054 * buffer, and try again.
1055 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001056 memset(setp, 0, howmany(active_state->connection_in + 1,
1057 NFDBITS) * sizeof(fd_mask));
1058 FD_SET(active_state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001059
Darren Tuckerf7288d72009-06-21 18:12:20 +10001060 if (active_state->packet_timeout_ms > 0) {
1061 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001062 timeoutp = &timeout;
1063 }
Damien Miller95def091999-11-25 00:26:21 +11001064 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001065 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001066 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001067 ms_to_timeval(&timeout, ms_remain);
1068 gettimeofday(&start, NULL);
1069 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001070 if ((ret = select(active_state->connection_in + 1, setp,
1071 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001072 break;
Damien Millerea437422009-10-02 11:49:03 +10001073 if (errno != EAGAIN && errno != EINTR &&
1074 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001075 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001076 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001077 continue;
1078 ms_subtract_diff(&start, &ms_remain);
1079 if (ms_remain <= 0) {
1080 ret = 0;
1081 break;
1082 }
1083 }
1084 if (ret == 0) {
1085 logit("Connection to %.200s timed out while "
1086 "waiting to read", get_remote_ipaddr());
1087 cleanup_exit(255);
1088 }
Damien Miller95def091999-11-25 00:26:21 +11001089 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001090 do {
1091 cont = 0;
1092 len = roaming_read(active_state->connection_in, buf,
1093 sizeof(buf), &cont);
1094 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001095 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001096 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +10001097 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001098 }
Damien Miller95def091999-11-25 00:26:21 +11001099 if (len < 0)
1100 fatal("Read from socket failed: %.100s", strerror(errno));
1101 /* Append it to the buffer. */
1102 packet_process_incoming(buf, len);
1103 }
1104 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001105}
1106
Damien Miller278f9072001-12-21 15:00:19 +11001107int
Damien Millerdff50992002-01-22 23:16:32 +11001108packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +11001109{
Damien Millerdff50992002-01-22 23:16:32 +11001110 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001111}
1112
Damien Miller5428f641999-11-25 11:54:57 +11001113/*
1114 * Waits until a packet has been received, verifies that its type matches
1115 * that given, and gives a fatal error and exits if there is a mismatch.
1116 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001117
1118void
Damien Millerdff50992002-01-22 23:16:32 +11001119packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120{
Damien Miller95def091999-11-25 00:26:21 +11001121 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001122
Damien Millerdff50992002-01-22 23:16:32 +11001123 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001124 if (type != expected_type)
1125 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001126 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127}
1128
1129/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001130 * packet_process_incoming. If so, reads the packet; otherwise returns
1131 * SSH_MSG_NONE. This does not wait for data from the connection.
1132 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001133 * SSH_MSG_DISCONNECT is handled specially here. Also,
1134 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1135 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001136 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137
Ben Lindstrombba81212001-06-25 05:01:22 +00001138static int
Damien Millerdff50992002-01-22 23:16:32 +11001139packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001141 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001142 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001143 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144
Damien Miller95def091999-11-25 00:26:21 +11001145 /* Check if input size is less than minimum packet size. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001146 if (buffer_len(&active_state->input) < 4 + 8)
Damien Miller95def091999-11-25 00:26:21 +11001147 return SSH_MSG_NONE;
1148 /* Get length of incoming packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001149 cp = buffer_ptr(&active_state->input);
Damien Miller3f941882006-03-31 23:13:02 +11001150 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001151 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001152 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +11001153 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001154
Damien Miller95def091999-11-25 00:26:21 +11001155 /* Check if the packet has been entirely received. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001156 if (buffer_len(&active_state->input) < 4 + padded_len)
Damien Miller95def091999-11-25 00:26:21 +11001157 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001158
Damien Miller95def091999-11-25 00:26:21 +11001159 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160
Damien Miller95def091999-11-25 00:26:21 +11001161 /* Consume packet length. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001162 buffer_consume(&active_state->input, 4);
Damien Miller95def091999-11-25 00:26:21 +11001163
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001164 /*
1165 * Cryptographic attack detector for ssh
1166 * (C)1998 CORE-SDI, Buenos Aires Argentina
1167 * Ariel Futoransky(futo@core-sdi.com)
1168 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001169 if (!active_state->receive_context.plaintext) {
1170 switch (detect_attack(buffer_ptr(&active_state->input),
1171 padded_len)) {
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001172 case DEATTACK_DETECTED:
1173 packet_disconnect("crc32 compensation attack: "
1174 "network attack detected");
1175 case DEATTACK_DOS_DETECTED:
1176 packet_disconnect("deattack denial of "
1177 "service detected");
1178 }
1179 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001180
1181 /* Decrypt data to incoming_packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001182 buffer_clear(&active_state->incoming_packet);
1183 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1184 cipher_crypt(&active_state->receive_context, cp,
1185 buffer_ptr(&active_state->input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001186
Darren Tuckerf7288d72009-06-21 18:12:20 +10001187 buffer_consume(&active_state->input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188
1189#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001190 fprintf(stderr, "read_poll plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001191 buffer_dump(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193
Damien Miller95def091999-11-25 00:26:21 +11001194 /* Compute packet checksum. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001195 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1196 buffer_len(&active_state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197
Damien Miller95def091999-11-25 00:26:21 +11001198 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001199 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001200
Damien Miller95def091999-11-25 00:26:21 +11001201 /* Test check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001202 if (len != buffer_len(&active_state->incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001203 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001204 len, buffer_len(&active_state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001205
Darren Tuckerf7288d72009-06-21 18:12:20 +10001206 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001207 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001208 if (checksum != stored_checksum)
1209 packet_disconnect("Corrupted check bytes on input.");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001210 buffer_consume_end(&active_state->incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211
Darren Tuckerf7288d72009-06-21 18:12:20 +10001212 if (active_state->packet_compression) {
1213 buffer_clear(&active_state->compression_buffer);
1214 buffer_uncompress(&active_state->incoming_packet,
1215 &active_state->compression_buffer);
1216 buffer_clear(&active_state->incoming_packet);
1217 buffer_append(&active_state->incoming_packet,
1218 buffer_ptr(&active_state->compression_buffer),
1219 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001220 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001221 active_state->p_read.packets++;
1222 active_state->p_read.bytes += padded_len + 4;
1223 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001224 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1225 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001226 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001227}
Damien Miller95def091999-11-25 00:26:21 +11001228
Ben Lindstrombba81212001-06-25 05:01:22 +00001229static int
Damien Millerdff50992002-01-22 23:16:32 +11001230packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001231{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001232 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001233 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001234 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001235 Enc *enc = NULL;
1236 Mac *mac = NULL;
1237 Comp *comp = NULL;
1238
Darren Tuckerf7288d72009-06-21 18:12:20 +10001239 if (active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001240 return SSH_MSG_NONE;
1241
Darren Tuckerf7288d72009-06-21 18:12:20 +10001242 if (active_state->newkeys[MODE_IN] != NULL) {
1243 enc = &active_state->newkeys[MODE_IN]->enc;
1244 mac = &active_state->newkeys[MODE_IN]->mac;
1245 comp = &active_state->newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001246 }
1247 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001248 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001249
Darren Tuckerf7288d72009-06-21 18:12:20 +10001250 if (active_state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001251 /*
1252 * check if input size is less than the cipher block size,
1253 * decrypt first block and extract length of incoming packet
1254 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001255 if (buffer_len(&active_state->input) < block_size)
Damien Miller33b13562000-04-04 14:38:59 +10001256 return SSH_MSG_NONE;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001257 buffer_clear(&active_state->incoming_packet);
1258 cp = buffer_append_space(&active_state->incoming_packet,
Damien Miller33b13562000-04-04 14:38:59 +10001259 block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001260 cipher_crypt(&active_state->receive_context, cp,
1261 buffer_ptr(&active_state->input), block_size);
1262 cp = buffer_ptr(&active_state->incoming_packet);
1263 active_state->packlen = get_u32(cp);
1264 if (active_state->packlen < 1 + 4 ||
1265 active_state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001266#ifdef PACKET_DEBUG
Darren Tuckerf7288d72009-06-21 18:12:20 +10001267 buffer_dump(&active_state->incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001268#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001269 logit("Bad packet length %u.", active_state->packlen);
1270 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001271 PACKET_MAX_SIZE);
1272 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001273 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001274 DBG(debug("input: packet len %u", active_state->packlen+4));
1275 buffer_consume(&active_state->input, block_size);
Damien Miller33b13562000-04-04 14:38:59 +10001276 }
1277 /* we have a partial packet of block_size bytes */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001278 need = 4 + active_state->packlen - block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001279 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1280 need, maclen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001281 if (need % block_size != 0) {
1282 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001283 need, block_size, need % block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001284 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001285 PACKET_MAX_SIZE - block_size);
1286 return SSH_MSG_NONE;
Darren Tucker99d11a32008-12-01 21:40:48 +11001287 }
Damien Miller33b13562000-04-04 14:38:59 +10001288 /*
1289 * check if the entire packet has been received and
1290 * decrypt into incoming_packet
1291 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001292 if (buffer_len(&active_state->input) < need + maclen)
Damien Miller33b13562000-04-04 14:38:59 +10001293 return SSH_MSG_NONE;
1294#ifdef PACKET_DEBUG
1295 fprintf(stderr, "read_poll enc/full: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001296 buffer_dump(&active_state->input);
Damien Miller33b13562000-04-04 14:38:59 +10001297#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001298 cp = buffer_append_space(&active_state->incoming_packet, need);
1299 cipher_crypt(&active_state->receive_context, cp,
1300 buffer_ptr(&active_state->input), need);
1301 buffer_consume(&active_state->input, need);
Damien Miller33b13562000-04-04 14:38:59 +10001302 /*
1303 * compute MAC over seqnr and packet,
1304 * increment sequence number for incoming packet
1305 */
Damien Miller4af51302000-04-16 11:18:38 +10001306 if (mac && mac->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001307 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1308 buffer_ptr(&active_state->incoming_packet),
1309 buffer_len(&active_state->incoming_packet));
1310 if (memcmp(macbuf, buffer_ptr(&active_state->input),
1311 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001312 logit("Corrupted MAC on input.");
1313 if (need > PACKET_MAX_SIZE)
1314 fatal("internal error need %d", need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001315 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001316 PACKET_MAX_SIZE - need);
1317 return SSH_MSG_NONE;
1318 }
1319
Darren Tuckerf7288d72009-06-21 18:12:20 +10001320 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1321 buffer_consume(&active_state->input, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +10001322 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001323 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001324 if (seqnr_p != NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001325 *seqnr_p = active_state->p_read.seqnr;
1326 if (++active_state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001327 logit("incoming seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001328 if (++active_state->p_read.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +10001329 if (!(datafellows & SSH_BUG_NOREKEY))
1330 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001331 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1332 active_state->p_read.bytes += active_state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001333
1334 /* get padlen */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001335 cp = buffer_ptr(&active_state->incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001336 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001337 DBG(debug("input: padlen %d", padlen));
1338 if (padlen < 4)
1339 packet_disconnect("Corrupted padlen %d on input.", padlen);
1340
1341 /* skip packet size + padlen, discard padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001342 buffer_consume(&active_state->incoming_packet, 4 + 1);
1343 buffer_consume_end(&active_state->incoming_packet, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001344
Darren Tuckerf7288d72009-06-21 18:12:20 +10001345 DBG(debug("input: len before de-compress %d",
1346 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001347 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001348 buffer_clear(&active_state->compression_buffer);
1349 buffer_uncompress(&active_state->incoming_packet,
1350 &active_state->compression_buffer);
1351 buffer_clear(&active_state->incoming_packet);
1352 buffer_append(&active_state->incoming_packet,
1353 buffer_ptr(&active_state->compression_buffer),
1354 buffer_len(&active_state->compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001355 DBG(debug("input: len after de-compress %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001356 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001357 }
1358 /*
1359 * get packet type, implies consume.
1360 * return length of payload (without type field)
1361 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001362 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001363 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1364 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001365 if (type == SSH2_MSG_NEWKEYS)
1366 set_newkeys(MODE_IN);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001367 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1368 !active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +10001369 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001370#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001371 fprintf(stderr, "read/plain[%d]:\r\n", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001372 buffer_dump(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001373#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001374 /* reset for next packet */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001375 active_state->packlen = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001376 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001377}
1378
1379int
Damien Millerdff50992002-01-22 23:16:32 +11001380packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001381{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001382 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001383 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001384 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001385
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001386 for (;;) {
1387 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001388 type = packet_read_poll2(seqnr_p);
Darren Tucker136e56f2008-06-08 12:49:30 +10001389 if (type) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001390 active_state->keep_alive_timeouts = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001391 DBG(debug("received packet type %d", type));
Darren Tucker136e56f2008-06-08 12:49:30 +10001392 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001393 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001394 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001395 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001396 break;
Damien Miller33b13562000-04-04 14:38:59 +10001397 case SSH2_MSG_DEBUG:
1398 packet_get_char();
1399 msg = packet_get_string(NULL);
1400 debug("Remote: %.900s", msg);
1401 xfree(msg);
1402 msg = packet_get_string(NULL);
1403 xfree(msg);
1404 break;
1405 case SSH2_MSG_DISCONNECT:
1406 reason = packet_get_int();
1407 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001408 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001409 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001410 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001411 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001412 break;
Damien Miller659811f2002-01-22 23:23:11 +11001413 case SSH2_MSG_UNIMPLEMENTED:
1414 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001415 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1416 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001417 break;
Damien Miller33b13562000-04-04 14:38:59 +10001418 default:
1419 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001420 }
Damien Miller33b13562000-04-04 14:38:59 +10001421 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001422 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001423 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001424 case SSH_MSG_IGNORE:
1425 break;
1426 case SSH_MSG_DEBUG:
1427 msg = packet_get_string(NULL);
1428 debug("Remote: %.900s", msg);
1429 xfree(msg);
1430 break;
1431 case SSH_MSG_DISCONNECT:
1432 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001433 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001434 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001435 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001436 break;
1437 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001438 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001439 DBG(debug("received packet type %d", type));
1440 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001441 }
Damien Miller33b13562000-04-04 14:38:59 +10001442 }
1443 }
1444}
1445
Damien Miller278f9072001-12-21 15:00:19 +11001446int
Damien Millerdff50992002-01-22 23:16:32 +11001447packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001448{
Damien Millerdff50992002-01-22 23:16:32 +11001449 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001450}
1451
Damien Miller5428f641999-11-25 11:54:57 +11001452/*
1453 * Buffers the given amount of input characters. This is intended to be used
1454 * together with packet_read_poll.
1455 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456
1457void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001458packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001459{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001460 if (active_state->packet_discard) {
1461 active_state->keep_alive_timeouts = 0; /* ?? */
1462 if (len >= active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001463 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001464 active_state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001465 return;
1466 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001467 buffer_append(&active_state->input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468}
1469
1470/* Returns a character from the packet. */
1471
Ben Lindstrom46c16222000-12-22 01:43:59 +00001472u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001473packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474{
Damien Miller95def091999-11-25 00:26:21 +11001475 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001476
Darren Tuckerf7288d72009-06-21 18:12:20 +10001477 buffer_get(&active_state->incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001478 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479}
1480
1481/* Returns an integer from the packet data. */
1482
Ben Lindstrom46c16222000-12-22 01:43:59 +00001483u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001484packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001486 return buffer_get_int(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001487}
1488
Darren Tucker761c3892009-06-21 18:16:26 +10001489/* Returns an 64 bit integer from the packet data. */
1490
1491u_int64_t
1492packet_get_int64(void)
1493{
1494 return buffer_get_int64(&active_state->incoming_packet);
1495}
1496
Damien Miller5428f641999-11-25 11:54:57 +11001497/*
1498 * Returns an arbitrary precision integer from the packet data. The integer
1499 * must have been initialized before this call.
1500 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001501
1502void
Damien Millerd432ccf2002-01-22 23:14:44 +11001503packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001505 buffer_get_bignum(&active_state->incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506}
1507
Damien Miller33b13562000-04-04 14:38:59 +10001508void
Damien Millerd432ccf2002-01-22 23:14:44 +11001509packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001510{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001511 buffer_get_bignum2(&active_state->incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001512}
1513
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001514void *
Damien Millereccb9de2005-06-17 12:59:34 +10001515packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001516{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001517 u_int bytes = buffer_len(&active_state->incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001518
Damien Miller33b13562000-04-04 14:38:59 +10001519 if (length_ptr != NULL)
1520 *length_ptr = bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001521 return buffer_ptr(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001522}
1523
Damien Miller4af51302000-04-16 11:18:38 +10001524int
1525packet_remaining(void)
1526{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001527 return buffer_len(&active_state->incoming_packet);
Damien Miller4af51302000-04-16 11:18:38 +10001528}
1529
Damien Miller5428f641999-11-25 11:54:57 +11001530/*
1531 * Returns a string from the packet data. The string is allocated using
1532 * xmalloc; it is the responsibility of the calling program to free it when
1533 * no longer needed. The length_ptr argument may be NULL, or point to an
1534 * integer into which the length of the string is stored.
1535 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001536
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001537void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001538packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001539{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001540 return buffer_get_string(&active_state->incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001541}
1542
Damien Millerdb255ca2008-05-19 14:59:37 +10001543void *
1544packet_get_string_ptr(u_int *length_ptr)
1545{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001546 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
Damien Millerdb255ca2008-05-19 14:59:37 +10001547}
1548
Damien Miller5428f641999-11-25 11:54:57 +11001549/*
1550 * Sends a diagnostic message from the server to the client. This message
1551 * can be sent at any time (but not while constructing another message). The
1552 * message is printed immediately, but only if the client is being executed
1553 * in verbose mode. These messages are primarily intended to ease debugging
1554 * authentication problems. The length of the formatted message must not
1555 * exceed 1024 bytes. This will automatically call packet_write_wait.
1556 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001557
1558void
Damien Miller95def091999-11-25 00:26:21 +11001559packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001560{
Damien Miller95def091999-11-25 00:26:21 +11001561 char buf[1024];
1562 va_list args;
1563
Ben Lindstroma14ee472000-12-07 01:24:58 +00001564 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1565 return;
1566
Damien Miller95def091999-11-25 00:26:21 +11001567 va_start(args, fmt);
1568 vsnprintf(buf, sizeof(buf), fmt, args);
1569 va_end(args);
1570
Damien Miller7c8af4f2000-05-01 08:24:07 +10001571 if (compat20) {
1572 packet_start(SSH2_MSG_DEBUG);
1573 packet_put_char(0); /* bool: always display */
1574 packet_put_cstring(buf);
1575 packet_put_cstring("");
1576 } else {
1577 packet_start(SSH_MSG_DEBUG);
1578 packet_put_cstring(buf);
1579 }
Damien Miller95def091999-11-25 00:26:21 +11001580 packet_send();
1581 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001582}
1583
Damien Miller5428f641999-11-25 11:54:57 +11001584/*
1585 * Logs the error plus constructs and sends a disconnect packet, closes the
1586 * connection, and exits. This function never returns. The error message
1587 * should not contain a newline. The length of the formatted message must
1588 * not exceed 1024 bytes.
1589 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001590
1591void
Damien Miller95def091999-11-25 00:26:21 +11001592packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001593{
Damien Miller95def091999-11-25 00:26:21 +11001594 char buf[1024];
1595 va_list args;
1596 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001597
Damien Miller95def091999-11-25 00:26:21 +11001598 if (disconnecting) /* Guard against recursive invocations. */
1599 fatal("packet_disconnect called recursively.");
1600 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001601
Damien Miller5428f641999-11-25 11:54:57 +11001602 /*
1603 * Format the message. Note that the caller must make sure the
1604 * message is of limited size.
1605 */
Damien Miller95def091999-11-25 00:26:21 +11001606 va_start(args, fmt);
1607 vsnprintf(buf, sizeof(buf), fmt, args);
1608 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001609
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001610 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001611 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001612
Damien Miller95def091999-11-25 00:26:21 +11001613 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001614 if (compat20) {
1615 packet_start(SSH2_MSG_DISCONNECT);
1616 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1617 packet_put_cstring(buf);
1618 packet_put_cstring("");
1619 } else {
1620 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001621 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001622 }
Damien Miller95def091999-11-25 00:26:21 +11001623 packet_send();
1624 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001625
Damien Miller95def091999-11-25 00:26:21 +11001626 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001627 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001628
Damien Miller95def091999-11-25 00:26:21 +11001629 /* Close the connection. */
1630 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001631 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001632}
1633
Damien Miller5428f641999-11-25 11:54:57 +11001634/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001635
1636void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001637packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001638{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001639 int len = buffer_len(&active_state->output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001640 int cont;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001641
Damien Miller95def091999-11-25 00:26:21 +11001642 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001643 cont = 0;
1644 len = roaming_write(active_state->connection_out,
1645 buffer_ptr(&active_state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001646 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001647 if (errno == EINTR || errno == EAGAIN ||
1648 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001649 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001650 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001651 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001652 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001653 fatal("Write connection closed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001654 buffer_consume(&active_state->output, len);
Damien Miller95def091999-11-25 00:26:21 +11001655 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001656}
1657
Damien Miller5428f641999-11-25 11:54:57 +11001658/*
1659 * Calls packet_write_poll repeatedly until all pending output data has been
1660 * written.
1661 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001662
1663void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001664packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001665{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001666 fd_set *setp;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001667 int ret, ms_remain;
1668 struct timeval start, timeout, *timeoutp = NULL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001669
Darren Tuckerf7288d72009-06-21 18:12:20 +10001670 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1671 NFDBITS), sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001672 packet_write_poll();
1673 while (packet_have_data_to_write()) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001674 memset(setp, 0, howmany(active_state->connection_out + 1,
1675 NFDBITS) * sizeof(fd_mask));
1676 FD_SET(active_state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001677
Darren Tuckerf7288d72009-06-21 18:12:20 +10001678 if (active_state->packet_timeout_ms > 0) {
1679 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001680 timeoutp = &timeout;
1681 }
1682 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001683 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001684 ms_to_timeval(&timeout, ms_remain);
1685 gettimeofday(&start, NULL);
1686 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001687 if ((ret = select(active_state->connection_out + 1,
1688 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001689 break;
Damien Millerea437422009-10-02 11:49:03 +10001690 if (errno != EAGAIN && errno != EINTR &&
1691 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001692 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001693 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001694 continue;
1695 ms_subtract_diff(&start, &ms_remain);
1696 if (ms_remain <= 0) {
1697 ret = 0;
1698 break;
1699 }
1700 }
1701 if (ret == 0) {
1702 logit("Connection to %.200s timed out while "
1703 "waiting to write", get_remote_ipaddr());
1704 cleanup_exit(255);
1705 }
Damien Miller95def091999-11-25 00:26:21 +11001706 packet_write_poll();
1707 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001708 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001709}
1710
1711/* Returns true if there is buffered data to write to the connection. */
1712
1713int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001714packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001715{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001716 return buffer_len(&active_state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001717}
1718
1719/* Returns true if there is not too much data to write to the connection. */
1720
1721int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001722packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001723{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001724 if (active_state->interactive_mode)
1725 return buffer_len(&active_state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11001726 else
Darren Tuckerf7288d72009-06-21 18:12:20 +10001727 return buffer_len(&active_state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001728}
1729
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001730static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001731packet_set_tos(int interactive)
1732{
Damien Miller5924ceb2003-11-22 15:02:42 +11001733#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001734 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1735
1736 if (!packet_connection_is_on_socket() ||
1737 !packet_connection_is_ipv4())
1738 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001739 if (setsockopt(active_state->connection_in, IPPROTO_IP, IP_TOS, &tos,
Ben Lindstroma7433982002-12-23 02:41:41 +00001740 sizeof(tos)) < 0)
1741 error("setsockopt IP_TOS %d: %.100s:",
1742 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001743#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001744}
Ben Lindstroma7433982002-12-23 02:41:41 +00001745
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001746/* Informs that the current session is interactive. Sets IP flags for that. */
1747
1748void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001749packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001750{
Darren Tucker7b935c72009-06-21 18:59:36 +10001751 if (active_state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001752 return;
Darren Tucker7b935c72009-06-21 18:59:36 +10001753 active_state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001754
Damien Miller95def091999-11-25 00:26:21 +11001755 /* Record that we are in interactive mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001756 active_state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001757
Damien Miller34132e52000-01-14 15:45:46 +11001758 /* Only set socket options if using a socket. */
1759 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001760 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001761 set_nodelay(active_state->connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001762 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001763}
1764
1765/* Returns true if the current connection is interactive. */
1766
1767int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001768packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001769{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001770 return active_state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001771}
Damien Miller6162d121999-11-21 13:23:52 +11001772
Darren Tucker1f8311c2004-05-13 16:39:33 +10001773int
Darren Tucker502d3842003-06-28 12:38:01 +10001774packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001775{
Darren Tucker7b935c72009-06-21 18:59:36 +10001776 if (active_state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10001777 logit("packet_set_maxsize: called twice: old %d new %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001778 active_state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001779 return -1;
1780 }
1781 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001782 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001783 return -1;
1784 }
Darren Tucker7b935c72009-06-21 18:59:36 +10001785 active_state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001786 debug("packet_set_maxsize: setting to %d", s);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001787 active_state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11001788 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001789}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001790
Darren Tuckerf7288d72009-06-21 18:12:20 +10001791int
1792packet_inc_alive_timeouts(void)
1793{
1794 return ++active_state->keep_alive_timeouts;
1795}
1796
1797void
1798packet_set_alive_timeouts(int ka)
1799{
1800 active_state->keep_alive_timeouts = ka;
1801}
1802
1803u_int
1804packet_get_maxsize(void)
1805{
1806 return active_state->max_packet_size;
1807}
1808
Damien Miller9f643902001-11-12 11:02:52 +11001809/* roundup current message to pad bytes */
1810void
1811packet_add_padding(u_char pad)
1812{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001813 active_state->extra_pad = pad;
Damien Miller9f643902001-11-12 11:02:52 +11001814}
1815
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001816/*
1817 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001818 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001819 * byte SSH_MSG_IGNORE
1820 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001821 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001822 * All implementations MUST understand (and ignore) this message at any
1823 * time (after receiving the protocol version). No implementation is
1824 * required to send them. This message can be used as an additional
1825 * protection measure against advanced traffic analysis techniques.
1826 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001827void
1828packet_send_ignore(int nbytes)
1829{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001830 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001831 int i;
1832
1833 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001834 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001835 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001836 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001837 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001838 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001839 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001840 }
1841}
Damien Millera5539d22003-04-09 20:50:06 +10001842
Darren Tucker1f8311c2004-05-13 16:39:33 +10001843#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001844int
1845packet_need_rekeying(void)
1846{
1847 if (datafellows & SSH_BUG_NOREKEY)
1848 return 0;
1849 return
Darren Tuckerf7288d72009-06-21 18:12:20 +10001850 (active_state->p_send.packets > MAX_PACKETS) ||
1851 (active_state->p_read.packets > MAX_PACKETS) ||
1852 (active_state->max_blocks_out &&
1853 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1854 (active_state->max_blocks_in &&
1855 (active_state->p_read.blocks > active_state->max_blocks_in));
Damien Millera5539d22003-04-09 20:50:06 +10001856}
1857
1858void
1859packet_set_rekey_limit(u_int32_t bytes)
1860{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001861 active_state->rekey_limit = bytes;
Damien Millera5539d22003-04-09 20:50:06 +10001862}
Damien Miller9786e6e2005-07-26 21:54:56 +10001863
1864void
1865packet_set_server(void)
1866{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001867 active_state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001868}
1869
1870void
1871packet_set_authenticated(void)
1872{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001873 active_state->after_authentication = 1;
1874}
1875
1876void *
1877packet_get_input(void)
1878{
1879 return (void *)&active_state->input;
1880}
1881
1882void *
1883packet_get_output(void)
1884{
1885 return (void *)&active_state->output;
1886}
1887
1888void *
1889packet_get_newkeys(int mode)
1890{
1891 return (void *)active_state->newkeys[mode];
Damien Miller9786e6e2005-07-26 21:54:56 +10001892}
Darren Tuckere841eb02009-07-06 07:11:13 +10001893
1894/*
1895 * Save the state for the real connection, and use a separate state when
1896 * resuming a suspended connection.
1897 */
1898void
1899packet_backup_state(void)
1900{
1901 struct session_state *tmp;
1902
1903 close(active_state->connection_in);
1904 active_state->connection_in = -1;
1905 close(active_state->connection_out);
1906 active_state->connection_out = -1;
1907 if (backup_state)
1908 tmp = backup_state;
1909 else
1910 tmp = alloc_session_state();
1911 backup_state = active_state;
1912 active_state = tmp;
1913}
1914
1915/*
1916 * Swap in the old state when resuming a connecion.
1917 */
1918void
1919packet_restore_state(void)
1920{
1921 struct session_state *tmp;
1922 void *buf;
1923 u_int len;
1924
1925 tmp = backup_state;
1926 backup_state = active_state;
1927 active_state = tmp;
1928 active_state->connection_in = backup_state->connection_in;
1929 backup_state->connection_in = -1;
1930 active_state->connection_out = backup_state->connection_out;
1931 backup_state->connection_out = -1;
1932 len = buffer_len(&backup_state->input);
1933 if (len > 0) {
1934 buf = buffer_ptr(&backup_state->input);
1935 buffer_append(&active_state->input, buf, len);
1936 buffer_clear(&backup_state->input);
1937 add_recv_bytes(len);
1938 }
1939}