blob: ea0a82e21e9cea26e9948529ede5aeffa63b2ba7 [file] [log] [blame]
Damien Miller3f941882006-03-31 23:13:02 +11001/* $OpenBSD: packet.c,v 1.131 2006/03/30 09:58:16 djm 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 Miller68f8e992006-03-15 11:24:12 +110043#include <netinet/in_systm.h>
44#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46#include "xmalloc.h"
47#include "buffer.h"
48#include "packet.h"
49#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
52#include "compress.h"
53#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000054#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
Damien Miller33b13562000-04-04 14:38:59 +100056#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100058#include "ssh2.h"
59
Damien Miller874d77b2000-10-14 16:23:11 +110060#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100061#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000062#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
64#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110065#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000066#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100067
68#ifdef PACKET_DEBUG
69#define DBG(x) x
70#else
71#define DBG(x)
72#endif
73
Damien Miller5428f641999-11-25 11:54:57 +110074/*
75 * This variable contains the file descriptors used for communicating with
76 * the other side. connection_in is used for reading; connection_out for
77 * writing. These can be the same descriptor, in which case it is assumed to
78 * be a socket.
79 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080static int connection_in = -1;
81static int connection_out = -1;
82
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000084static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86/* Encryption context for receiving data. This is only used for decryption. */
87static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110088
89/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090static CipherContext send_context;
91
92/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000093Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
95/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000096Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
98/* Buffer for the partial outgoing packet being constructed. */
99static Buffer outgoing_packet;
100
101/* Buffer for the incoming packet currently being processed. */
102static Buffer incoming_packet;
103
104/* Scratch buffer for packet compression/decompression. */
105static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000106static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
108/* Flag indicating whether packet compression/decompression is enabled. */
109static int packet_compression = 0;
110
Damien Miller6162d121999-11-21 13:23:52 +1100111/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000112u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100113
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114/* Flag indicating whether this module has been initialized. */
115static int initialized = 0;
116
117/* Set to true if the connection is interactive. */
118static int interactive_mode = 0;
119
Damien Miller9786e6e2005-07-26 21:54:56 +1000120/* Set to true if we are the server side. */
121static int server_side = 0;
122
123/* Set to true if we are authenticated. */
124static int after_authentication = 0;
125
Damien Miller33b13562000-04-04 14:38:59 +1000126/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000127Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000128static struct packet_state {
129 u_int32_t seqnr;
130 u_int32_t packets;
131 u_int64_t blocks;
132} p_read, p_send;
133
134static u_int64_t max_blocks_in, max_blocks_out;
135static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000136
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000137/* Session key for protocol v1 */
138static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
139static u_int ssh1_keylen;
140
Damien Miller9f643902001-11-12 11:02:52 +1100141/* roundup current message to extra_pad bytes */
142static u_char extra_pad = 0;
143
Damien Millera5539d22003-04-09 20:50:06 +1000144struct packet {
145 TAILQ_ENTRY(packet) next;
146 u_char type;
147 Buffer payload;
148};
149TAILQ_HEAD(, packet) outgoing;
150
Damien Miller5428f641999-11-25 11:54:57 +1100151/*
152 * Sets the descriptors used for communication. Disables encryption until
153 * packet_set_encryption_key is called.
154 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155void
156packet_set_connection(int fd_in, int fd_out)
157{
Damien Miller874d77b2000-10-14 16:23:11 +1100158 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000159
Damien Miller874d77b2000-10-14 16:23:11 +1100160 if (none == NULL)
161 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100162 connection_in = fd_in;
163 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000164 cipher_init(&send_context, none, (const u_char *)"",
165 0, NULL, 0, CIPHER_ENCRYPT);
166 cipher_init(&receive_context, none, (const u_char *)"",
167 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000168 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100169 if (!initialized) {
170 initialized = 1;
171 buffer_init(&input);
172 buffer_init(&output);
173 buffer_init(&outgoing_packet);
174 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000175 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100176 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177}
178
Damien Miller34132e52000-01-14 15:45:46 +1100179/* Returns 1 if remote host is connected via socket, 0 if not. */
180
181int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000182packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100183{
184 struct sockaddr_storage from, to;
185 socklen_t fromlen, tolen;
186
187 /* filedescriptors in and out are the same, so it's a socket */
188 if (connection_in == connection_out)
189 return 1;
190 fromlen = sizeof(from);
191 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100192 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100193 return 0;
194 tolen = sizeof(to);
195 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100196 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100197 return 0;
198 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
199 return 0;
200 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
201 return 0;
202 return 1;
203}
204
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000205/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000206 * Exports an IV from the CipherContext required to export the key
207 * state back from the unprivileged child to the privileged parent
208 * process.
209 */
210
211void
212packet_get_keyiv(int mode, u_char *iv, u_int len)
213{
214 CipherContext *cc;
215
216 if (mode == MODE_OUT)
217 cc = &send_context;
218 else
219 cc = &receive_context;
220
221 cipher_get_keyiv(cc, iv, len);
222}
223
224int
225packet_get_keycontext(int mode, u_char *dat)
226{
227 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000228
Ben Lindstromf6027d32002-03-22 01:42:04 +0000229 if (mode == MODE_OUT)
230 cc = &send_context;
231 else
232 cc = &receive_context;
233
234 return (cipher_get_keycontext(cc, dat));
235}
236
237void
238packet_set_keycontext(int mode, u_char *dat)
239{
240 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000241
Ben Lindstromf6027d32002-03-22 01:42:04 +0000242 if (mode == MODE_OUT)
243 cc = &send_context;
244 else
245 cc = &receive_context;
246
247 cipher_set_keycontext(cc, dat);
248}
249
250int
251packet_get_keyiv_len(int mode)
252{
253 CipherContext *cc;
254
255 if (mode == MODE_OUT)
256 cc = &send_context;
257 else
258 cc = &receive_context;
259
260 return (cipher_get_keyiv_len(cc));
261}
Damien Miller4f7becb2006-03-26 14:10:14 +1100262
Ben Lindstromf6027d32002-03-22 01:42:04 +0000263void
264packet_set_iv(int mode, u_char *dat)
265{
266 CipherContext *cc;
267
268 if (mode == MODE_OUT)
269 cc = &send_context;
270 else
271 cc = &receive_context;
272
273 cipher_set_keyiv(cc, dat);
274}
Damien Miller4f7becb2006-03-26 14:10:14 +1100275
Ben Lindstromf6027d32002-03-22 01:42:04 +0000276int
Damien Miller2b92d322003-06-11 22:05:06 +1000277packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000278{
279 return (cipher_get_number(receive_context.cipher));
280}
281
Damien Millera5539d22003-04-09 20:50:06 +1000282void
283packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000284{
Damien Millera5539d22003-04-09 20:50:06 +1000285 struct packet_state *state;
286
287 state = (mode == MODE_IN) ? &p_read : &p_send;
288 *seqnr = state->seqnr;
289 *blocks = state->blocks;
290 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000291}
292
293void
Damien Millera5539d22003-04-09 20:50:06 +1000294packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000295{
Damien Millera5539d22003-04-09 20:50:06 +1000296 struct packet_state *state;
297
298 state = (mode == MODE_IN) ? &p_read : &p_send;
299 state->seqnr = seqnr;
300 state->blocks = blocks;
301 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000302}
303
Damien Miller34132e52000-01-14 15:45:46 +1100304/* returns 1 if connection is via ipv4 */
305
306int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000307packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100308{
309 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100310 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100311
312 memset(&to, 0, sizeof(to));
313 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
314 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000315 if (to.ss_family == AF_INET)
316 return 1;
317#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100318 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000319 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
320 return 1;
321#endif
322 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100323}
324
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325/* Sets the connection into non-blocking mode. */
326
327void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000328packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329{
Damien Miller95def091999-11-25 00:26:21 +1100330 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000331 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller232711f2004-06-15 10:35:30 +1000333 if (connection_out != connection_in)
334 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335}
336
337/* Returns the socket used for reading. */
338
339int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000340packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341{
Damien Miller95def091999-11-25 00:26:21 +1100342 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343}
344
345/* Returns the descriptor used for writing. */
346
347int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000348packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349{
Damien Miller95def091999-11-25 00:26:21 +1100350 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351}
352
353/* Closes the connection and clears and frees internal data structures. */
354
355void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000356packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357{
Damien Miller95def091999-11-25 00:26:21 +1100358 if (!initialized)
359 return;
360 initialized = 0;
361 if (connection_in == connection_out) {
362 shutdown(connection_out, SHUT_RDWR);
363 close(connection_out);
364 } else {
365 close(connection_in);
366 close(connection_out);
367 }
368 buffer_free(&input);
369 buffer_free(&output);
370 buffer_free(&outgoing_packet);
371 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000372 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100373 buffer_free(&compression_buffer);
374 buffer_compress_uninit();
375 }
Damien Miller963f6b22002-02-19 15:21:23 +1100376 cipher_cleanup(&send_context);
377 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378}
379
380/* Sets remote side protocol flags. */
381
382void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000383packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384{
Damien Miller95def091999-11-25 00:26:21 +1100385 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386}
387
388/* Returns the remote protocol flags set earlier by the above function. */
389
Ben Lindstrom46c16222000-12-22 01:43:59 +0000390u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000391packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392{
Damien Miller95def091999-11-25 00:26:21 +1100393 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394}
395
Damien Miller5428f641999-11-25 11:54:57 +1100396/*
397 * Starts packet compression from the next packet on in both directions.
398 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
399 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400
Ben Lindstrombba81212001-06-25 05:01:22 +0000401static void
402packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000403{
404 if (compression_buffer_ready == 1)
405 return;
406 compression_buffer_ready = 1;
407 buffer_init(&compression_buffer);
408}
409
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410void
411packet_start_compression(int level)
412{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000413 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100414 fatal("Compression already enabled.");
415 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000416 packet_init_compression();
417 buffer_compress_init_send(level);
418 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419}
420
Damien Miller5428f641999-11-25 11:54:57 +1100421/*
Damien Miller5428f641999-11-25 11:54:57 +1100422 * Causes any further packets to be encrypted using the given key. The same
423 * key is used for both sending and reception. However, both directions are
424 * encrypted independently of each other.
425 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000426
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000428packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100429 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430{
Damien Miller874d77b2000-10-14 16:23:11 +1100431 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000432
Damien Miller874d77b2000-10-14 16:23:11 +1100433 if (cipher == NULL)
434 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000435 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100436 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000437 if (keylen > SSH_SESSION_KEY_LENGTH)
438 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
439 memcpy(ssh1_key, key, keylen);
440 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100441 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
442 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443}
444
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000445u_int
446packet_get_encryption_key(u_char *key)
447{
448 if (key == NULL)
449 return (ssh1_keylen);
450 memcpy(key, ssh1_key, ssh1_keylen);
451 return (ssh1_keylen);
452}
453
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000454/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000456packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000458 u_char buf[9];
459 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460
Ben Lindstrom204e4882001-03-05 06:47:00 +0000461 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000462 len = compat20 ? 6 : 9;
463 memset(buf, 0, len - 1);
464 buf[len - 1] = type;
465 buffer_clear(&outgoing_packet);
466 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000467}
468
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000469/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470void
471packet_put_char(int value)
472{
Damien Miller95def091999-11-25 00:26:21 +1100473 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000474
Damien Miller95def091999-11-25 00:26:21 +1100475 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476}
Damien Miller4f7becb2006-03-26 14:10:14 +1100477
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000479packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480{
Damien Miller95def091999-11-25 00:26:21 +1100481 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482}
Damien Miller4f7becb2006-03-26 14:10:14 +1100483
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100485packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486{
Damien Miller95def091999-11-25 00:26:21 +1100487 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488}
Damien Miller4f7becb2006-03-26 14:10:14 +1100489
Damien Miller33b13562000-04-04 14:38:59 +1000490void
491packet_put_cstring(const char *str)
492{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000493 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000494}
Damien Miller4f7becb2006-03-26 14:10:14 +1100495
Damien Miller33b13562000-04-04 14:38:59 +1000496void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100497packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000498{
499 buffer_append(&outgoing_packet, buf, len);
500}
Damien Miller4f7becb2006-03-26 14:10:14 +1100501
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502void
Damien Miller95def091999-11-25 00:26:21 +1100503packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504{
Damien Miller95def091999-11-25 00:26:21 +1100505 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506}
Damien Miller4f7becb2006-03-26 14:10:14 +1100507
Damien Miller33b13562000-04-04 14:38:59 +1000508void
509packet_put_bignum2(BIGNUM * value)
510{
511 buffer_put_bignum2(&outgoing_packet, value);
512}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Damien Miller5428f641999-11-25 11:54:57 +1100514/*
515 * Finalizes and sends the packet. If the encryption key has been set,
516 * encrypts the packet before sending.
517 */
Damien Miller95def091999-11-25 00:26:21 +1100518
Ben Lindstrombba81212001-06-25 05:01:22 +0000519static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000520packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000522 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100523 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000524 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000525 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Damien Miller5428f641999-11-25 11:54:57 +1100527 /*
528 * If using packet compression, compress the payload of the outgoing
529 * packet.
530 */
Damien Miller95def091999-11-25 00:26:21 +1100531 if (packet_compression) {
532 buffer_clear(&compression_buffer);
533 /* Skip padding. */
534 buffer_consume(&outgoing_packet, 8);
535 /* padding */
536 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
537 buffer_compress(&outgoing_packet, &compression_buffer);
538 buffer_clear(&outgoing_packet);
539 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100540 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100541 }
542 /* Compute packet length without padding (add checksum, remove padding). */
543 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544
Damien Millere247cc42000-05-07 12:03:14 +1000545 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100546 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100547 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100548 cp = buffer_ptr(&outgoing_packet);
549 for (i = 0; i < padding; i++) {
550 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000551 rnd = arc4random();
552 cp[7 - i] = rnd & 0xff;
553 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100554 }
555 }
556 buffer_consume(&outgoing_packet, 8 - padding);
557
558 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100559 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000560 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100561 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100562 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563
564#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100565 fprintf(stderr, "packet_send plain: ");
566 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567#endif
568
Damien Miller95def091999-11-25 00:26:21 +1100569 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100570 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100571 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100572 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100573 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100574 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100575
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100577 fprintf(stderr, "encrypted: ");
578 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579#endif
580
Damien Miller95def091999-11-25 00:26:21 +1100581 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582
Damien Miller5428f641999-11-25 11:54:57 +1100583 /*
Damien Miller788f2122005-11-05 15:14:59 +1100584 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100585 * actually sent until packet_write_wait or packet_write_poll is
586 * called.
587 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588}
589
Ben Lindstromf6027d32002-03-22 01:42:04 +0000590void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000591set_newkeys(int mode)
592{
593 Enc *enc;
594 Mac *mac;
595 Comp *comp;
596 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000597 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000598 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000599
Ben Lindstrom064496f2002-12-23 02:04:22 +0000600 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000601
Damien Miller963f6b22002-02-19 15:21:23 +1100602 if (mode == MODE_OUT) {
603 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000604 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000605 p_send.packets = p_send.blocks = 0;
606 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100607 } else {
608 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000609 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000610 p_read.packets = p_read.blocks = 0;
611 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100612 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000613 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000614 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100615 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000616 enc = &newkeys[mode]->enc;
617 mac = &newkeys[mode]->mac;
618 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000619 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000620 xfree(enc->name);
621 xfree(enc->iv);
622 xfree(enc->key);
623 xfree(mac->name);
624 xfree(mac->key);
625 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000626 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000627 }
628 newkeys[mode] = kex_get_newkeys(mode);
629 if (newkeys[mode] == NULL)
630 fatal("newkeys: no keys for mode %d", mode);
631 enc = &newkeys[mode]->enc;
632 mac = &newkeys[mode]->mac;
633 comp = &newkeys[mode]->comp;
634 if (mac->md != NULL)
635 mac->enabled = 1;
636 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100637 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000638 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000639 /* Deleting the keys does not gain extra security */
640 /* memset(enc->iv, 0, enc->block_size);
641 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000642 if ((comp->type == COMP_ZLIB ||
643 (comp->type == COMP_DELAYED && after_authentication)) &&
644 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000645 packet_init_compression();
646 if (mode == MODE_OUT)
647 buffer_compress_init_send(6);
648 else
649 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000650 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000651 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000652 /*
653 * The 2^(blocksize*2) limit is too expensive for 3DES,
654 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
655 */
656 if (enc->block_size >= 16)
657 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
658 else
659 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000660 if (rekey_limit)
661 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000662}
663
Damien Miller5428f641999-11-25 11:54:57 +1100664/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000665 * Delayed compression for SSH2 is enabled after authentication:
666 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
667 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
668 */
669static void
670packet_enable_delayed_compress(void)
671{
672 Comp *comp = NULL;
673 int mode;
674
675 /*
676 * Remember that we are past the authentication step, so rekeying
677 * with COMP_DELAYED will turn on compression immediately.
678 */
679 after_authentication = 1;
680 for (mode = 0; mode < MODE_MAX; mode++) {
681 comp = &newkeys[mode]->comp;
682 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000683 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000684 if (mode == MODE_OUT)
685 buffer_compress_init_send(6);
686 else
687 buffer_compress_init_recv();
688 comp->enabled = 1;
689 }
690 }
691}
692
693/*
Damien Miller33b13562000-04-04 14:38:59 +1000694 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
695 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000696static void
Damien Millera5539d22003-04-09 20:50:06 +1000697packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000698{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000699 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100700 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000701 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100702 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000703 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000704 Enc *enc = NULL;
705 Mac *mac = NULL;
706 Comp *comp = NULL;
707 int block_size;
708
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000709 if (newkeys[MODE_OUT] != NULL) {
710 enc = &newkeys[MODE_OUT]->enc;
711 mac = &newkeys[MODE_OUT]->mac;
712 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000713 }
Damien Miller963f6b22002-02-19 15:21:23 +1100714 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000715
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000716 cp = buffer_ptr(&outgoing_packet);
717 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000718
719#ifdef PACKET_DEBUG
720 fprintf(stderr, "plain: ");
721 buffer_dump(&outgoing_packet);
722#endif
723
724 if (comp && comp->enabled) {
725 len = buffer_len(&outgoing_packet);
726 /* skip header, compress only payload */
727 buffer_consume(&outgoing_packet, 5);
728 buffer_clear(&compression_buffer);
729 buffer_compress(&outgoing_packet, &compression_buffer);
730 buffer_clear(&outgoing_packet);
731 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
732 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
733 buffer_len(&compression_buffer));
734 DBG(debug("compression: raw %d compressed %d", len,
735 buffer_len(&outgoing_packet)));
736 }
737
738 /* sizeof (packet_len + pad_len + payload) */
739 len = buffer_len(&outgoing_packet);
740
741 /*
742 * calc size of padding, alloc space, get random data,
743 * minimum padding is 4 bytes
744 */
745 padlen = block_size - (len % block_size);
746 if (padlen < 4)
747 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100748 if (extra_pad) {
749 /* will wrap if extra_pad+padlen > 255 */
750 extra_pad = roundup(extra_pad, block_size);
751 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000752 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100753 pad, len, padlen, extra_pad);
754 padlen += pad;
755 extra_pad = 0;
756 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100757 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100758 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000759 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000760 for (i = 0; i < padlen; i++) {
761 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000762 rnd = arc4random();
763 cp[i] = rnd & 0xff;
764 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000765 }
Damien Millere247cc42000-05-07 12:03:14 +1000766 } else {
767 /* clear padding */
768 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000769 }
770 /* packet_length includes payload, padding and padding length field */
771 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000772 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100773 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000774 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000775 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
776
777 /* compute MAC over seqnr and packet(length fields, payload, padding) */
778 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000779 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100780 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000781 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000782 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000783 }
784 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100785 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100786 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000787 buffer_len(&outgoing_packet));
788 /* append unencrypted MAC */
789 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100790 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000791#ifdef PACKET_DEBUG
792 fprintf(stderr, "encrypted: ");
793 buffer_dump(&output);
794#endif
Damien Miller4af51302000-04-16 11:18:38 +1000795 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000796 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000797 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000798 if (++p_send.packets == 0)
799 if (!(datafellows & SSH_BUG_NOREKEY))
800 fatal("XXX too many packets with same key");
801 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000802 buffer_clear(&outgoing_packet);
803
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000804 if (type == SSH2_MSG_NEWKEYS)
805 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000806 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
807 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000808}
809
Damien Millera5539d22003-04-09 20:50:06 +1000810static void
811packet_send2(void)
812{
813 static int rekeying = 0;
814 struct packet *p;
815 u_char type, *cp;
816
817 cp = buffer_ptr(&outgoing_packet);
818 type = cp[5];
819
820 /* during rekeying we can only send key exchange messages */
821 if (rekeying) {
822 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
823 (type <= SSH2_MSG_TRANSPORT_MAX))) {
824 debug("enqueue packet: %u", type);
825 p = xmalloc(sizeof(*p));
826 p->type = type;
827 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
828 buffer_init(&outgoing_packet);
829 TAILQ_INSERT_TAIL(&outgoing, p, next);
830 return;
831 }
832 }
833
834 /* rekeying starts with sending KEXINIT */
835 if (type == SSH2_MSG_KEXINIT)
836 rekeying = 1;
837
838 packet_send2_wrapped();
839
840 /* after a NEWKEYS message we can send the complete queue */
841 if (type == SSH2_MSG_NEWKEYS) {
842 rekeying = 0;
843 while ((p = TAILQ_FIRST(&outgoing))) {
844 type = p->type;
845 debug("dequeue packet: %u", type);
846 buffer_free(&outgoing_packet);
847 memcpy(&outgoing_packet, &p->payload,
848 sizeof(Buffer));
849 TAILQ_REMOVE(&outgoing, p, next);
850 xfree(p);
851 packet_send2_wrapped();
852 }
853 }
854}
855
Damien Miller33b13562000-04-04 14:38:59 +1000856void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000857packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000858{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000859 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000860 packet_send2();
861 else
862 packet_send1();
863 DBG(debug("packet_send done"));
864}
865
Damien Miller33b13562000-04-04 14:38:59 +1000866/*
Damien Miller5428f641999-11-25 11:54:57 +1100867 * Waits until a packet has been received, and returns its type. Note that
868 * no other data is processed until this returns, so this function should not
869 * be used during the interactive session.
870 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871
872int
Damien Millerdff50992002-01-22 23:16:32 +1100873packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874{
Damien Miller95def091999-11-25 00:26:21 +1100875 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000876 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100877 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000878 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879
Damien Miller07d86be2006-03-26 14:19:21 +1100880 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000881 sizeof(fd_mask));
882
Damien Miller95def091999-11-25 00:26:21 +1100883 /* Since we are blocking, ensure that all written packets have been sent. */
884 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885
Damien Miller95def091999-11-25 00:26:21 +1100886 /* Stay in the loop until we have received a complete packet. */
887 for (;;) {
888 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100889 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000890 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000891 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100892 || type == SSH_SMSG_FAILURE
893 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000894 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100895 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100896 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000897 if (type != SSH_MSG_NONE) {
898 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100899 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000900 }
Damien Miller5428f641999-11-25 11:54:57 +1100901 /*
902 * Otherwise, wait for some data to arrive, add it to the
903 * buffer, and try again.
904 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000905 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
906 sizeof(fd_mask));
907 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100908
Damien Miller95def091999-11-25 00:26:21 +1100909 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000910 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000911 (errno == EAGAIN || errno == EINTR))
912 ;
Damien Miller5428f641999-11-25 11:54:57 +1100913
Damien Miller95def091999-11-25 00:26:21 +1100914 /* Read data from the socket. */
915 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100916 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000917 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000918 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100919 }
Damien Miller95def091999-11-25 00:26:21 +1100920 if (len < 0)
921 fatal("Read from socket failed: %.100s", strerror(errno));
922 /* Append it to the buffer. */
923 packet_process_incoming(buf, len);
924 }
925 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926}
927
Damien Miller278f9072001-12-21 15:00:19 +1100928int
Damien Millerdff50992002-01-22 23:16:32 +1100929packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100930{
Damien Millerdff50992002-01-22 23:16:32 +1100931 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100932}
933
Damien Miller5428f641999-11-25 11:54:57 +1100934/*
935 * Waits until a packet has been received, verifies that its type matches
936 * that given, and gives a fatal error and exits if there is a mismatch.
937 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938
939void
Damien Millerdff50992002-01-22 23:16:32 +1100940packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941{
Damien Miller95def091999-11-25 00:26:21 +1100942 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000943
Damien Millerdff50992002-01-22 23:16:32 +1100944 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100945 if (type != expected_type)
946 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000947 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948}
949
950/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100951 * packet_process_incoming. If so, reads the packet; otherwise returns
952 * SSH_MSG_NONE. This does not wait for data from the connection.
953 *
954 * SSH_MSG_DISCONNECT is handled specially here. Also,
955 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
956 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100957 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958
Ben Lindstrombba81212001-06-25 05:01:22 +0000959static int
Damien Millerdff50992002-01-22 23:16:32 +1100960packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000962 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000963 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000964 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965
Damien Miller95def091999-11-25 00:26:21 +1100966 /* Check if input size is less than minimum packet size. */
967 if (buffer_len(&input) < 4 + 8)
968 return SSH_MSG_NONE;
969 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000970 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100971 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100972 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000973 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100974 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975
Damien Miller95def091999-11-25 00:26:21 +1100976 /* Check if the packet has been entirely received. */
977 if (buffer_len(&input) < 4 + padded_len)
978 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
Damien Miller95def091999-11-25 00:26:21 +1100980 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Damien Miller95def091999-11-25 00:26:21 +1100982 /* Consume packet length. */
983 buffer_consume(&input, 4);
984
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000985 /*
986 * Cryptographic attack detector for ssh
987 * (C)1998 CORE-SDI, Buenos Aires Argentina
988 * Ariel Futoransky(futo@core-sdi.com)
989 */
Damien Miller963f6b22002-02-19 15:21:23 +1100990 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +1100991 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000992 packet_disconnect("crc32 compensation attack: network attack detected");
993
994 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100995 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100996 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100997 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000998
Damien Miller95def091999-11-25 00:26:21 +1100999 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
1001#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001002 fprintf(stderr, "read_poll plain: ");
1003 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005
Damien Miller95def091999-11-25 00:26:21 +11001006 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001007 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001008 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009
Damien Miller95def091999-11-25 00:26:21 +11001010 /* Skip padding. */
1011 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001012
Damien Miller95def091999-11-25 00:26:21 +11001013 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001014 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001015 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001016 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001018 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001019 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001020 if (checksum != stored_checksum)
1021 packet_disconnect("Corrupted check bytes on input.");
1022 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
Damien Miller95def091999-11-25 00:26:21 +11001024 if (packet_compression) {
1025 buffer_clear(&compression_buffer);
1026 buffer_uncompress(&incoming_packet, &compression_buffer);
1027 buffer_clear(&incoming_packet);
1028 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001029 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001030 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001031 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001032 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1033 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001034 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035}
Damien Miller95def091999-11-25 00:26:21 +11001036
Ben Lindstrombba81212001-06-25 05:01:22 +00001037static int
Damien Millerdff50992002-01-22 23:16:32 +11001038packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001039{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001040 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001041 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001042 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001043 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001044 Enc *enc = NULL;
1045 Mac *mac = NULL;
1046 Comp *comp = NULL;
1047
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001048 if (newkeys[MODE_IN] != NULL) {
1049 enc = &newkeys[MODE_IN]->enc;
1050 mac = &newkeys[MODE_IN]->mac;
1051 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001052 }
1053 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001054 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001055
1056 if (packet_length == 0) {
1057 /*
1058 * check if input size is less than the cipher block size,
1059 * decrypt first block and extract length of incoming packet
1060 */
1061 if (buffer_len(&input) < block_size)
1062 return SSH_MSG_NONE;
1063 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001064 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001065 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001066 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001067 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001068 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001069 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001070#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001071 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001072#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001073 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001074 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001075 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001076 buffer_consume(&input, block_size);
1077 }
1078 /* we have a partial packet of block_size bytes */
1079 need = 4 + packet_length - block_size;
1080 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1081 need, maclen));
1082 if (need % block_size != 0)
1083 fatal("padding error: need %d block %d mod %d",
1084 need, block_size, need % block_size);
1085 /*
1086 * check if the entire packet has been received and
1087 * decrypt into incoming_packet
1088 */
1089 if (buffer_len(&input) < need + maclen)
1090 return SSH_MSG_NONE;
1091#ifdef PACKET_DEBUG
1092 fprintf(stderr, "read_poll enc/full: ");
1093 buffer_dump(&input);
1094#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001095 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001096 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001097 buffer_consume(&input, need);
1098 /*
1099 * compute MAC over seqnr and packet,
1100 * increment sequence number for incoming packet
1101 */
Damien Miller4af51302000-04-16 11:18:38 +10001102 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001103 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001104 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001105 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001106 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001107 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001108 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001109 buffer_consume(&input, mac->mac_len);
1110 }
Damien Miller278f9072001-12-21 15:00:19 +11001111 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001112 *seqnr_p = p_read.seqnr;
1113 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001114 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001115 if (++p_read.packets == 0)
1116 if (!(datafellows & SSH_BUG_NOREKEY))
1117 fatal("XXX too many packets with same key");
1118 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001119
1120 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001121 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001122 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001123 DBG(debug("input: padlen %d", padlen));
1124 if (padlen < 4)
1125 packet_disconnect("Corrupted padlen %d on input.", padlen);
1126
1127 /* skip packet size + padlen, discard padding */
1128 buffer_consume(&incoming_packet, 4 + 1);
1129 buffer_consume_end(&incoming_packet, padlen);
1130
1131 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1132 if (comp && comp->enabled) {
1133 buffer_clear(&compression_buffer);
1134 buffer_uncompress(&incoming_packet, &compression_buffer);
1135 buffer_clear(&incoming_packet);
1136 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1137 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001138 DBG(debug("input: len after de-compress %d",
1139 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001140 }
1141 /*
1142 * get packet type, implies consume.
1143 * return length of payload (without type field)
1144 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001145 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001146 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1147 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001148 if (type == SSH2_MSG_NEWKEYS)
1149 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001150 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1151 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001152#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001153 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001154 buffer_dump(&incoming_packet);
1155#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001156 /* reset for next packet */
1157 packet_length = 0;
1158 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001159}
1160
1161int
Damien Millerdff50992002-01-22 23:16:32 +11001162packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001163{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001164 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001165 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001166 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001167
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001168 for (;;) {
1169 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001170 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001171 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001172 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001173 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001174 case SSH2_MSG_IGNORE:
1175 break;
1176 case SSH2_MSG_DEBUG:
1177 packet_get_char();
1178 msg = packet_get_string(NULL);
1179 debug("Remote: %.900s", msg);
1180 xfree(msg);
1181 msg = packet_get_string(NULL);
1182 xfree(msg);
1183 break;
1184 case SSH2_MSG_DISCONNECT:
1185 reason = packet_get_int();
1186 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001187 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001188 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001189 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001190 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001191 break;
Damien Miller659811f2002-01-22 23:23:11 +11001192 case SSH2_MSG_UNIMPLEMENTED:
1193 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001194 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1195 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001196 break;
Damien Miller33b13562000-04-04 14:38:59 +10001197 default:
1198 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001199 }
Damien Miller33b13562000-04-04 14:38:59 +10001200 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001201 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001202 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001203 case SSH_MSG_IGNORE:
1204 break;
1205 case SSH_MSG_DEBUG:
1206 msg = packet_get_string(NULL);
1207 debug("Remote: %.900s", msg);
1208 xfree(msg);
1209 break;
1210 case SSH_MSG_DISCONNECT:
1211 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001212 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001213 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001214 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001215 xfree(msg);
1216 break;
1217 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001218 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001219 DBG(debug("received packet type %d", type));
1220 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001221 }
Damien Miller33b13562000-04-04 14:38:59 +10001222 }
1223 }
1224}
1225
Damien Miller278f9072001-12-21 15:00:19 +11001226int
Damien Millerdff50992002-01-22 23:16:32 +11001227packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001228{
Damien Millerdff50992002-01-22 23:16:32 +11001229 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001230}
1231
Damien Miller5428f641999-11-25 11:54:57 +11001232/*
1233 * Buffers the given amount of input characters. This is intended to be used
1234 * together with packet_read_poll.
1235 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001236
1237void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001238packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001239{
Damien Miller95def091999-11-25 00:26:21 +11001240 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241}
1242
1243/* Returns a character from the packet. */
1244
Ben Lindstrom46c16222000-12-22 01:43:59 +00001245u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001246packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247{
Damien Miller95def091999-11-25 00:26:21 +11001248 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001249
Damien Miller95def091999-11-25 00:26:21 +11001250 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001251 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252}
1253
1254/* Returns an integer from the packet data. */
1255
Ben Lindstrom46c16222000-12-22 01:43:59 +00001256u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001257packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258{
Damien Miller95def091999-11-25 00:26:21 +11001259 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260}
1261
Damien Miller5428f641999-11-25 11:54:57 +11001262/*
1263 * Returns an arbitrary precision integer from the packet data. The integer
1264 * must have been initialized before this call.
1265 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266
1267void
Damien Millerd432ccf2002-01-22 23:14:44 +11001268packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001269{
Damien Miller76e1e362002-01-22 23:15:57 +11001270 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001271}
1272
Damien Miller33b13562000-04-04 14:38:59 +10001273void
Damien Millerd432ccf2002-01-22 23:14:44 +11001274packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001275{
Damien Miller76e1e362002-01-22 23:15:57 +11001276 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001277}
1278
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001279void *
Damien Millereccb9de2005-06-17 12:59:34 +10001280packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001281{
Damien Millereccb9de2005-06-17 12:59:34 +10001282 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001283
Damien Miller33b13562000-04-04 14:38:59 +10001284 if (length_ptr != NULL)
1285 *length_ptr = bytes;
1286 return buffer_ptr(&incoming_packet);
1287}
1288
Damien Miller4af51302000-04-16 11:18:38 +10001289int
1290packet_remaining(void)
1291{
1292 return buffer_len(&incoming_packet);
1293}
1294
Damien Miller5428f641999-11-25 11:54:57 +11001295/*
1296 * Returns a string from the packet data. The string is allocated using
1297 * xmalloc; it is the responsibility of the calling program to free it when
1298 * no longer needed. The length_ptr argument may be NULL, or point to an
1299 * integer into which the length of the string is stored.
1300 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001302void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001303packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001304{
Damien Miller95def091999-11-25 00:26:21 +11001305 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001306}
1307
Damien Miller5428f641999-11-25 11:54:57 +11001308/*
1309 * Sends a diagnostic message from the server to the client. This message
1310 * can be sent at any time (but not while constructing another message). The
1311 * message is printed immediately, but only if the client is being executed
1312 * in verbose mode. These messages are primarily intended to ease debugging
1313 * authentication problems. The length of the formatted message must not
1314 * exceed 1024 bytes. This will automatically call packet_write_wait.
1315 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316
1317void
Damien Miller95def091999-11-25 00:26:21 +11001318packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319{
Damien Miller95def091999-11-25 00:26:21 +11001320 char buf[1024];
1321 va_list args;
1322
Ben Lindstroma14ee472000-12-07 01:24:58 +00001323 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1324 return;
1325
Damien Miller95def091999-11-25 00:26:21 +11001326 va_start(args, fmt);
1327 vsnprintf(buf, sizeof(buf), fmt, args);
1328 va_end(args);
1329
Damien Miller7c8af4f2000-05-01 08:24:07 +10001330 if (compat20) {
1331 packet_start(SSH2_MSG_DEBUG);
1332 packet_put_char(0); /* bool: always display */
1333 packet_put_cstring(buf);
1334 packet_put_cstring("");
1335 } else {
1336 packet_start(SSH_MSG_DEBUG);
1337 packet_put_cstring(buf);
1338 }
Damien Miller95def091999-11-25 00:26:21 +11001339 packet_send();
1340 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001341}
1342
Damien Miller5428f641999-11-25 11:54:57 +11001343/*
1344 * Logs the error plus constructs and sends a disconnect packet, closes the
1345 * connection, and exits. This function never returns. The error message
1346 * should not contain a newline. The length of the formatted message must
1347 * not exceed 1024 bytes.
1348 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001349
1350void
Damien Miller95def091999-11-25 00:26:21 +11001351packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001352{
Damien Miller95def091999-11-25 00:26:21 +11001353 char buf[1024];
1354 va_list args;
1355 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001356
Damien Miller95def091999-11-25 00:26:21 +11001357 if (disconnecting) /* Guard against recursive invocations. */
1358 fatal("packet_disconnect called recursively.");
1359 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001360
Damien Miller5428f641999-11-25 11:54:57 +11001361 /*
1362 * Format the message. Note that the caller must make sure the
1363 * message is of limited size.
1364 */
Damien Miller95def091999-11-25 00:26:21 +11001365 va_start(args, fmt);
1366 vsnprintf(buf, sizeof(buf), fmt, args);
1367 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001369 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001370 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001371
Damien Miller95def091999-11-25 00:26:21 +11001372 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001373 if (compat20) {
1374 packet_start(SSH2_MSG_DISCONNECT);
1375 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1376 packet_put_cstring(buf);
1377 packet_put_cstring("");
1378 } else {
1379 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001380 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001381 }
Damien Miller95def091999-11-25 00:26:21 +11001382 packet_send();
1383 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001384
Damien Miller95def091999-11-25 00:26:21 +11001385 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001386 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
Damien Miller95def091999-11-25 00:26:21 +11001388 /* Close the connection. */
1389 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001390 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391}
1392
Damien Miller5428f641999-11-25 11:54:57 +11001393/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394
1395void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001396packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001397{
Damien Miller95def091999-11-25 00:26:21 +11001398 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001399
Damien Miller95def091999-11-25 00:26:21 +11001400 if (len > 0) {
1401 len = write(connection_out, buffer_ptr(&output), len);
1402 if (len <= 0) {
1403 if (errno == EAGAIN)
1404 return;
1405 else
1406 fatal("Write failed: %.100s", strerror(errno));
1407 }
1408 buffer_consume(&output, len);
1409 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001410}
1411
Damien Miller5428f641999-11-25 11:54:57 +11001412/*
1413 * Calls packet_write_poll repeatedly until all pending output data has been
1414 * written.
1415 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416
1417void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001418packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001419{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001420 fd_set *setp;
1421
Damien Miller07d86be2006-03-26 14:19:21 +11001422 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001423 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001424 packet_write_poll();
1425 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001426 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1427 sizeof(fd_mask));
1428 FD_SET(connection_out, setp);
1429 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001430 (errno == EAGAIN || errno == EINTR))
1431 ;
Damien Miller95def091999-11-25 00:26:21 +11001432 packet_write_poll();
1433 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001434 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435}
1436
1437/* Returns true if there is buffered data to write to the connection. */
1438
1439int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001440packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441{
Damien Miller95def091999-11-25 00:26:21 +11001442 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443}
1444
1445/* Returns true if there is not too much data to write to the connection. */
1446
1447int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001448packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449{
Damien Miller95def091999-11-25 00:26:21 +11001450 if (interactive_mode)
1451 return buffer_len(&output) < 16384;
1452 else
1453 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454}
1455
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001456
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001457static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001458packet_set_tos(int interactive)
1459{
Damien Miller5924ceb2003-11-22 15:02:42 +11001460#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001461 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1462
1463 if (!packet_connection_is_on_socket() ||
1464 !packet_connection_is_ipv4())
1465 return;
1466 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1467 sizeof(tos)) < 0)
1468 error("setsockopt IP_TOS %d: %.100s:",
1469 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001470#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001471}
Ben Lindstroma7433982002-12-23 02:41:41 +00001472
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001473/* Informs that the current session is interactive. Sets IP flags for that. */
1474
1475void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001476packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001477{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001478 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001480 if (called)
1481 return;
1482 called = 1;
1483
Damien Miller95def091999-11-25 00:26:21 +11001484 /* Record that we are in interactive mode. */
1485 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001486
Damien Miller34132e52000-01-14 15:45:46 +11001487 /* Only set socket options if using a socket. */
1488 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001489 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001490 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001491 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492}
1493
1494/* Returns true if the current connection is interactive. */
1495
1496int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001497packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498{
Damien Miller95def091999-11-25 00:26:21 +11001499 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500}
Damien Miller6162d121999-11-21 13:23:52 +11001501
Darren Tucker1f8311c2004-05-13 16:39:33 +10001502int
Darren Tucker502d3842003-06-28 12:38:01 +10001503packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001504{
Damien Miller95def091999-11-25 00:26:21 +11001505 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001506
Damien Miller95def091999-11-25 00:26:21 +11001507 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001508 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001509 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001510 return -1;
1511 }
1512 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001513 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001514 return -1;
1515 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001516 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001517 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001518 max_packet_size = s;
1519 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001520}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001521
Damien Miller9f643902001-11-12 11:02:52 +11001522/* roundup current message to pad bytes */
1523void
1524packet_add_padding(u_char pad)
1525{
1526 extra_pad = pad;
1527}
1528
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001529/*
1530 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001531 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001532 * byte SSH_MSG_IGNORE
1533 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001534 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001535 * All implementations MUST understand (and ignore) this message at any
1536 * time (after receiving the protocol version). No implementation is
1537 * required to send them. This message can be used as an additional
1538 * protection measure against advanced traffic analysis techniques.
1539 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001540void
1541packet_send_ignore(int nbytes)
1542{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001543 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001544 int i;
1545
1546 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001547 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001548 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001549 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001550 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001551 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001552 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001553 }
1554}
Damien Millera5539d22003-04-09 20:50:06 +10001555
Darren Tucker1f8311c2004-05-13 16:39:33 +10001556#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001557int
1558packet_need_rekeying(void)
1559{
1560 if (datafellows & SSH_BUG_NOREKEY)
1561 return 0;
1562 return
1563 (p_send.packets > MAX_PACKETS) ||
1564 (p_read.packets > MAX_PACKETS) ||
1565 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1566 (max_blocks_in && (p_read.blocks > max_blocks_in));
1567}
1568
1569void
1570packet_set_rekey_limit(u_int32_t bytes)
1571{
1572 rekey_limit = bytes;
1573}
Damien Miller9786e6e2005-07-26 21:54:56 +10001574
1575void
1576packet_set_server(void)
1577{
1578 server_side = 1;
1579}
1580
1581void
1582packet_set_authenticated(void)
1583{
1584 after_authentication = 1;
1585}