blob: f04d6fac93cfc85b7a709b1c4ee87eefaffaf596 [file] [log] [blame]
Damien Millere3b60b52006-07-10 21:08:03 +10001/* $OpenBSD: packet.c,v 1.133 2006/07/08 21:47:12 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 Millere3b60b52006-07-10 21:08:03 +100042#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100043#include "openbsd-compat/sys-queue.h"
Damien Miller8ec8c3e2006-07-10 20:35:38 +100044#include <sys/socket.h>
45
46#include <netinet/in_systm.h>
47#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110048#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
50#include "xmalloc.h"
51#include "buffer.h"
52#include "packet.h"
53#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
56#include "compress.h"
57#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000058#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller33b13562000-04-04 14:38:59 +100060#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100062#include "ssh2.h"
63
Damien Miller874d77b2000-10-14 16:23:11 +110064#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100065#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000066#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067#include "log.h"
68#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110069#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000070#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100071
72#ifdef PACKET_DEBUG
73#define DBG(x) x
74#else
75#define DBG(x)
76#endif
77
Damien Miller5428f641999-11-25 11:54:57 +110078/*
79 * This variable contains the file descriptors used for communicating with
80 * the other side. connection_in is used for reading; connection_out for
81 * writing. These can be the same descriptor, in which case it is assumed to
82 * be a socket.
83 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084static int connection_in = -1;
85static int connection_out = -1;
86
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000088static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
90/* Encryption context for receiving data. This is only used for decryption. */
91static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110092
93/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094static CipherContext send_context;
95
96/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000097Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000100Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
102/* Buffer for the partial outgoing packet being constructed. */
103static Buffer outgoing_packet;
104
105/* Buffer for the incoming packet currently being processed. */
106static Buffer incoming_packet;
107
108/* Scratch buffer for packet compression/decompression. */
109static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000110static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
112/* Flag indicating whether packet compression/decompression is enabled. */
113static int packet_compression = 0;
114
Damien Miller6162d121999-11-21 13:23:52 +1100115/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000116u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100117
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118/* Flag indicating whether this module has been initialized. */
119static int initialized = 0;
120
121/* Set to true if the connection is interactive. */
122static int interactive_mode = 0;
123
Damien Miller9786e6e2005-07-26 21:54:56 +1000124/* Set to true if we are the server side. */
125static int server_side = 0;
126
127/* Set to true if we are authenticated. */
128static int after_authentication = 0;
129
Damien Miller33b13562000-04-04 14:38:59 +1000130/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000131Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000132static struct packet_state {
133 u_int32_t seqnr;
134 u_int32_t packets;
135 u_int64_t blocks;
136} p_read, p_send;
137
138static u_int64_t max_blocks_in, max_blocks_out;
139static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000140
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000141/* Session key for protocol v1 */
142static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
143static u_int ssh1_keylen;
144
Damien Miller9f643902001-11-12 11:02:52 +1100145/* roundup current message to extra_pad bytes */
146static u_char extra_pad = 0;
147
Damien Millera5539d22003-04-09 20:50:06 +1000148struct packet {
149 TAILQ_ENTRY(packet) next;
150 u_char type;
151 Buffer payload;
152};
153TAILQ_HEAD(, packet) outgoing;
154
Damien Miller5428f641999-11-25 11:54:57 +1100155/*
156 * Sets the descriptors used for communication. Disables encryption until
157 * packet_set_encryption_key is called.
158 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159void
160packet_set_connection(int fd_in, int fd_out)
161{
Damien Miller874d77b2000-10-14 16:23:11 +1100162 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000163
Damien Miller874d77b2000-10-14 16:23:11 +1100164 if (none == NULL)
165 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100166 connection_in = fd_in;
167 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000168 cipher_init(&send_context, none, (const u_char *)"",
169 0, NULL, 0, CIPHER_ENCRYPT);
170 cipher_init(&receive_context, none, (const u_char *)"",
171 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000172 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100173 if (!initialized) {
174 initialized = 1;
175 buffer_init(&input);
176 buffer_init(&output);
177 buffer_init(&outgoing_packet);
178 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000179 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100180 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181}
182
Damien Miller34132e52000-01-14 15:45:46 +1100183/* Returns 1 if remote host is connected via socket, 0 if not. */
184
185int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000186packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100187{
188 struct sockaddr_storage from, to;
189 socklen_t fromlen, tolen;
190
191 /* filedescriptors in and out are the same, so it's a socket */
192 if (connection_in == connection_out)
193 return 1;
194 fromlen = sizeof(from);
195 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100196 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100197 return 0;
198 tolen = sizeof(to);
199 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100200 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100201 return 0;
202 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
203 return 0;
204 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
205 return 0;
206 return 1;
207}
208
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000209/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000210 * Exports an IV from the CipherContext required to export the key
211 * state back from the unprivileged child to the privileged parent
212 * process.
213 */
214
215void
216packet_get_keyiv(int mode, u_char *iv, u_int len)
217{
218 CipherContext *cc;
219
220 if (mode == MODE_OUT)
221 cc = &send_context;
222 else
223 cc = &receive_context;
224
225 cipher_get_keyiv(cc, iv, len);
226}
227
228int
229packet_get_keycontext(int mode, u_char *dat)
230{
231 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000232
Ben Lindstromf6027d32002-03-22 01:42:04 +0000233 if (mode == MODE_OUT)
234 cc = &send_context;
235 else
236 cc = &receive_context;
237
238 return (cipher_get_keycontext(cc, dat));
239}
240
241void
242packet_set_keycontext(int mode, u_char *dat)
243{
244 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000245
Ben Lindstromf6027d32002-03-22 01:42:04 +0000246 if (mode == MODE_OUT)
247 cc = &send_context;
248 else
249 cc = &receive_context;
250
251 cipher_set_keycontext(cc, dat);
252}
253
254int
255packet_get_keyiv_len(int mode)
256{
257 CipherContext *cc;
258
259 if (mode == MODE_OUT)
260 cc = &send_context;
261 else
262 cc = &receive_context;
263
264 return (cipher_get_keyiv_len(cc));
265}
Damien Miller4f7becb2006-03-26 14:10:14 +1100266
Ben Lindstromf6027d32002-03-22 01:42:04 +0000267void
268packet_set_iv(int mode, u_char *dat)
269{
270 CipherContext *cc;
271
272 if (mode == MODE_OUT)
273 cc = &send_context;
274 else
275 cc = &receive_context;
276
277 cipher_set_keyiv(cc, dat);
278}
Damien Miller4f7becb2006-03-26 14:10:14 +1100279
Ben Lindstromf6027d32002-03-22 01:42:04 +0000280int
Damien Miller2b92d322003-06-11 22:05:06 +1000281packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000282{
283 return (cipher_get_number(receive_context.cipher));
284}
285
Damien Millera5539d22003-04-09 20:50:06 +1000286void
287packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000288{
Damien Millera5539d22003-04-09 20:50:06 +1000289 struct packet_state *state;
290
291 state = (mode == MODE_IN) ? &p_read : &p_send;
292 *seqnr = state->seqnr;
293 *blocks = state->blocks;
294 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000295}
296
297void
Damien Millera5539d22003-04-09 20:50:06 +1000298packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000299{
Damien Millera5539d22003-04-09 20:50:06 +1000300 struct packet_state *state;
301
302 state = (mode == MODE_IN) ? &p_read : &p_send;
303 state->seqnr = seqnr;
304 state->blocks = blocks;
305 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000306}
307
Damien Miller34132e52000-01-14 15:45:46 +1100308/* returns 1 if connection is via ipv4 */
309
310int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000311packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100312{
313 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100314 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100315
316 memset(&to, 0, sizeof(to));
317 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
318 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000319 if (to.ss_family == AF_INET)
320 return 1;
321#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100322 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000323 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
324 return 1;
325#endif
326 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100327}
328
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329/* Sets the connection into non-blocking mode. */
330
331void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000332packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333{
Damien Miller95def091999-11-25 00:26:21 +1100334 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000335 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Damien Miller232711f2004-06-15 10:35:30 +1000337 if (connection_out != connection_in)
338 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339}
340
341/* Returns the socket used for reading. */
342
343int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000344packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345{
Damien Miller95def091999-11-25 00:26:21 +1100346 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347}
348
349/* Returns the descriptor used for writing. */
350
351int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000352packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353{
Damien Miller95def091999-11-25 00:26:21 +1100354 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355}
356
357/* Closes the connection and clears and frees internal data structures. */
358
359void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000360packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361{
Damien Miller95def091999-11-25 00:26:21 +1100362 if (!initialized)
363 return;
364 initialized = 0;
365 if (connection_in == connection_out) {
366 shutdown(connection_out, SHUT_RDWR);
367 close(connection_out);
368 } else {
369 close(connection_in);
370 close(connection_out);
371 }
372 buffer_free(&input);
373 buffer_free(&output);
374 buffer_free(&outgoing_packet);
375 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000376 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100377 buffer_free(&compression_buffer);
378 buffer_compress_uninit();
379 }
Damien Miller963f6b22002-02-19 15:21:23 +1100380 cipher_cleanup(&send_context);
381 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382}
383
384/* Sets remote side protocol flags. */
385
386void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000387packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388{
Damien Miller95def091999-11-25 00:26:21 +1100389 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390}
391
392/* Returns the remote protocol flags set earlier by the above function. */
393
Ben Lindstrom46c16222000-12-22 01:43:59 +0000394u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000395packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396{
Damien Miller95def091999-11-25 00:26:21 +1100397 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398}
399
Damien Miller5428f641999-11-25 11:54:57 +1100400/*
401 * Starts packet compression from the next packet on in both directions.
402 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
403 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404
Ben Lindstrombba81212001-06-25 05:01:22 +0000405static void
406packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000407{
408 if (compression_buffer_ready == 1)
409 return;
410 compression_buffer_ready = 1;
411 buffer_init(&compression_buffer);
412}
413
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414void
415packet_start_compression(int level)
416{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000417 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100418 fatal("Compression already enabled.");
419 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000420 packet_init_compression();
421 buffer_compress_init_send(level);
422 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423}
424
Damien Miller5428f641999-11-25 11:54:57 +1100425/*
Damien Miller5428f641999-11-25 11:54:57 +1100426 * Causes any further packets to be encrypted using the given key. The same
427 * key is used for both sending and reception. However, both directions are
428 * encrypted independently of each other.
429 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000430
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000432packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100433 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434{
Damien Miller874d77b2000-10-14 16:23:11 +1100435 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000436
Damien Miller874d77b2000-10-14 16:23:11 +1100437 if (cipher == NULL)
438 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000439 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100440 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000441 if (keylen > SSH_SESSION_KEY_LENGTH)
442 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
443 memcpy(ssh1_key, key, keylen);
444 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100445 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
446 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447}
448
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000449u_int
450packet_get_encryption_key(u_char *key)
451{
452 if (key == NULL)
453 return (ssh1_keylen);
454 memcpy(key, ssh1_key, ssh1_keylen);
455 return (ssh1_keylen);
456}
457
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000458/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000460packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000462 u_char buf[9];
463 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Ben Lindstrom204e4882001-03-05 06:47:00 +0000465 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000466 len = compat20 ? 6 : 9;
467 memset(buf, 0, len - 1);
468 buf[len - 1] = type;
469 buffer_clear(&outgoing_packet);
470 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000471}
472
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000473/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474void
475packet_put_char(int value)
476{
Damien Miller95def091999-11-25 00:26:21 +1100477 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000478
Damien Miller95def091999-11-25 00:26:21 +1100479 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480}
Damien Miller4f7becb2006-03-26 14:10:14 +1100481
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000483packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484{
Damien Miller95def091999-11-25 00:26:21 +1100485 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486}
Damien Miller4f7becb2006-03-26 14:10:14 +1100487
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100489packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490{
Damien Miller95def091999-11-25 00:26:21 +1100491 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492}
Damien Miller4f7becb2006-03-26 14:10:14 +1100493
Damien Miller33b13562000-04-04 14:38:59 +1000494void
495packet_put_cstring(const char *str)
496{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000497 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000498}
Damien Miller4f7becb2006-03-26 14:10:14 +1100499
Damien Miller33b13562000-04-04 14:38:59 +1000500void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100501packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000502{
503 buffer_append(&outgoing_packet, buf, len);
504}
Damien Miller4f7becb2006-03-26 14:10:14 +1100505
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506void
Damien Miller95def091999-11-25 00:26:21 +1100507packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508{
Damien Miller95def091999-11-25 00:26:21 +1100509 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510}
Damien Miller4f7becb2006-03-26 14:10:14 +1100511
Damien Miller33b13562000-04-04 14:38:59 +1000512void
513packet_put_bignum2(BIGNUM * value)
514{
515 buffer_put_bignum2(&outgoing_packet, value);
516}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517
Damien Miller5428f641999-11-25 11:54:57 +1100518/*
519 * Finalizes and sends the packet. If the encryption key has been set,
520 * encrypts the packet before sending.
521 */
Damien Miller95def091999-11-25 00:26:21 +1100522
Ben Lindstrombba81212001-06-25 05:01:22 +0000523static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000524packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000526 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100527 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000528 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000529 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530
Damien Miller5428f641999-11-25 11:54:57 +1100531 /*
532 * If using packet compression, compress the payload of the outgoing
533 * packet.
534 */
Damien Miller95def091999-11-25 00:26:21 +1100535 if (packet_compression) {
536 buffer_clear(&compression_buffer);
537 /* Skip padding. */
538 buffer_consume(&outgoing_packet, 8);
539 /* padding */
540 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
541 buffer_compress(&outgoing_packet, &compression_buffer);
542 buffer_clear(&outgoing_packet);
543 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100544 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100545 }
546 /* Compute packet length without padding (add checksum, remove padding). */
547 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000548
Damien Millere247cc42000-05-07 12:03:14 +1000549 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100550 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100551 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100552 cp = buffer_ptr(&outgoing_packet);
553 for (i = 0; i < padding; i++) {
554 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000555 rnd = arc4random();
556 cp[7 - i] = rnd & 0xff;
557 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100558 }
559 }
560 buffer_consume(&outgoing_packet, 8 - padding);
561
562 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100563 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000564 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100565 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100566 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567
568#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100569 fprintf(stderr, "packet_send plain: ");
570 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571#endif
572
Damien Miller95def091999-11-25 00:26:21 +1100573 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100574 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100575 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100576 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100577 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100578 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100579
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100581 fprintf(stderr, "encrypted: ");
582 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583#endif
584
Damien Miller95def091999-11-25 00:26:21 +1100585 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586
Damien Miller5428f641999-11-25 11:54:57 +1100587 /*
Damien Miller788f2122005-11-05 15:14:59 +1100588 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100589 * actually sent until packet_write_wait or packet_write_poll is
590 * called.
591 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592}
593
Ben Lindstromf6027d32002-03-22 01:42:04 +0000594void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000595set_newkeys(int mode)
596{
597 Enc *enc;
598 Mac *mac;
599 Comp *comp;
600 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000601 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000602 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000603
Ben Lindstrom064496f2002-12-23 02:04:22 +0000604 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000605
Damien Miller963f6b22002-02-19 15:21:23 +1100606 if (mode == MODE_OUT) {
607 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000608 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000609 p_send.packets = p_send.blocks = 0;
610 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100611 } else {
612 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000613 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000614 p_read.packets = p_read.blocks = 0;
615 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100616 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000617 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000618 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100619 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000620 enc = &newkeys[mode]->enc;
621 mac = &newkeys[mode]->mac;
622 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000623 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000624 xfree(enc->name);
625 xfree(enc->iv);
626 xfree(enc->key);
627 xfree(mac->name);
628 xfree(mac->key);
629 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000630 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000631 }
632 newkeys[mode] = kex_get_newkeys(mode);
633 if (newkeys[mode] == NULL)
634 fatal("newkeys: no keys for mode %d", mode);
635 enc = &newkeys[mode]->enc;
636 mac = &newkeys[mode]->mac;
637 comp = &newkeys[mode]->comp;
638 if (mac->md != NULL)
639 mac->enabled = 1;
640 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100641 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000642 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000643 /* Deleting the keys does not gain extra security */
644 /* memset(enc->iv, 0, enc->block_size);
645 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000646 if ((comp->type == COMP_ZLIB ||
647 (comp->type == COMP_DELAYED && after_authentication)) &&
648 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000649 packet_init_compression();
650 if (mode == MODE_OUT)
651 buffer_compress_init_send(6);
652 else
653 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000654 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000655 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000656 /*
657 * The 2^(blocksize*2) limit is too expensive for 3DES,
658 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
659 */
660 if (enc->block_size >= 16)
661 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
662 else
663 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000664 if (rekey_limit)
665 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000666}
667
Damien Miller5428f641999-11-25 11:54:57 +1100668/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000669 * Delayed compression for SSH2 is enabled after authentication:
670 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
671 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
672 */
673static void
674packet_enable_delayed_compress(void)
675{
676 Comp *comp = NULL;
677 int mode;
678
679 /*
680 * Remember that we are past the authentication step, so rekeying
681 * with COMP_DELAYED will turn on compression immediately.
682 */
683 after_authentication = 1;
684 for (mode = 0; mode < MODE_MAX; mode++) {
685 comp = &newkeys[mode]->comp;
686 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000687 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000688 if (mode == MODE_OUT)
689 buffer_compress_init_send(6);
690 else
691 buffer_compress_init_recv();
692 comp->enabled = 1;
693 }
694 }
695}
696
697/*
Damien Miller33b13562000-04-04 14:38:59 +1000698 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
699 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000700static void
Damien Millera5539d22003-04-09 20:50:06 +1000701packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000702{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000703 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100704 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000705 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100706 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000707 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000708 Enc *enc = NULL;
709 Mac *mac = NULL;
710 Comp *comp = NULL;
711 int block_size;
712
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000713 if (newkeys[MODE_OUT] != NULL) {
714 enc = &newkeys[MODE_OUT]->enc;
715 mac = &newkeys[MODE_OUT]->mac;
716 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000717 }
Damien Miller963f6b22002-02-19 15:21:23 +1100718 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000719
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000720 cp = buffer_ptr(&outgoing_packet);
721 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000722
723#ifdef PACKET_DEBUG
724 fprintf(stderr, "plain: ");
725 buffer_dump(&outgoing_packet);
726#endif
727
728 if (comp && comp->enabled) {
729 len = buffer_len(&outgoing_packet);
730 /* skip header, compress only payload */
731 buffer_consume(&outgoing_packet, 5);
732 buffer_clear(&compression_buffer);
733 buffer_compress(&outgoing_packet, &compression_buffer);
734 buffer_clear(&outgoing_packet);
735 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
736 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
737 buffer_len(&compression_buffer));
738 DBG(debug("compression: raw %d compressed %d", len,
739 buffer_len(&outgoing_packet)));
740 }
741
742 /* sizeof (packet_len + pad_len + payload) */
743 len = buffer_len(&outgoing_packet);
744
745 /*
746 * calc size of padding, alloc space, get random data,
747 * minimum padding is 4 bytes
748 */
749 padlen = block_size - (len % block_size);
750 if (padlen < 4)
751 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100752 if (extra_pad) {
753 /* will wrap if extra_pad+padlen > 255 */
754 extra_pad = roundup(extra_pad, block_size);
755 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000756 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100757 pad, len, padlen, extra_pad);
758 padlen += pad;
759 extra_pad = 0;
760 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100761 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100762 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000763 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000764 for (i = 0; i < padlen; i++) {
765 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000766 rnd = arc4random();
767 cp[i] = rnd & 0xff;
768 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000769 }
Damien Millere247cc42000-05-07 12:03:14 +1000770 } else {
771 /* clear padding */
772 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000773 }
774 /* packet_length includes payload, padding and padding length field */
775 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000776 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100777 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000778 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000779 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
780
781 /* compute MAC over seqnr and packet(length fields, payload, padding) */
782 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000783 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100784 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000785 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000786 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000787 }
788 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100789 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100790 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000791 buffer_len(&outgoing_packet));
792 /* append unencrypted MAC */
793 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100794 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000795#ifdef PACKET_DEBUG
796 fprintf(stderr, "encrypted: ");
797 buffer_dump(&output);
798#endif
Damien Miller4af51302000-04-16 11:18:38 +1000799 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000800 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000801 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000802 if (++p_send.packets == 0)
803 if (!(datafellows & SSH_BUG_NOREKEY))
804 fatal("XXX too many packets with same key");
805 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000806 buffer_clear(&outgoing_packet);
807
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000808 if (type == SSH2_MSG_NEWKEYS)
809 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000810 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
811 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000812}
813
Damien Millera5539d22003-04-09 20:50:06 +1000814static void
815packet_send2(void)
816{
817 static int rekeying = 0;
818 struct packet *p;
819 u_char type, *cp;
820
821 cp = buffer_ptr(&outgoing_packet);
822 type = cp[5];
823
824 /* during rekeying we can only send key exchange messages */
825 if (rekeying) {
826 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
827 (type <= SSH2_MSG_TRANSPORT_MAX))) {
828 debug("enqueue packet: %u", type);
829 p = xmalloc(sizeof(*p));
830 p->type = type;
831 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
832 buffer_init(&outgoing_packet);
833 TAILQ_INSERT_TAIL(&outgoing, p, next);
834 return;
835 }
836 }
837
838 /* rekeying starts with sending KEXINIT */
839 if (type == SSH2_MSG_KEXINIT)
840 rekeying = 1;
841
842 packet_send2_wrapped();
843
844 /* after a NEWKEYS message we can send the complete queue */
845 if (type == SSH2_MSG_NEWKEYS) {
846 rekeying = 0;
847 while ((p = TAILQ_FIRST(&outgoing))) {
848 type = p->type;
849 debug("dequeue packet: %u", type);
850 buffer_free(&outgoing_packet);
851 memcpy(&outgoing_packet, &p->payload,
852 sizeof(Buffer));
853 TAILQ_REMOVE(&outgoing, p, next);
854 xfree(p);
855 packet_send2_wrapped();
856 }
857 }
858}
859
Damien Miller33b13562000-04-04 14:38:59 +1000860void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000861packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000862{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000863 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000864 packet_send2();
865 else
866 packet_send1();
867 DBG(debug("packet_send done"));
868}
869
Damien Miller33b13562000-04-04 14:38:59 +1000870/*
Damien Miller5428f641999-11-25 11:54:57 +1100871 * Waits until a packet has been received, and returns its type. Note that
872 * no other data is processed until this returns, so this function should not
873 * be used during the interactive session.
874 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875
876int
Damien Millerdff50992002-01-22 23:16:32 +1100877packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878{
Damien Miller95def091999-11-25 00:26:21 +1100879 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000880 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100881 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000882 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883
Damien Miller07d86be2006-03-26 14:19:21 +1100884 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000885 sizeof(fd_mask));
886
Damien Miller95def091999-11-25 00:26:21 +1100887 /* Since we are blocking, ensure that all written packets have been sent. */
888 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889
Damien Miller95def091999-11-25 00:26:21 +1100890 /* Stay in the loop until we have received a complete packet. */
891 for (;;) {
892 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100893 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000894 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000895 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100896 || type == SSH_SMSG_FAILURE
897 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000898 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100899 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100900 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000901 if (type != SSH_MSG_NONE) {
902 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100903 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000904 }
Damien Miller5428f641999-11-25 11:54:57 +1100905 /*
906 * Otherwise, wait for some data to arrive, add it to the
907 * buffer, and try again.
908 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000909 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
910 sizeof(fd_mask));
911 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100912
Damien Miller95def091999-11-25 00:26:21 +1100913 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000914 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000915 (errno == EAGAIN || errno == EINTR))
916 ;
Damien Miller5428f641999-11-25 11:54:57 +1100917
Damien Miller95def091999-11-25 00:26:21 +1100918 /* Read data from the socket. */
919 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100920 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000921 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000922 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100923 }
Damien Miller95def091999-11-25 00:26:21 +1100924 if (len < 0)
925 fatal("Read from socket failed: %.100s", strerror(errno));
926 /* Append it to the buffer. */
927 packet_process_incoming(buf, len);
928 }
929 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930}
931
Damien Miller278f9072001-12-21 15:00:19 +1100932int
Damien Millerdff50992002-01-22 23:16:32 +1100933packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100934{
Damien Millerdff50992002-01-22 23:16:32 +1100935 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100936}
937
Damien Miller5428f641999-11-25 11:54:57 +1100938/*
939 * Waits until a packet has been received, verifies that its type matches
940 * that given, and gives a fatal error and exits if there is a mismatch.
941 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942
943void
Damien Millerdff50992002-01-22 23:16:32 +1100944packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945{
Damien Miller95def091999-11-25 00:26:21 +1100946 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947
Damien Millerdff50992002-01-22 23:16:32 +1100948 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100949 if (type != expected_type)
950 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000951 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952}
953
954/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100955 * packet_process_incoming. If so, reads the packet; otherwise returns
956 * SSH_MSG_NONE. This does not wait for data from the connection.
957 *
958 * SSH_MSG_DISCONNECT is handled specially here. Also,
959 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
960 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100961 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962
Ben Lindstrombba81212001-06-25 05:01:22 +0000963static int
Damien Millerdff50992002-01-22 23:16:32 +1100964packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000966 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000967 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000968 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969
Damien Miller95def091999-11-25 00:26:21 +1100970 /* Check if input size is less than minimum packet size. */
971 if (buffer_len(&input) < 4 + 8)
972 return SSH_MSG_NONE;
973 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000974 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100975 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100976 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000977 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100978 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
Damien Miller95def091999-11-25 00:26:21 +1100980 /* Check if the packet has been entirely received. */
981 if (buffer_len(&input) < 4 + padded_len)
982 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983
Damien Miller95def091999-11-25 00:26:21 +1100984 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000985
Damien Miller95def091999-11-25 00:26:21 +1100986 /* Consume packet length. */
987 buffer_consume(&input, 4);
988
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000989 /*
990 * Cryptographic attack detector for ssh
991 * (C)1998 CORE-SDI, Buenos Aires Argentina
992 * Ariel Futoransky(futo@core-sdi.com)
993 */
Damien Miller963f6b22002-02-19 15:21:23 +1100994 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +1100995 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000996 packet_disconnect("crc32 compensation attack: network attack detected");
997
998 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100999 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001000 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001001 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001002
Damien Miller95def091999-11-25 00:26:21 +11001003 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004
1005#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001006 fprintf(stderr, "read_poll plain: ");
1007 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009
Damien Miller95def091999-11-25 00:26:21 +11001010 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001011 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001012 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013
Damien Miller95def091999-11-25 00:26:21 +11001014 /* Skip padding. */
1015 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001016
Damien Miller95def091999-11-25 00:26:21 +11001017 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001018 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001019 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001020 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001021
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001022 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001023 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001024 if (checksum != stored_checksum)
1025 packet_disconnect("Corrupted check bytes on input.");
1026 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027
Damien Miller95def091999-11-25 00:26:21 +11001028 if (packet_compression) {
1029 buffer_clear(&compression_buffer);
1030 buffer_uncompress(&incoming_packet, &compression_buffer);
1031 buffer_clear(&incoming_packet);
1032 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001033 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001034 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001035 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001036 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1037 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001038 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039}
Damien Miller95def091999-11-25 00:26:21 +11001040
Ben Lindstrombba81212001-06-25 05:01:22 +00001041static int
Damien Millerdff50992002-01-22 23:16:32 +11001042packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001043{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001044 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001045 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001046 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001047 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001048 Enc *enc = NULL;
1049 Mac *mac = NULL;
1050 Comp *comp = NULL;
1051
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001052 if (newkeys[MODE_IN] != NULL) {
1053 enc = &newkeys[MODE_IN]->enc;
1054 mac = &newkeys[MODE_IN]->mac;
1055 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001056 }
1057 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001058 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001059
1060 if (packet_length == 0) {
1061 /*
1062 * check if input size is less than the cipher block size,
1063 * decrypt first block and extract length of incoming packet
1064 */
1065 if (buffer_len(&input) < block_size)
1066 return SSH_MSG_NONE;
1067 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001068 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001069 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001070 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001071 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001072 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001073 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001074#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001075 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001076#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001077 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001078 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001079 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001080 buffer_consume(&input, block_size);
1081 }
1082 /* we have a partial packet of block_size bytes */
1083 need = 4 + packet_length - block_size;
1084 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1085 need, maclen));
1086 if (need % block_size != 0)
1087 fatal("padding error: need %d block %d mod %d",
1088 need, block_size, need % block_size);
1089 /*
1090 * check if the entire packet has been received and
1091 * decrypt into incoming_packet
1092 */
1093 if (buffer_len(&input) < need + maclen)
1094 return SSH_MSG_NONE;
1095#ifdef PACKET_DEBUG
1096 fprintf(stderr, "read_poll enc/full: ");
1097 buffer_dump(&input);
1098#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001099 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001100 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001101 buffer_consume(&input, need);
1102 /*
1103 * compute MAC over seqnr and packet,
1104 * increment sequence number for incoming packet
1105 */
Damien Miller4af51302000-04-16 11:18:38 +10001106 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001107 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001108 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001109 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001110 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001111 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001112 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001113 buffer_consume(&input, mac->mac_len);
1114 }
Damien Miller278f9072001-12-21 15:00:19 +11001115 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001116 *seqnr_p = p_read.seqnr;
1117 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001118 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001119 if (++p_read.packets == 0)
1120 if (!(datafellows & SSH_BUG_NOREKEY))
1121 fatal("XXX too many packets with same key");
1122 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001123
1124 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001125 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001126 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001127 DBG(debug("input: padlen %d", padlen));
1128 if (padlen < 4)
1129 packet_disconnect("Corrupted padlen %d on input.", padlen);
1130
1131 /* skip packet size + padlen, discard padding */
1132 buffer_consume(&incoming_packet, 4 + 1);
1133 buffer_consume_end(&incoming_packet, padlen);
1134
1135 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1136 if (comp && comp->enabled) {
1137 buffer_clear(&compression_buffer);
1138 buffer_uncompress(&incoming_packet, &compression_buffer);
1139 buffer_clear(&incoming_packet);
1140 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1141 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001142 DBG(debug("input: len after de-compress %d",
1143 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001144 }
1145 /*
1146 * get packet type, implies consume.
1147 * return length of payload (without type field)
1148 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001149 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001150 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1151 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001152 if (type == SSH2_MSG_NEWKEYS)
1153 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001154 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1155 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001156#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001157 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001158 buffer_dump(&incoming_packet);
1159#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001160 /* reset for next packet */
1161 packet_length = 0;
1162 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001163}
1164
1165int
Damien Millerdff50992002-01-22 23:16:32 +11001166packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001167{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001168 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001169 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001170 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001171
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001172 for (;;) {
1173 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001174 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001175 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001176 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001177 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001178 case SSH2_MSG_IGNORE:
1179 break;
1180 case SSH2_MSG_DEBUG:
1181 packet_get_char();
1182 msg = packet_get_string(NULL);
1183 debug("Remote: %.900s", msg);
1184 xfree(msg);
1185 msg = packet_get_string(NULL);
1186 xfree(msg);
1187 break;
1188 case SSH2_MSG_DISCONNECT:
1189 reason = packet_get_int();
1190 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001191 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001192 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001193 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001194 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001195 break;
Damien Miller659811f2002-01-22 23:23:11 +11001196 case SSH2_MSG_UNIMPLEMENTED:
1197 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001198 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1199 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001200 break;
Damien Miller33b13562000-04-04 14:38:59 +10001201 default:
1202 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001203 }
Damien Miller33b13562000-04-04 14:38:59 +10001204 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001205 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001206 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001207 case SSH_MSG_IGNORE:
1208 break;
1209 case SSH_MSG_DEBUG:
1210 msg = packet_get_string(NULL);
1211 debug("Remote: %.900s", msg);
1212 xfree(msg);
1213 break;
1214 case SSH_MSG_DISCONNECT:
1215 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001216 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001217 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001218 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001219 xfree(msg);
1220 break;
1221 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001222 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001223 DBG(debug("received packet type %d", type));
1224 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001225 }
Damien Miller33b13562000-04-04 14:38:59 +10001226 }
1227 }
1228}
1229
Damien Miller278f9072001-12-21 15:00:19 +11001230int
Damien Millerdff50992002-01-22 23:16:32 +11001231packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001232{
Damien Millerdff50992002-01-22 23:16:32 +11001233 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001234}
1235
Damien Miller5428f641999-11-25 11:54:57 +11001236/*
1237 * Buffers the given amount of input characters. This is intended to be used
1238 * together with packet_read_poll.
1239 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240
1241void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001242packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243{
Damien Miller95def091999-11-25 00:26:21 +11001244 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245}
1246
1247/* Returns a character from the packet. */
1248
Ben Lindstrom46c16222000-12-22 01:43:59 +00001249u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001250packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251{
Damien Miller95def091999-11-25 00:26:21 +11001252 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001253
Damien Miller95def091999-11-25 00:26:21 +11001254 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001255 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256}
1257
1258/* Returns an integer from the packet data. */
1259
Ben Lindstrom46c16222000-12-22 01:43:59 +00001260u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001261packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001262{
Damien Miller95def091999-11-25 00:26:21 +11001263 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264}
1265
Damien Miller5428f641999-11-25 11:54:57 +11001266/*
1267 * Returns an arbitrary precision integer from the packet data. The integer
1268 * must have been initialized before this call.
1269 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270
1271void
Damien Millerd432ccf2002-01-22 23:14:44 +11001272packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001273{
Damien Miller76e1e362002-01-22 23:15:57 +11001274 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001275}
1276
Damien Miller33b13562000-04-04 14:38:59 +10001277void
Damien Millerd432ccf2002-01-22 23:14:44 +11001278packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001279{
Damien Miller76e1e362002-01-22 23:15:57 +11001280 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001281}
1282
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001283void *
Damien Millereccb9de2005-06-17 12:59:34 +10001284packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001285{
Damien Millereccb9de2005-06-17 12:59:34 +10001286 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001287
Damien Miller33b13562000-04-04 14:38:59 +10001288 if (length_ptr != NULL)
1289 *length_ptr = bytes;
1290 return buffer_ptr(&incoming_packet);
1291}
1292
Damien Miller4af51302000-04-16 11:18:38 +10001293int
1294packet_remaining(void)
1295{
1296 return buffer_len(&incoming_packet);
1297}
1298
Damien Miller5428f641999-11-25 11:54:57 +11001299/*
1300 * Returns a string from the packet data. The string is allocated using
1301 * xmalloc; it is the responsibility of the calling program to free it when
1302 * no longer needed. The length_ptr argument may be NULL, or point to an
1303 * integer into which the length of the string is stored.
1304 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001305
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001306void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001307packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001308{
Damien Miller95def091999-11-25 00:26:21 +11001309 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310}
1311
Damien Miller5428f641999-11-25 11:54:57 +11001312/*
1313 * Sends a diagnostic message from the server to the client. This message
1314 * can be sent at any time (but not while constructing another message). The
1315 * message is printed immediately, but only if the client is being executed
1316 * in verbose mode. These messages are primarily intended to ease debugging
1317 * authentication problems. The length of the formatted message must not
1318 * exceed 1024 bytes. This will automatically call packet_write_wait.
1319 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320
1321void
Damien Miller95def091999-11-25 00:26:21 +11001322packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001323{
Damien Miller95def091999-11-25 00:26:21 +11001324 char buf[1024];
1325 va_list args;
1326
Ben Lindstroma14ee472000-12-07 01:24:58 +00001327 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1328 return;
1329
Damien Miller95def091999-11-25 00:26:21 +11001330 va_start(args, fmt);
1331 vsnprintf(buf, sizeof(buf), fmt, args);
1332 va_end(args);
1333
Damien Miller7c8af4f2000-05-01 08:24:07 +10001334 if (compat20) {
1335 packet_start(SSH2_MSG_DEBUG);
1336 packet_put_char(0); /* bool: always display */
1337 packet_put_cstring(buf);
1338 packet_put_cstring("");
1339 } else {
1340 packet_start(SSH_MSG_DEBUG);
1341 packet_put_cstring(buf);
1342 }
Damien Miller95def091999-11-25 00:26:21 +11001343 packet_send();
1344 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001345}
1346
Damien Miller5428f641999-11-25 11:54:57 +11001347/*
1348 * Logs the error plus constructs and sends a disconnect packet, closes the
1349 * connection, and exits. This function never returns. The error message
1350 * should not contain a newline. The length of the formatted message must
1351 * not exceed 1024 bytes.
1352 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001353
1354void
Damien Miller95def091999-11-25 00:26:21 +11001355packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001356{
Damien Miller95def091999-11-25 00:26:21 +11001357 char buf[1024];
1358 va_list args;
1359 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001360
Damien Miller95def091999-11-25 00:26:21 +11001361 if (disconnecting) /* Guard against recursive invocations. */
1362 fatal("packet_disconnect called recursively.");
1363 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364
Damien Miller5428f641999-11-25 11:54:57 +11001365 /*
1366 * Format the message. Note that the caller must make sure the
1367 * message is of limited size.
1368 */
Damien Miller95def091999-11-25 00:26:21 +11001369 va_start(args, fmt);
1370 vsnprintf(buf, sizeof(buf), fmt, args);
1371 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001372
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001373 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001374 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001375
Damien Miller95def091999-11-25 00:26:21 +11001376 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001377 if (compat20) {
1378 packet_start(SSH2_MSG_DISCONNECT);
1379 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1380 packet_put_cstring(buf);
1381 packet_put_cstring("");
1382 } else {
1383 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001384 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001385 }
Damien Miller95def091999-11-25 00:26:21 +11001386 packet_send();
1387 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001388
Damien Miller95def091999-11-25 00:26:21 +11001389 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001390 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391
Damien Miller95def091999-11-25 00:26:21 +11001392 /* Close the connection. */
1393 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001394 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001395}
1396
Damien Miller5428f641999-11-25 11:54:57 +11001397/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001398
1399void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001400packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001401{
Damien Miller95def091999-11-25 00:26:21 +11001402 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001403
Damien Miller95def091999-11-25 00:26:21 +11001404 if (len > 0) {
1405 len = write(connection_out, buffer_ptr(&output), len);
1406 if (len <= 0) {
1407 if (errno == EAGAIN)
1408 return;
1409 else
1410 fatal("Write failed: %.100s", strerror(errno));
1411 }
1412 buffer_consume(&output, len);
1413 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414}
1415
Damien Miller5428f641999-11-25 11:54:57 +11001416/*
1417 * Calls packet_write_poll repeatedly until all pending output data has been
1418 * written.
1419 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420
1421void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001422packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001424 fd_set *setp;
1425
Damien Miller07d86be2006-03-26 14:19:21 +11001426 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001427 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001428 packet_write_poll();
1429 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001430 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1431 sizeof(fd_mask));
1432 FD_SET(connection_out, setp);
1433 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001434 (errno == EAGAIN || errno == EINTR))
1435 ;
Damien Miller95def091999-11-25 00:26:21 +11001436 packet_write_poll();
1437 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001438 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001439}
1440
1441/* Returns true if there is buffered data to write to the connection. */
1442
1443int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001444packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445{
Damien Miller95def091999-11-25 00:26:21 +11001446 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447}
1448
1449/* Returns true if there is not too much data to write to the connection. */
1450
1451int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001452packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453{
Damien Miller95def091999-11-25 00:26:21 +11001454 if (interactive_mode)
1455 return buffer_len(&output) < 16384;
1456 else
1457 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001458}
1459
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001460
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001461static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001462packet_set_tos(int interactive)
1463{
Damien Miller5924ceb2003-11-22 15:02:42 +11001464#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001465 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1466
1467 if (!packet_connection_is_on_socket() ||
1468 !packet_connection_is_ipv4())
1469 return;
1470 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1471 sizeof(tos)) < 0)
1472 error("setsockopt IP_TOS %d: %.100s:",
1473 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001474#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001475}
Ben Lindstroma7433982002-12-23 02:41:41 +00001476
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001477/* Informs that the current session is interactive. Sets IP flags for that. */
1478
1479void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001480packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001482 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001484 if (called)
1485 return;
1486 called = 1;
1487
Damien Miller95def091999-11-25 00:26:21 +11001488 /* Record that we are in interactive mode. */
1489 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490
Damien Miller34132e52000-01-14 15:45:46 +11001491 /* Only set socket options if using a socket. */
1492 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001493 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001494 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001495 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001496}
1497
1498/* Returns true if the current connection is interactive. */
1499
1500int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001501packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001502{
Damien Miller95def091999-11-25 00:26:21 +11001503 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504}
Damien Miller6162d121999-11-21 13:23:52 +11001505
Darren Tucker1f8311c2004-05-13 16:39:33 +10001506int
Darren Tucker502d3842003-06-28 12:38:01 +10001507packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001508{
Damien Miller95def091999-11-25 00:26:21 +11001509 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001510
Damien Miller95def091999-11-25 00:26:21 +11001511 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001512 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001513 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001514 return -1;
1515 }
1516 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001517 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001518 return -1;
1519 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001520 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001521 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001522 max_packet_size = s;
1523 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001524}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001525
Damien Miller9f643902001-11-12 11:02:52 +11001526/* roundup current message to pad bytes */
1527void
1528packet_add_padding(u_char pad)
1529{
1530 extra_pad = pad;
1531}
1532
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001533/*
1534 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001535 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001536 * byte SSH_MSG_IGNORE
1537 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001538 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001539 * All implementations MUST understand (and ignore) this message at any
1540 * time (after receiving the protocol version). No implementation is
1541 * required to send them. This message can be used as an additional
1542 * protection measure against advanced traffic analysis techniques.
1543 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001544void
1545packet_send_ignore(int nbytes)
1546{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001547 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001548 int i;
1549
1550 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001551 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001552 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001553 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001554 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001555 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001556 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001557 }
1558}
Damien Millera5539d22003-04-09 20:50:06 +10001559
Darren Tucker1f8311c2004-05-13 16:39:33 +10001560#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001561int
1562packet_need_rekeying(void)
1563{
1564 if (datafellows & SSH_BUG_NOREKEY)
1565 return 0;
1566 return
1567 (p_send.packets > MAX_PACKETS) ||
1568 (p_read.packets > MAX_PACKETS) ||
1569 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1570 (max_blocks_in && (p_read.blocks > max_blocks_in));
1571}
1572
1573void
1574packet_set_rekey_limit(u_int32_t bytes)
1575{
1576 rekey_limit = bytes;
1577}
Damien Miller9786e6e2005-07-26 21:54:56 +10001578
1579void
1580packet_set_server(void)
1581{
1582 server_side = 1;
1583}
1584
1585void
1586packet_set_authenticated(void)
1587{
1588 after_authentication = 1;
1589}