blob: fceeef6661902d3853247af97acc326150f1b6a2 [file] [log] [blame]
Damien Miller8ba29fe2006-03-26 14:25:19 +11001/* $OpenBSD: packet.c,v 1.129 2006/03/25 18:29:35 deraadt 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#include "getput.h"
52
53#include "compress.h"
54#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000055#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
Damien Miller33b13562000-04-04 14:38:59 +100057#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100059#include "ssh2.h"
60
Damien Miller874d77b2000-10-14 16:23:11 +110061#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100062#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000063#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "log.h"
65#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110066#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000067#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100068
69#ifdef PACKET_DEBUG
70#define DBG(x) x
71#else
72#define DBG(x)
73#endif
74
Damien Miller5428f641999-11-25 11:54:57 +110075/*
76 * This variable contains the file descriptors used for communicating with
77 * the other side. connection_in is used for reading; connection_out for
78 * writing. These can be the same descriptor, in which case it is assumed to
79 * be a socket.
80 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081static int connection_in = -1;
82static int connection_out = -1;
83
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000085static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
87/* Encryption context for receiving data. This is only used for decryption. */
88static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110089
90/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091static CipherContext send_context;
92
93/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000094Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
96/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000097Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99/* Buffer for the partial outgoing packet being constructed. */
100static Buffer outgoing_packet;
101
102/* Buffer for the incoming packet currently being processed. */
103static Buffer incoming_packet;
104
105/* Scratch buffer for packet compression/decompression. */
106static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000107static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
109/* Flag indicating whether packet compression/decompression is enabled. */
110static int packet_compression = 0;
111
Damien Miller6162d121999-11-21 13:23:52 +1100112/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000113u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100114
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115/* Flag indicating whether this module has been initialized. */
116static int initialized = 0;
117
118/* Set to true if the connection is interactive. */
119static int interactive_mode = 0;
120
Damien Miller9786e6e2005-07-26 21:54:56 +1000121/* Set to true if we are the server side. */
122static int server_side = 0;
123
124/* Set to true if we are authenticated. */
125static int after_authentication = 0;
126
Damien Miller33b13562000-04-04 14:38:59 +1000127/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000128Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000129static struct packet_state {
130 u_int32_t seqnr;
131 u_int32_t packets;
132 u_int64_t blocks;
133} p_read, p_send;
134
135static u_int64_t max_blocks_in, max_blocks_out;
136static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000137
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000138/* Session key for protocol v1 */
139static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
140static u_int ssh1_keylen;
141
Damien Miller9f643902001-11-12 11:02:52 +1100142/* roundup current message to extra_pad bytes */
143static u_char extra_pad = 0;
144
Damien Millera5539d22003-04-09 20:50:06 +1000145struct packet {
146 TAILQ_ENTRY(packet) next;
147 u_char type;
148 Buffer payload;
149};
150TAILQ_HEAD(, packet) outgoing;
151
Damien Miller5428f641999-11-25 11:54:57 +1100152/*
153 * Sets the descriptors used for communication. Disables encryption until
154 * packet_set_encryption_key is called.
155 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156void
157packet_set_connection(int fd_in, int fd_out)
158{
Damien Miller874d77b2000-10-14 16:23:11 +1100159 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000160
Damien Miller874d77b2000-10-14 16:23:11 +1100161 if (none == NULL)
162 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100163 connection_in = fd_in;
164 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000165 cipher_init(&send_context, none, (const u_char *)"",
166 0, NULL, 0, CIPHER_ENCRYPT);
167 cipher_init(&receive_context, none, (const u_char *)"",
168 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000169 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100170 if (!initialized) {
171 initialized = 1;
172 buffer_init(&input);
173 buffer_init(&output);
174 buffer_init(&outgoing_packet);
175 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000176 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100177 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
Damien Miller34132e52000-01-14 15:45:46 +1100180/* Returns 1 if remote host is connected via socket, 0 if not. */
181
182int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000183packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100184{
185 struct sockaddr_storage from, to;
186 socklen_t fromlen, tolen;
187
188 /* filedescriptors in and out are the same, so it's a socket */
189 if (connection_in == connection_out)
190 return 1;
191 fromlen = sizeof(from);
192 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100193 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100194 return 0;
195 tolen = sizeof(to);
196 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100197 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100198 return 0;
199 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
200 return 0;
201 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
202 return 0;
203 return 1;
204}
205
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000206/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000207 * Exports an IV from the CipherContext required to export the key
208 * state back from the unprivileged child to the privileged parent
209 * process.
210 */
211
212void
213packet_get_keyiv(int mode, u_char *iv, u_int len)
214{
215 CipherContext *cc;
216
217 if (mode == MODE_OUT)
218 cc = &send_context;
219 else
220 cc = &receive_context;
221
222 cipher_get_keyiv(cc, iv, len);
223}
224
225int
226packet_get_keycontext(int mode, u_char *dat)
227{
228 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000229
Ben Lindstromf6027d32002-03-22 01:42:04 +0000230 if (mode == MODE_OUT)
231 cc = &send_context;
232 else
233 cc = &receive_context;
234
235 return (cipher_get_keycontext(cc, dat));
236}
237
238void
239packet_set_keycontext(int mode, u_char *dat)
240{
241 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000242
Ben Lindstromf6027d32002-03-22 01:42:04 +0000243 if (mode == MODE_OUT)
244 cc = &send_context;
245 else
246 cc = &receive_context;
247
248 cipher_set_keycontext(cc, dat);
249}
250
251int
252packet_get_keyiv_len(int mode)
253{
254 CipherContext *cc;
255
256 if (mode == MODE_OUT)
257 cc = &send_context;
258 else
259 cc = &receive_context;
260
261 return (cipher_get_keyiv_len(cc));
262}
Damien Miller4f7becb2006-03-26 14:10:14 +1100263
Ben Lindstromf6027d32002-03-22 01:42:04 +0000264void
265packet_set_iv(int mode, u_char *dat)
266{
267 CipherContext *cc;
268
269 if (mode == MODE_OUT)
270 cc = &send_context;
271 else
272 cc = &receive_context;
273
274 cipher_set_keyiv(cc, dat);
275}
Damien Miller4f7becb2006-03-26 14:10:14 +1100276
Ben Lindstromf6027d32002-03-22 01:42:04 +0000277int
Damien Miller2b92d322003-06-11 22:05:06 +1000278packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000279{
280 return (cipher_get_number(receive_context.cipher));
281}
282
Damien Millera5539d22003-04-09 20:50:06 +1000283void
284packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000285{
Damien Millera5539d22003-04-09 20:50:06 +1000286 struct packet_state *state;
287
288 state = (mode == MODE_IN) ? &p_read : &p_send;
289 *seqnr = state->seqnr;
290 *blocks = state->blocks;
291 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000292}
293
294void
Damien Millera5539d22003-04-09 20:50:06 +1000295packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000296{
Damien Millera5539d22003-04-09 20:50:06 +1000297 struct packet_state *state;
298
299 state = (mode == MODE_IN) ? &p_read : &p_send;
300 state->seqnr = seqnr;
301 state->blocks = blocks;
302 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000303}
304
Damien Miller34132e52000-01-14 15:45:46 +1100305/* returns 1 if connection is via ipv4 */
306
307int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000308packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100309{
310 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100311 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100312
313 memset(&to, 0, sizeof(to));
314 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
315 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000316 if (to.ss_family == AF_INET)
317 return 1;
318#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100319 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000320 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
321 return 1;
322#endif
323 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100324}
325
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326/* Sets the connection into non-blocking mode. */
327
328void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000329packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330{
Damien Miller95def091999-11-25 00:26:21 +1100331 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000332 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333
Damien Miller232711f2004-06-15 10:35:30 +1000334 if (connection_out != connection_in)
335 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336}
337
338/* Returns the socket used for reading. */
339
340int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000341packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342{
Damien Miller95def091999-11-25 00:26:21 +1100343 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344}
345
346/* Returns the descriptor used for writing. */
347
348int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000349packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350{
Damien Miller95def091999-11-25 00:26:21 +1100351 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352}
353
354/* Closes the connection and clears and frees internal data structures. */
355
356void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000357packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358{
Damien Miller95def091999-11-25 00:26:21 +1100359 if (!initialized)
360 return;
361 initialized = 0;
362 if (connection_in == connection_out) {
363 shutdown(connection_out, SHUT_RDWR);
364 close(connection_out);
365 } else {
366 close(connection_in);
367 close(connection_out);
368 }
369 buffer_free(&input);
370 buffer_free(&output);
371 buffer_free(&outgoing_packet);
372 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000373 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100374 buffer_free(&compression_buffer);
375 buffer_compress_uninit();
376 }
Damien Miller963f6b22002-02-19 15:21:23 +1100377 cipher_cleanup(&send_context);
378 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379}
380
381/* Sets remote side protocol flags. */
382
383void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000384packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385{
Damien Miller95def091999-11-25 00:26:21 +1100386 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387}
388
389/* Returns the remote protocol flags set earlier by the above function. */
390
Ben Lindstrom46c16222000-12-22 01:43:59 +0000391u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000392packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393{
Damien Miller95def091999-11-25 00:26:21 +1100394 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395}
396
Damien Miller5428f641999-11-25 11:54:57 +1100397/*
398 * Starts packet compression from the next packet on in both directions.
399 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
400 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401
Ben Lindstrombba81212001-06-25 05:01:22 +0000402static void
403packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000404{
405 if (compression_buffer_ready == 1)
406 return;
407 compression_buffer_ready = 1;
408 buffer_init(&compression_buffer);
409}
410
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411void
412packet_start_compression(int level)
413{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000414 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100415 fatal("Compression already enabled.");
416 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000417 packet_init_compression();
418 buffer_compress_init_send(level);
419 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420}
421
Damien Miller5428f641999-11-25 11:54:57 +1100422/*
Damien Miller5428f641999-11-25 11:54:57 +1100423 * Causes any further packets to be encrypted using the given key. The same
424 * key is used for both sending and reception. However, both directions are
425 * encrypted independently of each other.
426 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000427
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000429packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100430 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431{
Damien Miller874d77b2000-10-14 16:23:11 +1100432 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000433
Damien Miller874d77b2000-10-14 16:23:11 +1100434 if (cipher == NULL)
435 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000436 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100437 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000438 if (keylen > SSH_SESSION_KEY_LENGTH)
439 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
440 memcpy(ssh1_key, key, keylen);
441 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100442 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
443 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444}
445
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000446u_int
447packet_get_encryption_key(u_char *key)
448{
449 if (key == NULL)
450 return (ssh1_keylen);
451 memcpy(key, ssh1_key, ssh1_keylen);
452 return (ssh1_keylen);
453}
454
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000455/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000457packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000459 u_char buf[9];
460 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461
Ben Lindstrom204e4882001-03-05 06:47:00 +0000462 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000463 len = compat20 ? 6 : 9;
464 memset(buf, 0, len - 1);
465 buf[len - 1] = type;
466 buffer_clear(&outgoing_packet);
467 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000468}
469
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000470/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471void
472packet_put_char(int value)
473{
Damien Miller95def091999-11-25 00:26:21 +1100474 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000475
Damien Miller95def091999-11-25 00:26:21 +1100476 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477}
Damien Miller4f7becb2006-03-26 14:10:14 +1100478
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000480packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481{
Damien Miller95def091999-11-25 00:26:21 +1100482 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483}
Damien Miller4f7becb2006-03-26 14:10:14 +1100484
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100486packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487{
Damien Miller95def091999-11-25 00:26:21 +1100488 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489}
Damien Miller4f7becb2006-03-26 14:10:14 +1100490
Damien Miller33b13562000-04-04 14:38:59 +1000491void
492packet_put_cstring(const char *str)
493{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000494 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000495}
Damien Miller4f7becb2006-03-26 14:10:14 +1100496
Damien Miller33b13562000-04-04 14:38:59 +1000497void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100498packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000499{
500 buffer_append(&outgoing_packet, buf, len);
501}
Damien Miller4f7becb2006-03-26 14:10:14 +1100502
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503void
Damien Miller95def091999-11-25 00:26:21 +1100504packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505{
Damien Miller95def091999-11-25 00:26:21 +1100506 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507}
Damien Miller4f7becb2006-03-26 14:10:14 +1100508
Damien Miller33b13562000-04-04 14:38:59 +1000509void
510packet_put_bignum2(BIGNUM * value)
511{
512 buffer_put_bignum2(&outgoing_packet, value);
513}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Damien Miller5428f641999-11-25 11:54:57 +1100515/*
516 * Finalizes and sends the packet. If the encryption key has been set,
517 * encrypts the packet before sending.
518 */
Damien Miller95def091999-11-25 00:26:21 +1100519
Ben Lindstrombba81212001-06-25 05:01:22 +0000520static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000521packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000523 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100524 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000525 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000526 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527
Damien Miller5428f641999-11-25 11:54:57 +1100528 /*
529 * If using packet compression, compress the payload of the outgoing
530 * packet.
531 */
Damien Miller95def091999-11-25 00:26:21 +1100532 if (packet_compression) {
533 buffer_clear(&compression_buffer);
534 /* Skip padding. */
535 buffer_consume(&outgoing_packet, 8);
536 /* padding */
537 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
538 buffer_compress(&outgoing_packet, &compression_buffer);
539 buffer_clear(&outgoing_packet);
540 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100541 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100542 }
543 /* Compute packet length without padding (add checksum, remove padding). */
544 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545
Damien Millere247cc42000-05-07 12:03:14 +1000546 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100547 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100548 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100549 cp = buffer_ptr(&outgoing_packet);
550 for (i = 0; i < padding; i++) {
551 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000552 rnd = arc4random();
553 cp[7 - i] = rnd & 0xff;
554 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100555 }
556 }
557 buffer_consume(&outgoing_packet, 8 - padding);
558
559 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100560 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000561 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100562 PUT_32BIT(buf, checksum);
563 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564
565#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100566 fprintf(stderr, "packet_send plain: ");
567 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568#endif
569
Damien Miller95def091999-11-25 00:26:21 +1100570 /* Append to output. */
571 PUT_32BIT(buf, len);
572 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100573 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100574 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100575 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100576
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100578 fprintf(stderr, "encrypted: ");
579 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580#endif
581
Damien Miller95def091999-11-25 00:26:21 +1100582 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583
Damien Miller5428f641999-11-25 11:54:57 +1100584 /*
Damien Miller788f2122005-11-05 15:14:59 +1100585 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100586 * actually sent until packet_write_wait or packet_write_poll is
587 * called.
588 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589}
590
Ben Lindstromf6027d32002-03-22 01:42:04 +0000591void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000592set_newkeys(int mode)
593{
594 Enc *enc;
595 Mac *mac;
596 Comp *comp;
597 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000598 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000599 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000600
Ben Lindstrom064496f2002-12-23 02:04:22 +0000601 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000602
Damien Miller963f6b22002-02-19 15:21:23 +1100603 if (mode == MODE_OUT) {
604 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000605 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000606 p_send.packets = p_send.blocks = 0;
607 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100608 } else {
609 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000610 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000611 p_read.packets = p_read.blocks = 0;
612 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100613 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000614 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000615 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100616 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000617 enc = &newkeys[mode]->enc;
618 mac = &newkeys[mode]->mac;
619 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000620 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000621 xfree(enc->name);
622 xfree(enc->iv);
623 xfree(enc->key);
624 xfree(mac->name);
625 xfree(mac->key);
626 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000627 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000628 }
629 newkeys[mode] = kex_get_newkeys(mode);
630 if (newkeys[mode] == NULL)
631 fatal("newkeys: no keys for mode %d", mode);
632 enc = &newkeys[mode]->enc;
633 mac = &newkeys[mode]->mac;
634 comp = &newkeys[mode]->comp;
635 if (mac->md != NULL)
636 mac->enabled = 1;
637 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100638 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000639 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000640 /* Deleting the keys does not gain extra security */
641 /* memset(enc->iv, 0, enc->block_size);
642 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000643 if ((comp->type == COMP_ZLIB ||
644 (comp->type == COMP_DELAYED && after_authentication)) &&
645 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000646 packet_init_compression();
647 if (mode == MODE_OUT)
648 buffer_compress_init_send(6);
649 else
650 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000651 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000652 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000653 /*
654 * The 2^(blocksize*2) limit is too expensive for 3DES,
655 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
656 */
657 if (enc->block_size >= 16)
658 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
659 else
660 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000661 if (rekey_limit)
662 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000663}
664
Damien Miller5428f641999-11-25 11:54:57 +1100665/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000666 * Delayed compression for SSH2 is enabled after authentication:
667 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
668 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
669 */
670static void
671packet_enable_delayed_compress(void)
672{
673 Comp *comp = NULL;
674 int mode;
675
676 /*
677 * Remember that we are past the authentication step, so rekeying
678 * with COMP_DELAYED will turn on compression immediately.
679 */
680 after_authentication = 1;
681 for (mode = 0; mode < MODE_MAX; mode++) {
682 comp = &newkeys[mode]->comp;
683 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000684 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000685 if (mode == MODE_OUT)
686 buffer_compress_init_send(6);
687 else
688 buffer_compress_init_recv();
689 comp->enabled = 1;
690 }
691 }
692}
693
694/*
Damien Miller33b13562000-04-04 14:38:59 +1000695 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
696 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000697static void
Damien Millera5539d22003-04-09 20:50:06 +1000698packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000699{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000700 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100701 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000702 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100703 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000704 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000705 Enc *enc = NULL;
706 Mac *mac = NULL;
707 Comp *comp = NULL;
708 int block_size;
709
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000710 if (newkeys[MODE_OUT] != NULL) {
711 enc = &newkeys[MODE_OUT]->enc;
712 mac = &newkeys[MODE_OUT]->mac;
713 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000714 }
Damien Miller963f6b22002-02-19 15:21:23 +1100715 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000716
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000717 cp = buffer_ptr(&outgoing_packet);
718 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000719
720#ifdef PACKET_DEBUG
721 fprintf(stderr, "plain: ");
722 buffer_dump(&outgoing_packet);
723#endif
724
725 if (comp && comp->enabled) {
726 len = buffer_len(&outgoing_packet);
727 /* skip header, compress only payload */
728 buffer_consume(&outgoing_packet, 5);
729 buffer_clear(&compression_buffer);
730 buffer_compress(&outgoing_packet, &compression_buffer);
731 buffer_clear(&outgoing_packet);
732 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
733 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
734 buffer_len(&compression_buffer));
735 DBG(debug("compression: raw %d compressed %d", len,
736 buffer_len(&outgoing_packet)));
737 }
738
739 /* sizeof (packet_len + pad_len + payload) */
740 len = buffer_len(&outgoing_packet);
741
742 /*
743 * calc size of padding, alloc space, get random data,
744 * minimum padding is 4 bytes
745 */
746 padlen = block_size - (len % block_size);
747 if (padlen < 4)
748 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100749 if (extra_pad) {
750 /* will wrap if extra_pad+padlen > 255 */
751 extra_pad = roundup(extra_pad, block_size);
752 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000753 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100754 pad, len, padlen, extra_pad);
755 padlen += pad;
756 extra_pad = 0;
757 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100758 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100759 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000760 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000761 for (i = 0; i < padlen; i++) {
762 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000763 rnd = arc4random();
764 cp[i] = rnd & 0xff;
765 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000766 }
Damien Millere247cc42000-05-07 12:03:14 +1000767 } else {
768 /* clear padding */
769 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000770 }
771 /* packet_length includes payload, padding and padding length field */
772 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000773 cp = buffer_ptr(&outgoing_packet);
774 PUT_32BIT(cp, packet_length);
775 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000776 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
777
778 /* compute MAC over seqnr and packet(length fields, payload, padding) */
779 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000780 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100781 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000782 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000783 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000784 }
785 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100786 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100787 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000788 buffer_len(&outgoing_packet));
789 /* append unencrypted MAC */
790 if (mac && mac->enabled)
791 buffer_append(&output, (char *)macbuf, mac->mac_len);
792#ifdef PACKET_DEBUG
793 fprintf(stderr, "encrypted: ");
794 buffer_dump(&output);
795#endif
Damien Miller4af51302000-04-16 11:18:38 +1000796 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000797 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000798 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000799 if (++p_send.packets == 0)
800 if (!(datafellows & SSH_BUG_NOREKEY))
801 fatal("XXX too many packets with same key");
802 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000803 buffer_clear(&outgoing_packet);
804
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000805 if (type == SSH2_MSG_NEWKEYS)
806 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000807 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
808 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000809}
810
Damien Millera5539d22003-04-09 20:50:06 +1000811static void
812packet_send2(void)
813{
814 static int rekeying = 0;
815 struct packet *p;
816 u_char type, *cp;
817
818 cp = buffer_ptr(&outgoing_packet);
819 type = cp[5];
820
821 /* during rekeying we can only send key exchange messages */
822 if (rekeying) {
823 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
824 (type <= SSH2_MSG_TRANSPORT_MAX))) {
825 debug("enqueue packet: %u", type);
826 p = xmalloc(sizeof(*p));
827 p->type = type;
828 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
829 buffer_init(&outgoing_packet);
830 TAILQ_INSERT_TAIL(&outgoing, p, next);
831 return;
832 }
833 }
834
835 /* rekeying starts with sending KEXINIT */
836 if (type == SSH2_MSG_KEXINIT)
837 rekeying = 1;
838
839 packet_send2_wrapped();
840
841 /* after a NEWKEYS message we can send the complete queue */
842 if (type == SSH2_MSG_NEWKEYS) {
843 rekeying = 0;
844 while ((p = TAILQ_FIRST(&outgoing))) {
845 type = p->type;
846 debug("dequeue packet: %u", type);
847 buffer_free(&outgoing_packet);
848 memcpy(&outgoing_packet, &p->payload,
849 sizeof(Buffer));
850 TAILQ_REMOVE(&outgoing, p, next);
851 xfree(p);
852 packet_send2_wrapped();
853 }
854 }
855}
856
Damien Miller33b13562000-04-04 14:38:59 +1000857void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000858packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000859{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000860 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000861 packet_send2();
862 else
863 packet_send1();
864 DBG(debug("packet_send done"));
865}
866
Damien Miller33b13562000-04-04 14:38:59 +1000867/*
Damien Miller5428f641999-11-25 11:54:57 +1100868 * Waits until a packet has been received, and returns its type. Note that
869 * no other data is processed until this returns, so this function should not
870 * be used during the interactive session.
871 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872
873int
Damien Millerdff50992002-01-22 23:16:32 +1100874packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875{
Damien Miller95def091999-11-25 00:26:21 +1100876 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000877 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100878 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000879 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880
Damien Miller07d86be2006-03-26 14:19:21 +1100881 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000882 sizeof(fd_mask));
883
Damien Miller95def091999-11-25 00:26:21 +1100884 /* Since we are blocking, ensure that all written packets have been sent. */
885 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886
Damien Miller95def091999-11-25 00:26:21 +1100887 /* Stay in the loop until we have received a complete packet. */
888 for (;;) {
889 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100890 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000891 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000892 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100893 || type == SSH_SMSG_FAILURE
894 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000895 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100896 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100897 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000898 if (type != SSH_MSG_NONE) {
899 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100900 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000901 }
Damien Miller5428f641999-11-25 11:54:57 +1100902 /*
903 * Otherwise, wait for some data to arrive, add it to the
904 * buffer, and try again.
905 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000906 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
907 sizeof(fd_mask));
908 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100909
Damien Miller95def091999-11-25 00:26:21 +1100910 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000911 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000912 (errno == EAGAIN || errno == EINTR))
913 ;
Damien Miller5428f641999-11-25 11:54:57 +1100914
Damien Miller95def091999-11-25 00:26:21 +1100915 /* Read data from the socket. */
916 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100917 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000918 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000919 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100920 }
Damien Miller95def091999-11-25 00:26:21 +1100921 if (len < 0)
922 fatal("Read from socket failed: %.100s", strerror(errno));
923 /* Append it to the buffer. */
924 packet_process_incoming(buf, len);
925 }
926 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927}
928
Damien Miller278f9072001-12-21 15:00:19 +1100929int
Damien Millerdff50992002-01-22 23:16:32 +1100930packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100931{
Damien Millerdff50992002-01-22 23:16:32 +1100932 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100933}
934
Damien Miller5428f641999-11-25 11:54:57 +1100935/*
936 * Waits until a packet has been received, verifies that its type matches
937 * that given, and gives a fatal error and exits if there is a mismatch.
938 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939
940void
Damien Millerdff50992002-01-22 23:16:32 +1100941packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942{
Damien Miller95def091999-11-25 00:26:21 +1100943 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944
Damien Millerdff50992002-01-22 23:16:32 +1100945 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100946 if (type != expected_type)
947 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000948 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949}
950
951/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100952 * packet_process_incoming. If so, reads the packet; otherwise returns
953 * SSH_MSG_NONE. This does not wait for data from the connection.
954 *
955 * SSH_MSG_DISCONNECT is handled specially here. Also,
956 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
957 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100958 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959
Ben Lindstrombba81212001-06-25 05:01:22 +0000960static int
Damien Millerdff50992002-01-22 23:16:32 +1100961packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000963 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000964 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000965 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966
Damien Miller95def091999-11-25 00:26:21 +1100967 /* Check if input size is less than minimum packet size. */
968 if (buffer_len(&input) < 4 + 8)
969 return SSH_MSG_NONE;
970 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000971 cp = buffer_ptr(&input);
972 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100973 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000974 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100975 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976
Damien Miller95def091999-11-25 00:26:21 +1100977 /* Check if the packet has been entirely received. */
978 if (buffer_len(&input) < 4 + padded_len)
979 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
Damien Miller95def091999-11-25 00:26:21 +1100981 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Damien Miller95def091999-11-25 00:26:21 +1100983 /* Consume packet length. */
984 buffer_consume(&input, 4);
985
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000986 /*
987 * Cryptographic attack detector for ssh
988 * (C)1998 CORE-SDI, Buenos Aires Argentina
989 * Ariel Futoransky(futo@core-sdi.com)
990 */
Damien Miller963f6b22002-02-19 15:21:23 +1100991 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +1100992 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000993 packet_disconnect("crc32 compensation attack: network attack detected");
994
995 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100996 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100997 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100998 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000999
Damien Miller95def091999-11-25 00:26:21 +11001000 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001
1002#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001003 fprintf(stderr, "read_poll plain: ");
1004 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006
Damien Miller95def091999-11-25 00:26:21 +11001007 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001008 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001009 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001010
Damien Miller95def091999-11-25 00:26:21 +11001011 /* Skip padding. */
1012 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001013
Damien Miller95def091999-11-25 00:26:21 +11001014 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001015 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001016 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001017 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001019 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
1020 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +11001021 if (checksum != stored_checksum)
1022 packet_disconnect("Corrupted check bytes on input.");
1023 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024
Damien Miller95def091999-11-25 00:26:21 +11001025 if (packet_compression) {
1026 buffer_clear(&compression_buffer);
1027 buffer_uncompress(&incoming_packet, &compression_buffer);
1028 buffer_clear(&incoming_packet);
1029 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001030 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001031 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001032 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001033 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1034 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001035 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036}
Damien Miller95def091999-11-25 00:26:21 +11001037
Ben Lindstrombba81212001-06-25 05:01:22 +00001038static int
Damien Millerdff50992002-01-22 23:16:32 +11001039packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001040{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001041 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001042 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001043 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001044 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001045 Enc *enc = NULL;
1046 Mac *mac = NULL;
1047 Comp *comp = NULL;
1048
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001049 if (newkeys[MODE_IN] != NULL) {
1050 enc = &newkeys[MODE_IN]->enc;
1051 mac = &newkeys[MODE_IN]->mac;
1052 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001053 }
1054 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001055 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001056
1057 if (packet_length == 0) {
1058 /*
1059 * check if input size is less than the cipher block size,
1060 * decrypt first block and extract length of incoming packet
1061 */
1062 if (buffer_len(&input) < block_size)
1063 return SSH_MSG_NONE;
1064 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001065 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001066 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001067 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001068 cp = buffer_ptr(&incoming_packet);
1069 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001070 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001071#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001072 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001073#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001074 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001075 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001076 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001077 buffer_consume(&input, block_size);
1078 }
1079 /* we have a partial packet of block_size bytes */
1080 need = 4 + packet_length - block_size;
1081 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1082 need, maclen));
1083 if (need % block_size != 0)
1084 fatal("padding error: need %d block %d mod %d",
1085 need, block_size, need % block_size);
1086 /*
1087 * check if the entire packet has been received and
1088 * decrypt into incoming_packet
1089 */
1090 if (buffer_len(&input) < need + maclen)
1091 return SSH_MSG_NONE;
1092#ifdef PACKET_DEBUG
1093 fprintf(stderr, "read_poll enc/full: ");
1094 buffer_dump(&input);
1095#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001096 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001097 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001098 buffer_consume(&input, need);
1099 /*
1100 * compute MAC over seqnr and packet,
1101 * increment sequence number for incoming packet
1102 */
Damien Miller4af51302000-04-16 11:18:38 +10001103 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001104 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001105 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001106 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001107 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001108 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001109 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001110 buffer_consume(&input, mac->mac_len);
1111 }
Damien Miller278f9072001-12-21 15:00:19 +11001112 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001113 *seqnr_p = p_read.seqnr;
1114 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001115 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001116 if (++p_read.packets == 0)
1117 if (!(datafellows & SSH_BUG_NOREKEY))
1118 fatal("XXX too many packets with same key");
1119 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001120
1121 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001122 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001123 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001124 DBG(debug("input: padlen %d", padlen));
1125 if (padlen < 4)
1126 packet_disconnect("Corrupted padlen %d on input.", padlen);
1127
1128 /* skip packet size + padlen, discard padding */
1129 buffer_consume(&incoming_packet, 4 + 1);
1130 buffer_consume_end(&incoming_packet, padlen);
1131
1132 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1133 if (comp && comp->enabled) {
1134 buffer_clear(&compression_buffer);
1135 buffer_uncompress(&incoming_packet, &compression_buffer);
1136 buffer_clear(&incoming_packet);
1137 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1138 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001139 DBG(debug("input: len after de-compress %d",
1140 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001141 }
1142 /*
1143 * get packet type, implies consume.
1144 * return length of payload (without type field)
1145 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001146 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001147 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1148 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001149 if (type == SSH2_MSG_NEWKEYS)
1150 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001151 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1152 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001153#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001154 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001155 buffer_dump(&incoming_packet);
1156#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001157 /* reset for next packet */
1158 packet_length = 0;
1159 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001160}
1161
1162int
Damien Millerdff50992002-01-22 23:16:32 +11001163packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001164{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001165 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001166 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001167 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001168
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001169 for (;;) {
1170 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001171 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001172 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001173 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001174 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001175 case SSH2_MSG_IGNORE:
1176 break;
1177 case SSH2_MSG_DEBUG:
1178 packet_get_char();
1179 msg = packet_get_string(NULL);
1180 debug("Remote: %.900s", msg);
1181 xfree(msg);
1182 msg = packet_get_string(NULL);
1183 xfree(msg);
1184 break;
1185 case SSH2_MSG_DISCONNECT:
1186 reason = packet_get_int();
1187 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001188 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001189 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001190 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001191 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001192 break;
Damien Miller659811f2002-01-22 23:23:11 +11001193 case SSH2_MSG_UNIMPLEMENTED:
1194 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001195 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1196 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001197 break;
Damien Miller33b13562000-04-04 14:38:59 +10001198 default:
1199 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001200 }
Damien Miller33b13562000-04-04 14:38:59 +10001201 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001202 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001203 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001204 case SSH_MSG_IGNORE:
1205 break;
1206 case SSH_MSG_DEBUG:
1207 msg = packet_get_string(NULL);
1208 debug("Remote: %.900s", msg);
1209 xfree(msg);
1210 break;
1211 case SSH_MSG_DISCONNECT:
1212 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001213 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001214 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001215 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001216 xfree(msg);
1217 break;
1218 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001219 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001220 DBG(debug("received packet type %d", type));
1221 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001222 }
Damien Miller33b13562000-04-04 14:38:59 +10001223 }
1224 }
1225}
1226
Damien Miller278f9072001-12-21 15:00:19 +11001227int
Damien Millerdff50992002-01-22 23:16:32 +11001228packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001229{
Damien Millerdff50992002-01-22 23:16:32 +11001230 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001231}
1232
Damien Miller5428f641999-11-25 11:54:57 +11001233/*
1234 * Buffers the given amount of input characters. This is intended to be used
1235 * together with packet_read_poll.
1236 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237
1238void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001239packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240{
Damien Miller95def091999-11-25 00:26:21 +11001241 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242}
1243
1244/* Returns a character from the packet. */
1245
Ben Lindstrom46c16222000-12-22 01:43:59 +00001246u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001247packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248{
Damien Miller95def091999-11-25 00:26:21 +11001249 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001250
Damien Miller95def091999-11-25 00:26:21 +11001251 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001252 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253}
1254
1255/* Returns an integer from the packet data. */
1256
Ben Lindstrom46c16222000-12-22 01:43:59 +00001257u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001258packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259{
Damien Miller95def091999-11-25 00:26:21 +11001260 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001261}
1262
Damien Miller5428f641999-11-25 11:54:57 +11001263/*
1264 * Returns an arbitrary precision integer from the packet data. The integer
1265 * must have been initialized before this call.
1266 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267
1268void
Damien Millerd432ccf2002-01-22 23:14:44 +11001269packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270{
Damien Miller76e1e362002-01-22 23:15:57 +11001271 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272}
1273
Damien Miller33b13562000-04-04 14:38:59 +10001274void
Damien Millerd432ccf2002-01-22 23:14:44 +11001275packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001276{
Damien Miller76e1e362002-01-22 23:15:57 +11001277 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001278}
1279
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001280void *
Damien Millereccb9de2005-06-17 12:59:34 +10001281packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001282{
Damien Millereccb9de2005-06-17 12:59:34 +10001283 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001284
Damien Miller33b13562000-04-04 14:38:59 +10001285 if (length_ptr != NULL)
1286 *length_ptr = bytes;
1287 return buffer_ptr(&incoming_packet);
1288}
1289
Damien Miller4af51302000-04-16 11:18:38 +10001290int
1291packet_remaining(void)
1292{
1293 return buffer_len(&incoming_packet);
1294}
1295
Damien Miller5428f641999-11-25 11:54:57 +11001296/*
1297 * Returns a string from the packet data. The string is allocated using
1298 * xmalloc; it is the responsibility of the calling program to free it when
1299 * no longer needed. The length_ptr argument may be NULL, or point to an
1300 * integer into which the length of the string is stored.
1301 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001302
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001303void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001304packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001305{
Damien Miller95def091999-11-25 00:26:21 +11001306 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001307}
1308
Damien Miller5428f641999-11-25 11:54:57 +11001309/*
1310 * Sends a diagnostic message from the server to the client. This message
1311 * can be sent at any time (but not while constructing another message). The
1312 * message is printed immediately, but only if the client is being executed
1313 * in verbose mode. These messages are primarily intended to ease debugging
1314 * authentication problems. The length of the formatted message must not
1315 * exceed 1024 bytes. This will automatically call packet_write_wait.
1316 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001317
1318void
Damien Miller95def091999-11-25 00:26:21 +11001319packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320{
Damien Miller95def091999-11-25 00:26:21 +11001321 char buf[1024];
1322 va_list args;
1323
Ben Lindstroma14ee472000-12-07 01:24:58 +00001324 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1325 return;
1326
Damien Miller95def091999-11-25 00:26:21 +11001327 va_start(args, fmt);
1328 vsnprintf(buf, sizeof(buf), fmt, args);
1329 va_end(args);
1330
Damien Miller7c8af4f2000-05-01 08:24:07 +10001331 if (compat20) {
1332 packet_start(SSH2_MSG_DEBUG);
1333 packet_put_char(0); /* bool: always display */
1334 packet_put_cstring(buf);
1335 packet_put_cstring("");
1336 } else {
1337 packet_start(SSH_MSG_DEBUG);
1338 packet_put_cstring(buf);
1339 }
Damien Miller95def091999-11-25 00:26:21 +11001340 packet_send();
1341 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001342}
1343
Damien Miller5428f641999-11-25 11:54:57 +11001344/*
1345 * Logs the error plus constructs and sends a disconnect packet, closes the
1346 * connection, and exits. This function never returns. The error message
1347 * should not contain a newline. The length of the formatted message must
1348 * not exceed 1024 bytes.
1349 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001350
1351void
Damien Miller95def091999-11-25 00:26:21 +11001352packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001353{
Damien Miller95def091999-11-25 00:26:21 +11001354 char buf[1024];
1355 va_list args;
1356 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001357
Damien Miller95def091999-11-25 00:26:21 +11001358 if (disconnecting) /* Guard against recursive invocations. */
1359 fatal("packet_disconnect called recursively.");
1360 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361
Damien Miller5428f641999-11-25 11:54:57 +11001362 /*
1363 * Format the message. Note that the caller must make sure the
1364 * message is of limited size.
1365 */
Damien Miller95def091999-11-25 00:26:21 +11001366 va_start(args, fmt);
1367 vsnprintf(buf, sizeof(buf), fmt, args);
1368 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001369
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001370 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001371 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001372
Damien Miller95def091999-11-25 00:26:21 +11001373 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001374 if (compat20) {
1375 packet_start(SSH2_MSG_DISCONNECT);
1376 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1377 packet_put_cstring(buf);
1378 packet_put_cstring("");
1379 } else {
1380 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001381 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001382 }
Damien Miller95def091999-11-25 00:26:21 +11001383 packet_send();
1384 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001385
Damien Miller95def091999-11-25 00:26:21 +11001386 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001387 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001388
Damien Miller95def091999-11-25 00:26:21 +11001389 /* Close the connection. */
1390 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001391 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392}
1393
Damien Miller5428f641999-11-25 11:54:57 +11001394/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001395
1396void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001397packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001398{
Damien Miller95def091999-11-25 00:26:21 +11001399 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001400
Damien Miller95def091999-11-25 00:26:21 +11001401 if (len > 0) {
1402 len = write(connection_out, buffer_ptr(&output), len);
1403 if (len <= 0) {
1404 if (errno == EAGAIN)
1405 return;
1406 else
1407 fatal("Write failed: %.100s", strerror(errno));
1408 }
1409 buffer_consume(&output, len);
1410 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001411}
1412
Damien Miller5428f641999-11-25 11:54:57 +11001413/*
1414 * Calls packet_write_poll repeatedly until all pending output data has been
1415 * written.
1416 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417
1418void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001419packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001421 fd_set *setp;
1422
Damien Miller07d86be2006-03-26 14:19:21 +11001423 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001424 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001425 packet_write_poll();
1426 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001427 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1428 sizeof(fd_mask));
1429 FD_SET(connection_out, setp);
1430 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001431 (errno == EAGAIN || errno == EINTR))
1432 ;
Damien Miller95def091999-11-25 00:26:21 +11001433 packet_write_poll();
1434 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001435 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001436}
1437
1438/* Returns true if there is buffered data to write to the connection. */
1439
1440int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001441packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001442{
Damien Miller95def091999-11-25 00:26:21 +11001443 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444}
1445
1446/* Returns true if there is not too much data to write to the connection. */
1447
1448int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001449packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001450{
Damien Miller95def091999-11-25 00:26:21 +11001451 if (interactive_mode)
1452 return buffer_len(&output) < 16384;
1453 else
1454 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455}
1456
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001457
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001458static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001459packet_set_tos(int interactive)
1460{
Damien Miller5924ceb2003-11-22 15:02:42 +11001461#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001462 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1463
1464 if (!packet_connection_is_on_socket() ||
1465 !packet_connection_is_ipv4())
1466 return;
1467 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1468 sizeof(tos)) < 0)
1469 error("setsockopt IP_TOS %d: %.100s:",
1470 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001471#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001472}
Ben Lindstroma7433982002-12-23 02:41:41 +00001473
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474/* Informs that the current session is interactive. Sets IP flags for that. */
1475
1476void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001477packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001478{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001479 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001481 if (called)
1482 return;
1483 called = 1;
1484
Damien Miller95def091999-11-25 00:26:21 +11001485 /* Record that we are in interactive mode. */
1486 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001487
Damien Miller34132e52000-01-14 15:45:46 +11001488 /* Only set socket options if using a socket. */
1489 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001490 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001491 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001492 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493}
1494
1495/* Returns true if the current connection is interactive. */
1496
1497int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001498packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499{
Damien Miller95def091999-11-25 00:26:21 +11001500 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001501}
Damien Miller6162d121999-11-21 13:23:52 +11001502
Darren Tucker1f8311c2004-05-13 16:39:33 +10001503int
Darren Tucker502d3842003-06-28 12:38:01 +10001504packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001505{
Damien Miller95def091999-11-25 00:26:21 +11001506 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001507
Damien Miller95def091999-11-25 00:26:21 +11001508 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001509 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001510 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001511 return -1;
1512 }
1513 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001514 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001515 return -1;
1516 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001517 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001518 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001519 max_packet_size = s;
1520 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001521}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001522
Damien Miller9f643902001-11-12 11:02:52 +11001523/* roundup current message to pad bytes */
1524void
1525packet_add_padding(u_char pad)
1526{
1527 extra_pad = pad;
1528}
1529
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001530/*
1531 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001532 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001533 * byte SSH_MSG_IGNORE
1534 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001535 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001536 * All implementations MUST understand (and ignore) this message at any
1537 * time (after receiving the protocol version). No implementation is
1538 * required to send them. This message can be used as an additional
1539 * protection measure against advanced traffic analysis techniques.
1540 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001541void
1542packet_send_ignore(int nbytes)
1543{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001544 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001545 int i;
1546
1547 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001548 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001549 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001550 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001551 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001552 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001553 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001554 }
1555}
Damien Millera5539d22003-04-09 20:50:06 +10001556
Darren Tucker1f8311c2004-05-13 16:39:33 +10001557#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001558int
1559packet_need_rekeying(void)
1560{
1561 if (datafellows & SSH_BUG_NOREKEY)
1562 return 0;
1563 return
1564 (p_send.packets > MAX_PACKETS) ||
1565 (p_read.packets > MAX_PACKETS) ||
1566 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1567 (max_blocks_in && (p_read.blocks > max_blocks_in));
1568}
1569
1570void
1571packet_set_rekey_limit(u_int32_t bytes)
1572{
1573 rekey_limit = bytes;
1574}
Damien Miller9786e6e2005-07-26 21:54:56 +10001575
1576void
1577packet_set_server(void)
1578{
1579 server_side = 1;
1580}
1581
1582void
1583packet_set_authenticated(void)
1584{
1585 after_authentication = 1;
1586}