blob: f74fe52e3f6816bb63bec76c27947842b0642b0c [file] [log] [blame]
Darren Tuckerb422afa2009-06-21 18:58:46 +10001/* $OpenBSD: packet.c,v 1.164 2009/06/12 20:43:22 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
185 TAILQ_HEAD(, packet) outgoing;
186};
187
188static struct session_state *active_state;
189
190static struct session_state *
Darren Tuckerb422afa2009-06-21 18:58:46 +1000191alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000192{
193 struct session_state *s = xcalloc(1, sizeof(*s));
194
195 s->connection_in = -1;
196 s->connection_out = -1;
197 s->max_packet_size = 32768;
198 s->packet_timeout_ms = -1;
199 return s;
200}
Damien Millera5539d22003-04-09 20:50:06 +1000201
Damien Miller5428f641999-11-25 11:54:57 +1100202/*
203 * Sets the descriptors used for communication. Disables encryption until
204 * packet_set_encryption_key is called.
205 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206void
207packet_set_connection(int fd_in, int fd_out)
208{
Damien Miller874d77b2000-10-14 16:23:11 +1100209 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000210
Damien Miller874d77b2000-10-14 16:23:11 +1100211 if (none == NULL)
212 fatal("packet_set_connection: cannot load cipher 'none'");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000213 if (active_state == NULL)
214 active_state = alloc_session_state();
215 active_state->connection_in = fd_in;
216 active_state->connection_out = fd_out;
217 cipher_init(&active_state->send_context, none, (const u_char *)"",
Darren Tucker1f8311c2004-05-13 16:39:33 +1000218 0, NULL, 0, CIPHER_ENCRYPT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000219 cipher_init(&active_state->receive_context, none, (const u_char *)"",
Darren Tucker1f8311c2004-05-13 16:39:33 +1000220 0, NULL, 0, CIPHER_DECRYPT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000221 active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL;
222 if (!active_state->initialized) {
223 active_state->initialized = 1;
224 buffer_init(&active_state->input);
225 buffer_init(&active_state->output);
226 buffer_init(&active_state->outgoing_packet);
227 buffer_init(&active_state->incoming_packet);
228 TAILQ_INIT(&active_state->outgoing);
229 active_state->p_send.packets = active_state->p_read.packets = 0;
Damien Miller95def091999-11-25 00:26:21 +1100230 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231}
232
Darren Tucker3fc464e2008-06-13 06:42:45 +1000233void
234packet_set_timeout(int timeout, int count)
235{
236 if (timeout == 0 || count == 0) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000237 active_state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000238 return;
239 }
240 if ((INT_MAX / 1000) / count < timeout)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000241 active_state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000242 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000243 active_state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000244}
245
Damien Miller13ae44c2009-01-28 16:38:41 +1100246static void
247packet_stop_discard(void)
248{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000249 if (active_state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100250 char buf[1024];
251
252 memset(buf, 'a', sizeof(buf));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000253 while (buffer_len(&active_state->incoming_packet) <
254 PACKET_MAX_SIZE)
255 buffer_append(&active_state->incoming_packet, buf,
256 sizeof(buf));
257 (void) mac_compute(active_state->packet_discard_mac,
258 active_state->p_read.seqnr,
259 buffer_ptr(&active_state->incoming_packet),
Damien Miller13ae44c2009-01-28 16:38:41 +1100260 PACKET_MAX_SIZE);
261 }
262 logit("Finished discarding for %.200s", get_remote_ipaddr());
263 cleanup_exit(255);
264}
265
266static void
267packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
268{
Damien Miller61433be2009-02-14 16:35:01 +1100269 if (enc == NULL || !cipher_is_cbc(enc->cipher))
Damien Miller13ae44c2009-01-28 16:38:41 +1100270 packet_disconnect("Packet corrupt");
271 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000272 active_state->packet_discard_mac = mac;
273 if (buffer_len(&active_state->input) >= discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100274 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +1000275 active_state->packet_discard = discard -
276 buffer_len(&active_state->input);
Damien Miller13ae44c2009-01-28 16:38:41 +1100277}
278
Damien Miller34132e52000-01-14 15:45:46 +1100279/* Returns 1 if remote host is connected via socket, 0 if not. */
280
281int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000282packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100283{
284 struct sockaddr_storage from, to;
285 socklen_t fromlen, tolen;
286
287 /* filedescriptors in and out are the same, so it's a socket */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000288 if (active_state->connection_in == active_state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100289 return 1;
290 fromlen = sizeof(from);
291 memset(&from, 0, sizeof(from));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000292 if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
293 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100294 return 0;
295 tolen = sizeof(to);
296 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000297 if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
298 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100299 return 0;
300 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
301 return 0;
302 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
303 return 0;
304 return 1;
305}
306
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000307/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000308 * Exports an IV from the CipherContext required to export the key
309 * state back from the unprivileged child to the privileged parent
310 * process.
311 */
312
313void
314packet_get_keyiv(int mode, u_char *iv, u_int len)
315{
316 CipherContext *cc;
317
318 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000319 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000320 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000321 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000322
323 cipher_get_keyiv(cc, iv, len);
324}
325
326int
327packet_get_keycontext(int mode, u_char *dat)
328{
329 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000330
Ben Lindstromf6027d32002-03-22 01:42:04 +0000331 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000332 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000333 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000334 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000335
336 return (cipher_get_keycontext(cc, dat));
337}
338
339void
340packet_set_keycontext(int mode, u_char *dat)
341{
342 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000343
Ben Lindstromf6027d32002-03-22 01:42:04 +0000344 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000345 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000346 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000347 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000348
349 cipher_set_keycontext(cc, dat);
350}
351
352int
353packet_get_keyiv_len(int mode)
354{
355 CipherContext *cc;
356
357 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000358 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000359 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000360 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000361
362 return (cipher_get_keyiv_len(cc));
363}
Damien Miller4f7becb2006-03-26 14:10:14 +1100364
Ben Lindstromf6027d32002-03-22 01:42:04 +0000365void
366packet_set_iv(int mode, u_char *dat)
367{
368 CipherContext *cc;
369
370 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000371 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000372 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000373 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000374
375 cipher_set_keyiv(cc, dat);
376}
Damien Miller4f7becb2006-03-26 14:10:14 +1100377
Ben Lindstromf6027d32002-03-22 01:42:04 +0000378int
Damien Miller2b92d322003-06-11 22:05:06 +1000379packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000380{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000381 return (cipher_get_number(active_state->receive_context.cipher));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000382}
383
Damien Millera5539d22003-04-09 20:50:06 +1000384void
Damien Millerb61f3fc2008-07-11 17:36:48 +1000385packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets,
386 u_int64_t *bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000387{
Damien Millera5539d22003-04-09 20:50:06 +1000388 struct packet_state *state;
389
Darren Tuckerf7288d72009-06-21 18:12:20 +1000390 state = (mode == MODE_IN) ?
391 &active_state->p_read : &active_state->p_send;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000392 if (seqnr)
393 *seqnr = state->seqnr;
394 if (blocks)
395 *blocks = state->blocks;
396 if (packets)
397 *packets = state->packets;
398 if (bytes)
399 *bytes = state->bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000400}
401
402void
Damien Millerb61f3fc2008-07-11 17:36:48 +1000403packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
404 u_int64_t bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000405{
Damien Millera5539d22003-04-09 20:50:06 +1000406 struct packet_state *state;
407
Darren Tuckerf7288d72009-06-21 18:12:20 +1000408 state = (mode == MODE_IN) ?
409 &active_state->p_read : &active_state->p_send;
Damien Millera5539d22003-04-09 20:50:06 +1000410 state->seqnr = seqnr;
411 state->blocks = blocks;
412 state->packets = packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000413 state->bytes = bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000414}
415
Damien Miller34132e52000-01-14 15:45:46 +1100416/* returns 1 if connection is via ipv4 */
417
418int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000419packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100420{
421 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100422 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100423
424 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000425 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
426 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100427 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000428 if (to.ss_family == AF_INET)
429 return 1;
430#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100431 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000432 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
433 return 1;
434#endif
435 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100436}
437
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438/* Sets the connection into non-blocking mode. */
439
440void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000441packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442{
Damien Miller95def091999-11-25 00:26:21 +1100443 /* Set the socket into non-blocking mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000444 set_nonblock(active_state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445
Darren Tuckerf7288d72009-06-21 18:12:20 +1000446 if (active_state->connection_out != active_state->connection_in)
447 set_nonblock(active_state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448}
449
450/* Returns the socket used for reading. */
451
452int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000453packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000455 return active_state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456}
457
458/* Returns the descriptor used for writing. */
459
460int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000461packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000463 return active_state->connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464}
465
466/* Closes the connection and clears and frees internal data structures. */
467
468void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000469packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000471 if (!active_state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100472 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000473 active_state->initialized = 0;
474 if (active_state->connection_in == active_state->connection_out) {
475 shutdown(active_state->connection_out, SHUT_RDWR);
476 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100477 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000478 close(active_state->connection_in);
479 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100480 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000481 buffer_free(&active_state->input);
482 buffer_free(&active_state->output);
483 buffer_free(&active_state->outgoing_packet);
484 buffer_free(&active_state->incoming_packet);
485 if (active_state->compression_buffer_ready) {
486 buffer_free(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100487 buffer_compress_uninit();
488 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000489 cipher_cleanup(&active_state->send_context);
490 cipher_cleanup(&active_state->receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491}
492
493/* Sets remote side protocol flags. */
494
495void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000496packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000498 active_state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499}
500
501/* Returns the remote protocol flags set earlier by the above function. */
502
Ben Lindstrom46c16222000-12-22 01:43:59 +0000503u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000504packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000506 return active_state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507}
508
Damien Miller5428f641999-11-25 11:54:57 +1100509/*
510 * Starts packet compression from the next packet on in both directions.
511 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
512 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Ben Lindstrombba81212001-06-25 05:01:22 +0000514static void
515packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000516{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000517 if (active_state->compression_buffer_ready == 1)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000518 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000519 active_state->compression_buffer_ready = 1;
520 buffer_init(&active_state->compression_buffer);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000521}
522
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523void
524packet_start_compression(int level)
525{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000526 if (active_state->packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100527 fatal("Compression already enabled.");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000528 active_state->packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000529 packet_init_compression();
530 buffer_compress_init_send(level);
531 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532}
533
Damien Miller5428f641999-11-25 11:54:57 +1100534/*
Damien Miller5428f641999-11-25 11:54:57 +1100535 * Causes any further packets to be encrypted using the given key. The same
536 * key is used for both sending and reception. However, both directions are
537 * encrypted independently of each other.
538 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000539
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000541packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100542 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543{
Damien Miller874d77b2000-10-14 16:23:11 +1100544 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000545
Damien Miller874d77b2000-10-14 16:23:11 +1100546 if (cipher == NULL)
547 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000548 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100549 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000550 if (keylen > SSH_SESSION_KEY_LENGTH)
551 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000552 memcpy(active_state->ssh1_key, key, keylen);
553 active_state->ssh1_keylen = keylen;
554 cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
555 0, CIPHER_ENCRYPT);
556 cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
557 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558}
559
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000560u_int
561packet_get_encryption_key(u_char *key)
562{
563 if (key == NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000564 return (active_state->ssh1_keylen);
565 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
566 return (active_state->ssh1_keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000567}
568
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000569/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000571packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000573 u_char buf[9];
574 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Ben Lindstrom204e4882001-03-05 06:47:00 +0000576 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000577 len = compat20 ? 6 : 9;
578 memset(buf, 0, len - 1);
579 buf[len - 1] = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000580 buffer_clear(&active_state->outgoing_packet);
581 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000582}
583
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000584/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585void
586packet_put_char(int value)
587{
Damien Miller95def091999-11-25 00:26:21 +1100588 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000589
Darren Tuckerf7288d72009-06-21 18:12:20 +1000590 buffer_append(&active_state->outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591}
Damien Miller4f7becb2006-03-26 14:10:14 +1100592
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000594packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000596 buffer_put_int(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597}
Damien Miller4f7becb2006-03-26 14:10:14 +1100598
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599void
Darren Tucker761c3892009-06-21 18:16:26 +1000600packet_put_int64(u_int64_t value)
601{
602 buffer_put_int64(&active_state->outgoing_packet, value);
603}
604
605void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100606packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000608 buffer_put_string(&active_state->outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609}
Damien Miller4f7becb2006-03-26 14:10:14 +1100610
Damien Miller33b13562000-04-04 14:38:59 +1000611void
612packet_put_cstring(const char *str)
613{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000614 buffer_put_cstring(&active_state->outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000615}
Damien Miller4f7becb2006-03-26 14:10:14 +1100616
Damien Miller33b13562000-04-04 14:38:59 +1000617void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100618packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000619{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000620 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000621}
Damien Miller4f7becb2006-03-26 14:10:14 +1100622
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623void
Damien Miller95def091999-11-25 00:26:21 +1100624packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000626 buffer_put_bignum(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627}
Damien Miller4f7becb2006-03-26 14:10:14 +1100628
Damien Miller33b13562000-04-04 14:38:59 +1000629void
630packet_put_bignum2(BIGNUM * value)
631{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000632 buffer_put_bignum2(&active_state->outgoing_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +1000633}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634
Damien Miller5428f641999-11-25 11:54:57 +1100635/*
636 * Finalizes and sends the packet. If the encryption key has been set,
637 * encrypts the packet before sending.
638 */
Damien Miller95def091999-11-25 00:26:21 +1100639
Ben Lindstrombba81212001-06-25 05:01:22 +0000640static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000641packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000643 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100644 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000645 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000646 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000647
Damien Miller5428f641999-11-25 11:54:57 +1100648 /*
649 * If using packet compression, compress the payload of the outgoing
650 * packet.
651 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000652 if (active_state->packet_compression) {
653 buffer_clear(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100654 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000655 buffer_consume(&active_state->outgoing_packet, 8);
Damien Miller95def091999-11-25 00:26:21 +1100656 /* padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000657 buffer_append(&active_state->compression_buffer,
658 "\0\0\0\0\0\0\0\0", 8);
659 buffer_compress(&active_state->outgoing_packet,
660 &active_state->compression_buffer);
661 buffer_clear(&active_state->outgoing_packet);
662 buffer_append(&active_state->outgoing_packet,
663 buffer_ptr(&active_state->compression_buffer),
664 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100665 }
666 /* Compute packet length without padding (add checksum, remove padding). */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000667 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668
Damien Millere247cc42000-05-07 12:03:14 +1000669 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100670 padding = 8 - len % 8;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000671 if (!active_state->send_context.plaintext) {
672 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller95def091999-11-25 00:26:21 +1100673 for (i = 0; i < padding; i++) {
674 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000675 rnd = arc4random();
676 cp[7 - i] = rnd & 0xff;
677 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100678 }
679 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000680 buffer_consume(&active_state->outgoing_packet, 8 - padding);
Damien Miller95def091999-11-25 00:26:21 +1100681
682 /* Add check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000683 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
684 buffer_len(&active_state->outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100685 put_u32(buf, checksum);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000686 buffer_append(&active_state->outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687
688#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100689 fprintf(stderr, "packet_send plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000690 buffer_dump(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691#endif
692
Damien Miller95def091999-11-25 00:26:21 +1100693 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100694 put_u32(buf, len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000695 buffer_append(&active_state->output, buf, 4);
696 cp = buffer_append_space(&active_state->output,
697 buffer_len(&active_state->outgoing_packet));
698 cipher_crypt(&active_state->send_context, cp,
699 buffer_ptr(&active_state->outgoing_packet),
700 buffer_len(&active_state->outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100701
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100703 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000704 buffer_dump(&active_state->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +1000706 active_state->p_send.packets++;
707 active_state->p_send.bytes += len +
708 buffer_len(&active_state->outgoing_packet);
709 buffer_clear(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710
Damien Miller5428f641999-11-25 11:54:57 +1100711 /*
Damien Miller788f2122005-11-05 15:14:59 +1100712 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100713 * actually sent until packet_write_wait or packet_write_poll is
714 * called.
715 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716}
717
Ben Lindstromf6027d32002-03-22 01:42:04 +0000718void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000719set_newkeys(int mode)
720{
721 Enc *enc;
722 Mac *mac;
723 Comp *comp;
724 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000725 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000726 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000727
Ben Lindstrom064496f2002-12-23 02:04:22 +0000728 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000729
Damien Miller963f6b22002-02-19 15:21:23 +1100730 if (mode == MODE_OUT) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000731 cc = &active_state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000732 crypt_type = CIPHER_ENCRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000733 active_state->p_send.packets = active_state->p_send.blocks = 0;
734 max_blocks = &active_state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100735 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000736 cc = &active_state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000737 crypt_type = CIPHER_DECRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000738 active_state->p_read.packets = active_state->p_read.blocks = 0;
739 max_blocks = &active_state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100740 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000741 if (active_state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000742 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100743 cipher_cleanup(cc);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000744 enc = &active_state->newkeys[mode]->enc;
745 mac = &active_state->newkeys[mode]->mac;
746 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000747 mac_clear(mac);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000748 xfree(enc->name);
749 xfree(enc->iv);
750 xfree(enc->key);
751 xfree(mac->name);
752 xfree(mac->key);
753 xfree(comp->name);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000754 xfree(active_state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000755 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000756 active_state->newkeys[mode] = kex_get_newkeys(mode);
757 if (active_state->newkeys[mode] == NULL)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000758 fatal("newkeys: no keys for mode %d", mode);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000759 enc = &active_state->newkeys[mode]->enc;
760 mac = &active_state->newkeys[mode]->mac;
761 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000762 if (mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000763 mac->enabled = 1;
764 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100765 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000766 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000767 /* Deleting the keys does not gain extra security */
768 /* memset(enc->iv, 0, enc->block_size);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000769 memset(enc->key, 0, enc->key_len);
770 memset(mac->key, 0, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000771 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000772 (comp->type == COMP_DELAYED &&
773 active_state->after_authentication)) && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000774 packet_init_compression();
775 if (mode == MODE_OUT)
776 buffer_compress_init_send(6);
777 else
778 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000779 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000780 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000781 /*
782 * The 2^(blocksize*2) limit is too expensive for 3DES,
783 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
784 */
785 if (enc->block_size >= 16)
786 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
787 else
788 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000789 if (active_state->rekey_limit)
790 *max_blocks = MIN(*max_blocks,
791 active_state->rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000792}
793
Damien Miller5428f641999-11-25 11:54:57 +1100794/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000795 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000796 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000797 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
798 */
799static void
800packet_enable_delayed_compress(void)
801{
802 Comp *comp = NULL;
803 int mode;
804
805 /*
806 * Remember that we are past the authentication step, so rekeying
807 * with COMP_DELAYED will turn on compression immediately.
808 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000809 active_state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +1000810 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000811 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000812 if (active_state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +1000813 continue;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000814 comp = &active_state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +1000815 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000816 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000817 if (mode == MODE_OUT)
818 buffer_compress_init_send(6);
819 else
820 buffer_compress_init_recv();
821 comp->enabled = 1;
822 }
823 }
824}
825
826/*
Damien Miller33b13562000-04-04 14:38:59 +1000827 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
828 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000829static void
Damien Millera5539d22003-04-09 20:50:06 +1000830packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000831{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000832 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100833 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000834 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100835 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000836 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000837 Enc *enc = NULL;
838 Mac *mac = NULL;
839 Comp *comp = NULL;
840 int block_size;
841
Darren Tuckerf7288d72009-06-21 18:12:20 +1000842 if (active_state->newkeys[MODE_OUT] != NULL) {
843 enc = &active_state->newkeys[MODE_OUT]->enc;
844 mac = &active_state->newkeys[MODE_OUT]->mac;
845 comp = &active_state->newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000846 }
Damien Miller963f6b22002-02-19 15:21:23 +1100847 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000848
Darren Tuckerf7288d72009-06-21 18:12:20 +1000849 cp = buffer_ptr(&active_state->outgoing_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000850 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000851
852#ifdef PACKET_DEBUG
853 fprintf(stderr, "plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000854 buffer_dump(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000855#endif
856
857 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000858 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000859 /* skip header, compress only payload */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000860 buffer_consume(&active_state->outgoing_packet, 5);
861 buffer_clear(&active_state->compression_buffer);
862 buffer_compress(&active_state->outgoing_packet,
863 &active_state->compression_buffer);
864 buffer_clear(&active_state->outgoing_packet);
865 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
866 buffer_append(&active_state->outgoing_packet,
867 buffer_ptr(&active_state->compression_buffer),
868 buffer_len(&active_state->compression_buffer));
Damien Miller33b13562000-04-04 14:38:59 +1000869 DBG(debug("compression: raw %d compressed %d", len,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000870 buffer_len(&active_state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +1000871 }
872
873 /* sizeof (packet_len + pad_len + payload) */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000874 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000875
876 /*
877 * calc size of padding, alloc space, get random data,
878 * minimum padding is 4 bytes
879 */
880 padlen = block_size - (len % block_size);
881 if (padlen < 4)
882 padlen += block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000883 if (active_state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +1100884 /* will wrap if extra_pad+padlen > 255 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000885 active_state->extra_pad =
886 roundup(active_state->extra_pad, block_size);
887 pad = active_state->extra_pad -
888 ((len + padlen) % active_state->extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000889 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000890 pad, len, padlen, active_state->extra_pad);
Damien Miller9f643902001-11-12 11:02:52 +1100891 padlen += pad;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000892 active_state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100893 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000894 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
895 if (enc && !active_state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000896 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000897 for (i = 0; i < padlen; i++) {
898 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000899 rnd = arc4random();
900 cp[i] = rnd & 0xff;
901 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000902 }
Damien Millere247cc42000-05-07 12:03:14 +1000903 } else {
904 /* clear padding */
905 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000906 }
907 /* packet_length includes payload, padding and padding length field */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000908 packet_length = buffer_len(&active_state->outgoing_packet) - 4;
909 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100910 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000911 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000912 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
913
914 /* compute MAC over seqnr and packet(length fields, payload, padding) */
915 if (mac && mac->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000916 macbuf = mac_compute(mac, active_state->p_send.seqnr,
917 buffer_ptr(&active_state->outgoing_packet),
918 buffer_len(&active_state->outgoing_packet));
919 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000920 }
921 /* encrypt packet and append to output buffer. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000922 cp = buffer_append_space(&active_state->output,
923 buffer_len(&active_state->outgoing_packet));
924 cipher_crypt(&active_state->send_context, cp,
925 buffer_ptr(&active_state->outgoing_packet),
926 buffer_len(&active_state->outgoing_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000927 /* append unencrypted MAC */
928 if (mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000929 buffer_append(&active_state->output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000930#ifdef PACKET_DEBUG
931 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000932 buffer_dump(&active_state->output);
Damien Miller33b13562000-04-04 14:38:59 +1000933#endif
Damien Miller4af51302000-04-16 11:18:38 +1000934 /* increment sequence number for outgoing packets */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000935 if (++active_state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000936 logit("outgoing seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000937 if (++active_state->p_send.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +1000938 if (!(datafellows & SSH_BUG_NOREKEY))
939 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000940 active_state->p_send.blocks += (packet_length + 4) / block_size;
941 active_state->p_send.bytes += packet_length + 4;
942 buffer_clear(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000943
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000944 if (type == SSH2_MSG_NEWKEYS)
945 set_newkeys(MODE_OUT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000946 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +1000947 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000948}
949
Damien Millera5539d22003-04-09 20:50:06 +1000950static void
951packet_send2(void)
952{
953 static int rekeying = 0;
954 struct packet *p;
955 u_char type, *cp;
956
Darren Tuckerf7288d72009-06-21 18:12:20 +1000957 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000958 type = cp[5];
959
960 /* during rekeying we can only send key exchange messages */
961 if (rekeying) {
962 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
963 (type <= SSH2_MSG_TRANSPORT_MAX))) {
964 debug("enqueue packet: %u", type);
965 p = xmalloc(sizeof(*p));
966 p->type = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000967 memcpy(&p->payload, &active_state->outgoing_packet,
968 sizeof(Buffer));
969 buffer_init(&active_state->outgoing_packet);
970 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +1000971 return;
972 }
973 }
974
975 /* rekeying starts with sending KEXINIT */
976 if (type == SSH2_MSG_KEXINIT)
977 rekeying = 1;
978
979 packet_send2_wrapped();
980
981 /* after a NEWKEYS message we can send the complete queue */
982 if (type == SSH2_MSG_NEWKEYS) {
983 rekeying = 0;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000984 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +1000985 type = p->type;
986 debug("dequeue packet: %u", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000987 buffer_free(&active_state->outgoing_packet);
988 memcpy(&active_state->outgoing_packet, &p->payload,
Damien Millera5539d22003-04-09 20:50:06 +1000989 sizeof(Buffer));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000990 TAILQ_REMOVE(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +1000991 xfree(p);
992 packet_send2_wrapped();
993 }
994 }
995}
996
Damien Miller33b13562000-04-04 14:38:59 +1000997void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000998packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000999{
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001000 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +10001001 packet_send2();
1002 else
1003 packet_send1();
1004 DBG(debug("packet_send done"));
1005}
1006
Damien Miller33b13562000-04-04 14:38:59 +10001007/*
Damien Miller5428f641999-11-25 11:54:57 +11001008 * Waits until a packet has been received, and returns its type. Note that
1009 * no other data is processed until this returns, so this function should not
1010 * be used during the interactive session.
1011 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
1013int
Damien Millerdff50992002-01-22 23:16:32 +11001014packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015{
Darren Tuckerc5564e12009-06-21 18:53:53 +10001016 int type, len, ret, ms_remain, cont;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001017 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001018 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001019 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020
Darren Tucker99bb7612008-06-13 22:02:50 +10001021 DBG(debug("packet_read()"));
1022
Darren Tuckerf7288d72009-06-21 18:12:20 +10001023 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1024 NFDBITS), sizeof(fd_mask));
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001025
Damien Miller95def091999-11-25 00:26:21 +11001026 /* Since we are blocking, ensure that all written packets have been sent. */
1027 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028
Damien Miller95def091999-11-25 00:26:21 +11001029 /* Stay in the loop until we have received a complete packet. */
1030 for (;;) {
1031 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +11001032 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001033 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +10001034 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +11001035 || type == SSH_SMSG_FAILURE
1036 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +10001037 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +11001038 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001039 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001040 if (type != SSH_MSG_NONE) {
1041 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +11001042 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001043 }
Damien Miller5428f641999-11-25 11:54:57 +11001044 /*
1045 * Otherwise, wait for some data to arrive, add it to the
1046 * buffer, and try again.
1047 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001048 memset(setp, 0, howmany(active_state->connection_in + 1,
1049 NFDBITS) * sizeof(fd_mask));
1050 FD_SET(active_state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001051
Darren Tuckerf7288d72009-06-21 18:12:20 +10001052 if (active_state->packet_timeout_ms > 0) {
1053 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001054 timeoutp = &timeout;
1055 }
Damien Miller95def091999-11-25 00:26:21 +11001056 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001057 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001058 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001059 ms_to_timeval(&timeout, ms_remain);
1060 gettimeofday(&start, NULL);
1061 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001062 if ((ret = select(active_state->connection_in + 1, setp,
1063 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001064 break;
Darren Tuckerc5564e12009-06-21 18:53:53 +10001065 if (errno != EAGAIN && errno != EINTR)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001066 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001067 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001068 continue;
1069 ms_subtract_diff(&start, &ms_remain);
1070 if (ms_remain <= 0) {
1071 ret = 0;
1072 break;
1073 }
1074 }
1075 if (ret == 0) {
1076 logit("Connection to %.200s timed out while "
1077 "waiting to read", get_remote_ipaddr());
1078 cleanup_exit(255);
1079 }
Damien Miller95def091999-11-25 00:26:21 +11001080 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001081 do {
1082 cont = 0;
1083 len = roaming_read(active_state->connection_in, buf,
1084 sizeof(buf), &cont);
1085 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001086 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001087 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +10001088 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001089 }
Damien Miller95def091999-11-25 00:26:21 +11001090 if (len < 0)
1091 fatal("Read from socket failed: %.100s", strerror(errno));
1092 /* Append it to the buffer. */
1093 packet_process_incoming(buf, len);
1094 }
1095 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001096}
1097
Damien Miller278f9072001-12-21 15:00:19 +11001098int
Damien Millerdff50992002-01-22 23:16:32 +11001099packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +11001100{
Damien Millerdff50992002-01-22 23:16:32 +11001101 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001102}
1103
Damien Miller5428f641999-11-25 11:54:57 +11001104/*
1105 * Waits until a packet has been received, verifies that its type matches
1106 * that given, and gives a fatal error and exits if there is a mismatch.
1107 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001108
1109void
Damien Millerdff50992002-01-22 23:16:32 +11001110packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001111{
Damien Miller95def091999-11-25 00:26:21 +11001112 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113
Damien Millerdff50992002-01-22 23:16:32 +11001114 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001115 if (type != expected_type)
1116 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001117 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001118}
1119
1120/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001121 * packet_process_incoming. If so, reads the packet; otherwise returns
1122 * SSH_MSG_NONE. This does not wait for data from the connection.
1123 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001124 * SSH_MSG_DISCONNECT is handled specially here. Also,
1125 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1126 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128
Ben Lindstrombba81212001-06-25 05:01:22 +00001129static int
Damien Millerdff50992002-01-22 23:16:32 +11001130packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001132 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001133 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001134 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135
Damien Miller95def091999-11-25 00:26:21 +11001136 /* Check if input size is less than minimum packet size. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001137 if (buffer_len(&active_state->input) < 4 + 8)
Damien Miller95def091999-11-25 00:26:21 +11001138 return SSH_MSG_NONE;
1139 /* Get length of incoming packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001140 cp = buffer_ptr(&active_state->input);
Damien Miller3f941882006-03-31 23:13:02 +11001141 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001142 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001143 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +11001144 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145
Damien Miller95def091999-11-25 00:26:21 +11001146 /* Check if the packet has been entirely received. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001147 if (buffer_len(&active_state->input) < 4 + padded_len)
Damien Miller95def091999-11-25 00:26:21 +11001148 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149
Damien Miller95def091999-11-25 00:26:21 +11001150 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001151
Damien Miller95def091999-11-25 00:26:21 +11001152 /* Consume packet length. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001153 buffer_consume(&active_state->input, 4);
Damien Miller95def091999-11-25 00:26:21 +11001154
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001155 /*
1156 * Cryptographic attack detector for ssh
1157 * (C)1998 CORE-SDI, Buenos Aires Argentina
1158 * Ariel Futoransky(futo@core-sdi.com)
1159 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001160 if (!active_state->receive_context.plaintext) {
1161 switch (detect_attack(buffer_ptr(&active_state->input),
1162 padded_len)) {
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001163 case DEATTACK_DETECTED:
1164 packet_disconnect("crc32 compensation attack: "
1165 "network attack detected");
1166 case DEATTACK_DOS_DETECTED:
1167 packet_disconnect("deattack denial of "
1168 "service detected");
1169 }
1170 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001171
1172 /* Decrypt data to incoming_packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001173 buffer_clear(&active_state->incoming_packet);
1174 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1175 cipher_crypt(&active_state->receive_context, cp,
1176 buffer_ptr(&active_state->input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001177
Darren Tuckerf7288d72009-06-21 18:12:20 +10001178 buffer_consume(&active_state->input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001179
1180#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001181 fprintf(stderr, "read_poll plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001182 buffer_dump(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184
Damien Miller95def091999-11-25 00:26:21 +11001185 /* Compute packet checksum. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001186 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1187 buffer_len(&active_state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188
Damien Miller95def091999-11-25 00:26:21 +11001189 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001190 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001191
Damien Miller95def091999-11-25 00:26:21 +11001192 /* Test check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001193 if (len != buffer_len(&active_state->incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001194 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001195 len, buffer_len(&active_state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001196
Darren Tuckerf7288d72009-06-21 18:12:20 +10001197 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001198 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001199 if (checksum != stored_checksum)
1200 packet_disconnect("Corrupted check bytes on input.");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001201 buffer_consume_end(&active_state->incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001202
Darren Tuckerf7288d72009-06-21 18:12:20 +10001203 if (active_state->packet_compression) {
1204 buffer_clear(&active_state->compression_buffer);
1205 buffer_uncompress(&active_state->incoming_packet,
1206 &active_state->compression_buffer);
1207 buffer_clear(&active_state->incoming_packet);
1208 buffer_append(&active_state->incoming_packet,
1209 buffer_ptr(&active_state->compression_buffer),
1210 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001211 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001212 active_state->p_read.packets++;
1213 active_state->p_read.bytes += padded_len + 4;
1214 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001215 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1216 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001217 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218}
Damien Miller95def091999-11-25 00:26:21 +11001219
Ben Lindstrombba81212001-06-25 05:01:22 +00001220static int
Damien Millerdff50992002-01-22 23:16:32 +11001221packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001222{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001223 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001224 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001225 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001226 Enc *enc = NULL;
1227 Mac *mac = NULL;
1228 Comp *comp = NULL;
1229
Darren Tuckerf7288d72009-06-21 18:12:20 +10001230 if (active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001231 return SSH_MSG_NONE;
1232
Darren Tuckerf7288d72009-06-21 18:12:20 +10001233 if (active_state->newkeys[MODE_IN] != NULL) {
1234 enc = &active_state->newkeys[MODE_IN]->enc;
1235 mac = &active_state->newkeys[MODE_IN]->mac;
1236 comp = &active_state->newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001237 }
1238 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001239 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001240
Darren Tuckerf7288d72009-06-21 18:12:20 +10001241 if (active_state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001242 /*
1243 * check if input size is less than the cipher block size,
1244 * decrypt first block and extract length of incoming packet
1245 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001246 if (buffer_len(&active_state->input) < block_size)
Damien Miller33b13562000-04-04 14:38:59 +10001247 return SSH_MSG_NONE;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001248 buffer_clear(&active_state->incoming_packet);
1249 cp = buffer_append_space(&active_state->incoming_packet,
Damien Miller33b13562000-04-04 14:38:59 +10001250 block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001251 cipher_crypt(&active_state->receive_context, cp,
1252 buffer_ptr(&active_state->input), block_size);
1253 cp = buffer_ptr(&active_state->incoming_packet);
1254 active_state->packlen = get_u32(cp);
1255 if (active_state->packlen < 1 + 4 ||
1256 active_state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001257#ifdef PACKET_DEBUG
Darren Tuckerf7288d72009-06-21 18:12:20 +10001258 buffer_dump(&active_state->incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001259#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001260 logit("Bad packet length %u.", active_state->packlen);
1261 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001262 PACKET_MAX_SIZE);
1263 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001264 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001265 DBG(debug("input: packet len %u", active_state->packlen+4));
1266 buffer_consume(&active_state->input, block_size);
Damien Miller33b13562000-04-04 14:38:59 +10001267 }
1268 /* we have a partial packet of block_size bytes */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001269 need = 4 + active_state->packlen - block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001270 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1271 need, maclen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001272 if (need % block_size != 0) {
1273 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001274 need, block_size, need % block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001275 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001276 PACKET_MAX_SIZE - block_size);
1277 return SSH_MSG_NONE;
Darren Tucker99d11a32008-12-01 21:40:48 +11001278 }
Damien Miller33b13562000-04-04 14:38:59 +10001279 /*
1280 * check if the entire packet has been received and
1281 * decrypt into incoming_packet
1282 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001283 if (buffer_len(&active_state->input) < need + maclen)
Damien Miller33b13562000-04-04 14:38:59 +10001284 return SSH_MSG_NONE;
1285#ifdef PACKET_DEBUG
1286 fprintf(stderr, "read_poll enc/full: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001287 buffer_dump(&active_state->input);
Damien Miller33b13562000-04-04 14:38:59 +10001288#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001289 cp = buffer_append_space(&active_state->incoming_packet, need);
1290 cipher_crypt(&active_state->receive_context, cp,
1291 buffer_ptr(&active_state->input), need);
1292 buffer_consume(&active_state->input, need);
Damien Miller33b13562000-04-04 14:38:59 +10001293 /*
1294 * compute MAC over seqnr and packet,
1295 * increment sequence number for incoming packet
1296 */
Damien Miller4af51302000-04-16 11:18:38 +10001297 if (mac && mac->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001298 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1299 buffer_ptr(&active_state->incoming_packet),
1300 buffer_len(&active_state->incoming_packet));
1301 if (memcmp(macbuf, buffer_ptr(&active_state->input),
1302 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001303 logit("Corrupted MAC on input.");
1304 if (need > PACKET_MAX_SIZE)
1305 fatal("internal error need %d", need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001306 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001307 PACKET_MAX_SIZE - need);
1308 return SSH_MSG_NONE;
1309 }
1310
Darren Tuckerf7288d72009-06-21 18:12:20 +10001311 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1312 buffer_consume(&active_state->input, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +10001313 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001314 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001315 if (seqnr_p != NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001316 *seqnr_p = active_state->p_read.seqnr;
1317 if (++active_state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001318 logit("incoming seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001319 if (++active_state->p_read.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +10001320 if (!(datafellows & SSH_BUG_NOREKEY))
1321 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001322 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1323 active_state->p_read.bytes += active_state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001324
1325 /* get padlen */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001326 cp = buffer_ptr(&active_state->incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001327 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001328 DBG(debug("input: padlen %d", padlen));
1329 if (padlen < 4)
1330 packet_disconnect("Corrupted padlen %d on input.", padlen);
1331
1332 /* skip packet size + padlen, discard padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001333 buffer_consume(&active_state->incoming_packet, 4 + 1);
1334 buffer_consume_end(&active_state->incoming_packet, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001335
Darren Tuckerf7288d72009-06-21 18:12:20 +10001336 DBG(debug("input: len before de-compress %d",
1337 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001338 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001339 buffer_clear(&active_state->compression_buffer);
1340 buffer_uncompress(&active_state->incoming_packet,
1341 &active_state->compression_buffer);
1342 buffer_clear(&active_state->incoming_packet);
1343 buffer_append(&active_state->incoming_packet,
1344 buffer_ptr(&active_state->compression_buffer),
1345 buffer_len(&active_state->compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001346 DBG(debug("input: len after de-compress %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001347 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001348 }
1349 /*
1350 * get packet type, implies consume.
1351 * return length of payload (without type field)
1352 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001353 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001354 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1355 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001356 if (type == SSH2_MSG_NEWKEYS)
1357 set_newkeys(MODE_IN);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001358 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1359 !active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +10001360 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001361#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001362 fprintf(stderr, "read/plain[%d]:\r\n", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001363 buffer_dump(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001364#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001365 /* reset for next packet */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001366 active_state->packlen = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001367 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001368}
1369
1370int
Damien Millerdff50992002-01-22 23:16:32 +11001371packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001372{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001373 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001374 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001375 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001376
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001377 for (;;) {
1378 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001379 type = packet_read_poll2(seqnr_p);
Darren Tucker136e56f2008-06-08 12:49:30 +10001380 if (type) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001381 active_state->keep_alive_timeouts = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001382 DBG(debug("received packet type %d", type));
Darren Tucker136e56f2008-06-08 12:49:30 +10001383 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001384 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001385 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001386 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001387 break;
Damien Miller33b13562000-04-04 14:38:59 +10001388 case SSH2_MSG_DEBUG:
1389 packet_get_char();
1390 msg = packet_get_string(NULL);
1391 debug("Remote: %.900s", msg);
1392 xfree(msg);
1393 msg = packet_get_string(NULL);
1394 xfree(msg);
1395 break;
1396 case SSH2_MSG_DISCONNECT:
1397 reason = packet_get_int();
1398 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001399 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001400 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001401 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001402 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001403 break;
Damien Miller659811f2002-01-22 23:23:11 +11001404 case SSH2_MSG_UNIMPLEMENTED:
1405 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001406 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1407 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001408 break;
Damien Miller33b13562000-04-04 14:38:59 +10001409 default:
1410 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001411 }
Damien Miller33b13562000-04-04 14:38:59 +10001412 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001413 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001414 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001415 case SSH_MSG_IGNORE:
1416 break;
1417 case SSH_MSG_DEBUG:
1418 msg = packet_get_string(NULL);
1419 debug("Remote: %.900s", msg);
1420 xfree(msg);
1421 break;
1422 case SSH_MSG_DISCONNECT:
1423 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001424 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001425 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001426 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001427 break;
1428 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001429 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001430 DBG(debug("received packet type %d", type));
1431 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001432 }
Damien Miller33b13562000-04-04 14:38:59 +10001433 }
1434 }
1435}
1436
Damien Miller278f9072001-12-21 15:00:19 +11001437int
Damien Millerdff50992002-01-22 23:16:32 +11001438packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001439{
Damien Millerdff50992002-01-22 23:16:32 +11001440 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001441}
1442
Damien Miller5428f641999-11-25 11:54:57 +11001443/*
1444 * Buffers the given amount of input characters. This is intended to be used
1445 * together with packet_read_poll.
1446 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447
1448void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001449packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001450{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001451 if (active_state->packet_discard) {
1452 active_state->keep_alive_timeouts = 0; /* ?? */
1453 if (len >= active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001454 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001455 active_state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001456 return;
1457 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001458 buffer_append(&active_state->input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001459}
1460
1461/* Returns a character from the packet. */
1462
Ben Lindstrom46c16222000-12-22 01:43:59 +00001463u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001464packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001465{
Damien Miller95def091999-11-25 00:26:21 +11001466 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001467
Darren Tuckerf7288d72009-06-21 18:12:20 +10001468 buffer_get(&active_state->incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001469 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470}
1471
1472/* Returns an integer from the packet data. */
1473
Ben Lindstrom46c16222000-12-22 01:43:59 +00001474u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001475packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001477 return buffer_get_int(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001478}
1479
Darren Tucker761c3892009-06-21 18:16:26 +10001480/* Returns an 64 bit integer from the packet data. */
1481
1482u_int64_t
1483packet_get_int64(void)
1484{
1485 return buffer_get_int64(&active_state->incoming_packet);
1486}
1487
Damien Miller5428f641999-11-25 11:54:57 +11001488/*
1489 * Returns an arbitrary precision integer from the packet data. The integer
1490 * must have been initialized before this call.
1491 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492
1493void
Damien Millerd432ccf2002-01-22 23:14:44 +11001494packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001496 buffer_get_bignum(&active_state->incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497}
1498
Damien Miller33b13562000-04-04 14:38:59 +10001499void
Damien Millerd432ccf2002-01-22 23:14:44 +11001500packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001501{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001502 buffer_get_bignum2(&active_state->incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001503}
1504
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001505void *
Damien Millereccb9de2005-06-17 12:59:34 +10001506packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001507{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001508 u_int bytes = buffer_len(&active_state->incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001509
Damien Miller33b13562000-04-04 14:38:59 +10001510 if (length_ptr != NULL)
1511 *length_ptr = bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001512 return buffer_ptr(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001513}
1514
Damien Miller4af51302000-04-16 11:18:38 +10001515int
1516packet_remaining(void)
1517{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001518 return buffer_len(&active_state->incoming_packet);
Damien Miller4af51302000-04-16 11:18:38 +10001519}
1520
Damien Miller5428f641999-11-25 11:54:57 +11001521/*
1522 * Returns a string from the packet data. The string is allocated using
1523 * xmalloc; it is the responsibility of the calling program to free it when
1524 * no longer needed. The length_ptr argument may be NULL, or point to an
1525 * integer into which the length of the string is stored.
1526 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001527
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001528void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001529packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001530{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001531 return buffer_get_string(&active_state->incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001532}
1533
Damien Millerdb255ca2008-05-19 14:59:37 +10001534void *
1535packet_get_string_ptr(u_int *length_ptr)
1536{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001537 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
Damien Millerdb255ca2008-05-19 14:59:37 +10001538}
1539
Damien Miller5428f641999-11-25 11:54:57 +11001540/*
1541 * Sends a diagnostic message from the server to the client. This message
1542 * can be sent at any time (but not while constructing another message). The
1543 * message is printed immediately, but only if the client is being executed
1544 * in verbose mode. These messages are primarily intended to ease debugging
1545 * authentication problems. The length of the formatted message must not
1546 * exceed 1024 bytes. This will automatically call packet_write_wait.
1547 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001548
1549void
Damien Miller95def091999-11-25 00:26:21 +11001550packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001551{
Damien Miller95def091999-11-25 00:26:21 +11001552 char buf[1024];
1553 va_list args;
1554
Ben Lindstroma14ee472000-12-07 01:24:58 +00001555 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1556 return;
1557
Damien Miller95def091999-11-25 00:26:21 +11001558 va_start(args, fmt);
1559 vsnprintf(buf, sizeof(buf), fmt, args);
1560 va_end(args);
1561
Damien Miller7c8af4f2000-05-01 08:24:07 +10001562 if (compat20) {
1563 packet_start(SSH2_MSG_DEBUG);
1564 packet_put_char(0); /* bool: always display */
1565 packet_put_cstring(buf);
1566 packet_put_cstring("");
1567 } else {
1568 packet_start(SSH_MSG_DEBUG);
1569 packet_put_cstring(buf);
1570 }
Damien Miller95def091999-11-25 00:26:21 +11001571 packet_send();
1572 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001573}
1574
Damien Miller5428f641999-11-25 11:54:57 +11001575/*
1576 * Logs the error plus constructs and sends a disconnect packet, closes the
1577 * connection, and exits. This function never returns. The error message
1578 * should not contain a newline. The length of the formatted message must
1579 * not exceed 1024 bytes.
1580 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001581
1582void
Damien Miller95def091999-11-25 00:26:21 +11001583packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001584{
Damien Miller95def091999-11-25 00:26:21 +11001585 char buf[1024];
1586 va_list args;
1587 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001588
Damien Miller95def091999-11-25 00:26:21 +11001589 if (disconnecting) /* Guard against recursive invocations. */
1590 fatal("packet_disconnect called recursively.");
1591 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001592
Damien Miller5428f641999-11-25 11:54:57 +11001593 /*
1594 * Format the message. Note that the caller must make sure the
1595 * message is of limited size.
1596 */
Damien Miller95def091999-11-25 00:26:21 +11001597 va_start(args, fmt);
1598 vsnprintf(buf, sizeof(buf), fmt, args);
1599 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001600
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001601 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001602 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001603
Damien Miller95def091999-11-25 00:26:21 +11001604 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001605 if (compat20) {
1606 packet_start(SSH2_MSG_DISCONNECT);
1607 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1608 packet_put_cstring(buf);
1609 packet_put_cstring("");
1610 } else {
1611 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001612 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001613 }
Damien Miller95def091999-11-25 00:26:21 +11001614 packet_send();
1615 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001616
Damien Miller95def091999-11-25 00:26:21 +11001617 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001618 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001619
Damien Miller95def091999-11-25 00:26:21 +11001620 /* Close the connection. */
1621 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001622 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001623}
1624
Damien Miller5428f641999-11-25 11:54:57 +11001625/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001626
1627void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001628packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001629{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001630 int len = buffer_len(&active_state->output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001631 int cont;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001632
Damien Miller95def091999-11-25 00:26:21 +11001633 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001634 cont = 0;
1635 len = roaming_write(active_state->connection_out,
1636 buffer_ptr(&active_state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001637 if (len == -1) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001638 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001639 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001640 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001641 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001642 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001643 fatal("Write connection closed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001644 buffer_consume(&active_state->output, len);
Damien Miller95def091999-11-25 00:26:21 +11001645 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001646}
1647
Damien Miller5428f641999-11-25 11:54:57 +11001648/*
1649 * Calls packet_write_poll repeatedly until all pending output data has been
1650 * written.
1651 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001652
1653void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001654packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001655{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001656 fd_set *setp;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001657 int ret, ms_remain;
1658 struct timeval start, timeout, *timeoutp = NULL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001659
Darren Tuckerf7288d72009-06-21 18:12:20 +10001660 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1661 NFDBITS), sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001662 packet_write_poll();
1663 while (packet_have_data_to_write()) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001664 memset(setp, 0, howmany(active_state->connection_out + 1,
1665 NFDBITS) * sizeof(fd_mask));
1666 FD_SET(active_state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001667
Darren Tuckerf7288d72009-06-21 18:12:20 +10001668 if (active_state->packet_timeout_ms > 0) {
1669 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001670 timeoutp = &timeout;
1671 }
1672 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001673 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001674 ms_to_timeval(&timeout, ms_remain);
1675 gettimeofday(&start, NULL);
1676 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001677 if ((ret = select(active_state->connection_out + 1,
1678 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001679 break;
Darren Tuckerc5564e12009-06-21 18:53:53 +10001680 if (errno != EAGAIN && errno != EINTR)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001681 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001682 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001683 continue;
1684 ms_subtract_diff(&start, &ms_remain);
1685 if (ms_remain <= 0) {
1686 ret = 0;
1687 break;
1688 }
1689 }
1690 if (ret == 0) {
1691 logit("Connection to %.200s timed out while "
1692 "waiting to write", get_remote_ipaddr());
1693 cleanup_exit(255);
1694 }
Damien Miller95def091999-11-25 00:26:21 +11001695 packet_write_poll();
1696 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001697 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001698}
1699
1700/* Returns true if there is buffered data to write to the connection. */
1701
1702int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001703packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001704{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001705 return buffer_len(&active_state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001706}
1707
1708/* Returns true if there is not too much data to write to the connection. */
1709
1710int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001711packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001712{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001713 if (active_state->interactive_mode)
1714 return buffer_len(&active_state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11001715 else
Darren Tuckerf7288d72009-06-21 18:12:20 +10001716 return buffer_len(&active_state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001717}
1718
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001719static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001720packet_set_tos(int interactive)
1721{
Damien Miller5924ceb2003-11-22 15:02:42 +11001722#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001723 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1724
1725 if (!packet_connection_is_on_socket() ||
1726 !packet_connection_is_ipv4())
1727 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001728 if (setsockopt(active_state->connection_in, IPPROTO_IP, IP_TOS, &tos,
Ben Lindstroma7433982002-12-23 02:41:41 +00001729 sizeof(tos)) < 0)
1730 error("setsockopt IP_TOS %d: %.100s:",
1731 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001732#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001733}
Ben Lindstroma7433982002-12-23 02:41:41 +00001734
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001735/* Informs that the current session is interactive. Sets IP flags for that. */
1736
1737void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001738packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001739{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001740 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001741
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001742 if (called)
1743 return;
1744 called = 1;
1745
Damien Miller95def091999-11-25 00:26:21 +11001746 /* Record that we are in interactive mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001747 active_state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001748
Damien Miller34132e52000-01-14 15:45:46 +11001749 /* Only set socket options if using a socket. */
1750 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001751 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001752 set_nodelay(active_state->connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001753 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001754}
1755
1756/* Returns true if the current connection is interactive. */
1757
1758int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001759packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001760{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001761 return active_state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001762}
Damien Miller6162d121999-11-21 13:23:52 +11001763
Darren Tucker1f8311c2004-05-13 16:39:33 +10001764int
Darren Tucker502d3842003-06-28 12:38:01 +10001765packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001766{
Damien Miller95def091999-11-25 00:26:21 +11001767 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001768
Damien Miller95def091999-11-25 00:26:21 +11001769 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001770 logit("packet_set_maxsize: called twice: old %d new %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001771 active_state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001772 return -1;
1773 }
1774 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001775 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001776 return -1;
1777 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001778 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001779 debug("packet_set_maxsize: setting to %d", s);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001780 active_state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11001781 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001782}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001783
Darren Tuckerf7288d72009-06-21 18:12:20 +10001784int
1785packet_inc_alive_timeouts(void)
1786{
1787 return ++active_state->keep_alive_timeouts;
1788}
1789
1790void
1791packet_set_alive_timeouts(int ka)
1792{
1793 active_state->keep_alive_timeouts = ka;
1794}
1795
1796u_int
1797packet_get_maxsize(void)
1798{
1799 return active_state->max_packet_size;
1800}
1801
Damien Miller9f643902001-11-12 11:02:52 +11001802/* roundup current message to pad bytes */
1803void
1804packet_add_padding(u_char pad)
1805{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001806 active_state->extra_pad = pad;
Damien Miller9f643902001-11-12 11:02:52 +11001807}
1808
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001809/*
1810 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001811 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001812 * byte SSH_MSG_IGNORE
1813 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001814 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001815 * All implementations MUST understand (and ignore) this message at any
1816 * time (after receiving the protocol version). No implementation is
1817 * required to send them. This message can be used as an additional
1818 * protection measure against advanced traffic analysis techniques.
1819 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001820void
1821packet_send_ignore(int nbytes)
1822{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001823 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001824 int i;
1825
1826 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001827 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001828 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001829 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001830 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001831 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001832 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001833 }
1834}
Damien Millera5539d22003-04-09 20:50:06 +10001835
Darren Tucker1f8311c2004-05-13 16:39:33 +10001836#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001837int
1838packet_need_rekeying(void)
1839{
1840 if (datafellows & SSH_BUG_NOREKEY)
1841 return 0;
1842 return
Darren Tuckerf7288d72009-06-21 18:12:20 +10001843 (active_state->p_send.packets > MAX_PACKETS) ||
1844 (active_state->p_read.packets > MAX_PACKETS) ||
1845 (active_state->max_blocks_out &&
1846 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1847 (active_state->max_blocks_in &&
1848 (active_state->p_read.blocks > active_state->max_blocks_in));
Damien Millera5539d22003-04-09 20:50:06 +10001849}
1850
1851void
1852packet_set_rekey_limit(u_int32_t bytes)
1853{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001854 active_state->rekey_limit = bytes;
Damien Millera5539d22003-04-09 20:50:06 +10001855}
Damien Miller9786e6e2005-07-26 21:54:56 +10001856
1857void
1858packet_set_server(void)
1859{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001860 active_state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001861}
1862
1863void
1864packet_set_authenticated(void)
1865{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001866 active_state->after_authentication = 1;
1867}
1868
1869void *
1870packet_get_input(void)
1871{
1872 return (void *)&active_state->input;
1873}
1874
1875void *
1876packet_get_output(void)
1877{
1878 return (void *)&active_state->output;
1879}
1880
1881void *
1882packet_get_newkeys(int mode)
1883{
1884 return (void *)active_state->newkeys[mode];
Damien Miller9786e6e2005-07-26 21:54:56 +10001885}