blob: ba2843786f4aaef4c08540743db0f79bd7d7191e [file] [log] [blame]
Damien Miller8ec8c3e2006-07-10 20:35:38 +10001/* $OpenBSD: packet.c,v 1.132 2006/07/05 02:42:09 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
Damien Millera0898b82003-04-09 21:05:52 +100042#include "openbsd-compat/sys-queue.h"
Damien Miller8ec8c3e2006-07-10 20:35:38 +100043#include <sys/socket.h>
44
45#include <netinet/in_systm.h>
46#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110047#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048
49#include "xmalloc.h"
50#include "buffer.h"
51#include "packet.h"
52#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
55#include "compress.h"
56#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000057#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
Damien Miller33b13562000-04-04 14:38:59 +100059#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100061#include "ssh2.h"
62
Damien Miller874d77b2000-10-14 16:23:11 +110063#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100064#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000065#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "log.h"
67#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110068#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000069#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100070
71#ifdef PACKET_DEBUG
72#define DBG(x) x
73#else
74#define DBG(x)
75#endif
76
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * This variable contains the file descriptors used for communicating with
79 * the other side. connection_in is used for reading; connection_out for
80 * writing. These can be the same descriptor, in which case it is assumed to
81 * be a socket.
82 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083static int connection_in = -1;
84static int connection_out = -1;
85
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000087static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89/* Encryption context for receiving data. This is only used for decryption. */
90static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110091
92/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093static CipherContext send_context;
94
95/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000096Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
98/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000099Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
101/* Buffer for the partial outgoing packet being constructed. */
102static Buffer outgoing_packet;
103
104/* Buffer for the incoming packet currently being processed. */
105static Buffer incoming_packet;
106
107/* Scratch buffer for packet compression/decompression. */
108static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000109static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
111/* Flag indicating whether packet compression/decompression is enabled. */
112static int packet_compression = 0;
113
Damien Miller6162d121999-11-21 13:23:52 +1100114/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000115u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100116
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117/* Flag indicating whether this module has been initialized. */
118static int initialized = 0;
119
120/* Set to true if the connection is interactive. */
121static int interactive_mode = 0;
122
Damien Miller9786e6e2005-07-26 21:54:56 +1000123/* Set to true if we are the server side. */
124static int server_side = 0;
125
126/* Set to true if we are authenticated. */
127static int after_authentication = 0;
128
Damien Miller33b13562000-04-04 14:38:59 +1000129/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000130Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000131static struct packet_state {
132 u_int32_t seqnr;
133 u_int32_t packets;
134 u_int64_t blocks;
135} p_read, p_send;
136
137static u_int64_t max_blocks_in, max_blocks_out;
138static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000139
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000140/* Session key for protocol v1 */
141static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
142static u_int ssh1_keylen;
143
Damien Miller9f643902001-11-12 11:02:52 +1100144/* roundup current message to extra_pad bytes */
145static u_char extra_pad = 0;
146
Damien Millera5539d22003-04-09 20:50:06 +1000147struct packet {
148 TAILQ_ENTRY(packet) next;
149 u_char type;
150 Buffer payload;
151};
152TAILQ_HEAD(, packet) outgoing;
153
Damien Miller5428f641999-11-25 11:54:57 +1100154/*
155 * Sets the descriptors used for communication. Disables encryption until
156 * packet_set_encryption_key is called.
157 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158void
159packet_set_connection(int fd_in, int fd_out)
160{
Damien Miller874d77b2000-10-14 16:23:11 +1100161 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000162
Damien Miller874d77b2000-10-14 16:23:11 +1100163 if (none == NULL)
164 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100165 connection_in = fd_in;
166 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000167 cipher_init(&send_context, none, (const u_char *)"",
168 0, NULL, 0, CIPHER_ENCRYPT);
169 cipher_init(&receive_context, none, (const u_char *)"",
170 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000171 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100172 if (!initialized) {
173 initialized = 1;
174 buffer_init(&input);
175 buffer_init(&output);
176 buffer_init(&outgoing_packet);
177 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000178 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100179 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180}
181
Damien Miller34132e52000-01-14 15:45:46 +1100182/* Returns 1 if remote host is connected via socket, 0 if not. */
183
184int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000185packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100186{
187 struct sockaddr_storage from, to;
188 socklen_t fromlen, tolen;
189
190 /* filedescriptors in and out are the same, so it's a socket */
191 if (connection_in == connection_out)
192 return 1;
193 fromlen = sizeof(from);
194 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100195 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100196 return 0;
197 tolen = sizeof(to);
198 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100199 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100200 return 0;
201 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
202 return 0;
203 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
204 return 0;
205 return 1;
206}
207
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000208/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000209 * Exports an IV from the CipherContext required to export the key
210 * state back from the unprivileged child to the privileged parent
211 * process.
212 */
213
214void
215packet_get_keyiv(int mode, u_char *iv, u_int len)
216{
217 CipherContext *cc;
218
219 if (mode == MODE_OUT)
220 cc = &send_context;
221 else
222 cc = &receive_context;
223
224 cipher_get_keyiv(cc, iv, len);
225}
226
227int
228packet_get_keycontext(int mode, u_char *dat)
229{
230 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000231
Ben Lindstromf6027d32002-03-22 01:42:04 +0000232 if (mode == MODE_OUT)
233 cc = &send_context;
234 else
235 cc = &receive_context;
236
237 return (cipher_get_keycontext(cc, dat));
238}
239
240void
241packet_set_keycontext(int mode, u_char *dat)
242{
243 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000244
Ben Lindstromf6027d32002-03-22 01:42:04 +0000245 if (mode == MODE_OUT)
246 cc = &send_context;
247 else
248 cc = &receive_context;
249
250 cipher_set_keycontext(cc, dat);
251}
252
253int
254packet_get_keyiv_len(int mode)
255{
256 CipherContext *cc;
257
258 if (mode == MODE_OUT)
259 cc = &send_context;
260 else
261 cc = &receive_context;
262
263 return (cipher_get_keyiv_len(cc));
264}
Damien Miller4f7becb2006-03-26 14:10:14 +1100265
Ben Lindstromf6027d32002-03-22 01:42:04 +0000266void
267packet_set_iv(int mode, u_char *dat)
268{
269 CipherContext *cc;
270
271 if (mode == MODE_OUT)
272 cc = &send_context;
273 else
274 cc = &receive_context;
275
276 cipher_set_keyiv(cc, dat);
277}
Damien Miller4f7becb2006-03-26 14:10:14 +1100278
Ben Lindstromf6027d32002-03-22 01:42:04 +0000279int
Damien Miller2b92d322003-06-11 22:05:06 +1000280packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000281{
282 return (cipher_get_number(receive_context.cipher));
283}
284
Damien Millera5539d22003-04-09 20:50:06 +1000285void
286packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000287{
Damien Millera5539d22003-04-09 20:50:06 +1000288 struct packet_state *state;
289
290 state = (mode == MODE_IN) ? &p_read : &p_send;
291 *seqnr = state->seqnr;
292 *blocks = state->blocks;
293 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000294}
295
296void
Damien Millera5539d22003-04-09 20:50:06 +1000297packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000298{
Damien Millera5539d22003-04-09 20:50:06 +1000299 struct packet_state *state;
300
301 state = (mode == MODE_IN) ? &p_read : &p_send;
302 state->seqnr = seqnr;
303 state->blocks = blocks;
304 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000305}
306
Damien Miller34132e52000-01-14 15:45:46 +1100307/* returns 1 if connection is via ipv4 */
308
309int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000310packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100311{
312 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100313 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100314
315 memset(&to, 0, sizeof(to));
316 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
317 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000318 if (to.ss_family == AF_INET)
319 return 1;
320#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100321 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000322 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
323 return 1;
324#endif
325 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100326}
327
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328/* Sets the connection into non-blocking mode. */
329
330void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000331packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332{
Damien Miller95def091999-11-25 00:26:21 +1100333 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000334 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335
Damien Miller232711f2004-06-15 10:35:30 +1000336 if (connection_out != connection_in)
337 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338}
339
340/* Returns the socket used for reading. */
341
342int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000343packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344{
Damien Miller95def091999-11-25 00:26:21 +1100345 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346}
347
348/* Returns the descriptor used for writing. */
349
350int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000351packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352{
Damien Miller95def091999-11-25 00:26:21 +1100353 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354}
355
356/* Closes the connection and clears and frees internal data structures. */
357
358void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000359packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360{
Damien Miller95def091999-11-25 00:26:21 +1100361 if (!initialized)
362 return;
363 initialized = 0;
364 if (connection_in == connection_out) {
365 shutdown(connection_out, SHUT_RDWR);
366 close(connection_out);
367 } else {
368 close(connection_in);
369 close(connection_out);
370 }
371 buffer_free(&input);
372 buffer_free(&output);
373 buffer_free(&outgoing_packet);
374 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000375 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100376 buffer_free(&compression_buffer);
377 buffer_compress_uninit();
378 }
Damien Miller963f6b22002-02-19 15:21:23 +1100379 cipher_cleanup(&send_context);
380 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381}
382
383/* Sets remote side protocol flags. */
384
385void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000386packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387{
Damien Miller95def091999-11-25 00:26:21 +1100388 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389}
390
391/* Returns the remote protocol flags set earlier by the above function. */
392
Ben Lindstrom46c16222000-12-22 01:43:59 +0000393u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000394packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395{
Damien Miller95def091999-11-25 00:26:21 +1100396 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397}
398
Damien Miller5428f641999-11-25 11:54:57 +1100399/*
400 * Starts packet compression from the next packet on in both directions.
401 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
402 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403
Ben Lindstrombba81212001-06-25 05:01:22 +0000404static void
405packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000406{
407 if (compression_buffer_ready == 1)
408 return;
409 compression_buffer_ready = 1;
410 buffer_init(&compression_buffer);
411}
412
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413void
414packet_start_compression(int level)
415{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000416 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100417 fatal("Compression already enabled.");
418 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000419 packet_init_compression();
420 buffer_compress_init_send(level);
421 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422}
423
Damien Miller5428f641999-11-25 11:54:57 +1100424/*
Damien Miller5428f641999-11-25 11:54:57 +1100425 * Causes any further packets to be encrypted using the given key. The same
426 * key is used for both sending and reception. However, both directions are
427 * encrypted independently of each other.
428 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000429
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000431packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100432 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433{
Damien Miller874d77b2000-10-14 16:23:11 +1100434 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000435
Damien Miller874d77b2000-10-14 16:23:11 +1100436 if (cipher == NULL)
437 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000438 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100439 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000440 if (keylen > SSH_SESSION_KEY_LENGTH)
441 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
442 memcpy(ssh1_key, key, keylen);
443 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100444 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
445 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446}
447
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000448u_int
449packet_get_encryption_key(u_char *key)
450{
451 if (key == NULL)
452 return (ssh1_keylen);
453 memcpy(key, ssh1_key, ssh1_keylen);
454 return (ssh1_keylen);
455}
456
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000457/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000459packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000461 u_char buf[9];
462 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463
Ben Lindstrom204e4882001-03-05 06:47:00 +0000464 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000465 len = compat20 ? 6 : 9;
466 memset(buf, 0, len - 1);
467 buf[len - 1] = type;
468 buffer_clear(&outgoing_packet);
469 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000470}
471
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000472/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473void
474packet_put_char(int value)
475{
Damien Miller95def091999-11-25 00:26:21 +1100476 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000477
Damien Miller95def091999-11-25 00:26:21 +1100478 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479}
Damien Miller4f7becb2006-03-26 14:10:14 +1100480
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000482packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483{
Damien Miller95def091999-11-25 00:26:21 +1100484 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485}
Damien Miller4f7becb2006-03-26 14:10:14 +1100486
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100488packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489{
Damien Miller95def091999-11-25 00:26:21 +1100490 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491}
Damien Miller4f7becb2006-03-26 14:10:14 +1100492
Damien Miller33b13562000-04-04 14:38:59 +1000493void
494packet_put_cstring(const char *str)
495{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000496 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000497}
Damien Miller4f7becb2006-03-26 14:10:14 +1100498
Damien Miller33b13562000-04-04 14:38:59 +1000499void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100500packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000501{
502 buffer_append(&outgoing_packet, buf, len);
503}
Damien Miller4f7becb2006-03-26 14:10:14 +1100504
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505void
Damien Miller95def091999-11-25 00:26:21 +1100506packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507{
Damien Miller95def091999-11-25 00:26:21 +1100508 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509}
Damien Miller4f7becb2006-03-26 14:10:14 +1100510
Damien Miller33b13562000-04-04 14:38:59 +1000511void
512packet_put_bignum2(BIGNUM * value)
513{
514 buffer_put_bignum2(&outgoing_packet, value);
515}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516
Damien Miller5428f641999-11-25 11:54:57 +1100517/*
518 * Finalizes and sends the packet. If the encryption key has been set,
519 * encrypts the packet before sending.
520 */
Damien Miller95def091999-11-25 00:26:21 +1100521
Ben Lindstrombba81212001-06-25 05:01:22 +0000522static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000523packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000525 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100526 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000527 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000528 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529
Damien Miller5428f641999-11-25 11:54:57 +1100530 /*
531 * If using packet compression, compress the payload of the outgoing
532 * packet.
533 */
Damien Miller95def091999-11-25 00:26:21 +1100534 if (packet_compression) {
535 buffer_clear(&compression_buffer);
536 /* Skip padding. */
537 buffer_consume(&outgoing_packet, 8);
538 /* padding */
539 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
540 buffer_compress(&outgoing_packet, &compression_buffer);
541 buffer_clear(&outgoing_packet);
542 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100543 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100544 }
545 /* Compute packet length without padding (add checksum, remove padding). */
546 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547
Damien Millere247cc42000-05-07 12:03:14 +1000548 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100549 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100550 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100551 cp = buffer_ptr(&outgoing_packet);
552 for (i = 0; i < padding; i++) {
553 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000554 rnd = arc4random();
555 cp[7 - i] = rnd & 0xff;
556 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100557 }
558 }
559 buffer_consume(&outgoing_packet, 8 - padding);
560
561 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100562 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000563 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100564 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100565 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566
567#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100568 fprintf(stderr, "packet_send plain: ");
569 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570#endif
571
Damien Miller95def091999-11-25 00:26:21 +1100572 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100573 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100574 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100575 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100576 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100577 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100578
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100580 fprintf(stderr, "encrypted: ");
581 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582#endif
583
Damien Miller95def091999-11-25 00:26:21 +1100584 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585
Damien Miller5428f641999-11-25 11:54:57 +1100586 /*
Damien Miller788f2122005-11-05 15:14:59 +1100587 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100588 * actually sent until packet_write_wait or packet_write_poll is
589 * called.
590 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591}
592
Ben Lindstromf6027d32002-03-22 01:42:04 +0000593void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000594set_newkeys(int mode)
595{
596 Enc *enc;
597 Mac *mac;
598 Comp *comp;
599 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000600 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000601 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000602
Ben Lindstrom064496f2002-12-23 02:04:22 +0000603 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000604
Damien Miller963f6b22002-02-19 15:21:23 +1100605 if (mode == MODE_OUT) {
606 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000607 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000608 p_send.packets = p_send.blocks = 0;
609 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100610 } else {
611 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000612 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000613 p_read.packets = p_read.blocks = 0;
614 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100615 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000616 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000617 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100618 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000619 enc = &newkeys[mode]->enc;
620 mac = &newkeys[mode]->mac;
621 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000622 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000623 xfree(enc->name);
624 xfree(enc->iv);
625 xfree(enc->key);
626 xfree(mac->name);
627 xfree(mac->key);
628 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000629 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000630 }
631 newkeys[mode] = kex_get_newkeys(mode);
632 if (newkeys[mode] == NULL)
633 fatal("newkeys: no keys for mode %d", mode);
634 enc = &newkeys[mode]->enc;
635 mac = &newkeys[mode]->mac;
636 comp = &newkeys[mode]->comp;
637 if (mac->md != NULL)
638 mac->enabled = 1;
639 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100640 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000641 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000642 /* Deleting the keys does not gain extra security */
643 /* memset(enc->iv, 0, enc->block_size);
644 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000645 if ((comp->type == COMP_ZLIB ||
646 (comp->type == COMP_DELAYED && after_authentication)) &&
647 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000648 packet_init_compression();
649 if (mode == MODE_OUT)
650 buffer_compress_init_send(6);
651 else
652 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000653 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000654 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000655 /*
656 * The 2^(blocksize*2) limit is too expensive for 3DES,
657 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
658 */
659 if (enc->block_size >= 16)
660 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
661 else
662 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000663 if (rekey_limit)
664 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000665}
666
Damien Miller5428f641999-11-25 11:54:57 +1100667/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000668 * Delayed compression for SSH2 is enabled after authentication:
669 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
670 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
671 */
672static void
673packet_enable_delayed_compress(void)
674{
675 Comp *comp = NULL;
676 int mode;
677
678 /*
679 * Remember that we are past the authentication step, so rekeying
680 * with COMP_DELAYED will turn on compression immediately.
681 */
682 after_authentication = 1;
683 for (mode = 0; mode < MODE_MAX; mode++) {
684 comp = &newkeys[mode]->comp;
685 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000686 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000687 if (mode == MODE_OUT)
688 buffer_compress_init_send(6);
689 else
690 buffer_compress_init_recv();
691 comp->enabled = 1;
692 }
693 }
694}
695
696/*
Damien Miller33b13562000-04-04 14:38:59 +1000697 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
698 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000699static void
Damien Millera5539d22003-04-09 20:50:06 +1000700packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000701{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000702 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100703 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000704 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100705 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000706 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000707 Enc *enc = NULL;
708 Mac *mac = NULL;
709 Comp *comp = NULL;
710 int block_size;
711
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000712 if (newkeys[MODE_OUT] != NULL) {
713 enc = &newkeys[MODE_OUT]->enc;
714 mac = &newkeys[MODE_OUT]->mac;
715 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000716 }
Damien Miller963f6b22002-02-19 15:21:23 +1100717 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000718
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000719 cp = buffer_ptr(&outgoing_packet);
720 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000721
722#ifdef PACKET_DEBUG
723 fprintf(stderr, "plain: ");
724 buffer_dump(&outgoing_packet);
725#endif
726
727 if (comp && comp->enabled) {
728 len = buffer_len(&outgoing_packet);
729 /* skip header, compress only payload */
730 buffer_consume(&outgoing_packet, 5);
731 buffer_clear(&compression_buffer);
732 buffer_compress(&outgoing_packet, &compression_buffer);
733 buffer_clear(&outgoing_packet);
734 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
735 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
736 buffer_len(&compression_buffer));
737 DBG(debug("compression: raw %d compressed %d", len,
738 buffer_len(&outgoing_packet)));
739 }
740
741 /* sizeof (packet_len + pad_len + payload) */
742 len = buffer_len(&outgoing_packet);
743
744 /*
745 * calc size of padding, alloc space, get random data,
746 * minimum padding is 4 bytes
747 */
748 padlen = block_size - (len % block_size);
749 if (padlen < 4)
750 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100751 if (extra_pad) {
752 /* will wrap if extra_pad+padlen > 255 */
753 extra_pad = roundup(extra_pad, block_size);
754 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000755 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100756 pad, len, padlen, extra_pad);
757 padlen += pad;
758 extra_pad = 0;
759 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100760 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100761 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000762 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000763 for (i = 0; i < padlen; i++) {
764 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000765 rnd = arc4random();
766 cp[i] = rnd & 0xff;
767 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000768 }
Damien Millere247cc42000-05-07 12:03:14 +1000769 } else {
770 /* clear padding */
771 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000772 }
773 /* packet_length includes payload, padding and padding length field */
774 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000775 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100776 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000777 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000778 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
779
780 /* compute MAC over seqnr and packet(length fields, payload, padding) */
781 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000782 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100783 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000784 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000785 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000786 }
787 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100788 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100789 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000790 buffer_len(&outgoing_packet));
791 /* append unencrypted MAC */
792 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100793 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000794#ifdef PACKET_DEBUG
795 fprintf(stderr, "encrypted: ");
796 buffer_dump(&output);
797#endif
Damien Miller4af51302000-04-16 11:18:38 +1000798 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000799 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000800 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000801 if (++p_send.packets == 0)
802 if (!(datafellows & SSH_BUG_NOREKEY))
803 fatal("XXX too many packets with same key");
804 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000805 buffer_clear(&outgoing_packet);
806
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000807 if (type == SSH2_MSG_NEWKEYS)
808 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000809 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
810 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000811}
812
Damien Millera5539d22003-04-09 20:50:06 +1000813static void
814packet_send2(void)
815{
816 static int rekeying = 0;
817 struct packet *p;
818 u_char type, *cp;
819
820 cp = buffer_ptr(&outgoing_packet);
821 type = cp[5];
822
823 /* during rekeying we can only send key exchange messages */
824 if (rekeying) {
825 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
826 (type <= SSH2_MSG_TRANSPORT_MAX))) {
827 debug("enqueue packet: %u", type);
828 p = xmalloc(sizeof(*p));
829 p->type = type;
830 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
831 buffer_init(&outgoing_packet);
832 TAILQ_INSERT_TAIL(&outgoing, p, next);
833 return;
834 }
835 }
836
837 /* rekeying starts with sending KEXINIT */
838 if (type == SSH2_MSG_KEXINIT)
839 rekeying = 1;
840
841 packet_send2_wrapped();
842
843 /* after a NEWKEYS message we can send the complete queue */
844 if (type == SSH2_MSG_NEWKEYS) {
845 rekeying = 0;
846 while ((p = TAILQ_FIRST(&outgoing))) {
847 type = p->type;
848 debug("dequeue packet: %u", type);
849 buffer_free(&outgoing_packet);
850 memcpy(&outgoing_packet, &p->payload,
851 sizeof(Buffer));
852 TAILQ_REMOVE(&outgoing, p, next);
853 xfree(p);
854 packet_send2_wrapped();
855 }
856 }
857}
858
Damien Miller33b13562000-04-04 14:38:59 +1000859void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000860packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000861{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000862 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000863 packet_send2();
864 else
865 packet_send1();
866 DBG(debug("packet_send done"));
867}
868
Damien Miller33b13562000-04-04 14:38:59 +1000869/*
Damien Miller5428f641999-11-25 11:54:57 +1100870 * Waits until a packet has been received, and returns its type. Note that
871 * no other data is processed until this returns, so this function should not
872 * be used during the interactive session.
873 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874
875int
Damien Millerdff50992002-01-22 23:16:32 +1100876packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877{
Damien Miller95def091999-11-25 00:26:21 +1100878 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000879 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100880 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000881 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882
Damien Miller07d86be2006-03-26 14:19:21 +1100883 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000884 sizeof(fd_mask));
885
Damien Miller95def091999-11-25 00:26:21 +1100886 /* Since we are blocking, ensure that all written packets have been sent. */
887 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888
Damien Miller95def091999-11-25 00:26:21 +1100889 /* Stay in the loop until we have received a complete packet. */
890 for (;;) {
891 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100892 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000893 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000894 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100895 || type == SSH_SMSG_FAILURE
896 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000897 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100898 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100899 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000900 if (type != SSH_MSG_NONE) {
901 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100902 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000903 }
Damien Miller5428f641999-11-25 11:54:57 +1100904 /*
905 * Otherwise, wait for some data to arrive, add it to the
906 * buffer, and try again.
907 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000908 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
909 sizeof(fd_mask));
910 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100911
Damien Miller95def091999-11-25 00:26:21 +1100912 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000913 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000914 (errno == EAGAIN || errno == EINTR))
915 ;
Damien Miller5428f641999-11-25 11:54:57 +1100916
Damien Miller95def091999-11-25 00:26:21 +1100917 /* Read data from the socket. */
918 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100919 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000920 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000921 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100922 }
Damien Miller95def091999-11-25 00:26:21 +1100923 if (len < 0)
924 fatal("Read from socket failed: %.100s", strerror(errno));
925 /* Append it to the buffer. */
926 packet_process_incoming(buf, len);
927 }
928 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929}
930
Damien Miller278f9072001-12-21 15:00:19 +1100931int
Damien Millerdff50992002-01-22 23:16:32 +1100932packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100933{
Damien Millerdff50992002-01-22 23:16:32 +1100934 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100935}
936
Damien Miller5428f641999-11-25 11:54:57 +1100937/*
938 * Waits until a packet has been received, verifies that its type matches
939 * that given, and gives a fatal error and exits if there is a mismatch.
940 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941
942void
Damien Millerdff50992002-01-22 23:16:32 +1100943packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944{
Damien Miller95def091999-11-25 00:26:21 +1100945 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946
Damien Millerdff50992002-01-22 23:16:32 +1100947 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100948 if (type != expected_type)
949 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000950 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951}
952
953/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100954 * packet_process_incoming. If so, reads the packet; otherwise returns
955 * SSH_MSG_NONE. This does not wait for data from the connection.
956 *
957 * SSH_MSG_DISCONNECT is handled specially here. Also,
958 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
959 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100960 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961
Ben Lindstrombba81212001-06-25 05:01:22 +0000962static int
Damien Millerdff50992002-01-22 23:16:32 +1100963packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000965 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000966 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000967 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Damien Miller95def091999-11-25 00:26:21 +1100969 /* Check if input size is less than minimum packet size. */
970 if (buffer_len(&input) < 4 + 8)
971 return SSH_MSG_NONE;
972 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000973 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100974 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100975 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000976 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100977 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979 /* Check if the packet has been entirely received. */
980 if (buffer_len(&input) < 4 + padded_len)
981 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Damien Miller95def091999-11-25 00:26:21 +1100983 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000984
Damien Miller95def091999-11-25 00:26:21 +1100985 /* Consume packet length. */
986 buffer_consume(&input, 4);
987
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000988 /*
989 * Cryptographic attack detector for ssh
990 * (C)1998 CORE-SDI, Buenos Aires Argentina
991 * Ariel Futoransky(futo@core-sdi.com)
992 */
Damien Miller963f6b22002-02-19 15:21:23 +1100993 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +1100994 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000995 packet_disconnect("crc32 compensation attack: network attack detected");
996
997 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100998 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100999 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001000 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001001
Damien Miller95def091999-11-25 00:26:21 +11001002 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003
1004#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001005 fprintf(stderr, "read_poll plain: ");
1006 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001007#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Damien Miller95def091999-11-25 00:26:21 +11001009 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001010 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001011 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
Damien Miller95def091999-11-25 00:26:21 +11001013 /* Skip padding. */
1014 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001015
Damien Miller95def091999-11-25 00:26:21 +11001016 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001017 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001018 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001019 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001021 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001022 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001023 if (checksum != stored_checksum)
1024 packet_disconnect("Corrupted check bytes on input.");
1025 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001026
Damien Miller95def091999-11-25 00:26:21 +11001027 if (packet_compression) {
1028 buffer_clear(&compression_buffer);
1029 buffer_uncompress(&incoming_packet, &compression_buffer);
1030 buffer_clear(&incoming_packet);
1031 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001032 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001033 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001034 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001035 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1036 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001037 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001038}
Damien Miller95def091999-11-25 00:26:21 +11001039
Ben Lindstrombba81212001-06-25 05:01:22 +00001040static int
Damien Millerdff50992002-01-22 23:16:32 +11001041packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001042{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001043 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001044 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001045 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001046 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001047 Enc *enc = NULL;
1048 Mac *mac = NULL;
1049 Comp *comp = NULL;
1050
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001051 if (newkeys[MODE_IN] != NULL) {
1052 enc = &newkeys[MODE_IN]->enc;
1053 mac = &newkeys[MODE_IN]->mac;
1054 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001055 }
1056 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001057 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001058
1059 if (packet_length == 0) {
1060 /*
1061 * check if input size is less than the cipher block size,
1062 * decrypt first block and extract length of incoming packet
1063 */
1064 if (buffer_len(&input) < block_size)
1065 return SSH_MSG_NONE;
1066 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001067 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001068 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001069 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001070 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001071 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001072 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001073#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001074 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001075#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001076 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001077 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001078 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001079 buffer_consume(&input, block_size);
1080 }
1081 /* we have a partial packet of block_size bytes */
1082 need = 4 + packet_length - block_size;
1083 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1084 need, maclen));
1085 if (need % block_size != 0)
1086 fatal("padding error: need %d block %d mod %d",
1087 need, block_size, need % block_size);
1088 /*
1089 * check if the entire packet has been received and
1090 * decrypt into incoming_packet
1091 */
1092 if (buffer_len(&input) < need + maclen)
1093 return SSH_MSG_NONE;
1094#ifdef PACKET_DEBUG
1095 fprintf(stderr, "read_poll enc/full: ");
1096 buffer_dump(&input);
1097#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001098 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001099 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001100 buffer_consume(&input, need);
1101 /*
1102 * compute MAC over seqnr and packet,
1103 * increment sequence number for incoming packet
1104 */
Damien Miller4af51302000-04-16 11:18:38 +10001105 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001106 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001107 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001108 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001109 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001110 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001111 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001112 buffer_consume(&input, mac->mac_len);
1113 }
Damien Miller278f9072001-12-21 15:00:19 +11001114 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001115 *seqnr_p = p_read.seqnr;
1116 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001117 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001118 if (++p_read.packets == 0)
1119 if (!(datafellows & SSH_BUG_NOREKEY))
1120 fatal("XXX too many packets with same key");
1121 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001122
1123 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001124 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001125 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001126 DBG(debug("input: padlen %d", padlen));
1127 if (padlen < 4)
1128 packet_disconnect("Corrupted padlen %d on input.", padlen);
1129
1130 /* skip packet size + padlen, discard padding */
1131 buffer_consume(&incoming_packet, 4 + 1);
1132 buffer_consume_end(&incoming_packet, padlen);
1133
1134 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1135 if (comp && comp->enabled) {
1136 buffer_clear(&compression_buffer);
1137 buffer_uncompress(&incoming_packet, &compression_buffer);
1138 buffer_clear(&incoming_packet);
1139 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1140 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001141 DBG(debug("input: len after de-compress %d",
1142 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001143 }
1144 /*
1145 * get packet type, implies consume.
1146 * return length of payload (without type field)
1147 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001148 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001149 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1150 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001151 if (type == SSH2_MSG_NEWKEYS)
1152 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001153 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1154 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001155#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001156 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001157 buffer_dump(&incoming_packet);
1158#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001159 /* reset for next packet */
1160 packet_length = 0;
1161 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001162}
1163
1164int
Damien Millerdff50992002-01-22 23:16:32 +11001165packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001166{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001167 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001168 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001169 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001170
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001171 for (;;) {
1172 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001173 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001174 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001175 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001176 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001177 case SSH2_MSG_IGNORE:
1178 break;
1179 case SSH2_MSG_DEBUG:
1180 packet_get_char();
1181 msg = packet_get_string(NULL);
1182 debug("Remote: %.900s", msg);
1183 xfree(msg);
1184 msg = packet_get_string(NULL);
1185 xfree(msg);
1186 break;
1187 case SSH2_MSG_DISCONNECT:
1188 reason = packet_get_int();
1189 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001190 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001191 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001192 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001193 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001194 break;
Damien Miller659811f2002-01-22 23:23:11 +11001195 case SSH2_MSG_UNIMPLEMENTED:
1196 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001197 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1198 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001199 break;
Damien Miller33b13562000-04-04 14:38:59 +10001200 default:
1201 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001202 }
Damien Miller33b13562000-04-04 14:38:59 +10001203 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001204 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001205 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001206 case SSH_MSG_IGNORE:
1207 break;
1208 case SSH_MSG_DEBUG:
1209 msg = packet_get_string(NULL);
1210 debug("Remote: %.900s", msg);
1211 xfree(msg);
1212 break;
1213 case SSH_MSG_DISCONNECT:
1214 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001215 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001216 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001217 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001218 xfree(msg);
1219 break;
1220 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001221 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001222 DBG(debug("received packet type %d", type));
1223 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001224 }
Damien Miller33b13562000-04-04 14:38:59 +10001225 }
1226 }
1227}
1228
Damien Miller278f9072001-12-21 15:00:19 +11001229int
Damien Millerdff50992002-01-22 23:16:32 +11001230packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001231{
Damien Millerdff50992002-01-22 23:16:32 +11001232 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001233}
1234
Damien Miller5428f641999-11-25 11:54:57 +11001235/*
1236 * Buffers the given amount of input characters. This is intended to be used
1237 * together with packet_read_poll.
1238 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001239
1240void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001241packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242{
Damien Miller95def091999-11-25 00:26:21 +11001243 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001244}
1245
1246/* Returns a character from the packet. */
1247
Ben Lindstrom46c16222000-12-22 01:43:59 +00001248u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001249packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001250{
Damien Miller95def091999-11-25 00:26:21 +11001251 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001252
Damien Miller95def091999-11-25 00:26:21 +11001253 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001254 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255}
1256
1257/* Returns an integer from the packet data. */
1258
Ben Lindstrom46c16222000-12-22 01:43:59 +00001259u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001260packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001261{
Damien Miller95def091999-11-25 00:26:21 +11001262 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001263}
1264
Damien Miller5428f641999-11-25 11:54:57 +11001265/*
1266 * Returns an arbitrary precision integer from the packet data. The integer
1267 * must have been initialized before this call.
1268 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001269
1270void
Damien Millerd432ccf2002-01-22 23:14:44 +11001271packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272{
Damien Miller76e1e362002-01-22 23:15:57 +11001273 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001274}
1275
Damien Miller33b13562000-04-04 14:38:59 +10001276void
Damien Millerd432ccf2002-01-22 23:14:44 +11001277packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001278{
Damien Miller76e1e362002-01-22 23:15:57 +11001279 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001280}
1281
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001282void *
Damien Millereccb9de2005-06-17 12:59:34 +10001283packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001284{
Damien Millereccb9de2005-06-17 12:59:34 +10001285 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001286
Damien Miller33b13562000-04-04 14:38:59 +10001287 if (length_ptr != NULL)
1288 *length_ptr = bytes;
1289 return buffer_ptr(&incoming_packet);
1290}
1291
Damien Miller4af51302000-04-16 11:18:38 +10001292int
1293packet_remaining(void)
1294{
1295 return buffer_len(&incoming_packet);
1296}
1297
Damien Miller5428f641999-11-25 11:54:57 +11001298/*
1299 * Returns a string from the packet data. The string is allocated using
1300 * xmalloc; it is the responsibility of the calling program to free it when
1301 * no longer needed. The length_ptr argument may be NULL, or point to an
1302 * integer into which the length of the string is stored.
1303 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001304
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001305void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001306packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001307{
Damien Miller95def091999-11-25 00:26:21 +11001308 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309}
1310
Damien Miller5428f641999-11-25 11:54:57 +11001311/*
1312 * Sends a diagnostic message from the server to the client. This message
1313 * can be sent at any time (but not while constructing another message). The
1314 * message is printed immediately, but only if the client is being executed
1315 * in verbose mode. These messages are primarily intended to ease debugging
1316 * authentication problems. The length of the formatted message must not
1317 * exceed 1024 bytes. This will automatically call packet_write_wait.
1318 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319
1320void
Damien Miller95def091999-11-25 00:26:21 +11001321packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001322{
Damien Miller95def091999-11-25 00:26:21 +11001323 char buf[1024];
1324 va_list args;
1325
Ben Lindstroma14ee472000-12-07 01:24:58 +00001326 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1327 return;
1328
Damien Miller95def091999-11-25 00:26:21 +11001329 va_start(args, fmt);
1330 vsnprintf(buf, sizeof(buf), fmt, args);
1331 va_end(args);
1332
Damien Miller7c8af4f2000-05-01 08:24:07 +10001333 if (compat20) {
1334 packet_start(SSH2_MSG_DEBUG);
1335 packet_put_char(0); /* bool: always display */
1336 packet_put_cstring(buf);
1337 packet_put_cstring("");
1338 } else {
1339 packet_start(SSH_MSG_DEBUG);
1340 packet_put_cstring(buf);
1341 }
Damien Miller95def091999-11-25 00:26:21 +11001342 packet_send();
1343 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001344}
1345
Damien Miller5428f641999-11-25 11:54:57 +11001346/*
1347 * Logs the error plus constructs and sends a disconnect packet, closes the
1348 * connection, and exits. This function never returns. The error message
1349 * should not contain a newline. The length of the formatted message must
1350 * not exceed 1024 bytes.
1351 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001352
1353void
Damien Miller95def091999-11-25 00:26:21 +11001354packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355{
Damien Miller95def091999-11-25 00:26:21 +11001356 char buf[1024];
1357 va_list args;
1358 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001359
Damien Miller95def091999-11-25 00:26:21 +11001360 if (disconnecting) /* Guard against recursive invocations. */
1361 fatal("packet_disconnect called recursively.");
1362 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363
Damien Miller5428f641999-11-25 11:54:57 +11001364 /*
1365 * Format the message. Note that the caller must make sure the
1366 * message is of limited size.
1367 */
Damien Miller95def091999-11-25 00:26:21 +11001368 va_start(args, fmt);
1369 vsnprintf(buf, sizeof(buf), fmt, args);
1370 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001371
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001372 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001373 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001374
Damien Miller95def091999-11-25 00:26:21 +11001375 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001376 if (compat20) {
1377 packet_start(SSH2_MSG_DISCONNECT);
1378 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1379 packet_put_cstring(buf);
1380 packet_put_cstring("");
1381 } else {
1382 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001383 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001384 }
Damien Miller95def091999-11-25 00:26:21 +11001385 packet_send();
1386 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
Damien Miller95def091999-11-25 00:26:21 +11001388 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001389 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390
Damien Miller95def091999-11-25 00:26:21 +11001391 /* Close the connection. */
1392 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001393 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394}
1395
Damien Miller5428f641999-11-25 11:54:57 +11001396/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001397
1398void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001399packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400{
Damien Miller95def091999-11-25 00:26:21 +11001401 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001402
Damien Miller95def091999-11-25 00:26:21 +11001403 if (len > 0) {
1404 len = write(connection_out, buffer_ptr(&output), len);
1405 if (len <= 0) {
1406 if (errno == EAGAIN)
1407 return;
1408 else
1409 fatal("Write failed: %.100s", strerror(errno));
1410 }
1411 buffer_consume(&output, len);
1412 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001413}
1414
Damien Miller5428f641999-11-25 11:54:57 +11001415/*
1416 * Calls packet_write_poll repeatedly until all pending output data has been
1417 * written.
1418 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001419
1420void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001421packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001422{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001423 fd_set *setp;
1424
Damien Miller07d86be2006-03-26 14:19:21 +11001425 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001426 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001427 packet_write_poll();
1428 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001429 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1430 sizeof(fd_mask));
1431 FD_SET(connection_out, setp);
1432 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001433 (errno == EAGAIN || errno == EINTR))
1434 ;
Damien Miller95def091999-11-25 00:26:21 +11001435 packet_write_poll();
1436 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001437 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438}
1439
1440/* Returns true if there is buffered data to write to the connection. */
1441
1442int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001443packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444{
Damien Miller95def091999-11-25 00:26:21 +11001445 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446}
1447
1448/* Returns true if there is not too much data to write to the connection. */
1449
1450int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001451packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001452{
Damien Miller95def091999-11-25 00:26:21 +11001453 if (interactive_mode)
1454 return buffer_len(&output) < 16384;
1455 else
1456 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001457}
1458
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001459
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001460static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001461packet_set_tos(int interactive)
1462{
Damien Miller5924ceb2003-11-22 15:02:42 +11001463#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001464 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1465
1466 if (!packet_connection_is_on_socket() ||
1467 !packet_connection_is_ipv4())
1468 return;
1469 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1470 sizeof(tos)) < 0)
1471 error("setsockopt IP_TOS %d: %.100s:",
1472 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001473#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001474}
Ben Lindstroma7433982002-12-23 02:41:41 +00001475
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476/* Informs that the current session is interactive. Sets IP flags for that. */
1477
1478void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001479packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001481 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001482
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001483 if (called)
1484 return;
1485 called = 1;
1486
Damien Miller95def091999-11-25 00:26:21 +11001487 /* Record that we are in interactive mode. */
1488 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001489
Damien Miller34132e52000-01-14 15:45:46 +11001490 /* Only set socket options if using a socket. */
1491 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001492 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001493 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001494 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495}
1496
1497/* Returns true if the current connection is interactive. */
1498
1499int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001500packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001501{
Damien Miller95def091999-11-25 00:26:21 +11001502 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001503}
Damien Miller6162d121999-11-21 13:23:52 +11001504
Darren Tucker1f8311c2004-05-13 16:39:33 +10001505int
Darren Tucker502d3842003-06-28 12:38:01 +10001506packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001507{
Damien Miller95def091999-11-25 00:26:21 +11001508 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001509
Damien Miller95def091999-11-25 00:26:21 +11001510 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001511 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001512 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001513 return -1;
1514 }
1515 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001516 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001517 return -1;
1518 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001519 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001520 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001521 max_packet_size = s;
1522 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001523}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001524
Damien Miller9f643902001-11-12 11:02:52 +11001525/* roundup current message to pad bytes */
1526void
1527packet_add_padding(u_char pad)
1528{
1529 extra_pad = pad;
1530}
1531
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001532/*
1533 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001534 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001535 * byte SSH_MSG_IGNORE
1536 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001537 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001538 * All implementations MUST understand (and ignore) this message at any
1539 * time (after receiving the protocol version). No implementation is
1540 * required to send them. This message can be used as an additional
1541 * protection measure against advanced traffic analysis techniques.
1542 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001543void
1544packet_send_ignore(int nbytes)
1545{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001546 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001547 int i;
1548
1549 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001550 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001551 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001552 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001553 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001554 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001555 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001556 }
1557}
Damien Millera5539d22003-04-09 20:50:06 +10001558
Darren Tucker1f8311c2004-05-13 16:39:33 +10001559#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001560int
1561packet_need_rekeying(void)
1562{
1563 if (datafellows & SSH_BUG_NOREKEY)
1564 return 0;
1565 return
1566 (p_send.packets > MAX_PACKETS) ||
1567 (p_read.packets > MAX_PACKETS) ||
1568 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1569 (max_blocks_in && (p_read.blocks > max_blocks_in));
1570}
1571
1572void
1573packet_set_rekey_limit(u_int32_t bytes)
1574{
1575 rekey_limit = bytes;
1576}
Damien Miller9786e6e2005-07-26 21:54:56 +10001577
1578void
1579packet_set_server(void)
1580{
1581 server_side = 1;
1582}
1583
1584void
1585packet_set_authenticated(void)
1586{
1587 after_authentication = 1;
1588}