blob: cecab82e90e8af44d68b86735a7d5b73e902af05 [file] [log] [blame]
Darren Tucker761c3892009-06-21 18:16:26 +10001/* $OpenBSD: packet.c,v 1.162 2009/05/27 06:36:07 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"
Damien Miller33b13562000-04-04 14:38:59 +100080
81#ifdef PACKET_DEBUG
82#define DBG(x) x
83#else
84#define DBG(x)
85#endif
86
Damien Miller13ae44c2009-01-28 16:38:41 +110087#define PACKET_MAX_SIZE (256 * 1024)
88
Darren Tuckerf7288d72009-06-21 18:12:20 +100089struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100090 u_int32_t seqnr;
91 u_int32_t packets;
92 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +100093 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +100094};
Damien Miller13ae44c2009-01-28 16:38:41 +110095
Damien Millera5539d22003-04-09 20:50:06 +100096struct packet {
97 TAILQ_ENTRY(packet) next;
98 u_char type;
99 Buffer payload;
100};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000101
102struct session_state {
103 /*
104 * This variable contains the file descriptors used for
105 * communicating with the other side. connection_in is used for
106 * reading; connection_out for writing. These can be the same
107 * descriptor, in which case it is assumed to be a socket.
108 */
109 int connection_in;
110 int connection_out;
111
112 /* Protocol flags for the remote side. */
113 u_int remote_protocol_flags;
114
115 /* Encryption context for receiving data. Only used for decryption. */
116 CipherContext receive_context;
117
118 /* Encryption context for sending data. Only used for encryption. */
119 CipherContext send_context;
120
121 /* Buffer for raw input data from the socket. */
122 Buffer input;
123
124 /* Buffer for raw output data going to the socket. */
125 Buffer output;
126
127 /* Buffer for the partial outgoing packet being constructed. */
128 Buffer outgoing_packet;
129
130 /* Buffer for the incoming packet currently being processed. */
131 Buffer incoming_packet;
132
133 /* Scratch buffer for packet compression/decompression. */
134 Buffer compression_buffer;
135 int compression_buffer_ready;
136
137 /*
138 * Flag indicating whether packet compression/decompression is
139 * enabled.
140 */
141 int packet_compression;
142
143 /* default maximum packet size */
144 u_int max_packet_size;
145
146 /* Flag indicating whether this module has been initialized. */
147 int initialized;
148
149 /* Set to true if the connection is interactive. */
150 int interactive_mode;
151
152 /* Set to true if we are the server side. */
153 int server_side;
154
155 /* Set to true if we are authenticated. */
156 int after_authentication;
157
158 int keep_alive_timeouts;
159
160 /* The maximum time that we will wait to send or receive a packet */
161 int packet_timeout_ms;
162
163 /* Session key information for Encryption and MAC */
164 Newkeys *newkeys[MODE_MAX];
165 struct packet_state p_read, p_send;
166
167 u_int64_t max_blocks_in, max_blocks_out;
168 u_int32_t rekey_limit;
169
170 /* Session key for protocol v1 */
171 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
172 u_int ssh1_keylen;
173
174 /* roundup current message to extra_pad bytes */
175 u_char extra_pad;
176
177 /* XXX discard incoming data after MAC error */
178 u_int packet_discard;
179 Mac *packet_discard_mac;
180
181 /* Used in packet_read_poll2() */
182 u_int packlen;
183
184 TAILQ_HEAD(, packet) outgoing;
185};
186
187static struct session_state *active_state;
188
189static struct session_state *
190alloc_session_state()
191{
192 struct session_state *s = xcalloc(1, sizeof(*s));
193
194 s->connection_in = -1;
195 s->connection_out = -1;
196 s->max_packet_size = 32768;
197 s->packet_timeout_ms = -1;
198 return s;
199}
Damien Millera5539d22003-04-09 20:50:06 +1000200
Damien Miller5428f641999-11-25 11:54:57 +1100201/*
202 * Sets the descriptors used for communication. Disables encryption until
203 * packet_set_encryption_key is called.
204 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205void
206packet_set_connection(int fd_in, int fd_out)
207{
Damien Miller874d77b2000-10-14 16:23:11 +1100208 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000209
Damien Miller874d77b2000-10-14 16:23:11 +1100210 if (none == NULL)
211 fatal("packet_set_connection: cannot load cipher 'none'");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000212 if (active_state == NULL)
213 active_state = alloc_session_state();
214 active_state->connection_in = fd_in;
215 active_state->connection_out = fd_out;
216 cipher_init(&active_state->send_context, none, (const u_char *)"",
Darren Tucker1f8311c2004-05-13 16:39:33 +1000217 0, NULL, 0, CIPHER_ENCRYPT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000218 cipher_init(&active_state->receive_context, none, (const u_char *)"",
Darren Tucker1f8311c2004-05-13 16:39:33 +1000219 0, NULL, 0, CIPHER_DECRYPT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000220 active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL;
221 if (!active_state->initialized) {
222 active_state->initialized = 1;
223 buffer_init(&active_state->input);
224 buffer_init(&active_state->output);
225 buffer_init(&active_state->outgoing_packet);
226 buffer_init(&active_state->incoming_packet);
227 TAILQ_INIT(&active_state->outgoing);
228 active_state->p_send.packets = active_state->p_read.packets = 0;
Damien Miller95def091999-11-25 00:26:21 +1100229 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230}
231
Darren Tucker3fc464e2008-06-13 06:42:45 +1000232void
233packet_set_timeout(int timeout, int count)
234{
235 if (timeout == 0 || count == 0) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000236 active_state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000237 return;
238 }
239 if ((INT_MAX / 1000) / count < timeout)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000240 active_state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000241 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000242 active_state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000243}
244
Damien Miller13ae44c2009-01-28 16:38:41 +1100245static void
246packet_stop_discard(void)
247{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000248 if (active_state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100249 char buf[1024];
250
251 memset(buf, 'a', sizeof(buf));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000252 while (buffer_len(&active_state->incoming_packet) <
253 PACKET_MAX_SIZE)
254 buffer_append(&active_state->incoming_packet, buf,
255 sizeof(buf));
256 (void) mac_compute(active_state->packet_discard_mac,
257 active_state->p_read.seqnr,
258 buffer_ptr(&active_state->incoming_packet),
Damien Miller13ae44c2009-01-28 16:38:41 +1100259 PACKET_MAX_SIZE);
260 }
261 logit("Finished discarding for %.200s", get_remote_ipaddr());
262 cleanup_exit(255);
263}
264
265static void
266packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
267{
Damien Miller61433be2009-02-14 16:35:01 +1100268 if (enc == NULL || !cipher_is_cbc(enc->cipher))
Damien Miller13ae44c2009-01-28 16:38:41 +1100269 packet_disconnect("Packet corrupt");
270 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000271 active_state->packet_discard_mac = mac;
272 if (buffer_len(&active_state->input) >= discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100273 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +1000274 active_state->packet_discard = discard -
275 buffer_len(&active_state->input);
Damien Miller13ae44c2009-01-28 16:38:41 +1100276}
277
Damien Miller34132e52000-01-14 15:45:46 +1100278/* Returns 1 if remote host is connected via socket, 0 if not. */
279
280int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000281packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100282{
283 struct sockaddr_storage from, to;
284 socklen_t fromlen, tolen;
285
286 /* filedescriptors in and out are the same, so it's a socket */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000287 if (active_state->connection_in == active_state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100288 return 1;
289 fromlen = sizeof(from);
290 memset(&from, 0, sizeof(from));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000291 if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
292 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100293 return 0;
294 tolen = sizeof(to);
295 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000296 if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
297 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100298 return 0;
299 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
300 return 0;
301 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
302 return 0;
303 return 1;
304}
305
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000306/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000307 * Exports an IV from the CipherContext required to export the key
308 * state back from the unprivileged child to the privileged parent
309 * process.
310 */
311
312void
313packet_get_keyiv(int mode, u_char *iv, u_int len)
314{
315 CipherContext *cc;
316
317 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000318 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000319 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000320 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000321
322 cipher_get_keyiv(cc, iv, len);
323}
324
325int
326packet_get_keycontext(int mode, u_char *dat)
327{
328 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000329
Ben Lindstromf6027d32002-03-22 01:42:04 +0000330 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000331 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000332 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000333 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000334
335 return (cipher_get_keycontext(cc, dat));
336}
337
338void
339packet_set_keycontext(int mode, u_char *dat)
340{
341 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000342
Ben Lindstromf6027d32002-03-22 01:42:04 +0000343 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000344 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000345 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000346 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000347
348 cipher_set_keycontext(cc, dat);
349}
350
351int
352packet_get_keyiv_len(int mode)
353{
354 CipherContext *cc;
355
356 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000357 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000358 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000359 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000360
361 return (cipher_get_keyiv_len(cc));
362}
Damien Miller4f7becb2006-03-26 14:10:14 +1100363
Ben Lindstromf6027d32002-03-22 01:42:04 +0000364void
365packet_set_iv(int mode, u_char *dat)
366{
367 CipherContext *cc;
368
369 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000370 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000371 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000372 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000373
374 cipher_set_keyiv(cc, dat);
375}
Damien Miller4f7becb2006-03-26 14:10:14 +1100376
Ben Lindstromf6027d32002-03-22 01:42:04 +0000377int
Damien Miller2b92d322003-06-11 22:05:06 +1000378packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000379{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000380 return (cipher_get_number(active_state->receive_context.cipher));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000381}
382
Damien Millera5539d22003-04-09 20:50:06 +1000383void
Damien Millerb61f3fc2008-07-11 17:36:48 +1000384packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets,
385 u_int64_t *bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000386{
Damien Millera5539d22003-04-09 20:50:06 +1000387 struct packet_state *state;
388
Darren Tuckerf7288d72009-06-21 18:12:20 +1000389 state = (mode == MODE_IN) ?
390 &active_state->p_read : &active_state->p_send;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000391 if (seqnr)
392 *seqnr = state->seqnr;
393 if (blocks)
394 *blocks = state->blocks;
395 if (packets)
396 *packets = state->packets;
397 if (bytes)
398 *bytes = state->bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000399}
400
401void
Damien Millerb61f3fc2008-07-11 17:36:48 +1000402packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
403 u_int64_t bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000404{
Damien Millera5539d22003-04-09 20:50:06 +1000405 struct packet_state *state;
406
Darren Tuckerf7288d72009-06-21 18:12:20 +1000407 state = (mode == MODE_IN) ?
408 &active_state->p_read : &active_state->p_send;
Damien Millera5539d22003-04-09 20:50:06 +1000409 state->seqnr = seqnr;
410 state->blocks = blocks;
411 state->packets = packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000412 state->bytes = bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000413}
414
Damien Miller34132e52000-01-14 15:45:46 +1100415/* returns 1 if connection is via ipv4 */
416
417int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000418packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100419{
420 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100421 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100422
423 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000424 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
425 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100426 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000427 if (to.ss_family == AF_INET)
428 return 1;
429#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100430 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000431 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
432 return 1;
433#endif
434 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100435}
436
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437/* Sets the connection into non-blocking mode. */
438
439void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000440packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441{
Damien Miller95def091999-11-25 00:26:21 +1100442 /* Set the socket into non-blocking mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000443 set_nonblock(active_state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444
Darren Tuckerf7288d72009-06-21 18:12:20 +1000445 if (active_state->connection_out != active_state->connection_in)
446 set_nonblock(active_state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447}
448
449/* Returns the socket used for reading. */
450
451int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000452packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000454 return active_state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455}
456
457/* Returns the descriptor used for writing. */
458
459int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000460packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000462 return active_state->connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463}
464
465/* Closes the connection and clears and frees internal data structures. */
466
467void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000468packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000470 if (!active_state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100471 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000472 active_state->initialized = 0;
473 if (active_state->connection_in == active_state->connection_out) {
474 shutdown(active_state->connection_out, SHUT_RDWR);
475 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100476 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000477 close(active_state->connection_in);
478 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100479 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000480 buffer_free(&active_state->input);
481 buffer_free(&active_state->output);
482 buffer_free(&active_state->outgoing_packet);
483 buffer_free(&active_state->incoming_packet);
484 if (active_state->compression_buffer_ready) {
485 buffer_free(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100486 buffer_compress_uninit();
487 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000488 cipher_cleanup(&active_state->send_context);
489 cipher_cleanup(&active_state->receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490}
491
492/* Sets remote side protocol flags. */
493
494void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000495packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000497 active_state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498}
499
500/* Returns the remote protocol flags set earlier by the above function. */
501
Ben Lindstrom46c16222000-12-22 01:43:59 +0000502u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000503packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000505 return active_state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506}
507
Damien Miller5428f641999-11-25 11:54:57 +1100508/*
509 * Starts packet compression from the next packet on in both directions.
510 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
511 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Ben Lindstrombba81212001-06-25 05:01:22 +0000513static void
514packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000515{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000516 if (active_state->compression_buffer_ready == 1)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000517 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000518 active_state->compression_buffer_ready = 1;
519 buffer_init(&active_state->compression_buffer);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000520}
521
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522void
523packet_start_compression(int level)
524{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000525 if (active_state->packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100526 fatal("Compression already enabled.");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000527 active_state->packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000528 packet_init_compression();
529 buffer_compress_init_send(level);
530 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531}
532
Damien Miller5428f641999-11-25 11:54:57 +1100533/*
Damien Miller5428f641999-11-25 11:54:57 +1100534 * Causes any further packets to be encrypted using the given key. The same
535 * key is used for both sending and reception. However, both directions are
536 * encrypted independently of each other.
537 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000538
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000540packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100541 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542{
Damien Miller874d77b2000-10-14 16:23:11 +1100543 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000544
Damien Miller874d77b2000-10-14 16:23:11 +1100545 if (cipher == NULL)
546 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000547 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100548 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000549 if (keylen > SSH_SESSION_KEY_LENGTH)
550 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000551 memcpy(active_state->ssh1_key, key, keylen);
552 active_state->ssh1_keylen = keylen;
553 cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
554 0, CIPHER_ENCRYPT);
555 cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
556 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557}
558
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000559u_int
560packet_get_encryption_key(u_char *key)
561{
562 if (key == NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000563 return (active_state->ssh1_keylen);
564 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
565 return (active_state->ssh1_keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000566}
567
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000568/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000570packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000572 u_char buf[9];
573 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574
Ben Lindstrom204e4882001-03-05 06:47:00 +0000575 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000576 len = compat20 ? 6 : 9;
577 memset(buf, 0, len - 1);
578 buf[len - 1] = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000579 buffer_clear(&active_state->outgoing_packet);
580 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000581}
582
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000583/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584void
585packet_put_char(int value)
586{
Damien Miller95def091999-11-25 00:26:21 +1100587 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000588
Darren Tuckerf7288d72009-06-21 18:12:20 +1000589 buffer_append(&active_state->outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590}
Damien Miller4f7becb2006-03-26 14:10:14 +1100591
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000593packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000595 buffer_put_int(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596}
Damien Miller4f7becb2006-03-26 14:10:14 +1100597
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000598void
Darren Tucker761c3892009-06-21 18:16:26 +1000599packet_put_int64(u_int64_t value)
600{
601 buffer_put_int64(&active_state->outgoing_packet, value);
602}
603
604void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100605packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000607 buffer_put_string(&active_state->outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608}
Damien Miller4f7becb2006-03-26 14:10:14 +1100609
Damien Miller33b13562000-04-04 14:38:59 +1000610void
611packet_put_cstring(const char *str)
612{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000613 buffer_put_cstring(&active_state->outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000614}
Damien Miller4f7becb2006-03-26 14:10:14 +1100615
Damien Miller33b13562000-04-04 14:38:59 +1000616void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100617packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000618{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000619 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000620}
Damien Miller4f7becb2006-03-26 14:10:14 +1100621
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622void
Damien Miller95def091999-11-25 00:26:21 +1100623packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000625 buffer_put_bignum(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626}
Damien Miller4f7becb2006-03-26 14:10:14 +1100627
Damien Miller33b13562000-04-04 14:38:59 +1000628void
629packet_put_bignum2(BIGNUM * value)
630{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000631 buffer_put_bignum2(&active_state->outgoing_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +1000632}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633
Damien Miller5428f641999-11-25 11:54:57 +1100634/*
635 * Finalizes and sends the packet. If the encryption key has been set,
636 * encrypts the packet before sending.
637 */
Damien Miller95def091999-11-25 00:26:21 +1100638
Ben Lindstrombba81212001-06-25 05:01:22 +0000639static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000640packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000641{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000642 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100643 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000644 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000645 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000646
Damien Miller5428f641999-11-25 11:54:57 +1100647 /*
648 * If using packet compression, compress the payload of the outgoing
649 * packet.
650 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000651 if (active_state->packet_compression) {
652 buffer_clear(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100653 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000654 buffer_consume(&active_state->outgoing_packet, 8);
Damien Miller95def091999-11-25 00:26:21 +1100655 /* padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000656 buffer_append(&active_state->compression_buffer,
657 "\0\0\0\0\0\0\0\0", 8);
658 buffer_compress(&active_state->outgoing_packet,
659 &active_state->compression_buffer);
660 buffer_clear(&active_state->outgoing_packet);
661 buffer_append(&active_state->outgoing_packet,
662 buffer_ptr(&active_state->compression_buffer),
663 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100664 }
665 /* Compute packet length without padding (add checksum, remove padding). */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000666 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667
Damien Millere247cc42000-05-07 12:03:14 +1000668 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100669 padding = 8 - len % 8;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000670 if (!active_state->send_context.plaintext) {
671 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller95def091999-11-25 00:26:21 +1100672 for (i = 0; i < padding; i++) {
673 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000674 rnd = arc4random();
675 cp[7 - i] = rnd & 0xff;
676 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100677 }
678 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000679 buffer_consume(&active_state->outgoing_packet, 8 - padding);
Damien Miller95def091999-11-25 00:26:21 +1100680
681 /* Add check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000682 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
683 buffer_len(&active_state->outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100684 put_u32(buf, checksum);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000685 buffer_append(&active_state->outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686
687#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100688 fprintf(stderr, "packet_send plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000689 buffer_dump(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000690#endif
691
Damien Miller95def091999-11-25 00:26:21 +1100692 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100693 put_u32(buf, len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000694 buffer_append(&active_state->output, buf, 4);
695 cp = buffer_append_space(&active_state->output,
696 buffer_len(&active_state->outgoing_packet));
697 cipher_crypt(&active_state->send_context, cp,
698 buffer_ptr(&active_state->outgoing_packet),
699 buffer_len(&active_state->outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100700
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100702 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000703 buffer_dump(&active_state->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +1000705 active_state->p_send.packets++;
706 active_state->p_send.bytes += len +
707 buffer_len(&active_state->outgoing_packet);
708 buffer_clear(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709
Damien Miller5428f641999-11-25 11:54:57 +1100710 /*
Damien Miller788f2122005-11-05 15:14:59 +1100711 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100712 * actually sent until packet_write_wait or packet_write_poll is
713 * called.
714 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715}
716
Ben Lindstromf6027d32002-03-22 01:42:04 +0000717void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000718set_newkeys(int mode)
719{
720 Enc *enc;
721 Mac *mac;
722 Comp *comp;
723 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000724 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000725 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000726
Ben Lindstrom064496f2002-12-23 02:04:22 +0000727 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000728
Damien Miller963f6b22002-02-19 15:21:23 +1100729 if (mode == MODE_OUT) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000730 cc = &active_state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000731 crypt_type = CIPHER_ENCRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000732 active_state->p_send.packets = active_state->p_send.blocks = 0;
733 max_blocks = &active_state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100734 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000735 cc = &active_state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000736 crypt_type = CIPHER_DECRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000737 active_state->p_read.packets = active_state->p_read.blocks = 0;
738 max_blocks = &active_state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100739 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000740 if (active_state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000741 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100742 cipher_cleanup(cc);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000743 enc = &active_state->newkeys[mode]->enc;
744 mac = &active_state->newkeys[mode]->mac;
745 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000746 mac_clear(mac);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000747 xfree(enc->name);
748 xfree(enc->iv);
749 xfree(enc->key);
750 xfree(mac->name);
751 xfree(mac->key);
752 xfree(comp->name);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000753 xfree(active_state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000754 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000755 active_state->newkeys[mode] = kex_get_newkeys(mode);
756 if (active_state->newkeys[mode] == NULL)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000757 fatal("newkeys: no keys for mode %d", mode);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000758 enc = &active_state->newkeys[mode]->enc;
759 mac = &active_state->newkeys[mode]->mac;
760 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000761 if (mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000762 mac->enabled = 1;
763 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100764 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000765 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000766 /* Deleting the keys does not gain extra security */
767 /* memset(enc->iv, 0, enc->block_size);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000768 memset(enc->key, 0, enc->key_len);
769 memset(mac->key, 0, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000770 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000771 (comp->type == COMP_DELAYED &&
772 active_state->after_authentication)) && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000773 packet_init_compression();
774 if (mode == MODE_OUT)
775 buffer_compress_init_send(6);
776 else
777 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000778 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000779 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000780 /*
781 * The 2^(blocksize*2) limit is too expensive for 3DES,
782 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
783 */
784 if (enc->block_size >= 16)
785 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
786 else
787 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000788 if (active_state->rekey_limit)
789 *max_blocks = MIN(*max_blocks,
790 active_state->rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000791}
792
Damien Miller5428f641999-11-25 11:54:57 +1100793/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000794 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000795 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000796 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
797 */
798static void
799packet_enable_delayed_compress(void)
800{
801 Comp *comp = NULL;
802 int mode;
803
804 /*
805 * Remember that we are past the authentication step, so rekeying
806 * with COMP_DELAYED will turn on compression immediately.
807 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000808 active_state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +1000809 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000810 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000811 if (active_state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +1000812 continue;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000813 comp = &active_state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +1000814 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000815 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000816 if (mode == MODE_OUT)
817 buffer_compress_init_send(6);
818 else
819 buffer_compress_init_recv();
820 comp->enabled = 1;
821 }
822 }
823}
824
825/*
Damien Miller33b13562000-04-04 14:38:59 +1000826 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
827 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000828static void
Damien Millera5539d22003-04-09 20:50:06 +1000829packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000830{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000831 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100832 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000833 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100834 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000835 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000836 Enc *enc = NULL;
837 Mac *mac = NULL;
838 Comp *comp = NULL;
839 int block_size;
840
Darren Tuckerf7288d72009-06-21 18:12:20 +1000841 if (active_state->newkeys[MODE_OUT] != NULL) {
842 enc = &active_state->newkeys[MODE_OUT]->enc;
843 mac = &active_state->newkeys[MODE_OUT]->mac;
844 comp = &active_state->newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000845 }
Damien Miller963f6b22002-02-19 15:21:23 +1100846 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000847
Darren Tuckerf7288d72009-06-21 18:12:20 +1000848 cp = buffer_ptr(&active_state->outgoing_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000849 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000850
851#ifdef PACKET_DEBUG
852 fprintf(stderr, "plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000853 buffer_dump(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000854#endif
855
856 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000857 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000858 /* skip header, compress only payload */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000859 buffer_consume(&active_state->outgoing_packet, 5);
860 buffer_clear(&active_state->compression_buffer);
861 buffer_compress(&active_state->outgoing_packet,
862 &active_state->compression_buffer);
863 buffer_clear(&active_state->outgoing_packet);
864 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
865 buffer_append(&active_state->outgoing_packet,
866 buffer_ptr(&active_state->compression_buffer),
867 buffer_len(&active_state->compression_buffer));
Damien Miller33b13562000-04-04 14:38:59 +1000868 DBG(debug("compression: raw %d compressed %d", len,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000869 buffer_len(&active_state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +1000870 }
871
872 /* sizeof (packet_len + pad_len + payload) */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000873 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000874
875 /*
876 * calc size of padding, alloc space, get random data,
877 * minimum padding is 4 bytes
878 */
879 padlen = block_size - (len % block_size);
880 if (padlen < 4)
881 padlen += block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000882 if (active_state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +1100883 /* will wrap if extra_pad+padlen > 255 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000884 active_state->extra_pad =
885 roundup(active_state->extra_pad, block_size);
886 pad = active_state->extra_pad -
887 ((len + padlen) % active_state->extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000888 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000889 pad, len, padlen, active_state->extra_pad);
Damien Miller9f643902001-11-12 11:02:52 +1100890 padlen += pad;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000891 active_state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100892 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000893 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
894 if (enc && !active_state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000895 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000896 for (i = 0; i < padlen; i++) {
897 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000898 rnd = arc4random();
899 cp[i] = rnd & 0xff;
900 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000901 }
Damien Millere247cc42000-05-07 12:03:14 +1000902 } else {
903 /* clear padding */
904 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000905 }
906 /* packet_length includes payload, padding and padding length field */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000907 packet_length = buffer_len(&active_state->outgoing_packet) - 4;
908 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100909 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000910 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000911 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
912
913 /* compute MAC over seqnr and packet(length fields, payload, padding) */
914 if (mac && mac->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000915 macbuf = mac_compute(mac, active_state->p_send.seqnr,
916 buffer_ptr(&active_state->outgoing_packet),
917 buffer_len(&active_state->outgoing_packet));
918 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000919 }
920 /* encrypt packet and append to output buffer. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000921 cp = buffer_append_space(&active_state->output,
922 buffer_len(&active_state->outgoing_packet));
923 cipher_crypt(&active_state->send_context, cp,
924 buffer_ptr(&active_state->outgoing_packet),
925 buffer_len(&active_state->outgoing_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000926 /* append unencrypted MAC */
927 if (mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000928 buffer_append(&active_state->output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000929#ifdef PACKET_DEBUG
930 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000931 buffer_dump(&active_state->output);
Damien Miller33b13562000-04-04 14:38:59 +1000932#endif
Damien Miller4af51302000-04-16 11:18:38 +1000933 /* increment sequence number for outgoing packets */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000934 if (++active_state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000935 logit("outgoing seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000936 if (++active_state->p_send.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +1000937 if (!(datafellows & SSH_BUG_NOREKEY))
938 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000939 active_state->p_send.blocks += (packet_length + 4) / block_size;
940 active_state->p_send.bytes += packet_length + 4;
941 buffer_clear(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000942
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000943 if (type == SSH2_MSG_NEWKEYS)
944 set_newkeys(MODE_OUT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000945 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +1000946 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000947}
948
Damien Millera5539d22003-04-09 20:50:06 +1000949static void
950packet_send2(void)
951{
952 static int rekeying = 0;
953 struct packet *p;
954 u_char type, *cp;
955
Darren Tuckerf7288d72009-06-21 18:12:20 +1000956 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000957 type = cp[5];
958
959 /* during rekeying we can only send key exchange messages */
960 if (rekeying) {
961 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
962 (type <= SSH2_MSG_TRANSPORT_MAX))) {
963 debug("enqueue packet: %u", type);
964 p = xmalloc(sizeof(*p));
965 p->type = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000966 memcpy(&p->payload, &active_state->outgoing_packet,
967 sizeof(Buffer));
968 buffer_init(&active_state->outgoing_packet);
969 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +1000970 return;
971 }
972 }
973
974 /* rekeying starts with sending KEXINIT */
975 if (type == SSH2_MSG_KEXINIT)
976 rekeying = 1;
977
978 packet_send2_wrapped();
979
980 /* after a NEWKEYS message we can send the complete queue */
981 if (type == SSH2_MSG_NEWKEYS) {
982 rekeying = 0;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000983 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +1000984 type = p->type;
985 debug("dequeue packet: %u", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000986 buffer_free(&active_state->outgoing_packet);
987 memcpy(&active_state->outgoing_packet, &p->payload,
Damien Millera5539d22003-04-09 20:50:06 +1000988 sizeof(Buffer));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000989 TAILQ_REMOVE(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +1000990 xfree(p);
991 packet_send2_wrapped();
992 }
993 }
994}
995
Damien Miller33b13562000-04-04 14:38:59 +1000996void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000997packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000998{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000999 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +10001000 packet_send2();
1001 else
1002 packet_send1();
1003 DBG(debug("packet_send done"));
1004}
1005
Damien Miller33b13562000-04-04 14:38:59 +10001006/*
Damien Miller5428f641999-11-25 11:54:57 +11001007 * Waits until a packet has been received, and returns its type. Note that
1008 * no other data is processed until this returns, so this function should not
1009 * be used during the interactive session.
1010 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011
1012int
Damien Millerdff50992002-01-22 23:16:32 +11001013packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014{
Darren Tucker3fc464e2008-06-13 06:42:45 +10001015 int type, len, ret, ms_remain;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001016 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001017 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001018 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019
Darren Tucker99bb7612008-06-13 22:02:50 +10001020 DBG(debug("packet_read()"));
1021
Darren Tuckerf7288d72009-06-21 18:12:20 +10001022 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1023 NFDBITS), sizeof(fd_mask));
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001024
Damien Miller95def091999-11-25 00:26:21 +11001025 /* Since we are blocking, ensure that all written packets have been sent. */
1026 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027
Damien Miller95def091999-11-25 00:26:21 +11001028 /* Stay in the loop until we have received a complete packet. */
1029 for (;;) {
1030 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +11001031 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001032 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +10001033 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +11001034 || type == SSH_SMSG_FAILURE
1035 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +10001036 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +11001037 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001038 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001039 if (type != SSH_MSG_NONE) {
1040 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +11001041 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001042 }
Damien Miller5428f641999-11-25 11:54:57 +11001043 /*
1044 * Otherwise, wait for some data to arrive, add it to the
1045 * buffer, and try again.
1046 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001047 memset(setp, 0, howmany(active_state->connection_in + 1,
1048 NFDBITS) * sizeof(fd_mask));
1049 FD_SET(active_state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001050
Darren Tuckerf7288d72009-06-21 18:12:20 +10001051 if (active_state->packet_timeout_ms > 0) {
1052 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001053 timeoutp = &timeout;
1054 }
Damien Miller95def091999-11-25 00:26:21 +11001055 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001056 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001057 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001058 ms_to_timeval(&timeout, ms_remain);
1059 gettimeofday(&start, NULL);
1060 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001061 if ((ret = select(active_state->connection_in + 1, setp,
1062 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001063 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001064 if (errno != EAGAIN && errno != EINTR &&
Damien Millerd8968ad2008-07-04 23:10:49 +10001065 errno != EWOULDBLOCK)
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 Tuckerf7288d72009-06-21 18:12:20 +10001081 len = read(active_state->connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +11001082 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001083 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +10001084 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001085 }
Damien Miller95def091999-11-25 00:26:21 +11001086 if (len < 0)
1087 fatal("Read from socket failed: %.100s", strerror(errno));
1088 /* Append it to the buffer. */
1089 packet_process_incoming(buf, len);
1090 }
1091 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001092}
1093
Damien Miller278f9072001-12-21 15:00:19 +11001094int
Damien Millerdff50992002-01-22 23:16:32 +11001095packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +11001096{
Damien Millerdff50992002-01-22 23:16:32 +11001097 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001098}
1099
Damien Miller5428f641999-11-25 11:54:57 +11001100/*
1101 * Waits until a packet has been received, verifies that its type matches
1102 * that given, and gives a fatal error and exits if there is a mismatch.
1103 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104
1105void
Damien Millerdff50992002-01-22 23:16:32 +11001106packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107{
Damien Miller95def091999-11-25 00:26:21 +11001108 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001109
Damien Millerdff50992002-01-22 23:16:32 +11001110 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001111 if (type != expected_type)
1112 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001113 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001114}
1115
1116/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001117 * packet_process_incoming. If so, reads the packet; otherwise returns
1118 * SSH_MSG_NONE. This does not wait for data from the connection.
1119 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001120 * SSH_MSG_DISCONNECT is handled specially here. Also,
1121 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1122 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001123 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124
Ben Lindstrombba81212001-06-25 05:01:22 +00001125static int
Damien Millerdff50992002-01-22 23:16:32 +11001126packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001128 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001129 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001130 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131
Damien Miller95def091999-11-25 00:26:21 +11001132 /* Check if input size is less than minimum packet size. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001133 if (buffer_len(&active_state->input) < 4 + 8)
Damien Miller95def091999-11-25 00:26:21 +11001134 return SSH_MSG_NONE;
1135 /* Get length of incoming packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001136 cp = buffer_ptr(&active_state->input);
Damien Miller3f941882006-03-31 23:13:02 +11001137 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001138 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001139 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +11001140 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001141
Damien Miller95def091999-11-25 00:26:21 +11001142 /* Check if the packet has been entirely received. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001143 if (buffer_len(&active_state->input) < 4 + padded_len)
Damien Miller95def091999-11-25 00:26:21 +11001144 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145
Damien Miller95def091999-11-25 00:26:21 +11001146 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147
Damien Miller95def091999-11-25 00:26:21 +11001148 /* Consume packet length. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001149 buffer_consume(&active_state->input, 4);
Damien Miller95def091999-11-25 00:26:21 +11001150
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001151 /*
1152 * Cryptographic attack detector for ssh
1153 * (C)1998 CORE-SDI, Buenos Aires Argentina
1154 * Ariel Futoransky(futo@core-sdi.com)
1155 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001156 if (!active_state->receive_context.plaintext) {
1157 switch (detect_attack(buffer_ptr(&active_state->input),
1158 padded_len)) {
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001159 case DEATTACK_DETECTED:
1160 packet_disconnect("crc32 compensation attack: "
1161 "network attack detected");
1162 case DEATTACK_DOS_DETECTED:
1163 packet_disconnect("deattack denial of "
1164 "service detected");
1165 }
1166 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001167
1168 /* Decrypt data to incoming_packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001169 buffer_clear(&active_state->incoming_packet);
1170 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1171 cipher_crypt(&active_state->receive_context, cp,
1172 buffer_ptr(&active_state->input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001173
Darren Tuckerf7288d72009-06-21 18:12:20 +10001174 buffer_consume(&active_state->input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175
1176#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001177 fprintf(stderr, "read_poll plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001178 buffer_dump(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001179#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001180
Damien Miller95def091999-11-25 00:26:21 +11001181 /* Compute packet checksum. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001182 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1183 buffer_len(&active_state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184
Damien Miller95def091999-11-25 00:26:21 +11001185 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001186 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001187
Damien Miller95def091999-11-25 00:26:21 +11001188 /* Test check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001189 if (len != buffer_len(&active_state->incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001190 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001191 len, buffer_len(&active_state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192
Darren Tuckerf7288d72009-06-21 18:12:20 +10001193 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001194 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001195 if (checksum != stored_checksum)
1196 packet_disconnect("Corrupted check bytes on input.");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001197 buffer_consume_end(&active_state->incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198
Darren Tuckerf7288d72009-06-21 18:12:20 +10001199 if (active_state->packet_compression) {
1200 buffer_clear(&active_state->compression_buffer);
1201 buffer_uncompress(&active_state->incoming_packet,
1202 &active_state->compression_buffer);
1203 buffer_clear(&active_state->incoming_packet);
1204 buffer_append(&active_state->incoming_packet,
1205 buffer_ptr(&active_state->compression_buffer),
1206 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001207 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001208 active_state->p_read.packets++;
1209 active_state->p_read.bytes += padded_len + 4;
1210 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001211 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1212 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001213 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214}
Damien Miller95def091999-11-25 00:26:21 +11001215
Ben Lindstrombba81212001-06-25 05:01:22 +00001216static int
Damien Millerdff50992002-01-22 23:16:32 +11001217packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001218{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001219 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001220 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001221 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001222 Enc *enc = NULL;
1223 Mac *mac = NULL;
1224 Comp *comp = NULL;
1225
Darren Tuckerf7288d72009-06-21 18:12:20 +10001226 if (active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001227 return SSH_MSG_NONE;
1228
Darren Tuckerf7288d72009-06-21 18:12:20 +10001229 if (active_state->newkeys[MODE_IN] != NULL) {
1230 enc = &active_state->newkeys[MODE_IN]->enc;
1231 mac = &active_state->newkeys[MODE_IN]->mac;
1232 comp = &active_state->newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001233 }
1234 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001235 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001236
Darren Tuckerf7288d72009-06-21 18:12:20 +10001237 if (active_state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001238 /*
1239 * check if input size is less than the cipher block size,
1240 * decrypt first block and extract length of incoming packet
1241 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001242 if (buffer_len(&active_state->input) < block_size)
Damien Miller33b13562000-04-04 14:38:59 +10001243 return SSH_MSG_NONE;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001244 buffer_clear(&active_state->incoming_packet);
1245 cp = buffer_append_space(&active_state->incoming_packet,
Damien Miller33b13562000-04-04 14:38:59 +10001246 block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001247 cipher_crypt(&active_state->receive_context, cp,
1248 buffer_ptr(&active_state->input), block_size);
1249 cp = buffer_ptr(&active_state->incoming_packet);
1250 active_state->packlen = get_u32(cp);
1251 if (active_state->packlen < 1 + 4 ||
1252 active_state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001253#ifdef PACKET_DEBUG
Darren Tuckerf7288d72009-06-21 18:12:20 +10001254 buffer_dump(&active_state->incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001255#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001256 logit("Bad packet length %u.", active_state->packlen);
1257 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001258 PACKET_MAX_SIZE);
1259 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001260 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001261 DBG(debug("input: packet len %u", active_state->packlen+4));
1262 buffer_consume(&active_state->input, block_size);
Damien Miller33b13562000-04-04 14:38:59 +10001263 }
1264 /* we have a partial packet of block_size bytes */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001265 need = 4 + active_state->packlen - block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001266 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1267 need, maclen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001268 if (need % block_size != 0) {
1269 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001270 need, block_size, need % block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001271 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001272 PACKET_MAX_SIZE - block_size);
1273 return SSH_MSG_NONE;
Darren Tucker99d11a32008-12-01 21:40:48 +11001274 }
Damien Miller33b13562000-04-04 14:38:59 +10001275 /*
1276 * check if the entire packet has been received and
1277 * decrypt into incoming_packet
1278 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001279 if (buffer_len(&active_state->input) < need + maclen)
Damien Miller33b13562000-04-04 14:38:59 +10001280 return SSH_MSG_NONE;
1281#ifdef PACKET_DEBUG
1282 fprintf(stderr, "read_poll enc/full: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001283 buffer_dump(&active_state->input);
Damien Miller33b13562000-04-04 14:38:59 +10001284#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001285 cp = buffer_append_space(&active_state->incoming_packet, need);
1286 cipher_crypt(&active_state->receive_context, cp,
1287 buffer_ptr(&active_state->input), need);
1288 buffer_consume(&active_state->input, need);
Damien Miller33b13562000-04-04 14:38:59 +10001289 /*
1290 * compute MAC over seqnr and packet,
1291 * increment sequence number for incoming packet
1292 */
Damien Miller4af51302000-04-16 11:18:38 +10001293 if (mac && mac->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001294 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1295 buffer_ptr(&active_state->incoming_packet),
1296 buffer_len(&active_state->incoming_packet));
1297 if (memcmp(macbuf, buffer_ptr(&active_state->input),
1298 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001299 logit("Corrupted MAC on input.");
1300 if (need > PACKET_MAX_SIZE)
1301 fatal("internal error need %d", need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001302 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001303 PACKET_MAX_SIZE - need);
1304 return SSH_MSG_NONE;
1305 }
1306
Darren Tuckerf7288d72009-06-21 18:12:20 +10001307 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1308 buffer_consume(&active_state->input, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +10001309 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001310 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001311 if (seqnr_p != NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001312 *seqnr_p = active_state->p_read.seqnr;
1313 if (++active_state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001314 logit("incoming seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001315 if (++active_state->p_read.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +10001316 if (!(datafellows & SSH_BUG_NOREKEY))
1317 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001318 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1319 active_state->p_read.bytes += active_state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001320
1321 /* get padlen */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001322 cp = buffer_ptr(&active_state->incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001323 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001324 DBG(debug("input: padlen %d", padlen));
1325 if (padlen < 4)
1326 packet_disconnect("Corrupted padlen %d on input.", padlen);
1327
1328 /* skip packet size + padlen, discard padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001329 buffer_consume(&active_state->incoming_packet, 4 + 1);
1330 buffer_consume_end(&active_state->incoming_packet, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001331
Darren Tuckerf7288d72009-06-21 18:12:20 +10001332 DBG(debug("input: len before de-compress %d",
1333 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001334 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001335 buffer_clear(&active_state->compression_buffer);
1336 buffer_uncompress(&active_state->incoming_packet,
1337 &active_state->compression_buffer);
1338 buffer_clear(&active_state->incoming_packet);
1339 buffer_append(&active_state->incoming_packet,
1340 buffer_ptr(&active_state->compression_buffer),
1341 buffer_len(&active_state->compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001342 DBG(debug("input: len after de-compress %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001343 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001344 }
1345 /*
1346 * get packet type, implies consume.
1347 * return length of payload (without type field)
1348 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001349 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001350 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1351 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001352 if (type == SSH2_MSG_NEWKEYS)
1353 set_newkeys(MODE_IN);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001354 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1355 !active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +10001356 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001357#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001358 fprintf(stderr, "read/plain[%d]:\r\n", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001359 buffer_dump(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001360#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001361 /* reset for next packet */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001362 active_state->packlen = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001363 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001364}
1365
1366int
Damien Millerdff50992002-01-22 23:16:32 +11001367packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001368{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001369 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001370 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001371 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001372
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001373 for (;;) {
1374 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001375 type = packet_read_poll2(seqnr_p);
Darren Tucker136e56f2008-06-08 12:49:30 +10001376 if (type) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001377 active_state->keep_alive_timeouts = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001378 DBG(debug("received packet type %d", type));
Darren Tucker136e56f2008-06-08 12:49:30 +10001379 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001380 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001381 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001382 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001383 break;
Damien Miller33b13562000-04-04 14:38:59 +10001384 case SSH2_MSG_DEBUG:
1385 packet_get_char();
1386 msg = packet_get_string(NULL);
1387 debug("Remote: %.900s", msg);
1388 xfree(msg);
1389 msg = packet_get_string(NULL);
1390 xfree(msg);
1391 break;
1392 case SSH2_MSG_DISCONNECT:
1393 reason = packet_get_int();
1394 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001395 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001396 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001397 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001398 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001399 break;
Damien Miller659811f2002-01-22 23:23:11 +11001400 case SSH2_MSG_UNIMPLEMENTED:
1401 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001402 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1403 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001404 break;
Damien Miller33b13562000-04-04 14:38:59 +10001405 default:
1406 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001407 }
Damien Miller33b13562000-04-04 14:38:59 +10001408 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001409 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001410 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001411 case SSH_MSG_IGNORE:
1412 break;
1413 case SSH_MSG_DEBUG:
1414 msg = packet_get_string(NULL);
1415 debug("Remote: %.900s", msg);
1416 xfree(msg);
1417 break;
1418 case SSH_MSG_DISCONNECT:
1419 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001420 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001421 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001422 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001423 break;
1424 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001425 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001426 DBG(debug("received packet type %d", type));
1427 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001428 }
Damien Miller33b13562000-04-04 14:38:59 +10001429 }
1430 }
1431}
1432
Damien Miller278f9072001-12-21 15:00:19 +11001433int
Damien Millerdff50992002-01-22 23:16:32 +11001434packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001435{
Damien Millerdff50992002-01-22 23:16:32 +11001436 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001437}
1438
Damien Miller5428f641999-11-25 11:54:57 +11001439/*
1440 * Buffers the given amount of input characters. This is intended to be used
1441 * together with packet_read_poll.
1442 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443
1444void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001445packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001447 if (active_state->packet_discard) {
1448 active_state->keep_alive_timeouts = 0; /* ?? */
1449 if (len >= active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001450 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001451 active_state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001452 return;
1453 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001454 buffer_append(&active_state->input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455}
1456
1457/* Returns a character from the packet. */
1458
Ben Lindstrom46c16222000-12-22 01:43:59 +00001459u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001460packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461{
Damien Miller95def091999-11-25 00:26:21 +11001462 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001463
Darren Tuckerf7288d72009-06-21 18:12:20 +10001464 buffer_get(&active_state->incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001465 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466}
1467
1468/* Returns an integer from the packet data. */
1469
Ben Lindstrom46c16222000-12-22 01:43:59 +00001470u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001471packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001473 return buffer_get_int(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474}
1475
Darren Tucker761c3892009-06-21 18:16:26 +10001476/* Returns an 64 bit integer from the packet data. */
1477
1478u_int64_t
1479packet_get_int64(void)
1480{
1481 return buffer_get_int64(&active_state->incoming_packet);
1482}
1483
Damien Miller5428f641999-11-25 11:54:57 +11001484/*
1485 * Returns an arbitrary precision integer from the packet data. The integer
1486 * must have been initialized before this call.
1487 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488
1489void
Damien Millerd432ccf2002-01-22 23:14:44 +11001490packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001492 buffer_get_bignum(&active_state->incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493}
1494
Damien Miller33b13562000-04-04 14:38:59 +10001495void
Damien Millerd432ccf2002-01-22 23:14:44 +11001496packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001497{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001498 buffer_get_bignum2(&active_state->incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001499}
1500
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001501void *
Damien Millereccb9de2005-06-17 12:59:34 +10001502packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001503{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001504 u_int bytes = buffer_len(&active_state->incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001505
Damien Miller33b13562000-04-04 14:38:59 +10001506 if (length_ptr != NULL)
1507 *length_ptr = bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001508 return buffer_ptr(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001509}
1510
Damien Miller4af51302000-04-16 11:18:38 +10001511int
1512packet_remaining(void)
1513{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001514 return buffer_len(&active_state->incoming_packet);
Damien Miller4af51302000-04-16 11:18:38 +10001515}
1516
Damien Miller5428f641999-11-25 11:54:57 +11001517/*
1518 * Returns a string from the packet data. The string is allocated using
1519 * xmalloc; it is the responsibility of the calling program to free it when
1520 * no longer needed. The length_ptr argument may be NULL, or point to an
1521 * integer into which the length of the string is stored.
1522 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001523
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001524void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001525packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001526{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001527 return buffer_get_string(&active_state->incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001528}
1529
Damien Millerdb255ca2008-05-19 14:59:37 +10001530void *
1531packet_get_string_ptr(u_int *length_ptr)
1532{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001533 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
Damien Millerdb255ca2008-05-19 14:59:37 +10001534}
1535
Damien Miller5428f641999-11-25 11:54:57 +11001536/*
1537 * Sends a diagnostic message from the server to the client. This message
1538 * can be sent at any time (but not while constructing another message). The
1539 * message is printed immediately, but only if the client is being executed
1540 * in verbose mode. These messages are primarily intended to ease debugging
1541 * authentication problems. The length of the formatted message must not
1542 * exceed 1024 bytes. This will automatically call packet_write_wait.
1543 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001544
1545void
Damien Miller95def091999-11-25 00:26:21 +11001546packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001547{
Damien Miller95def091999-11-25 00:26:21 +11001548 char buf[1024];
1549 va_list args;
1550
Ben Lindstroma14ee472000-12-07 01:24:58 +00001551 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1552 return;
1553
Damien Miller95def091999-11-25 00:26:21 +11001554 va_start(args, fmt);
1555 vsnprintf(buf, sizeof(buf), fmt, args);
1556 va_end(args);
1557
Damien Miller7c8af4f2000-05-01 08:24:07 +10001558 if (compat20) {
1559 packet_start(SSH2_MSG_DEBUG);
1560 packet_put_char(0); /* bool: always display */
1561 packet_put_cstring(buf);
1562 packet_put_cstring("");
1563 } else {
1564 packet_start(SSH_MSG_DEBUG);
1565 packet_put_cstring(buf);
1566 }
Damien Miller95def091999-11-25 00:26:21 +11001567 packet_send();
1568 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001569}
1570
Damien Miller5428f641999-11-25 11:54:57 +11001571/*
1572 * Logs the error plus constructs and sends a disconnect packet, closes the
1573 * connection, and exits. This function never returns. The error message
1574 * should not contain a newline. The length of the formatted message must
1575 * not exceed 1024 bytes.
1576 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001577
1578void
Damien Miller95def091999-11-25 00:26:21 +11001579packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001580{
Damien Miller95def091999-11-25 00:26:21 +11001581 char buf[1024];
1582 va_list args;
1583 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001584
Damien Miller95def091999-11-25 00:26:21 +11001585 if (disconnecting) /* Guard against recursive invocations. */
1586 fatal("packet_disconnect called recursively.");
1587 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001588
Damien Miller5428f641999-11-25 11:54:57 +11001589 /*
1590 * Format the message. Note that the caller must make sure the
1591 * message is of limited size.
1592 */
Damien Miller95def091999-11-25 00:26:21 +11001593 va_start(args, fmt);
1594 vsnprintf(buf, sizeof(buf), fmt, args);
1595 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001596
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001597 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001598 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001599
Damien Miller95def091999-11-25 00:26:21 +11001600 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001601 if (compat20) {
1602 packet_start(SSH2_MSG_DISCONNECT);
1603 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1604 packet_put_cstring(buf);
1605 packet_put_cstring("");
1606 } else {
1607 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001608 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001609 }
Damien Miller95def091999-11-25 00:26:21 +11001610 packet_send();
1611 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001612
Damien Miller95def091999-11-25 00:26:21 +11001613 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001614 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001615
Damien Miller95def091999-11-25 00:26:21 +11001616 /* Close the connection. */
1617 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001618 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001619}
1620
Damien Miller5428f641999-11-25 11:54:57 +11001621/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001622
1623void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001624packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001625{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001626 int len = buffer_len(&active_state->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001627
Damien Miller95def091999-11-25 00:26:21 +11001628 if (len > 0) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001629 len = write(active_state->connection_out,
1630 buffer_ptr(&active_state->output), len);
Damien Millerd874fa52008-07-05 09:40:56 +10001631 if (len == -1) {
1632 if (errno == EINTR || errno == EAGAIN ||
1633 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001634 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001635 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001636 }
Damien Millerd874fa52008-07-05 09:40:56 +10001637 if (len == 0)
1638 fatal("Write connection closed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001639 buffer_consume(&active_state->output, len);
Damien Miller95def091999-11-25 00:26:21 +11001640 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001641}
1642
Damien Millerd874fa52008-07-05 09:40:56 +10001643
Damien Miller5428f641999-11-25 11:54:57 +11001644/*
1645 * Calls packet_write_poll repeatedly until all pending output data has been
1646 * written.
1647 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001648
1649void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001650packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001651{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001652 fd_set *setp;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001653 int ret, ms_remain;
1654 struct timeval start, timeout, *timeoutp = NULL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001655
Darren Tuckerf7288d72009-06-21 18:12:20 +10001656 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1657 NFDBITS), sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001658 packet_write_poll();
1659 while (packet_have_data_to_write()) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001660 memset(setp, 0, howmany(active_state->connection_out + 1,
1661 NFDBITS) * sizeof(fd_mask));
1662 FD_SET(active_state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001663
Darren Tuckerf7288d72009-06-21 18:12:20 +10001664 if (active_state->packet_timeout_ms > 0) {
1665 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001666 timeoutp = &timeout;
1667 }
1668 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001669 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001670 ms_to_timeval(&timeout, ms_remain);
1671 gettimeofday(&start, NULL);
1672 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001673 if ((ret = select(active_state->connection_out + 1,
1674 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001675 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001676 if (errno != EAGAIN && errno != EINTR &&
Damien Millerd8968ad2008-07-04 23:10:49 +10001677 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001678 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001679 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001680 continue;
1681 ms_subtract_diff(&start, &ms_remain);
1682 if (ms_remain <= 0) {
1683 ret = 0;
1684 break;
1685 }
1686 }
1687 if (ret == 0) {
1688 logit("Connection to %.200s timed out while "
1689 "waiting to write", get_remote_ipaddr());
1690 cleanup_exit(255);
1691 }
Damien Miller95def091999-11-25 00:26:21 +11001692 packet_write_poll();
1693 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001694 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001695}
1696
1697/* Returns true if there is buffered data to write to the connection. */
1698
1699int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001700packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001701{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001702 return buffer_len(&active_state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001703}
1704
1705/* Returns true if there is not too much data to write to the connection. */
1706
1707int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001708packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001709{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001710 if (active_state->interactive_mode)
1711 return buffer_len(&active_state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11001712 else
Darren Tuckerf7288d72009-06-21 18:12:20 +10001713 return buffer_len(&active_state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001714}
1715
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001716
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001717static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001718packet_set_tos(int interactive)
1719{
Damien Miller5924ceb2003-11-22 15:02:42 +11001720#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001721 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1722
1723 if (!packet_connection_is_on_socket() ||
1724 !packet_connection_is_ipv4())
1725 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001726 if (setsockopt(active_state->connection_in, IPPROTO_IP, IP_TOS, &tos,
Ben Lindstroma7433982002-12-23 02:41:41 +00001727 sizeof(tos)) < 0)
1728 error("setsockopt IP_TOS %d: %.100s:",
1729 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001730#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001731}
Ben Lindstroma7433982002-12-23 02:41:41 +00001732
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001733/* Informs that the current session is interactive. Sets IP flags for that. */
1734
1735void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001736packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001737{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001738 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001739
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001740 if (called)
1741 return;
1742 called = 1;
1743
Damien Miller95def091999-11-25 00:26:21 +11001744 /* Record that we are in interactive mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001745 active_state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001746
Damien Miller34132e52000-01-14 15:45:46 +11001747 /* Only set socket options if using a socket. */
1748 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001749 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001750 set_nodelay(active_state->connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001751 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001752}
1753
1754/* Returns true if the current connection is interactive. */
1755
1756int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001757packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001758{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001759 return active_state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001760}
Damien Miller6162d121999-11-21 13:23:52 +11001761
Darren Tucker1f8311c2004-05-13 16:39:33 +10001762int
Darren Tucker502d3842003-06-28 12:38:01 +10001763packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001764{
Damien Miller95def091999-11-25 00:26:21 +11001765 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001766
Damien Miller95def091999-11-25 00:26:21 +11001767 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001768 logit("packet_set_maxsize: called twice: old %d new %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001769 active_state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001770 return -1;
1771 }
1772 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001773 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001774 return -1;
1775 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001776 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001777 debug("packet_set_maxsize: setting to %d", s);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001778 active_state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11001779 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001780}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001781
Darren Tuckerf7288d72009-06-21 18:12:20 +10001782int
1783packet_inc_alive_timeouts(void)
1784{
1785 return ++active_state->keep_alive_timeouts;
1786}
1787
1788void
1789packet_set_alive_timeouts(int ka)
1790{
1791 active_state->keep_alive_timeouts = ka;
1792}
1793
1794u_int
1795packet_get_maxsize(void)
1796{
1797 return active_state->max_packet_size;
1798}
1799
Damien Miller9f643902001-11-12 11:02:52 +11001800/* roundup current message to pad bytes */
1801void
1802packet_add_padding(u_char pad)
1803{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001804 active_state->extra_pad = pad;
Damien Miller9f643902001-11-12 11:02:52 +11001805}
1806
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001807/*
1808 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001809 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001810 * byte SSH_MSG_IGNORE
1811 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001812 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001813 * All implementations MUST understand (and ignore) this message at any
1814 * time (after receiving the protocol version). No implementation is
1815 * required to send them. This message can be used as an additional
1816 * protection measure against advanced traffic analysis techniques.
1817 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001818void
1819packet_send_ignore(int nbytes)
1820{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001821 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001822 int i;
1823
1824 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001825 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001826 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001827 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001828 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001829 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001830 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001831 }
1832}
Damien Millera5539d22003-04-09 20:50:06 +10001833
Darren Tucker1f8311c2004-05-13 16:39:33 +10001834#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001835int
1836packet_need_rekeying(void)
1837{
1838 if (datafellows & SSH_BUG_NOREKEY)
1839 return 0;
1840 return
Darren Tuckerf7288d72009-06-21 18:12:20 +10001841 (active_state->p_send.packets > MAX_PACKETS) ||
1842 (active_state->p_read.packets > MAX_PACKETS) ||
1843 (active_state->max_blocks_out &&
1844 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1845 (active_state->max_blocks_in &&
1846 (active_state->p_read.blocks > active_state->max_blocks_in));
Damien Millera5539d22003-04-09 20:50:06 +10001847}
1848
1849void
1850packet_set_rekey_limit(u_int32_t bytes)
1851{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001852 active_state->rekey_limit = bytes;
Damien Millera5539d22003-04-09 20:50:06 +10001853}
Damien Miller9786e6e2005-07-26 21:54:56 +10001854
1855void
1856packet_set_server(void)
1857{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001858 active_state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001859}
1860
1861void
1862packet_set_authenticated(void)
1863{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001864 active_state->after_authentication = 1;
1865}
1866
1867void *
1868packet_get_input(void)
1869{
1870 return (void *)&active_state->input;
1871}
1872
1873void *
1874packet_get_output(void)
1875{
1876 return (void *)&active_state->output;
1877}
1878
1879void *
1880packet_get_newkeys(int mode)
1881{
1882 return (void *)active_state->newkeys[mode];
Damien Miller9786e6e2005-07-26 21:54:56 +10001883}