blob: 24d2bb312be1baf90f49c7e2712b7c19ed1c0e53 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains code implementing the packet protocol and communication
6 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100013 *
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000016 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110040RCSID("$OpenBSD: packet.c,v 1.121 2006/02/08 14:38:18 stevesk Exp $");
41
Damien Millera0898b82003-04-09 21:05:52 +100042#include "openbsd-compat/sys-queue.h"
Damien Miller68f8e992006-03-15 11:24:12 +110043#include <netinet/in_systm.h>
44#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46#include "xmalloc.h"
47#include "buffer.h"
48#include "packet.h"
49#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "getput.h"
52
53#include "compress.h"
54#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000055#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
Damien Miller33b13562000-04-04 14:38:59 +100057#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100059#include "ssh2.h"
60
Damien Miller874d77b2000-10-14 16:23:11 +110061#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100062#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000063#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "log.h"
65#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110066#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000067#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100068
69#ifdef PACKET_DEBUG
70#define DBG(x) x
71#else
72#define DBG(x)
73#endif
74
Damien Miller5428f641999-11-25 11:54:57 +110075/*
76 * This variable contains the file descriptors used for communicating with
77 * the other side. connection_in is used for reading; connection_out for
78 * writing. These can be the same descriptor, in which case it is assumed to
79 * be a socket.
80 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081static int connection_in = -1;
82static int connection_out = -1;
83
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000085static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
87/* Encryption context for receiving data. This is only used for decryption. */
88static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110089
90/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091static CipherContext send_context;
92
93/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000094Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
96/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000097Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99/* Buffer for the partial outgoing packet being constructed. */
100static Buffer outgoing_packet;
101
102/* Buffer for the incoming packet currently being processed. */
103static Buffer incoming_packet;
104
105/* Scratch buffer for packet compression/decompression. */
106static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000107static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
109/* Flag indicating whether packet compression/decompression is enabled. */
110static int packet_compression = 0;
111
Damien Miller6162d121999-11-21 13:23:52 +1100112/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000113u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100114
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115/* Flag indicating whether this module has been initialized. */
116static int initialized = 0;
117
118/* Set to true if the connection is interactive. */
119static int interactive_mode = 0;
120
Damien Miller9786e6e2005-07-26 21:54:56 +1000121/* Set to true if we are the server side. */
122static int server_side = 0;
123
124/* Set to true if we are authenticated. */
125static int after_authentication = 0;
126
Damien Miller33b13562000-04-04 14:38:59 +1000127/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000128Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000129static struct packet_state {
130 u_int32_t seqnr;
131 u_int32_t packets;
132 u_int64_t blocks;
133} p_read, p_send;
134
135static u_int64_t max_blocks_in, max_blocks_out;
136static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000137
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000138/* Session key for protocol v1 */
139static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
140static u_int ssh1_keylen;
141
Damien Miller9f643902001-11-12 11:02:52 +1100142/* roundup current message to extra_pad bytes */
143static u_char extra_pad = 0;
144
Damien Millera5539d22003-04-09 20:50:06 +1000145struct packet {
146 TAILQ_ENTRY(packet) next;
147 u_char type;
148 Buffer payload;
149};
150TAILQ_HEAD(, packet) outgoing;
151
Damien Miller5428f641999-11-25 11:54:57 +1100152/*
153 * Sets the descriptors used for communication. Disables encryption until
154 * packet_set_encryption_key is called.
155 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156void
157packet_set_connection(int fd_in, int fd_out)
158{
Damien Miller874d77b2000-10-14 16:23:11 +1100159 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000160
Damien Miller874d77b2000-10-14 16:23:11 +1100161 if (none == NULL)
162 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100163 connection_in = fd_in;
164 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000165 cipher_init(&send_context, none, (const u_char *)"",
166 0, NULL, 0, CIPHER_ENCRYPT);
167 cipher_init(&receive_context, none, (const u_char *)"",
168 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000169 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100170 if (!initialized) {
171 initialized = 1;
172 buffer_init(&input);
173 buffer_init(&output);
174 buffer_init(&outgoing_packet);
175 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000176 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100177 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
Damien Miller34132e52000-01-14 15:45:46 +1100180/* Returns 1 if remote host is connected via socket, 0 if not. */
181
182int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000183packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100184{
185 struct sockaddr_storage from, to;
186 socklen_t fromlen, tolen;
187
188 /* filedescriptors in and out are the same, so it's a socket */
189 if (connection_in == connection_out)
190 return 1;
191 fromlen = sizeof(from);
192 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100193 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100194 return 0;
195 tolen = sizeof(to);
196 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100197 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100198 return 0;
199 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
200 return 0;
201 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
202 return 0;
203 return 1;
204}
205
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000206/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000207 * Exports an IV from the CipherContext required to export the key
208 * state back from the unprivileged child to the privileged parent
209 * process.
210 */
211
212void
213packet_get_keyiv(int mode, u_char *iv, u_int len)
214{
215 CipherContext *cc;
216
217 if (mode == MODE_OUT)
218 cc = &send_context;
219 else
220 cc = &receive_context;
221
222 cipher_get_keyiv(cc, iv, len);
223}
224
225int
226packet_get_keycontext(int mode, u_char *dat)
227{
228 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000229
Ben Lindstromf6027d32002-03-22 01:42:04 +0000230 if (mode == MODE_OUT)
231 cc = &send_context;
232 else
233 cc = &receive_context;
234
235 return (cipher_get_keycontext(cc, dat));
236}
237
238void
239packet_set_keycontext(int mode, u_char *dat)
240{
241 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000242
Ben Lindstromf6027d32002-03-22 01:42:04 +0000243 if (mode == MODE_OUT)
244 cc = &send_context;
245 else
246 cc = &receive_context;
247
248 cipher_set_keycontext(cc, dat);
249}
250
251int
252packet_get_keyiv_len(int mode)
253{
254 CipherContext *cc;
255
256 if (mode == MODE_OUT)
257 cc = &send_context;
258 else
259 cc = &receive_context;
260
261 return (cipher_get_keyiv_len(cc));
262}
263void
264packet_set_iv(int mode, u_char *dat)
265{
266 CipherContext *cc;
267
268 if (mode == MODE_OUT)
269 cc = &send_context;
270 else
271 cc = &receive_context;
272
273 cipher_set_keyiv(cc, dat);
274}
275int
Damien Miller2b92d322003-06-11 22:05:06 +1000276packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000277{
278 return (cipher_get_number(receive_context.cipher));
279}
280
Damien Millera5539d22003-04-09 20:50:06 +1000281void
282packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000283{
Damien Millera5539d22003-04-09 20:50:06 +1000284 struct packet_state *state;
285
286 state = (mode == MODE_IN) ? &p_read : &p_send;
287 *seqnr = state->seqnr;
288 *blocks = state->blocks;
289 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000290}
291
292void
Damien Millera5539d22003-04-09 20:50:06 +1000293packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000294{
Damien Millera5539d22003-04-09 20:50:06 +1000295 struct packet_state *state;
296
297 state = (mode == MODE_IN) ? &p_read : &p_send;
298 state->seqnr = seqnr;
299 state->blocks = blocks;
300 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000301}
302
Damien Miller34132e52000-01-14 15:45:46 +1100303/* returns 1 if connection is via ipv4 */
304
305int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000306packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100307{
308 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100309 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100310
311 memset(&to, 0, sizeof(to));
312 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
313 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000314 if (to.ss_family == AF_INET)
315 return 1;
316#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100317 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000318 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
319 return 1;
320#endif
321 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100322}
323
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324/* Sets the connection into non-blocking mode. */
325
326void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000327packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328{
Damien Miller95def091999-11-25 00:26:21 +1100329 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000330 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Damien Miller232711f2004-06-15 10:35:30 +1000332 if (connection_out != connection_in)
333 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334}
335
336/* Returns the socket used for reading. */
337
338int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000339packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340{
Damien Miller95def091999-11-25 00:26:21 +1100341 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342}
343
344/* Returns the descriptor used for writing. */
345
346int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000347packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348{
Damien Miller95def091999-11-25 00:26:21 +1100349 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350}
351
352/* Closes the connection and clears and frees internal data structures. */
353
354void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000355packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356{
Damien Miller95def091999-11-25 00:26:21 +1100357 if (!initialized)
358 return;
359 initialized = 0;
360 if (connection_in == connection_out) {
361 shutdown(connection_out, SHUT_RDWR);
362 close(connection_out);
363 } else {
364 close(connection_in);
365 close(connection_out);
366 }
367 buffer_free(&input);
368 buffer_free(&output);
369 buffer_free(&outgoing_packet);
370 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000371 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100372 buffer_free(&compression_buffer);
373 buffer_compress_uninit();
374 }
Damien Miller963f6b22002-02-19 15:21:23 +1100375 cipher_cleanup(&send_context);
376 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377}
378
379/* Sets remote side protocol flags. */
380
381void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000382packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383{
Damien Miller95def091999-11-25 00:26:21 +1100384 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385}
386
387/* Returns the remote protocol flags set earlier by the above function. */
388
Ben Lindstrom46c16222000-12-22 01:43:59 +0000389u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000390packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391{
Damien Miller95def091999-11-25 00:26:21 +1100392 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393}
394
Damien Miller5428f641999-11-25 11:54:57 +1100395/*
396 * Starts packet compression from the next packet on in both directions.
397 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
398 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399
Ben Lindstrombba81212001-06-25 05:01:22 +0000400static void
401packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000402{
403 if (compression_buffer_ready == 1)
404 return;
405 compression_buffer_ready = 1;
406 buffer_init(&compression_buffer);
407}
408
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409void
410packet_start_compression(int level)
411{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000412 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100413 fatal("Compression already enabled.");
414 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000415 packet_init_compression();
416 buffer_compress_init_send(level);
417 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418}
419
Damien Miller5428f641999-11-25 11:54:57 +1100420/*
Damien Miller5428f641999-11-25 11:54:57 +1100421 * Causes any further packets to be encrypted using the given key. The same
422 * key is used for both sending and reception. However, both directions are
423 * encrypted independently of each other.
424 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000425
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000427packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100428 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429{
Damien Miller874d77b2000-10-14 16:23:11 +1100430 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000431
Damien Miller874d77b2000-10-14 16:23:11 +1100432 if (cipher == NULL)
433 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000434 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100435 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000436 if (keylen > SSH_SESSION_KEY_LENGTH)
437 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
438 memcpy(ssh1_key, key, keylen);
439 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100440 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
441 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442}
443
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000444u_int
445packet_get_encryption_key(u_char *key)
446{
447 if (key == NULL)
448 return (ssh1_keylen);
449 memcpy(key, ssh1_key, ssh1_keylen);
450 return (ssh1_keylen);
451}
452
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000453/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000455packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000457 u_char buf[9];
458 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Ben Lindstrom204e4882001-03-05 06:47:00 +0000460 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000461 len = compat20 ? 6 : 9;
462 memset(buf, 0, len - 1);
463 buf[len - 1] = type;
464 buffer_clear(&outgoing_packet);
465 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000466}
467
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000468/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469void
470packet_put_char(int value)
471{
Damien Miller95def091999-11-25 00:26:21 +1100472 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000473
Damien Miller95def091999-11-25 00:26:21 +1100474 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000477packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478{
Damien Miller95def091999-11-25 00:26:21 +1100479 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100482packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483{
Damien Miller95def091999-11-25 00:26:21 +1100484 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485}
Damien Miller33b13562000-04-04 14:38:59 +1000486void
487packet_put_cstring(const char *str)
488{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000489 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000490}
Damien Miller33b13562000-04-04 14:38:59 +1000491void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100492packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000493{
494 buffer_append(&outgoing_packet, buf, len);
495}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496void
Damien Miller95def091999-11-25 00:26:21 +1100497packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498{
Damien Miller95def091999-11-25 00:26:21 +1100499 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500}
Damien Miller33b13562000-04-04 14:38:59 +1000501void
502packet_put_bignum2(BIGNUM * value)
503{
504 buffer_put_bignum2(&outgoing_packet, value);
505}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506
Damien Miller5428f641999-11-25 11:54:57 +1100507/*
508 * Finalizes and sends the packet. If the encryption key has been set,
509 * encrypts the packet before sending.
510 */
Damien Miller95def091999-11-25 00:26:21 +1100511
Ben Lindstrombba81212001-06-25 05:01:22 +0000512static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000513packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000515 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100516 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000517 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000518 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Damien Miller5428f641999-11-25 11:54:57 +1100520 /*
521 * If using packet compression, compress the payload of the outgoing
522 * packet.
523 */
Damien Miller95def091999-11-25 00:26:21 +1100524 if (packet_compression) {
525 buffer_clear(&compression_buffer);
526 /* Skip padding. */
527 buffer_consume(&outgoing_packet, 8);
528 /* padding */
529 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
530 buffer_compress(&outgoing_packet, &compression_buffer);
531 buffer_clear(&outgoing_packet);
532 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100533 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100534 }
535 /* Compute packet length without padding (add checksum, remove padding). */
536 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537
Damien Millere247cc42000-05-07 12:03:14 +1000538 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100539 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100540 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100541 cp = buffer_ptr(&outgoing_packet);
542 for (i = 0; i < padding; i++) {
543 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000544 rnd = arc4random();
545 cp[7 - i] = rnd & 0xff;
546 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100547 }
548 }
549 buffer_consume(&outgoing_packet, 8 - padding);
550
551 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100552 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000553 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100554 PUT_32BIT(buf, checksum);
555 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556
557#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100558 fprintf(stderr, "packet_send plain: ");
559 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560#endif
561
Damien Miller95def091999-11-25 00:26:21 +1100562 /* Append to output. */
563 PUT_32BIT(buf, len);
564 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100565 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100566 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100567 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100568
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100570 fprintf(stderr, "encrypted: ");
571 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572#endif
573
Damien Miller95def091999-11-25 00:26:21 +1100574 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Damien Miller5428f641999-11-25 11:54:57 +1100576 /*
Damien Miller788f2122005-11-05 15:14:59 +1100577 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100578 * actually sent until packet_write_wait or packet_write_poll is
579 * called.
580 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581}
582
Ben Lindstromf6027d32002-03-22 01:42:04 +0000583void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000584set_newkeys(int mode)
585{
586 Enc *enc;
587 Mac *mac;
588 Comp *comp;
589 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000590 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000591 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000592
Ben Lindstrom064496f2002-12-23 02:04:22 +0000593 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000594
Damien Miller963f6b22002-02-19 15:21:23 +1100595 if (mode == MODE_OUT) {
596 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000597 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000598 p_send.packets = p_send.blocks = 0;
599 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100600 } else {
601 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000602 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000603 p_read.packets = p_read.blocks = 0;
604 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100605 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000606 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000607 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100608 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000609 enc = &newkeys[mode]->enc;
610 mac = &newkeys[mode]->mac;
611 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000612 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000613 xfree(enc->name);
614 xfree(enc->iv);
615 xfree(enc->key);
616 xfree(mac->name);
617 xfree(mac->key);
618 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000619 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000620 }
621 newkeys[mode] = kex_get_newkeys(mode);
622 if (newkeys[mode] == NULL)
623 fatal("newkeys: no keys for mode %d", mode);
624 enc = &newkeys[mode]->enc;
625 mac = &newkeys[mode]->mac;
626 comp = &newkeys[mode]->comp;
627 if (mac->md != NULL)
628 mac->enabled = 1;
629 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100630 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000631 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000632 /* Deleting the keys does not gain extra security */
633 /* memset(enc->iv, 0, enc->block_size);
634 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000635 if ((comp->type == COMP_ZLIB ||
636 (comp->type == COMP_DELAYED && after_authentication)) &&
637 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000638 packet_init_compression();
639 if (mode == MODE_OUT)
640 buffer_compress_init_send(6);
641 else
642 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000643 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000644 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000645 /*
646 * The 2^(blocksize*2) limit is too expensive for 3DES,
647 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
648 */
649 if (enc->block_size >= 16)
650 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
651 else
652 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000653 if (rekey_limit)
654 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000655}
656
Damien Miller5428f641999-11-25 11:54:57 +1100657/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000658 * Delayed compression for SSH2 is enabled after authentication:
659 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
660 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
661 */
662static void
663packet_enable_delayed_compress(void)
664{
665 Comp *comp = NULL;
666 int mode;
667
668 /*
669 * Remember that we are past the authentication step, so rekeying
670 * with COMP_DELAYED will turn on compression immediately.
671 */
672 after_authentication = 1;
673 for (mode = 0; mode < MODE_MAX; mode++) {
674 comp = &newkeys[mode]->comp;
675 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000676 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000677 if (mode == MODE_OUT)
678 buffer_compress_init_send(6);
679 else
680 buffer_compress_init_recv();
681 comp->enabled = 1;
682 }
683 }
684}
685
686/*
Damien Miller33b13562000-04-04 14:38:59 +1000687 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
688 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000689static void
Damien Millera5539d22003-04-09 20:50:06 +1000690packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000691{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000692 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100693 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000694 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100695 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000696 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000697 Enc *enc = NULL;
698 Mac *mac = NULL;
699 Comp *comp = NULL;
700 int block_size;
701
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000702 if (newkeys[MODE_OUT] != NULL) {
703 enc = &newkeys[MODE_OUT]->enc;
704 mac = &newkeys[MODE_OUT]->mac;
705 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000706 }
Damien Miller963f6b22002-02-19 15:21:23 +1100707 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000708
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000709 cp = buffer_ptr(&outgoing_packet);
710 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000711
712#ifdef PACKET_DEBUG
713 fprintf(stderr, "plain: ");
714 buffer_dump(&outgoing_packet);
715#endif
716
717 if (comp && comp->enabled) {
718 len = buffer_len(&outgoing_packet);
719 /* skip header, compress only payload */
720 buffer_consume(&outgoing_packet, 5);
721 buffer_clear(&compression_buffer);
722 buffer_compress(&outgoing_packet, &compression_buffer);
723 buffer_clear(&outgoing_packet);
724 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
725 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
726 buffer_len(&compression_buffer));
727 DBG(debug("compression: raw %d compressed %d", len,
728 buffer_len(&outgoing_packet)));
729 }
730
731 /* sizeof (packet_len + pad_len + payload) */
732 len = buffer_len(&outgoing_packet);
733
734 /*
735 * calc size of padding, alloc space, get random data,
736 * minimum padding is 4 bytes
737 */
738 padlen = block_size - (len % block_size);
739 if (padlen < 4)
740 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100741 if (extra_pad) {
742 /* will wrap if extra_pad+padlen > 255 */
743 extra_pad = roundup(extra_pad, block_size);
744 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000745 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100746 pad, len, padlen, extra_pad);
747 padlen += pad;
748 extra_pad = 0;
749 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100750 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100751 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000752 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000753 for (i = 0; i < padlen; i++) {
754 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000755 rnd = arc4random();
756 cp[i] = rnd & 0xff;
757 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000758 }
Damien Millere247cc42000-05-07 12:03:14 +1000759 } else {
760 /* clear padding */
761 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000762 }
763 /* packet_length includes payload, padding and padding length field */
764 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000765 cp = buffer_ptr(&outgoing_packet);
766 PUT_32BIT(cp, packet_length);
767 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000768 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
769
770 /* compute MAC over seqnr and packet(length fields, payload, padding) */
771 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000772 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100773 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000774 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000775 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000776 }
777 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100778 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100779 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000780 buffer_len(&outgoing_packet));
781 /* append unencrypted MAC */
782 if (mac && mac->enabled)
783 buffer_append(&output, (char *)macbuf, mac->mac_len);
784#ifdef PACKET_DEBUG
785 fprintf(stderr, "encrypted: ");
786 buffer_dump(&output);
787#endif
Damien Miller4af51302000-04-16 11:18:38 +1000788 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000789 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000790 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000791 if (++p_send.packets == 0)
792 if (!(datafellows & SSH_BUG_NOREKEY))
793 fatal("XXX too many packets with same key");
794 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000795 buffer_clear(&outgoing_packet);
796
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000797 if (type == SSH2_MSG_NEWKEYS)
798 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000799 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
800 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000801}
802
Damien Millera5539d22003-04-09 20:50:06 +1000803static void
804packet_send2(void)
805{
806 static int rekeying = 0;
807 struct packet *p;
808 u_char type, *cp;
809
810 cp = buffer_ptr(&outgoing_packet);
811 type = cp[5];
812
813 /* during rekeying we can only send key exchange messages */
814 if (rekeying) {
815 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
816 (type <= SSH2_MSG_TRANSPORT_MAX))) {
817 debug("enqueue packet: %u", type);
818 p = xmalloc(sizeof(*p));
819 p->type = type;
820 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
821 buffer_init(&outgoing_packet);
822 TAILQ_INSERT_TAIL(&outgoing, p, next);
823 return;
824 }
825 }
826
827 /* rekeying starts with sending KEXINIT */
828 if (type == SSH2_MSG_KEXINIT)
829 rekeying = 1;
830
831 packet_send2_wrapped();
832
833 /* after a NEWKEYS message we can send the complete queue */
834 if (type == SSH2_MSG_NEWKEYS) {
835 rekeying = 0;
836 while ((p = TAILQ_FIRST(&outgoing))) {
837 type = p->type;
838 debug("dequeue packet: %u", type);
839 buffer_free(&outgoing_packet);
840 memcpy(&outgoing_packet, &p->payload,
841 sizeof(Buffer));
842 TAILQ_REMOVE(&outgoing, p, next);
843 xfree(p);
844 packet_send2_wrapped();
845 }
846 }
847}
848
Damien Miller33b13562000-04-04 14:38:59 +1000849void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000850packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000851{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000852 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000853 packet_send2();
854 else
855 packet_send1();
856 DBG(debug("packet_send done"));
857}
858
Damien Miller33b13562000-04-04 14:38:59 +1000859/*
Damien Miller5428f641999-11-25 11:54:57 +1100860 * Waits until a packet has been received, and returns its type. Note that
861 * no other data is processed until this returns, so this function should not
862 * be used during the interactive session.
863 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864
865int
Damien Millerdff50992002-01-22 23:16:32 +1100866packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867{
Damien Miller95def091999-11-25 00:26:21 +1100868 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000869 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100870 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000871 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000873 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
874 sizeof(fd_mask));
875
Damien Miller95def091999-11-25 00:26:21 +1100876 /* Since we are blocking, ensure that all written packets have been sent. */
877 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878
Damien Miller95def091999-11-25 00:26:21 +1100879 /* Stay in the loop until we have received a complete packet. */
880 for (;;) {
881 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100882 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000883 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000884 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100885 || type == SSH_SMSG_FAILURE
886 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000887 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100888 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100889 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000890 if (type != SSH_MSG_NONE) {
891 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100892 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000893 }
Damien Miller5428f641999-11-25 11:54:57 +1100894 /*
895 * Otherwise, wait for some data to arrive, add it to the
896 * buffer, and try again.
897 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000898 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
899 sizeof(fd_mask));
900 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100901
Damien Miller95def091999-11-25 00:26:21 +1100902 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000903 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000904 (errno == EAGAIN || errno == EINTR))
905 ;
Damien Miller5428f641999-11-25 11:54:57 +1100906
Damien Miller95def091999-11-25 00:26:21 +1100907 /* Read data from the socket. */
908 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100909 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000910 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000911 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100912 }
Damien Miller95def091999-11-25 00:26:21 +1100913 if (len < 0)
914 fatal("Read from socket failed: %.100s", strerror(errno));
915 /* Append it to the buffer. */
916 packet_process_incoming(buf, len);
917 }
918 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919}
920
Damien Miller278f9072001-12-21 15:00:19 +1100921int
Damien Millerdff50992002-01-22 23:16:32 +1100922packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100923{
Damien Millerdff50992002-01-22 23:16:32 +1100924 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100925}
926
Damien Miller5428f641999-11-25 11:54:57 +1100927/*
928 * Waits until a packet has been received, verifies that its type matches
929 * that given, and gives a fatal error and exits if there is a mismatch.
930 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931
932void
Damien Millerdff50992002-01-22 23:16:32 +1100933packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934{
Damien Miller95def091999-11-25 00:26:21 +1100935 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936
Damien Millerdff50992002-01-22 23:16:32 +1100937 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100938 if (type != expected_type)
939 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000940 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941}
942
943/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100944 * packet_process_incoming. If so, reads the packet; otherwise returns
945 * SSH_MSG_NONE. This does not wait for data from the connection.
946 *
947 * SSH_MSG_DISCONNECT is handled specially here. Also,
948 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
949 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100950 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Ben Lindstrombba81212001-06-25 05:01:22 +0000952static int
Damien Millerdff50992002-01-22 23:16:32 +1100953packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000955 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000956 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000957 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958
Damien Miller95def091999-11-25 00:26:21 +1100959 /* Check if input size is less than minimum packet size. */
960 if (buffer_len(&input) < 4 + 8)
961 return SSH_MSG_NONE;
962 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000963 cp = buffer_ptr(&input);
964 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100965 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000966 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100967 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Damien Miller95def091999-11-25 00:26:21 +1100969 /* Check if the packet has been entirely received. */
970 if (buffer_len(&input) < 4 + padded_len)
971 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Damien Miller95def091999-11-25 00:26:21 +1100973 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974
Damien Miller95def091999-11-25 00:26:21 +1100975 /* Consume packet length. */
976 buffer_consume(&input, 4);
977
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000978 /*
979 * Cryptographic attack detector for ssh
980 * (C)1998 CORE-SDI, Buenos Aires Argentina
981 * Ariel Futoransky(futo@core-sdi.com)
982 */
Damien Miller963f6b22002-02-19 15:21:23 +1100983 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000984 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
985 packet_disconnect("crc32 compensation attack: network attack detected");
986
987 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100988 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100989 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100990 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000991
Damien Miller95def091999-11-25 00:26:21 +1100992 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993
994#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100995 fprintf(stderr, "read_poll plain: ");
996 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998
Damien Miller95def091999-11-25 00:26:21 +1100999 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001000 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001001 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
Damien Miller95def091999-11-25 00:26:21 +11001003 /* Skip padding. */
1004 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001005
Damien Miller95def091999-11-25 00:26:21 +11001006 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001007 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001008 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001009 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001010
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001011 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
1012 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +11001013 if (checksum != stored_checksum)
1014 packet_disconnect("Corrupted check bytes on input.");
1015 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016
Damien Miller95def091999-11-25 00:26:21 +11001017 if (packet_compression) {
1018 buffer_clear(&compression_buffer);
1019 buffer_uncompress(&incoming_packet, &compression_buffer);
1020 buffer_clear(&incoming_packet);
1021 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001022 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001023 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001024 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001025 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1026 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001027 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028}
Damien Miller95def091999-11-25 00:26:21 +11001029
Ben Lindstrombba81212001-06-25 05:01:22 +00001030static int
Damien Millerdff50992002-01-22 23:16:32 +11001031packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001032{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001033 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001034 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001035 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001036 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001037 Enc *enc = NULL;
1038 Mac *mac = NULL;
1039 Comp *comp = NULL;
1040
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001041 if (newkeys[MODE_IN] != NULL) {
1042 enc = &newkeys[MODE_IN]->enc;
1043 mac = &newkeys[MODE_IN]->mac;
1044 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001045 }
1046 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001047 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001048
1049 if (packet_length == 0) {
1050 /*
1051 * check if input size is less than the cipher block size,
1052 * decrypt first block and extract length of incoming packet
1053 */
1054 if (buffer_len(&input) < block_size)
1055 return SSH_MSG_NONE;
1056 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001057 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001058 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001059 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001060 cp = buffer_ptr(&incoming_packet);
1061 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001062 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001063#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001064 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001065#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001066 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001067 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001068 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001069 buffer_consume(&input, block_size);
1070 }
1071 /* we have a partial packet of block_size bytes */
1072 need = 4 + packet_length - block_size;
1073 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1074 need, maclen));
1075 if (need % block_size != 0)
1076 fatal("padding error: need %d block %d mod %d",
1077 need, block_size, need % block_size);
1078 /*
1079 * check if the entire packet has been received and
1080 * decrypt into incoming_packet
1081 */
1082 if (buffer_len(&input) < need + maclen)
1083 return SSH_MSG_NONE;
1084#ifdef PACKET_DEBUG
1085 fprintf(stderr, "read_poll enc/full: ");
1086 buffer_dump(&input);
1087#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001088 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001089 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001090 buffer_consume(&input, need);
1091 /*
1092 * compute MAC over seqnr and packet,
1093 * increment sequence number for incoming packet
1094 */
Damien Miller4af51302000-04-16 11:18:38 +10001095 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001096 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001097 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001098 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001099 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001100 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001101 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001102 buffer_consume(&input, mac->mac_len);
1103 }
Damien Miller278f9072001-12-21 15:00:19 +11001104 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001105 *seqnr_p = p_read.seqnr;
1106 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001107 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001108 if (++p_read.packets == 0)
1109 if (!(datafellows & SSH_BUG_NOREKEY))
1110 fatal("XXX too many packets with same key");
1111 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001112
1113 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001114 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001115 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001116 DBG(debug("input: padlen %d", padlen));
1117 if (padlen < 4)
1118 packet_disconnect("Corrupted padlen %d on input.", padlen);
1119
1120 /* skip packet size + padlen, discard padding */
1121 buffer_consume(&incoming_packet, 4 + 1);
1122 buffer_consume_end(&incoming_packet, padlen);
1123
1124 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1125 if (comp && comp->enabled) {
1126 buffer_clear(&compression_buffer);
1127 buffer_uncompress(&incoming_packet, &compression_buffer);
1128 buffer_clear(&incoming_packet);
1129 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1130 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001131 DBG(debug("input: len after de-compress %d",
1132 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001133 }
1134 /*
1135 * get packet type, implies consume.
1136 * return length of payload (without type field)
1137 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001138 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001139 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1140 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001141 if (type == SSH2_MSG_NEWKEYS)
1142 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001143 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1144 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001145#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001146 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001147 buffer_dump(&incoming_packet);
1148#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001149 /* reset for next packet */
1150 packet_length = 0;
1151 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001152}
1153
1154int
Damien Millerdff50992002-01-22 23:16:32 +11001155packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001156{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001157 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001158 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001159 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001160
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001161 for (;;) {
1162 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001163 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001164 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001165 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001166 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001167 case SSH2_MSG_IGNORE:
1168 break;
1169 case SSH2_MSG_DEBUG:
1170 packet_get_char();
1171 msg = packet_get_string(NULL);
1172 debug("Remote: %.900s", msg);
1173 xfree(msg);
1174 msg = packet_get_string(NULL);
1175 xfree(msg);
1176 break;
1177 case SSH2_MSG_DISCONNECT:
1178 reason = packet_get_int();
1179 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001180 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001181 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001182 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001183 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001184 break;
Damien Miller659811f2002-01-22 23:23:11 +11001185 case SSH2_MSG_UNIMPLEMENTED:
1186 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001187 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1188 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001189 break;
Damien Miller33b13562000-04-04 14:38:59 +10001190 default:
1191 return type;
1192 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001193 }
Damien Miller33b13562000-04-04 14:38:59 +10001194 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001195 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001196 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001197 case SSH_MSG_IGNORE:
1198 break;
1199 case SSH_MSG_DEBUG:
1200 msg = packet_get_string(NULL);
1201 debug("Remote: %.900s", msg);
1202 xfree(msg);
1203 break;
1204 case SSH_MSG_DISCONNECT:
1205 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001206 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001207 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001208 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001209 xfree(msg);
1210 break;
1211 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001212 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001213 DBG(debug("received packet type %d", type));
1214 return type;
1215 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001216 }
Damien Miller33b13562000-04-04 14:38:59 +10001217 }
1218 }
1219}
1220
Damien Miller278f9072001-12-21 15:00:19 +11001221int
Damien Millerdff50992002-01-22 23:16:32 +11001222packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001223{
Damien Millerdff50992002-01-22 23:16:32 +11001224 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001225}
1226
Damien Miller5428f641999-11-25 11:54:57 +11001227/*
1228 * Buffers the given amount of input characters. This is intended to be used
1229 * together with packet_read_poll.
1230 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001231
1232void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001233packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234{
Damien Miller95def091999-11-25 00:26:21 +11001235 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001236}
1237
1238/* Returns a character from the packet. */
1239
Ben Lindstrom46c16222000-12-22 01:43:59 +00001240u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001241packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242{
Damien Miller95def091999-11-25 00:26:21 +11001243 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001244
Damien Miller95def091999-11-25 00:26:21 +11001245 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001246 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247}
1248
1249/* Returns an integer from the packet data. */
1250
Ben Lindstrom46c16222000-12-22 01:43:59 +00001251u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001252packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253{
Damien Miller95def091999-11-25 00:26:21 +11001254 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255}
1256
Damien Miller5428f641999-11-25 11:54:57 +11001257/*
1258 * Returns an arbitrary precision integer from the packet data. The integer
1259 * must have been initialized before this call.
1260 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001261
1262void
Damien Millerd432ccf2002-01-22 23:14:44 +11001263packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264{
Damien Miller76e1e362002-01-22 23:15:57 +11001265 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266}
1267
Damien Miller33b13562000-04-04 14:38:59 +10001268void
Damien Millerd432ccf2002-01-22 23:14:44 +11001269packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001270{
Damien Miller76e1e362002-01-22 23:15:57 +11001271 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001272}
1273
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001274void *
Damien Millereccb9de2005-06-17 12:59:34 +10001275packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001276{
Damien Millereccb9de2005-06-17 12:59:34 +10001277 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001278
Damien Miller33b13562000-04-04 14:38:59 +10001279 if (length_ptr != NULL)
1280 *length_ptr = bytes;
1281 return buffer_ptr(&incoming_packet);
1282}
1283
Damien Miller4af51302000-04-16 11:18:38 +10001284int
1285packet_remaining(void)
1286{
1287 return buffer_len(&incoming_packet);
1288}
1289
Damien Miller5428f641999-11-25 11:54:57 +11001290/*
1291 * Returns a string from the packet data. The string is allocated using
1292 * xmalloc; it is the responsibility of the calling program to free it when
1293 * no longer needed. The length_ptr argument may be NULL, or point to an
1294 * integer into which the length of the string is stored.
1295 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001297void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001298packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299{
Damien Miller95def091999-11-25 00:26:21 +11001300 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301}
1302
Damien Miller5428f641999-11-25 11:54:57 +11001303/*
1304 * Sends a diagnostic message from the server to the client. This message
1305 * can be sent at any time (but not while constructing another message). The
1306 * message is printed immediately, but only if the client is being executed
1307 * in verbose mode. These messages are primarily intended to ease debugging
1308 * authentication problems. The length of the formatted message must not
1309 * exceed 1024 bytes. This will automatically call packet_write_wait.
1310 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001311
1312void
Damien Miller95def091999-11-25 00:26:21 +11001313packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001314{
Damien Miller95def091999-11-25 00:26:21 +11001315 char buf[1024];
1316 va_list args;
1317
Ben Lindstroma14ee472000-12-07 01:24:58 +00001318 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1319 return;
1320
Damien Miller95def091999-11-25 00:26:21 +11001321 va_start(args, fmt);
1322 vsnprintf(buf, sizeof(buf), fmt, args);
1323 va_end(args);
1324
Damien Miller7c8af4f2000-05-01 08:24:07 +10001325 if (compat20) {
1326 packet_start(SSH2_MSG_DEBUG);
1327 packet_put_char(0); /* bool: always display */
1328 packet_put_cstring(buf);
1329 packet_put_cstring("");
1330 } else {
1331 packet_start(SSH_MSG_DEBUG);
1332 packet_put_cstring(buf);
1333 }
Damien Miller95def091999-11-25 00:26:21 +11001334 packet_send();
1335 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001336}
1337
Damien Miller5428f641999-11-25 11:54:57 +11001338/*
1339 * Logs the error plus constructs and sends a disconnect packet, closes the
1340 * connection, and exits. This function never returns. The error message
1341 * should not contain a newline. The length of the formatted message must
1342 * not exceed 1024 bytes.
1343 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001344
1345void
Damien Miller95def091999-11-25 00:26:21 +11001346packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001347{
Damien Miller95def091999-11-25 00:26:21 +11001348 char buf[1024];
1349 va_list args;
1350 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001351
Damien Miller95def091999-11-25 00:26:21 +11001352 if (disconnecting) /* Guard against recursive invocations. */
1353 fatal("packet_disconnect called recursively.");
1354 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355
Damien Miller5428f641999-11-25 11:54:57 +11001356 /*
1357 * Format the message. Note that the caller must make sure the
1358 * message is of limited size.
1359 */
Damien Miller95def091999-11-25 00:26:21 +11001360 va_start(args, fmt);
1361 vsnprintf(buf, sizeof(buf), fmt, args);
1362 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001364 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001365 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001366
Damien Miller95def091999-11-25 00:26:21 +11001367 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001368 if (compat20) {
1369 packet_start(SSH2_MSG_DISCONNECT);
1370 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1371 packet_put_cstring(buf);
1372 packet_put_cstring("");
1373 } else {
1374 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001375 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001376 }
Damien Miller95def091999-11-25 00:26:21 +11001377 packet_send();
1378 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379
Damien Miller95def091999-11-25 00:26:21 +11001380 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001381 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001382
Damien Miller95def091999-11-25 00:26:21 +11001383 /* Close the connection. */
1384 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001385 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001386}
1387
Damien Miller5428f641999-11-25 11:54:57 +11001388/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389
1390void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001391packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392{
Damien Miller95def091999-11-25 00:26:21 +11001393 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001394
Damien Miller95def091999-11-25 00:26:21 +11001395 if (len > 0) {
1396 len = write(connection_out, buffer_ptr(&output), len);
1397 if (len <= 0) {
1398 if (errno == EAGAIN)
1399 return;
1400 else
1401 fatal("Write failed: %.100s", strerror(errno));
1402 }
1403 buffer_consume(&output, len);
1404 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405}
1406
Damien Miller5428f641999-11-25 11:54:57 +11001407/*
1408 * Calls packet_write_poll repeatedly until all pending output data has been
1409 * written.
1410 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001411
1412void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001413packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001415 fd_set *setp;
1416
1417 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1418 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001419 packet_write_poll();
1420 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001421 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1422 sizeof(fd_mask));
1423 FD_SET(connection_out, setp);
1424 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001425 (errno == EAGAIN || errno == EINTR))
1426 ;
Damien Miller95def091999-11-25 00:26:21 +11001427 packet_write_poll();
1428 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001429 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430}
1431
1432/* Returns true if there is buffered data to write to the connection. */
1433
1434int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001435packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001436{
Damien Miller95def091999-11-25 00:26:21 +11001437 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438}
1439
1440/* Returns true if there is not too much data to write to the connection. */
1441
1442int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001443packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444{
Damien Miller95def091999-11-25 00:26:21 +11001445 if (interactive_mode)
1446 return buffer_len(&output) < 16384;
1447 else
1448 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449}
1450
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001451
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001452static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001453packet_set_tos(int interactive)
1454{
Damien Miller5924ceb2003-11-22 15:02:42 +11001455#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001456 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1457
1458 if (!packet_connection_is_on_socket() ||
1459 !packet_connection_is_ipv4())
1460 return;
1461 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1462 sizeof(tos)) < 0)
1463 error("setsockopt IP_TOS %d: %.100s:",
1464 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001465#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001466}
Ben Lindstroma7433982002-12-23 02:41:41 +00001467
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468/* Informs that the current session is interactive. Sets IP flags for that. */
1469
1470void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001471packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001473 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001475 if (called)
1476 return;
1477 called = 1;
1478
Damien Miller95def091999-11-25 00:26:21 +11001479 /* Record that we are in interactive mode. */
1480 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481
Damien Miller34132e52000-01-14 15:45:46 +11001482 /* Only set socket options if using a socket. */
1483 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001484 return;
Ben Lindstroma7433982002-12-23 02:41:41 +00001485 if (interactive)
Damien Miller398e1cf2002-02-05 11:52:13 +11001486 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001487 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488}
1489
1490/* Returns true if the current connection is interactive. */
1491
1492int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001493packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494{
Damien Miller95def091999-11-25 00:26:21 +11001495 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001496}
Damien Miller6162d121999-11-21 13:23:52 +11001497
Darren Tucker1f8311c2004-05-13 16:39:33 +10001498int
Darren Tucker502d3842003-06-28 12:38:01 +10001499packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001500{
Damien Miller95def091999-11-25 00:26:21 +11001501 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001502
Damien Miller95def091999-11-25 00:26:21 +11001503 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001504 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001505 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001506 return -1;
1507 }
1508 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001509 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001510 return -1;
1511 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001512 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001513 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001514 max_packet_size = s;
1515 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001516}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001517
Damien Miller9f643902001-11-12 11:02:52 +11001518/* roundup current message to pad bytes */
1519void
1520packet_add_padding(u_char pad)
1521{
1522 extra_pad = pad;
1523}
1524
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001525/*
1526 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001527 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001528 * byte SSH_MSG_IGNORE
1529 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001530 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001531 * All implementations MUST understand (and ignore) this message at any
1532 * time (after receiving the protocol version). No implementation is
1533 * required to send them. This message can be used as an additional
1534 * protection measure against advanced traffic analysis techniques.
1535 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001536void
1537packet_send_ignore(int nbytes)
1538{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001539 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001540 int i;
1541
1542 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001543 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001544 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001545 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001546 rnd = arc4random();
1547 packet_put_char(rnd & 0xff);
1548 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001549 }
1550}
Damien Millera5539d22003-04-09 20:50:06 +10001551
Darren Tucker1f8311c2004-05-13 16:39:33 +10001552#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001553int
1554packet_need_rekeying(void)
1555{
1556 if (datafellows & SSH_BUG_NOREKEY)
1557 return 0;
1558 return
1559 (p_send.packets > MAX_PACKETS) ||
1560 (p_read.packets > MAX_PACKETS) ||
1561 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1562 (max_blocks_in && (p_read.blocks > max_blocks_in));
1563}
1564
1565void
1566packet_set_rekey_limit(u_int32_t bytes)
1567{
1568 rekey_limit = bytes;
1569}
Damien Miller9786e6e2005-07-26 21:54:56 +10001570
1571void
1572packet_set_server(void)
1573{
1574 server_side = 1;
1575}
1576
1577void
1578packet_set_authenticated(void)
1579{
1580 after_authentication = 1;
1581}