blob: db2aa24119d184496bb128bcc7baea62d185910d [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 Miller788f2122005-11-05 15:14:59 +110040RCSID("$OpenBSD: packet.c,v 1.120 2005/10/30 08:52:17 djm Exp $");
Damien Millera5539d22003-04-09 20:50:06 +100041
Damien Millera0898b82003-04-09 21:05:52 +100042#include "openbsd-compat/sys-queue.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include "xmalloc.h"
45#include "buffer.h"
46#include "packet.h"
47#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "getput.h"
50
51#include "compress.h"
52#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000053#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller33b13562000-04-04 14:38:59 +100055#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100057#include "ssh2.h"
58
Damien Miller874d77b2000-10-14 16:23:11 +110059#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100060#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000061#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "log.h"
63#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110064#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000065#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100066
67#ifdef PACKET_DEBUG
68#define DBG(x) x
69#else
70#define DBG(x)
71#endif
72
Damien Miller5428f641999-11-25 11:54:57 +110073/*
74 * This variable contains the file descriptors used for communicating with
75 * the other side. connection_in is used for reading; connection_out for
76 * writing. These can be the same descriptor, in which case it is assumed to
77 * be a socket.
78 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079static int connection_in = -1;
80static int connection_out = -1;
81
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000083static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
85/* Encryption context for receiving data. This is only used for decryption. */
86static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110087
88/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089static CipherContext send_context;
90
91/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000092Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000095Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
97/* Buffer for the partial outgoing packet being constructed. */
98static Buffer outgoing_packet;
99
100/* Buffer for the incoming packet currently being processed. */
101static Buffer incoming_packet;
102
103/* Scratch buffer for packet compression/decompression. */
104static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000105static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
107/* Flag indicating whether packet compression/decompression is enabled. */
108static int packet_compression = 0;
109
Damien Miller6162d121999-11-21 13:23:52 +1100110/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000111u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100112
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113/* Flag indicating whether this module has been initialized. */
114static int initialized = 0;
115
116/* Set to true if the connection is interactive. */
117static int interactive_mode = 0;
118
Damien Miller9786e6e2005-07-26 21:54:56 +1000119/* Set to true if we are the server side. */
120static int server_side = 0;
121
122/* Set to true if we are authenticated. */
123static int after_authentication = 0;
124
Damien Miller33b13562000-04-04 14:38:59 +1000125/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000126Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000127static struct packet_state {
128 u_int32_t seqnr;
129 u_int32_t packets;
130 u_int64_t blocks;
131} p_read, p_send;
132
133static u_int64_t max_blocks_in, max_blocks_out;
134static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000135
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000136/* Session key for protocol v1 */
137static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
138static u_int ssh1_keylen;
139
Damien Miller9f643902001-11-12 11:02:52 +1100140/* roundup current message to extra_pad bytes */
141static u_char extra_pad = 0;
142
Damien Millera5539d22003-04-09 20:50:06 +1000143struct packet {
144 TAILQ_ENTRY(packet) next;
145 u_char type;
146 Buffer payload;
147};
148TAILQ_HEAD(, packet) outgoing;
149
Damien Miller5428f641999-11-25 11:54:57 +1100150/*
151 * Sets the descriptors used for communication. Disables encryption until
152 * packet_set_encryption_key is called.
153 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154void
155packet_set_connection(int fd_in, int fd_out)
156{
Damien Miller874d77b2000-10-14 16:23:11 +1100157 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000158
Damien Miller874d77b2000-10-14 16:23:11 +1100159 if (none == NULL)
160 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100161 connection_in = fd_in;
162 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000163 cipher_init(&send_context, none, (const u_char *)"",
164 0, NULL, 0, CIPHER_ENCRYPT);
165 cipher_init(&receive_context, none, (const u_char *)"",
166 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000167 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100168 if (!initialized) {
169 initialized = 1;
170 buffer_init(&input);
171 buffer_init(&output);
172 buffer_init(&outgoing_packet);
173 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000174 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100175 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176}
177
Damien Miller34132e52000-01-14 15:45:46 +1100178/* Returns 1 if remote host is connected via socket, 0 if not. */
179
180int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000181packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100182{
183 struct sockaddr_storage from, to;
184 socklen_t fromlen, tolen;
185
186 /* filedescriptors in and out are the same, so it's a socket */
187 if (connection_in == connection_out)
188 return 1;
189 fromlen = sizeof(from);
190 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100191 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100192 return 0;
193 tolen = sizeof(to);
194 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100195 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100196 return 0;
197 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
198 return 0;
199 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
200 return 0;
201 return 1;
202}
203
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000204/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000205 * Exports an IV from the CipherContext required to export the key
206 * state back from the unprivileged child to the privileged parent
207 * process.
208 */
209
210void
211packet_get_keyiv(int mode, u_char *iv, u_int len)
212{
213 CipherContext *cc;
214
215 if (mode == MODE_OUT)
216 cc = &send_context;
217 else
218 cc = &receive_context;
219
220 cipher_get_keyiv(cc, iv, len);
221}
222
223int
224packet_get_keycontext(int mode, u_char *dat)
225{
226 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000227
Ben Lindstromf6027d32002-03-22 01:42:04 +0000228 if (mode == MODE_OUT)
229 cc = &send_context;
230 else
231 cc = &receive_context;
232
233 return (cipher_get_keycontext(cc, dat));
234}
235
236void
237packet_set_keycontext(int mode, u_char *dat)
238{
239 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000240
Ben Lindstromf6027d32002-03-22 01:42:04 +0000241 if (mode == MODE_OUT)
242 cc = &send_context;
243 else
244 cc = &receive_context;
245
246 cipher_set_keycontext(cc, dat);
247}
248
249int
250packet_get_keyiv_len(int mode)
251{
252 CipherContext *cc;
253
254 if (mode == MODE_OUT)
255 cc = &send_context;
256 else
257 cc = &receive_context;
258
259 return (cipher_get_keyiv_len(cc));
260}
261void
262packet_set_iv(int mode, u_char *dat)
263{
264 CipherContext *cc;
265
266 if (mode == MODE_OUT)
267 cc = &send_context;
268 else
269 cc = &receive_context;
270
271 cipher_set_keyiv(cc, dat);
272}
273int
Damien Miller2b92d322003-06-11 22:05:06 +1000274packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000275{
276 return (cipher_get_number(receive_context.cipher));
277}
278
Damien Millera5539d22003-04-09 20:50:06 +1000279void
280packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000281{
Damien Millera5539d22003-04-09 20:50:06 +1000282 struct packet_state *state;
283
284 state = (mode == MODE_IN) ? &p_read : &p_send;
285 *seqnr = state->seqnr;
286 *blocks = state->blocks;
287 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000288}
289
290void
Damien Millera5539d22003-04-09 20:50:06 +1000291packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000292{
Damien Millera5539d22003-04-09 20:50:06 +1000293 struct packet_state *state;
294
295 state = (mode == MODE_IN) ? &p_read : &p_send;
296 state->seqnr = seqnr;
297 state->blocks = blocks;
298 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000299}
300
Damien Miller34132e52000-01-14 15:45:46 +1100301/* returns 1 if connection is via ipv4 */
302
303int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000304packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100305{
306 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100307 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100308
309 memset(&to, 0, sizeof(to));
310 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
311 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000312 if (to.ss_family == AF_INET)
313 return 1;
314#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100315 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000316 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
317 return 1;
318#endif
319 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100320}
321
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322/* Sets the connection into non-blocking mode. */
323
324void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000325packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326{
Damien Miller95def091999-11-25 00:26:21 +1100327 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000328 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329
Damien Miller232711f2004-06-15 10:35:30 +1000330 if (connection_out != connection_in)
331 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332}
333
334/* Returns the socket used for reading. */
335
336int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000337packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338{
Damien Miller95def091999-11-25 00:26:21 +1100339 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340}
341
342/* Returns the descriptor used for writing. */
343
344int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000345packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346{
Damien Miller95def091999-11-25 00:26:21 +1100347 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348}
349
350/* Closes the connection and clears and frees internal data structures. */
351
352void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000353packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354{
Damien Miller95def091999-11-25 00:26:21 +1100355 if (!initialized)
356 return;
357 initialized = 0;
358 if (connection_in == connection_out) {
359 shutdown(connection_out, SHUT_RDWR);
360 close(connection_out);
361 } else {
362 close(connection_in);
363 close(connection_out);
364 }
365 buffer_free(&input);
366 buffer_free(&output);
367 buffer_free(&outgoing_packet);
368 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000369 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100370 buffer_free(&compression_buffer);
371 buffer_compress_uninit();
372 }
Damien Miller963f6b22002-02-19 15:21:23 +1100373 cipher_cleanup(&send_context);
374 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375}
376
377/* Sets remote side protocol flags. */
378
379void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000380packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381{
Damien Miller95def091999-11-25 00:26:21 +1100382 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383}
384
385/* Returns the remote protocol flags set earlier by the above function. */
386
Ben Lindstrom46c16222000-12-22 01:43:59 +0000387u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000388packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389{
Damien Miller95def091999-11-25 00:26:21 +1100390 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391}
392
Damien Miller5428f641999-11-25 11:54:57 +1100393/*
394 * Starts packet compression from the next packet on in both directions.
395 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
396 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397
Ben Lindstrombba81212001-06-25 05:01:22 +0000398static void
399packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000400{
401 if (compression_buffer_ready == 1)
402 return;
403 compression_buffer_ready = 1;
404 buffer_init(&compression_buffer);
405}
406
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407void
408packet_start_compression(int level)
409{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000410 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100411 fatal("Compression already enabled.");
412 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000413 packet_init_compression();
414 buffer_compress_init_send(level);
415 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416}
417
Damien Miller5428f641999-11-25 11:54:57 +1100418/*
Damien Miller5428f641999-11-25 11:54:57 +1100419 * Causes any further packets to be encrypted using the given key. The same
420 * key is used for both sending and reception. However, both directions are
421 * encrypted independently of each other.
422 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000423
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000425packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100426 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427{
Damien Miller874d77b2000-10-14 16:23:11 +1100428 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000429
Damien Miller874d77b2000-10-14 16:23:11 +1100430 if (cipher == NULL)
431 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000432 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100433 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000434 if (keylen > SSH_SESSION_KEY_LENGTH)
435 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
436 memcpy(ssh1_key, key, keylen);
437 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100438 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
439 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440}
441
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000442u_int
443packet_get_encryption_key(u_char *key)
444{
445 if (key == NULL)
446 return (ssh1_keylen);
447 memcpy(key, ssh1_key, ssh1_keylen);
448 return (ssh1_keylen);
449}
450
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000451/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000453packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000455 u_char buf[9];
456 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457
Ben Lindstrom204e4882001-03-05 06:47:00 +0000458 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000459 len = compat20 ? 6 : 9;
460 memset(buf, 0, len - 1);
461 buf[len - 1] = type;
462 buffer_clear(&outgoing_packet);
463 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000464}
465
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000466/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467void
468packet_put_char(int value)
469{
Damien Miller95def091999-11-25 00:26:21 +1100470 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000471
Damien Miller95def091999-11-25 00:26:21 +1100472 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000475packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476{
Damien Miller95def091999-11-25 00:26:21 +1100477 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100480packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481{
Damien Miller95def091999-11-25 00:26:21 +1100482 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483}
Damien Miller33b13562000-04-04 14:38:59 +1000484void
485packet_put_cstring(const char *str)
486{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000487 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000488}
Damien Miller33b13562000-04-04 14:38:59 +1000489void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100490packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000491{
492 buffer_append(&outgoing_packet, buf, len);
493}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494void
Damien Miller95def091999-11-25 00:26:21 +1100495packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496{
Damien Miller95def091999-11-25 00:26:21 +1100497 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498}
Damien Miller33b13562000-04-04 14:38:59 +1000499void
500packet_put_bignum2(BIGNUM * value)
501{
502 buffer_put_bignum2(&outgoing_packet, value);
503}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504
Damien Miller5428f641999-11-25 11:54:57 +1100505/*
506 * Finalizes and sends the packet. If the encryption key has been set,
507 * encrypts the packet before sending.
508 */
Damien Miller95def091999-11-25 00:26:21 +1100509
Ben Lindstrombba81212001-06-25 05:01:22 +0000510static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000511packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000513 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100514 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000515 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000516 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517
Damien Miller5428f641999-11-25 11:54:57 +1100518 /*
519 * If using packet compression, compress the payload of the outgoing
520 * packet.
521 */
Damien Miller95def091999-11-25 00:26:21 +1100522 if (packet_compression) {
523 buffer_clear(&compression_buffer);
524 /* Skip padding. */
525 buffer_consume(&outgoing_packet, 8);
526 /* padding */
527 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
528 buffer_compress(&outgoing_packet, &compression_buffer);
529 buffer_clear(&outgoing_packet);
530 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100531 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100532 }
533 /* Compute packet length without padding (add checksum, remove padding). */
534 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535
Damien Millere247cc42000-05-07 12:03:14 +1000536 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100537 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100538 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100539 cp = buffer_ptr(&outgoing_packet);
540 for (i = 0; i < padding; i++) {
541 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000542 rnd = arc4random();
543 cp[7 - i] = rnd & 0xff;
544 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100545 }
546 }
547 buffer_consume(&outgoing_packet, 8 - padding);
548
549 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100550 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000551 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100552 PUT_32BIT(buf, checksum);
553 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000554
555#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100556 fprintf(stderr, "packet_send plain: ");
557 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558#endif
559
Damien Miller95def091999-11-25 00:26:21 +1100560 /* Append to output. */
561 PUT_32BIT(buf, len);
562 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100563 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100564 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100565 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100566
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100568 fprintf(stderr, "encrypted: ");
569 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570#endif
571
Damien Miller95def091999-11-25 00:26:21 +1100572 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573
Damien Miller5428f641999-11-25 11:54:57 +1100574 /*
Damien Miller788f2122005-11-05 15:14:59 +1100575 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100576 * actually sent until packet_write_wait or packet_write_poll is
577 * called.
578 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579}
580
Ben Lindstromf6027d32002-03-22 01:42:04 +0000581void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000582set_newkeys(int mode)
583{
584 Enc *enc;
585 Mac *mac;
586 Comp *comp;
587 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000588 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000589 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000590
Ben Lindstrom064496f2002-12-23 02:04:22 +0000591 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000592
Damien Miller963f6b22002-02-19 15:21:23 +1100593 if (mode == MODE_OUT) {
594 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000595 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000596 p_send.packets = p_send.blocks = 0;
597 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100598 } else {
599 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000600 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000601 p_read.packets = p_read.blocks = 0;
602 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100603 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000604 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000605 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100606 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000607 enc = &newkeys[mode]->enc;
608 mac = &newkeys[mode]->mac;
609 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000610 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000611 xfree(enc->name);
612 xfree(enc->iv);
613 xfree(enc->key);
614 xfree(mac->name);
615 xfree(mac->key);
616 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000617 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000618 }
619 newkeys[mode] = kex_get_newkeys(mode);
620 if (newkeys[mode] == NULL)
621 fatal("newkeys: no keys for mode %d", mode);
622 enc = &newkeys[mode]->enc;
623 mac = &newkeys[mode]->mac;
624 comp = &newkeys[mode]->comp;
625 if (mac->md != NULL)
626 mac->enabled = 1;
627 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100628 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000629 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000630 /* Deleting the keys does not gain extra security */
631 /* memset(enc->iv, 0, enc->block_size);
632 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000633 if ((comp->type == COMP_ZLIB ||
634 (comp->type == COMP_DELAYED && after_authentication)) &&
635 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000636 packet_init_compression();
637 if (mode == MODE_OUT)
638 buffer_compress_init_send(6);
639 else
640 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000641 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000642 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000643 /*
644 * The 2^(blocksize*2) limit is too expensive for 3DES,
645 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
646 */
647 if (enc->block_size >= 16)
648 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
649 else
650 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000651 if (rekey_limit)
652 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000653}
654
Damien Miller5428f641999-11-25 11:54:57 +1100655/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000656 * Delayed compression for SSH2 is enabled after authentication:
657 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
658 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
659 */
660static void
661packet_enable_delayed_compress(void)
662{
663 Comp *comp = NULL;
664 int mode;
665
666 /*
667 * Remember that we are past the authentication step, so rekeying
668 * with COMP_DELAYED will turn on compression immediately.
669 */
670 after_authentication = 1;
671 for (mode = 0; mode < MODE_MAX; mode++) {
672 comp = &newkeys[mode]->comp;
673 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000674 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000675 if (mode == MODE_OUT)
676 buffer_compress_init_send(6);
677 else
678 buffer_compress_init_recv();
679 comp->enabled = 1;
680 }
681 }
682}
683
684/*
Damien Miller33b13562000-04-04 14:38:59 +1000685 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
686 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000687static void
Damien Millera5539d22003-04-09 20:50:06 +1000688packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000689{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000690 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100691 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000692 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100693 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000694 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000695 Enc *enc = NULL;
696 Mac *mac = NULL;
697 Comp *comp = NULL;
698 int block_size;
699
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000700 if (newkeys[MODE_OUT] != NULL) {
701 enc = &newkeys[MODE_OUT]->enc;
702 mac = &newkeys[MODE_OUT]->mac;
703 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000704 }
Damien Miller963f6b22002-02-19 15:21:23 +1100705 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000706
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000707 cp = buffer_ptr(&outgoing_packet);
708 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000709
710#ifdef PACKET_DEBUG
711 fprintf(stderr, "plain: ");
712 buffer_dump(&outgoing_packet);
713#endif
714
715 if (comp && comp->enabled) {
716 len = buffer_len(&outgoing_packet);
717 /* skip header, compress only payload */
718 buffer_consume(&outgoing_packet, 5);
719 buffer_clear(&compression_buffer);
720 buffer_compress(&outgoing_packet, &compression_buffer);
721 buffer_clear(&outgoing_packet);
722 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
723 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
724 buffer_len(&compression_buffer));
725 DBG(debug("compression: raw %d compressed %d", len,
726 buffer_len(&outgoing_packet)));
727 }
728
729 /* sizeof (packet_len + pad_len + payload) */
730 len = buffer_len(&outgoing_packet);
731
732 /*
733 * calc size of padding, alloc space, get random data,
734 * minimum padding is 4 bytes
735 */
736 padlen = block_size - (len % block_size);
737 if (padlen < 4)
738 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100739 if (extra_pad) {
740 /* will wrap if extra_pad+padlen > 255 */
741 extra_pad = roundup(extra_pad, block_size);
742 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000743 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100744 pad, len, padlen, extra_pad);
745 padlen += pad;
746 extra_pad = 0;
747 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100748 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100749 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000750 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000751 for (i = 0; i < padlen; i++) {
752 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000753 rnd = arc4random();
754 cp[i] = rnd & 0xff;
755 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000756 }
Damien Millere247cc42000-05-07 12:03:14 +1000757 } else {
758 /* clear padding */
759 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000760 }
761 /* packet_length includes payload, padding and padding length field */
762 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000763 cp = buffer_ptr(&outgoing_packet);
764 PUT_32BIT(cp, packet_length);
765 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000766 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
767
768 /* compute MAC over seqnr and packet(length fields, payload, padding) */
769 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000770 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100771 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000772 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000773 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000774 }
775 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100776 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100777 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000778 buffer_len(&outgoing_packet));
779 /* append unencrypted MAC */
780 if (mac && mac->enabled)
781 buffer_append(&output, (char *)macbuf, mac->mac_len);
782#ifdef PACKET_DEBUG
783 fprintf(stderr, "encrypted: ");
784 buffer_dump(&output);
785#endif
Damien Miller4af51302000-04-16 11:18:38 +1000786 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000787 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000788 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000789 if (++p_send.packets == 0)
790 if (!(datafellows & SSH_BUG_NOREKEY))
791 fatal("XXX too many packets with same key");
792 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000793 buffer_clear(&outgoing_packet);
794
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000795 if (type == SSH2_MSG_NEWKEYS)
796 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000797 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
798 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000799}
800
Damien Millera5539d22003-04-09 20:50:06 +1000801static void
802packet_send2(void)
803{
804 static int rekeying = 0;
805 struct packet *p;
806 u_char type, *cp;
807
808 cp = buffer_ptr(&outgoing_packet);
809 type = cp[5];
810
811 /* during rekeying we can only send key exchange messages */
812 if (rekeying) {
813 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
814 (type <= SSH2_MSG_TRANSPORT_MAX))) {
815 debug("enqueue packet: %u", type);
816 p = xmalloc(sizeof(*p));
817 p->type = type;
818 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
819 buffer_init(&outgoing_packet);
820 TAILQ_INSERT_TAIL(&outgoing, p, next);
821 return;
822 }
823 }
824
825 /* rekeying starts with sending KEXINIT */
826 if (type == SSH2_MSG_KEXINIT)
827 rekeying = 1;
828
829 packet_send2_wrapped();
830
831 /* after a NEWKEYS message we can send the complete queue */
832 if (type == SSH2_MSG_NEWKEYS) {
833 rekeying = 0;
834 while ((p = TAILQ_FIRST(&outgoing))) {
835 type = p->type;
836 debug("dequeue packet: %u", type);
837 buffer_free(&outgoing_packet);
838 memcpy(&outgoing_packet, &p->payload,
839 sizeof(Buffer));
840 TAILQ_REMOVE(&outgoing, p, next);
841 xfree(p);
842 packet_send2_wrapped();
843 }
844 }
845}
846
Damien Miller33b13562000-04-04 14:38:59 +1000847void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000848packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000849{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000850 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000851 packet_send2();
852 else
853 packet_send1();
854 DBG(debug("packet_send done"));
855}
856
Damien Miller33b13562000-04-04 14:38:59 +1000857/*
Damien Miller5428f641999-11-25 11:54:57 +1100858 * Waits until a packet has been received, and returns its type. Note that
859 * no other data is processed until this returns, so this function should not
860 * be used during the interactive session.
861 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000862
863int
Damien Millerdff50992002-01-22 23:16:32 +1100864packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865{
Damien Miller95def091999-11-25 00:26:21 +1100866 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000867 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100868 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000869 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000871 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
872 sizeof(fd_mask));
873
Damien Miller95def091999-11-25 00:26:21 +1100874 /* Since we are blocking, ensure that all written packets have been sent. */
875 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876
Damien Miller95def091999-11-25 00:26:21 +1100877 /* Stay in the loop until we have received a complete packet. */
878 for (;;) {
879 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100880 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000881 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000882 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100883 || type == SSH_SMSG_FAILURE
884 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000885 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100886 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100887 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000888 if (type != SSH_MSG_NONE) {
889 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100890 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000891 }
Damien Miller5428f641999-11-25 11:54:57 +1100892 /*
893 * Otherwise, wait for some data to arrive, add it to the
894 * buffer, and try again.
895 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000896 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
897 sizeof(fd_mask));
898 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100899
Damien Miller95def091999-11-25 00:26:21 +1100900 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000901 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000902 (errno == EAGAIN || errno == EINTR))
903 ;
Damien Miller5428f641999-11-25 11:54:57 +1100904
Damien Miller95def091999-11-25 00:26:21 +1100905 /* Read data from the socket. */
906 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100907 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000908 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000909 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100910 }
Damien Miller95def091999-11-25 00:26:21 +1100911 if (len < 0)
912 fatal("Read from socket failed: %.100s", strerror(errno));
913 /* Append it to the buffer. */
914 packet_process_incoming(buf, len);
915 }
916 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917}
918
Damien Miller278f9072001-12-21 15:00:19 +1100919int
Damien Millerdff50992002-01-22 23:16:32 +1100920packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100921{
Damien Millerdff50992002-01-22 23:16:32 +1100922 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100923}
924
Damien Miller5428f641999-11-25 11:54:57 +1100925/*
926 * Waits until a packet has been received, verifies that its type matches
927 * that given, and gives a fatal error and exits if there is a mismatch.
928 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929
930void
Damien Millerdff50992002-01-22 23:16:32 +1100931packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000932{
Damien Miller95def091999-11-25 00:26:21 +1100933 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934
Damien Millerdff50992002-01-22 23:16:32 +1100935 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100936 if (type != expected_type)
937 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000938 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939}
940
941/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100942 * packet_process_incoming. If so, reads the packet; otherwise returns
943 * SSH_MSG_NONE. This does not wait for data from the connection.
944 *
945 * SSH_MSG_DISCONNECT is handled specially here. Also,
946 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
947 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100948 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949
Ben Lindstrombba81212001-06-25 05:01:22 +0000950static int
Damien Millerdff50992002-01-22 23:16:32 +1100951packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000953 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000954 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000955 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956
Damien Miller95def091999-11-25 00:26:21 +1100957 /* Check if input size is less than minimum packet size. */
958 if (buffer_len(&input) < 4 + 8)
959 return SSH_MSG_NONE;
960 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000961 cp = buffer_ptr(&input);
962 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100963 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000964 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100965 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966
Damien Miller95def091999-11-25 00:26:21 +1100967 /* Check if the packet has been entirely received. */
968 if (buffer_len(&input) < 4 + padded_len)
969 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970
Damien Miller95def091999-11-25 00:26:21 +1100971 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Damien Miller95def091999-11-25 00:26:21 +1100973 /* Consume packet length. */
974 buffer_consume(&input, 4);
975
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000976 /*
977 * Cryptographic attack detector for ssh
978 * (C)1998 CORE-SDI, Buenos Aires Argentina
979 * Ariel Futoransky(futo@core-sdi.com)
980 */
Damien Miller963f6b22002-02-19 15:21:23 +1100981 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000982 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
983 packet_disconnect("crc32 compensation attack: network attack detected");
984
985 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100986 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100987 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100988 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000989
Damien Miller95def091999-11-25 00:26:21 +1100990 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991
992#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100993 fprintf(stderr, "read_poll plain: ");
994 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996
Damien Miller95def091999-11-25 00:26:21 +1100997 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100998 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000999 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
Damien Miller95def091999-11-25 00:26:21 +11001001 /* Skip padding. */
1002 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001003
Damien Miller95def091999-11-25 00:26:21 +11001004 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001005 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001006 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001007 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001009 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
1010 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +11001011 if (checksum != stored_checksum)
1012 packet_disconnect("Corrupted check bytes on input.");
1013 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014
Damien Miller95def091999-11-25 00:26:21 +11001015 if (packet_compression) {
1016 buffer_clear(&compression_buffer);
1017 buffer_uncompress(&incoming_packet, &compression_buffer);
1018 buffer_clear(&incoming_packet);
1019 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001020 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001021 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001022 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001023 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1024 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001025 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001026}
Damien Miller95def091999-11-25 00:26:21 +11001027
Ben Lindstrombba81212001-06-25 05:01:22 +00001028static int
Damien Millerdff50992002-01-22 23:16:32 +11001029packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001030{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001031 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001032 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001033 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001034 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001035 Enc *enc = NULL;
1036 Mac *mac = NULL;
1037 Comp *comp = NULL;
1038
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001039 if (newkeys[MODE_IN] != NULL) {
1040 enc = &newkeys[MODE_IN]->enc;
1041 mac = &newkeys[MODE_IN]->mac;
1042 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001043 }
1044 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001045 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001046
1047 if (packet_length == 0) {
1048 /*
1049 * check if input size is less than the cipher block size,
1050 * decrypt first block and extract length of incoming packet
1051 */
1052 if (buffer_len(&input) < block_size)
1053 return SSH_MSG_NONE;
1054 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001055 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001056 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001057 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001058 cp = buffer_ptr(&incoming_packet);
1059 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001060 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001061#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001062 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001063#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001064 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001065 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001066 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001067 buffer_consume(&input, block_size);
1068 }
1069 /* we have a partial packet of block_size bytes */
1070 need = 4 + packet_length - block_size;
1071 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1072 need, maclen));
1073 if (need % block_size != 0)
1074 fatal("padding error: need %d block %d mod %d",
1075 need, block_size, need % block_size);
1076 /*
1077 * check if the entire packet has been received and
1078 * decrypt into incoming_packet
1079 */
1080 if (buffer_len(&input) < need + maclen)
1081 return SSH_MSG_NONE;
1082#ifdef PACKET_DEBUG
1083 fprintf(stderr, "read_poll enc/full: ");
1084 buffer_dump(&input);
1085#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001086 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001087 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001088 buffer_consume(&input, need);
1089 /*
1090 * compute MAC over seqnr and packet,
1091 * increment sequence number for incoming packet
1092 */
Damien Miller4af51302000-04-16 11:18:38 +10001093 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001094 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001095 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001096 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001097 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001098 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001099 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001100 buffer_consume(&input, mac->mac_len);
1101 }
Damien Miller278f9072001-12-21 15:00:19 +11001102 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001103 *seqnr_p = p_read.seqnr;
1104 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001105 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001106 if (++p_read.packets == 0)
1107 if (!(datafellows & SSH_BUG_NOREKEY))
1108 fatal("XXX too many packets with same key");
1109 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001110
1111 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001112 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001113 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001114 DBG(debug("input: padlen %d", padlen));
1115 if (padlen < 4)
1116 packet_disconnect("Corrupted padlen %d on input.", padlen);
1117
1118 /* skip packet size + padlen, discard padding */
1119 buffer_consume(&incoming_packet, 4 + 1);
1120 buffer_consume_end(&incoming_packet, padlen);
1121
1122 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1123 if (comp && comp->enabled) {
1124 buffer_clear(&compression_buffer);
1125 buffer_uncompress(&incoming_packet, &compression_buffer);
1126 buffer_clear(&incoming_packet);
1127 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1128 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001129 DBG(debug("input: len after de-compress %d",
1130 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001131 }
1132 /*
1133 * get packet type, implies consume.
1134 * return length of payload (without type field)
1135 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001136 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001137 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1138 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001139 if (type == SSH2_MSG_NEWKEYS)
1140 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001141 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1142 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001143#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001144 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001145 buffer_dump(&incoming_packet);
1146#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001147 /* reset for next packet */
1148 packet_length = 0;
1149 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001150}
1151
1152int
Damien Millerdff50992002-01-22 23:16:32 +11001153packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001154{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001155 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001156 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001157 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001158
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001159 for (;;) {
1160 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001161 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001162 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001163 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001164 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001165 case SSH2_MSG_IGNORE:
1166 break;
1167 case SSH2_MSG_DEBUG:
1168 packet_get_char();
1169 msg = packet_get_string(NULL);
1170 debug("Remote: %.900s", msg);
1171 xfree(msg);
1172 msg = packet_get_string(NULL);
1173 xfree(msg);
1174 break;
1175 case SSH2_MSG_DISCONNECT:
1176 reason = packet_get_int();
1177 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001178 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001179 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001180 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001181 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001182 break;
Damien Miller659811f2002-01-22 23:23:11 +11001183 case SSH2_MSG_UNIMPLEMENTED:
1184 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001185 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1186 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001187 break;
Damien Miller33b13562000-04-04 14:38:59 +10001188 default:
1189 return type;
1190 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001191 }
Damien Miller33b13562000-04-04 14:38:59 +10001192 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001193 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001194 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001195 case SSH_MSG_IGNORE:
1196 break;
1197 case SSH_MSG_DEBUG:
1198 msg = packet_get_string(NULL);
1199 debug("Remote: %.900s", msg);
1200 xfree(msg);
1201 break;
1202 case SSH_MSG_DISCONNECT:
1203 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001204 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001205 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001206 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001207 xfree(msg);
1208 break;
1209 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001210 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001211 DBG(debug("received packet type %d", type));
1212 return type;
1213 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001214 }
Damien Miller33b13562000-04-04 14:38:59 +10001215 }
1216 }
1217}
1218
Damien Miller278f9072001-12-21 15:00:19 +11001219int
Damien Millerdff50992002-01-22 23:16:32 +11001220packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001221{
Damien Millerdff50992002-01-22 23:16:32 +11001222 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001223}
1224
Damien Miller5428f641999-11-25 11:54:57 +11001225/*
1226 * Buffers the given amount of input characters. This is intended to be used
1227 * together with packet_read_poll.
1228 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001229
1230void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001231packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001232{
Damien Miller95def091999-11-25 00:26:21 +11001233 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234}
1235
1236/* Returns a character from the packet. */
1237
Ben Lindstrom46c16222000-12-22 01:43:59 +00001238u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001239packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240{
Damien Miller95def091999-11-25 00:26:21 +11001241 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001242
Damien Miller95def091999-11-25 00:26:21 +11001243 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001244 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245}
1246
1247/* Returns an integer from the packet data. */
1248
Ben Lindstrom46c16222000-12-22 01:43:59 +00001249u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001250packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251{
Damien Miller95def091999-11-25 00:26:21 +11001252 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253}
1254
Damien Miller5428f641999-11-25 11:54:57 +11001255/*
1256 * Returns an arbitrary precision integer from the packet data. The integer
1257 * must have been initialized before this call.
1258 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259
1260void
Damien Millerd432ccf2002-01-22 23:14:44 +11001261packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001262{
Damien Miller76e1e362002-01-22 23:15:57 +11001263 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264}
1265
Damien Miller33b13562000-04-04 14:38:59 +10001266void
Damien Millerd432ccf2002-01-22 23:14:44 +11001267packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001268{
Damien Miller76e1e362002-01-22 23:15:57 +11001269 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001270}
1271
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001272void *
Damien Millereccb9de2005-06-17 12:59:34 +10001273packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001274{
Damien Millereccb9de2005-06-17 12:59:34 +10001275 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001276
Damien Miller33b13562000-04-04 14:38:59 +10001277 if (length_ptr != NULL)
1278 *length_ptr = bytes;
1279 return buffer_ptr(&incoming_packet);
1280}
1281
Damien Miller4af51302000-04-16 11:18:38 +10001282int
1283packet_remaining(void)
1284{
1285 return buffer_len(&incoming_packet);
1286}
1287
Damien Miller5428f641999-11-25 11:54:57 +11001288/*
1289 * Returns a string from the packet data. The string is allocated using
1290 * xmalloc; it is the responsibility of the calling program to free it when
1291 * no longer needed. The length_ptr argument may be NULL, or point to an
1292 * integer into which the length of the string is stored.
1293 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001294
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001295void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001296packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001297{
Damien Miller95def091999-11-25 00:26:21 +11001298 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299}
1300
Damien Miller5428f641999-11-25 11:54:57 +11001301/*
1302 * Sends a diagnostic message from the server to the client. This message
1303 * can be sent at any time (but not while constructing another message). The
1304 * message is printed immediately, but only if the client is being executed
1305 * in verbose mode. These messages are primarily intended to ease debugging
1306 * authentication problems. The length of the formatted message must not
1307 * exceed 1024 bytes. This will automatically call packet_write_wait.
1308 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309
1310void
Damien Miller95def091999-11-25 00:26:21 +11001311packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312{
Damien Miller95def091999-11-25 00:26:21 +11001313 char buf[1024];
1314 va_list args;
1315
Ben Lindstroma14ee472000-12-07 01:24:58 +00001316 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1317 return;
1318
Damien Miller95def091999-11-25 00:26:21 +11001319 va_start(args, fmt);
1320 vsnprintf(buf, sizeof(buf), fmt, args);
1321 va_end(args);
1322
Damien Miller7c8af4f2000-05-01 08:24:07 +10001323 if (compat20) {
1324 packet_start(SSH2_MSG_DEBUG);
1325 packet_put_char(0); /* bool: always display */
1326 packet_put_cstring(buf);
1327 packet_put_cstring("");
1328 } else {
1329 packet_start(SSH_MSG_DEBUG);
1330 packet_put_cstring(buf);
1331 }
Damien Miller95def091999-11-25 00:26:21 +11001332 packet_send();
1333 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001334}
1335
Damien Miller5428f641999-11-25 11:54:57 +11001336/*
1337 * Logs the error plus constructs and sends a disconnect packet, closes the
1338 * connection, and exits. This function never returns. The error message
1339 * should not contain a newline. The length of the formatted message must
1340 * not exceed 1024 bytes.
1341 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001342
1343void
Damien Miller95def091999-11-25 00:26:21 +11001344packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001345{
Damien Miller95def091999-11-25 00:26:21 +11001346 char buf[1024];
1347 va_list args;
1348 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001349
Damien Miller95def091999-11-25 00:26:21 +11001350 if (disconnecting) /* Guard against recursive invocations. */
1351 fatal("packet_disconnect called recursively.");
1352 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001353
Damien Miller5428f641999-11-25 11:54:57 +11001354 /*
1355 * Format the message. Note that the caller must make sure the
1356 * message is of limited size.
1357 */
Damien Miller95def091999-11-25 00:26:21 +11001358 va_start(args, fmt);
1359 vsnprintf(buf, sizeof(buf), fmt, args);
1360 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001362 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001363 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001364
Damien Miller95def091999-11-25 00:26:21 +11001365 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001366 if (compat20) {
1367 packet_start(SSH2_MSG_DISCONNECT);
1368 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1369 packet_put_cstring(buf);
1370 packet_put_cstring("");
1371 } else {
1372 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001373 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001374 }
Damien Miller95def091999-11-25 00:26:21 +11001375 packet_send();
1376 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001377
Damien Miller95def091999-11-25 00:26:21 +11001378 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001379 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001380
Damien Miller95def091999-11-25 00:26:21 +11001381 /* Close the connection. */
1382 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001383 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001384}
1385
Damien Miller5428f641999-11-25 11:54:57 +11001386/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
1388void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001389packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390{
Damien Miller95def091999-11-25 00:26:21 +11001391 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001392
Damien Miller95def091999-11-25 00:26:21 +11001393 if (len > 0) {
1394 len = write(connection_out, buffer_ptr(&output), len);
1395 if (len <= 0) {
1396 if (errno == EAGAIN)
1397 return;
1398 else
1399 fatal("Write failed: %.100s", strerror(errno));
1400 }
1401 buffer_consume(&output, len);
1402 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403}
1404
Damien Miller5428f641999-11-25 11:54:57 +11001405/*
1406 * Calls packet_write_poll repeatedly until all pending output data has been
1407 * written.
1408 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001409
1410void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001411packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001412{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001413 fd_set *setp;
1414
1415 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1416 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001417 packet_write_poll();
1418 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001419 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1420 sizeof(fd_mask));
1421 FD_SET(connection_out, setp);
1422 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001423 (errno == EAGAIN || errno == EINTR))
1424 ;
Damien Miller95def091999-11-25 00:26:21 +11001425 packet_write_poll();
1426 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001427 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428}
1429
1430/* Returns true if there is buffered data to write to the connection. */
1431
1432int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001433packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434{
Damien Miller95def091999-11-25 00:26:21 +11001435 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001436}
1437
1438/* Returns true if there is not too much data to write to the connection. */
1439
1440int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001441packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001442{
Damien Miller95def091999-11-25 00:26:21 +11001443 if (interactive_mode)
1444 return buffer_len(&output) < 16384;
1445 else
1446 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447}
1448
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001449
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001450static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001451packet_set_tos(int interactive)
1452{
Damien Miller5924ceb2003-11-22 15:02:42 +11001453#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001454 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1455
1456 if (!packet_connection_is_on_socket() ||
1457 !packet_connection_is_ipv4())
1458 return;
1459 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1460 sizeof(tos)) < 0)
1461 error("setsockopt IP_TOS %d: %.100s:",
1462 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001463#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001464}
Ben Lindstroma7433982002-12-23 02:41:41 +00001465
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466/* Informs that the current session is interactive. Sets IP flags for that. */
1467
1468void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001469packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001471 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001473 if (called)
1474 return;
1475 called = 1;
1476
Damien Miller95def091999-11-25 00:26:21 +11001477 /* Record that we are in interactive mode. */
1478 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479
Damien Miller34132e52000-01-14 15:45:46 +11001480 /* Only set socket options if using a socket. */
1481 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001482 return;
Ben Lindstroma7433982002-12-23 02:41:41 +00001483 if (interactive)
Damien Miller398e1cf2002-02-05 11:52:13 +11001484 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001485 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001486}
1487
1488/* Returns true if the current connection is interactive. */
1489
1490int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001491packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492{
Damien Miller95def091999-11-25 00:26:21 +11001493 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494}
Damien Miller6162d121999-11-21 13:23:52 +11001495
Darren Tucker1f8311c2004-05-13 16:39:33 +10001496int
Darren Tucker502d3842003-06-28 12:38:01 +10001497packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001498{
Damien Miller95def091999-11-25 00:26:21 +11001499 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001500
Damien Miller95def091999-11-25 00:26:21 +11001501 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001502 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001503 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001504 return -1;
1505 }
1506 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001507 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001508 return -1;
1509 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001510 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001511 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001512 max_packet_size = s;
1513 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001514}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001515
Damien Miller9f643902001-11-12 11:02:52 +11001516/* roundup current message to pad bytes */
1517void
1518packet_add_padding(u_char pad)
1519{
1520 extra_pad = pad;
1521}
1522
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001523/*
1524 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001525 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001526 * byte SSH_MSG_IGNORE
1527 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001528 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001529 * All implementations MUST understand (and ignore) this message at any
1530 * time (after receiving the protocol version). No implementation is
1531 * required to send them. This message can be used as an additional
1532 * protection measure against advanced traffic analysis techniques.
1533 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001534void
1535packet_send_ignore(int nbytes)
1536{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001537 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001538 int i;
1539
1540 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001541 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001542 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001543 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001544 rnd = arc4random();
1545 packet_put_char(rnd & 0xff);
1546 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001547 }
1548}
Damien Millera5539d22003-04-09 20:50:06 +10001549
Darren Tucker1f8311c2004-05-13 16:39:33 +10001550#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001551int
1552packet_need_rekeying(void)
1553{
1554 if (datafellows & SSH_BUG_NOREKEY)
1555 return 0;
1556 return
1557 (p_send.packets > MAX_PACKETS) ||
1558 (p_read.packets > MAX_PACKETS) ||
1559 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1560 (max_blocks_in && (p_read.blocks > max_blocks_in));
1561}
1562
1563void
1564packet_set_rekey_limit(u_int32_t bytes)
1565{
1566 rekey_limit = bytes;
1567}
Damien Miller9786e6e2005-07-26 21:54:56 +10001568
1569void
1570packet_set_server(void)
1571{
1572 server_side = 1;
1573}
1574
1575void
1576packet_set_authenticated(void)
1577{
1578 after_authentication = 1;
1579}