blob: bc0baf3fcd1f383ca35ae0ff549271a3a61f4032 [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/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "xmalloc.h"
46#include "buffer.h"
47#include "packet.h"
48#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
51#include "compress.h"
52#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000053#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller33b13562000-04-04 14:38:59 +100055#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100057#include "ssh2.h"
58
Damien Miller874d77b2000-10-14 16:23:11 +110059#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100060#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000061#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "log.h"
63#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110064#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000065#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100066
67#ifdef PACKET_DEBUG
68#define DBG(x) x
69#else
70#define DBG(x)
71#endif
72
Damien Miller5428f641999-11-25 11:54:57 +110073/*
74 * This variable contains the file descriptors used for communicating with
75 * the other side. connection_in is used for reading; connection_out for
76 * writing. These can be the same descriptor, in which case it is assumed to
77 * be a socket.
78 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079static int connection_in = -1;
80static int connection_out = -1;
81
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000083static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
85/* Encryption context for receiving data. This is only used for decryption. */
86static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110087
88/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089static CipherContext send_context;
90
91/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000092Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000095Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
97/* Buffer for the partial outgoing packet being constructed. */
98static Buffer outgoing_packet;
99
100/* Buffer for the incoming packet currently being processed. */
101static Buffer incoming_packet;
102
103/* Scratch buffer for packet compression/decompression. */
104static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000105static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
107/* Flag indicating whether packet compression/decompression is enabled. */
108static int packet_compression = 0;
109
Damien Miller6162d121999-11-21 13:23:52 +1100110/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000111u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100112
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113/* Flag indicating whether this module has been initialized. */
114static int initialized = 0;
115
116/* Set to true if the connection is interactive. */
117static int interactive_mode = 0;
118
Damien Miller9786e6e2005-07-26 21:54:56 +1000119/* Set to true if we are the server side. */
120static int server_side = 0;
121
122/* Set to true if we are authenticated. */
123static int after_authentication = 0;
124
Damien Miller33b13562000-04-04 14:38:59 +1000125/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000126Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000127static struct packet_state {
128 u_int32_t seqnr;
129 u_int32_t packets;
130 u_int64_t blocks;
131} p_read, p_send;
132
133static u_int64_t max_blocks_in, max_blocks_out;
134static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000135
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000136/* Session key for protocol v1 */
137static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
138static u_int ssh1_keylen;
139
Damien Miller9f643902001-11-12 11:02:52 +1100140/* roundup current message to extra_pad bytes */
141static u_char extra_pad = 0;
142
Damien Millera5539d22003-04-09 20:50:06 +1000143struct packet {
144 TAILQ_ENTRY(packet) next;
145 u_char type;
146 Buffer payload;
147};
148TAILQ_HEAD(, packet) outgoing;
149
Damien Miller5428f641999-11-25 11:54:57 +1100150/*
151 * Sets the descriptors used for communication. Disables encryption until
152 * packet_set_encryption_key is called.
153 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154void
155packet_set_connection(int fd_in, int fd_out)
156{
Damien Miller874d77b2000-10-14 16:23:11 +1100157 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000158
Damien Miller874d77b2000-10-14 16:23:11 +1100159 if (none == NULL)
160 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100161 connection_in = fd_in;
162 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000163 cipher_init(&send_context, none, (const u_char *)"",
164 0, NULL, 0, CIPHER_ENCRYPT);
165 cipher_init(&receive_context, none, (const u_char *)"",
166 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000167 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100168 if (!initialized) {
169 initialized = 1;
170 buffer_init(&input);
171 buffer_init(&output);
172 buffer_init(&outgoing_packet);
173 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000174 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100175 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176}
177
Damien Miller34132e52000-01-14 15:45:46 +1100178/* Returns 1 if remote host is connected via socket, 0 if not. */
179
180int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000181packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100182{
183 struct sockaddr_storage from, to;
184 socklen_t fromlen, tolen;
185
186 /* filedescriptors in and out are the same, so it's a socket */
187 if (connection_in == connection_out)
188 return 1;
189 fromlen = sizeof(from);
190 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100191 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100192 return 0;
193 tolen = sizeof(to);
194 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100195 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100196 return 0;
197 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
198 return 0;
199 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
200 return 0;
201 return 1;
202}
203
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000204/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000205 * Exports an IV from the CipherContext required to export the key
206 * state back from the unprivileged child to the privileged parent
207 * process.
208 */
209
210void
211packet_get_keyiv(int mode, u_char *iv, u_int len)
212{
213 CipherContext *cc;
214
215 if (mode == MODE_OUT)
216 cc = &send_context;
217 else
218 cc = &receive_context;
219
220 cipher_get_keyiv(cc, iv, len);
221}
222
223int
224packet_get_keycontext(int mode, u_char *dat)
225{
226 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000227
Ben Lindstromf6027d32002-03-22 01:42:04 +0000228 if (mode == MODE_OUT)
229 cc = &send_context;
230 else
231 cc = &receive_context;
232
233 return (cipher_get_keycontext(cc, dat));
234}
235
236void
237packet_set_keycontext(int mode, u_char *dat)
238{
239 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000240
Ben Lindstromf6027d32002-03-22 01:42:04 +0000241 if (mode == MODE_OUT)
242 cc = &send_context;
243 else
244 cc = &receive_context;
245
246 cipher_set_keycontext(cc, dat);
247}
248
249int
250packet_get_keyiv_len(int mode)
251{
252 CipherContext *cc;
253
254 if (mode == MODE_OUT)
255 cc = &send_context;
256 else
257 cc = &receive_context;
258
259 return (cipher_get_keyiv_len(cc));
260}
Damien Miller4f7becb2006-03-26 14:10:14 +1100261
Ben Lindstromf6027d32002-03-22 01:42:04 +0000262void
263packet_set_iv(int mode, u_char *dat)
264{
265 CipherContext *cc;
266
267 if (mode == MODE_OUT)
268 cc = &send_context;
269 else
270 cc = &receive_context;
271
272 cipher_set_keyiv(cc, dat);
273}
Damien Miller4f7becb2006-03-26 14:10:14 +1100274
Ben Lindstromf6027d32002-03-22 01:42:04 +0000275int
Damien Miller2b92d322003-06-11 22:05:06 +1000276packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000277{
278 return (cipher_get_number(receive_context.cipher));
279}
280
Damien Millera5539d22003-04-09 20:50:06 +1000281void
282packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000283{
Damien Millera5539d22003-04-09 20:50:06 +1000284 struct packet_state *state;
285
286 state = (mode == MODE_IN) ? &p_read : &p_send;
287 *seqnr = state->seqnr;
288 *blocks = state->blocks;
289 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000290}
291
292void
Damien Millera5539d22003-04-09 20:50:06 +1000293packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000294{
Damien Millera5539d22003-04-09 20:50:06 +1000295 struct packet_state *state;
296
297 state = (mode == MODE_IN) ? &p_read : &p_send;
298 state->seqnr = seqnr;
299 state->blocks = blocks;
300 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000301}
302
Damien Miller34132e52000-01-14 15:45:46 +1100303/* returns 1 if connection is via ipv4 */
304
305int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000306packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100307{
308 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100309 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100310
311 memset(&to, 0, sizeof(to));
312 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
313 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000314 if (to.ss_family == AF_INET)
315 return 1;
316#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100317 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000318 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
319 return 1;
320#endif
321 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100322}
323
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324/* Sets the connection into non-blocking mode. */
325
326void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000327packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328{
Damien Miller95def091999-11-25 00:26:21 +1100329 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000330 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Damien Miller232711f2004-06-15 10:35:30 +1000332 if (connection_out != connection_in)
333 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334}
335
336/* Returns the socket used for reading. */
337
338int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000339packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340{
Damien Miller95def091999-11-25 00:26:21 +1100341 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342}
343
344/* Returns the descriptor used for writing. */
345
346int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000347packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348{
Damien Miller95def091999-11-25 00:26:21 +1100349 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350}
351
352/* Closes the connection and clears and frees internal data structures. */
353
354void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000355packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356{
Damien Miller95def091999-11-25 00:26:21 +1100357 if (!initialized)
358 return;
359 initialized = 0;
360 if (connection_in == connection_out) {
361 shutdown(connection_out, SHUT_RDWR);
362 close(connection_out);
363 } else {
364 close(connection_in);
365 close(connection_out);
366 }
367 buffer_free(&input);
368 buffer_free(&output);
369 buffer_free(&outgoing_packet);
370 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000371 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100372 buffer_free(&compression_buffer);
373 buffer_compress_uninit();
374 }
Damien Miller963f6b22002-02-19 15:21:23 +1100375 cipher_cleanup(&send_context);
376 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377}
378
379/* Sets remote side protocol flags. */
380
381void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000382packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383{
Damien Miller95def091999-11-25 00:26:21 +1100384 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385}
386
387/* Returns the remote protocol flags set earlier by the above function. */
388
Ben Lindstrom46c16222000-12-22 01:43:59 +0000389u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000390packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391{
Damien Miller95def091999-11-25 00:26:21 +1100392 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393}
394
Damien Miller5428f641999-11-25 11:54:57 +1100395/*
396 * Starts packet compression from the next packet on in both directions.
397 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
398 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399
Ben Lindstrombba81212001-06-25 05:01:22 +0000400static void
401packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000402{
403 if (compression_buffer_ready == 1)
404 return;
405 compression_buffer_ready = 1;
406 buffer_init(&compression_buffer);
407}
408
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409void
410packet_start_compression(int level)
411{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000412 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100413 fatal("Compression already enabled.");
414 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000415 packet_init_compression();
416 buffer_compress_init_send(level);
417 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418}
419
Damien Miller5428f641999-11-25 11:54:57 +1100420/*
Damien Miller5428f641999-11-25 11:54:57 +1100421 * Causes any further packets to be encrypted using the given key. The same
422 * key is used for both sending and reception. However, both directions are
423 * encrypted independently of each other.
424 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000425
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000427packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100428 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429{
Damien Miller874d77b2000-10-14 16:23:11 +1100430 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000431
Damien Miller874d77b2000-10-14 16:23:11 +1100432 if (cipher == NULL)
433 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000434 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100435 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000436 if (keylen > SSH_SESSION_KEY_LENGTH)
437 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
438 memcpy(ssh1_key, key, keylen);
439 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100440 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
441 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442}
443
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000444u_int
445packet_get_encryption_key(u_char *key)
446{
447 if (key == NULL)
448 return (ssh1_keylen);
449 memcpy(key, ssh1_key, ssh1_keylen);
450 return (ssh1_keylen);
451}
452
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000453/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000455packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000457 u_char buf[9];
458 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Ben Lindstrom204e4882001-03-05 06:47:00 +0000460 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000461 len = compat20 ? 6 : 9;
462 memset(buf, 0, len - 1);
463 buf[len - 1] = type;
464 buffer_clear(&outgoing_packet);
465 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000466}
467
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000468/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469void
470packet_put_char(int value)
471{
Damien Miller95def091999-11-25 00:26:21 +1100472 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000473
Damien Miller95def091999-11-25 00:26:21 +1100474 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
Damien Miller4f7becb2006-03-26 14:10:14 +1100476
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000478packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479{
Damien Miller95def091999-11-25 00:26:21 +1100480 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481}
Damien Miller4f7becb2006-03-26 14:10:14 +1100482
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100484packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485{
Damien Miller95def091999-11-25 00:26:21 +1100486 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487}
Damien Miller4f7becb2006-03-26 14:10:14 +1100488
Damien Miller33b13562000-04-04 14:38:59 +1000489void
490packet_put_cstring(const char *str)
491{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000492 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000493}
Damien Miller4f7becb2006-03-26 14:10:14 +1100494
Damien Miller33b13562000-04-04 14:38:59 +1000495void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100496packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000497{
498 buffer_append(&outgoing_packet, buf, len);
499}
Damien Miller4f7becb2006-03-26 14:10:14 +1100500
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501void
Damien Miller95def091999-11-25 00:26:21 +1100502packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503{
Damien Miller95def091999-11-25 00:26:21 +1100504 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505}
Damien Miller4f7becb2006-03-26 14:10:14 +1100506
Damien Miller33b13562000-04-04 14:38:59 +1000507void
508packet_put_bignum2(BIGNUM * value)
509{
510 buffer_put_bignum2(&outgoing_packet, value);
511}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Damien Miller5428f641999-11-25 11:54:57 +1100513/*
514 * Finalizes and sends the packet. If the encryption key has been set,
515 * encrypts the packet before sending.
516 */
Damien Miller95def091999-11-25 00:26:21 +1100517
Ben Lindstrombba81212001-06-25 05:01:22 +0000518static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000519packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000521 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100522 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000523 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000524 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525
Damien Miller5428f641999-11-25 11:54:57 +1100526 /*
527 * If using packet compression, compress the payload of the outgoing
528 * packet.
529 */
Damien Miller95def091999-11-25 00:26:21 +1100530 if (packet_compression) {
531 buffer_clear(&compression_buffer);
532 /* Skip padding. */
533 buffer_consume(&outgoing_packet, 8);
534 /* padding */
535 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
536 buffer_compress(&outgoing_packet, &compression_buffer);
537 buffer_clear(&outgoing_packet);
538 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100539 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100540 }
541 /* Compute packet length without padding (add checksum, remove padding). */
542 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543
Damien Millere247cc42000-05-07 12:03:14 +1000544 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100545 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100546 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100547 cp = buffer_ptr(&outgoing_packet);
548 for (i = 0; i < padding; i++) {
549 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000550 rnd = arc4random();
551 cp[7 - i] = rnd & 0xff;
552 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100553 }
554 }
555 buffer_consume(&outgoing_packet, 8 - padding);
556
557 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100558 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000559 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100560 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100561 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562
563#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100564 fprintf(stderr, "packet_send plain: ");
565 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566#endif
567
Damien Miller95def091999-11-25 00:26:21 +1100568 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100569 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100570 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100571 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100572 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100573 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100574
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100576 fprintf(stderr, "encrypted: ");
577 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578#endif
579
Damien Miller95def091999-11-25 00:26:21 +1100580 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581
Damien Miller5428f641999-11-25 11:54:57 +1100582 /*
Damien Miller788f2122005-11-05 15:14:59 +1100583 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100584 * actually sent until packet_write_wait or packet_write_poll is
585 * called.
586 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587}
588
Ben Lindstromf6027d32002-03-22 01:42:04 +0000589void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000590set_newkeys(int mode)
591{
592 Enc *enc;
593 Mac *mac;
594 Comp *comp;
595 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000596 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000597 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000598
Ben Lindstrom064496f2002-12-23 02:04:22 +0000599 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000600
Damien Miller963f6b22002-02-19 15:21:23 +1100601 if (mode == MODE_OUT) {
602 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000603 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000604 p_send.packets = p_send.blocks = 0;
605 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100606 } else {
607 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000608 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000609 p_read.packets = p_read.blocks = 0;
610 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100611 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000612 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000613 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100614 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000615 enc = &newkeys[mode]->enc;
616 mac = &newkeys[mode]->mac;
617 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000618 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000619 xfree(enc->name);
620 xfree(enc->iv);
621 xfree(enc->key);
622 xfree(mac->name);
623 xfree(mac->key);
624 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000625 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000626 }
627 newkeys[mode] = kex_get_newkeys(mode);
628 if (newkeys[mode] == NULL)
629 fatal("newkeys: no keys for mode %d", mode);
630 enc = &newkeys[mode]->enc;
631 mac = &newkeys[mode]->mac;
632 comp = &newkeys[mode]->comp;
633 if (mac->md != NULL)
634 mac->enabled = 1;
635 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100636 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000637 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000638 /* Deleting the keys does not gain extra security */
639 /* memset(enc->iv, 0, enc->block_size);
640 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000641 if ((comp->type == COMP_ZLIB ||
642 (comp->type == COMP_DELAYED && after_authentication)) &&
643 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000644 packet_init_compression();
645 if (mode == MODE_OUT)
646 buffer_compress_init_send(6);
647 else
648 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000649 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000650 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000651 /*
652 * The 2^(blocksize*2) limit is too expensive for 3DES,
653 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
654 */
655 if (enc->block_size >= 16)
656 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
657 else
658 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000659 if (rekey_limit)
660 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000661}
662
Damien Miller5428f641999-11-25 11:54:57 +1100663/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000664 * Delayed compression for SSH2 is enabled after authentication:
665 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
666 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
667 */
668static void
669packet_enable_delayed_compress(void)
670{
671 Comp *comp = NULL;
672 int mode;
673
674 /*
675 * Remember that we are past the authentication step, so rekeying
676 * with COMP_DELAYED will turn on compression immediately.
677 */
678 after_authentication = 1;
679 for (mode = 0; mode < MODE_MAX; mode++) {
680 comp = &newkeys[mode]->comp;
681 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000682 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000683 if (mode == MODE_OUT)
684 buffer_compress_init_send(6);
685 else
686 buffer_compress_init_recv();
687 comp->enabled = 1;
688 }
689 }
690}
691
692/*
Damien Miller33b13562000-04-04 14:38:59 +1000693 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
694 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000695static void
Damien Millera5539d22003-04-09 20:50:06 +1000696packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000697{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000698 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100699 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000700 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100701 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000702 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000703 Enc *enc = NULL;
704 Mac *mac = NULL;
705 Comp *comp = NULL;
706 int block_size;
707
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000708 if (newkeys[MODE_OUT] != NULL) {
709 enc = &newkeys[MODE_OUT]->enc;
710 mac = &newkeys[MODE_OUT]->mac;
711 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000712 }
Damien Miller963f6b22002-02-19 15:21:23 +1100713 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000714
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000715 cp = buffer_ptr(&outgoing_packet);
716 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000717
718#ifdef PACKET_DEBUG
719 fprintf(stderr, "plain: ");
720 buffer_dump(&outgoing_packet);
721#endif
722
723 if (comp && comp->enabled) {
724 len = buffer_len(&outgoing_packet);
725 /* skip header, compress only payload */
726 buffer_consume(&outgoing_packet, 5);
727 buffer_clear(&compression_buffer);
728 buffer_compress(&outgoing_packet, &compression_buffer);
729 buffer_clear(&outgoing_packet);
730 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
731 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
732 buffer_len(&compression_buffer));
733 DBG(debug("compression: raw %d compressed %d", len,
734 buffer_len(&outgoing_packet)));
735 }
736
737 /* sizeof (packet_len + pad_len + payload) */
738 len = buffer_len(&outgoing_packet);
739
740 /*
741 * calc size of padding, alloc space, get random data,
742 * minimum padding is 4 bytes
743 */
744 padlen = block_size - (len % block_size);
745 if (padlen < 4)
746 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100747 if (extra_pad) {
748 /* will wrap if extra_pad+padlen > 255 */
749 extra_pad = roundup(extra_pad, block_size);
750 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000751 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100752 pad, len, padlen, extra_pad);
753 padlen += pad;
754 extra_pad = 0;
755 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100756 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100757 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000758 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000759 for (i = 0; i < padlen; i++) {
760 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000761 rnd = arc4random();
762 cp[i] = rnd & 0xff;
763 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000764 }
Damien Millere247cc42000-05-07 12:03:14 +1000765 } else {
766 /* clear padding */
767 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000768 }
769 /* packet_length includes payload, padding and padding length field */
770 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000771 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100772 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000773 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000774 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
775
776 /* compute MAC over seqnr and packet(length fields, payload, padding) */
777 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000778 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100779 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000780 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000781 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000782 }
783 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100784 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100785 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000786 buffer_len(&outgoing_packet));
787 /* append unencrypted MAC */
788 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100789 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000790#ifdef PACKET_DEBUG
791 fprintf(stderr, "encrypted: ");
792 buffer_dump(&output);
793#endif
Damien Miller4af51302000-04-16 11:18:38 +1000794 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000795 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000796 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000797 if (++p_send.packets == 0)
798 if (!(datafellows & SSH_BUG_NOREKEY))
799 fatal("XXX too many packets with same key");
800 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000801 buffer_clear(&outgoing_packet);
802
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000803 if (type == SSH2_MSG_NEWKEYS)
804 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000805 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
806 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000807}
808
Damien Millera5539d22003-04-09 20:50:06 +1000809static void
810packet_send2(void)
811{
812 static int rekeying = 0;
813 struct packet *p;
814 u_char type, *cp;
815
816 cp = buffer_ptr(&outgoing_packet);
817 type = cp[5];
818
819 /* during rekeying we can only send key exchange messages */
820 if (rekeying) {
821 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
822 (type <= SSH2_MSG_TRANSPORT_MAX))) {
823 debug("enqueue packet: %u", type);
824 p = xmalloc(sizeof(*p));
825 p->type = type;
826 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
827 buffer_init(&outgoing_packet);
828 TAILQ_INSERT_TAIL(&outgoing, p, next);
829 return;
830 }
831 }
832
833 /* rekeying starts with sending KEXINIT */
834 if (type == SSH2_MSG_KEXINIT)
835 rekeying = 1;
836
837 packet_send2_wrapped();
838
839 /* after a NEWKEYS message we can send the complete queue */
840 if (type == SSH2_MSG_NEWKEYS) {
841 rekeying = 0;
842 while ((p = TAILQ_FIRST(&outgoing))) {
843 type = p->type;
844 debug("dequeue packet: %u", type);
845 buffer_free(&outgoing_packet);
846 memcpy(&outgoing_packet, &p->payload,
847 sizeof(Buffer));
848 TAILQ_REMOVE(&outgoing, p, next);
849 xfree(p);
850 packet_send2_wrapped();
851 }
852 }
853}
854
Damien Miller33b13562000-04-04 14:38:59 +1000855void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000856packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000857{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000858 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000859 packet_send2();
860 else
861 packet_send1();
862 DBG(debug("packet_send done"));
863}
864
Damien Miller33b13562000-04-04 14:38:59 +1000865/*
Damien Miller5428f641999-11-25 11:54:57 +1100866 * Waits until a packet has been received, and returns its type. Note that
867 * no other data is processed until this returns, so this function should not
868 * be used during the interactive session.
869 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870
871int
Damien Millerdff50992002-01-22 23:16:32 +1100872packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000873{
Damien Miller95def091999-11-25 00:26:21 +1100874 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000875 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100876 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000877 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878
Damien Miller07d86be2006-03-26 14:19:21 +1100879 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000880 sizeof(fd_mask));
881
Damien Miller95def091999-11-25 00:26:21 +1100882 /* Since we are blocking, ensure that all written packets have been sent. */
883 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884
Damien Miller95def091999-11-25 00:26:21 +1100885 /* Stay in the loop until we have received a complete packet. */
886 for (;;) {
887 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100888 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000889 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000890 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100891 || type == SSH_SMSG_FAILURE
892 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000893 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100894 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100895 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000896 if (type != SSH_MSG_NONE) {
897 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100898 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000899 }
Damien Miller5428f641999-11-25 11:54:57 +1100900 /*
901 * Otherwise, wait for some data to arrive, add it to the
902 * buffer, and try again.
903 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000904 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
905 sizeof(fd_mask));
906 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100907
Damien Miller95def091999-11-25 00:26:21 +1100908 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000909 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000910 (errno == EAGAIN || errno == EINTR))
911 ;
Damien Miller5428f641999-11-25 11:54:57 +1100912
Damien Miller95def091999-11-25 00:26:21 +1100913 /* Read data from the socket. */
914 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100915 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000916 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000917 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100918 }
Damien Miller95def091999-11-25 00:26:21 +1100919 if (len < 0)
920 fatal("Read from socket failed: %.100s", strerror(errno));
921 /* Append it to the buffer. */
922 packet_process_incoming(buf, len);
923 }
924 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925}
926
Damien Miller278f9072001-12-21 15:00:19 +1100927int
Damien Millerdff50992002-01-22 23:16:32 +1100928packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100929{
Damien Millerdff50992002-01-22 23:16:32 +1100930 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100931}
932
Damien Miller5428f641999-11-25 11:54:57 +1100933/*
934 * Waits until a packet has been received, verifies that its type matches
935 * that given, and gives a fatal error and exits if there is a mismatch.
936 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937
938void
Damien Millerdff50992002-01-22 23:16:32 +1100939packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940{
Damien Miller95def091999-11-25 00:26:21 +1100941 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942
Damien Millerdff50992002-01-22 23:16:32 +1100943 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100944 if (type != expected_type)
945 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000946 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947}
948
949/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100950 * packet_process_incoming. If so, reads the packet; otherwise returns
951 * SSH_MSG_NONE. This does not wait for data from the connection.
952 *
953 * SSH_MSG_DISCONNECT is handled specially here. Also,
954 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
955 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100956 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
Ben Lindstrombba81212001-06-25 05:01:22 +0000958static int
Damien Millerdff50992002-01-22 23:16:32 +1100959packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000961 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000962 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000963 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964
Damien Miller95def091999-11-25 00:26:21 +1100965 /* Check if input size is less than minimum packet size. */
966 if (buffer_len(&input) < 4 + 8)
967 return SSH_MSG_NONE;
968 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000969 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100970 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100971 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000972 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100973 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974
Damien Miller95def091999-11-25 00:26:21 +1100975 /* Check if the packet has been entirely received. */
976 if (buffer_len(&input) < 4 + padded_len)
977 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
Damien Miller95def091999-11-25 00:26:21 +1100981 /* Consume packet length. */
982 buffer_consume(&input, 4);
983
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000984 /*
985 * Cryptographic attack detector for ssh
986 * (C)1998 CORE-SDI, Buenos Aires Argentina
987 * Ariel Futoransky(futo@core-sdi.com)
988 */
Damien Miller963f6b22002-02-19 15:21:23 +1100989 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +1100990 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000991 packet_disconnect("crc32 compensation attack: network attack detected");
992
993 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100994 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100995 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100996 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000997
Damien Miller95def091999-11-25 00:26:21 +1100998 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000999
1000#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001001 fprintf(stderr, "read_poll plain: ");
1002 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004
Damien Miller95def091999-11-25 00:26:21 +11001005 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001006 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001007 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Damien Miller95def091999-11-25 00:26:21 +11001009 /* Skip padding. */
1010 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001011
Damien Miller95def091999-11-25 00:26:21 +11001012 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001013 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001014 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001015 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001017 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001018 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001019 if (checksum != stored_checksum)
1020 packet_disconnect("Corrupted check bytes on input.");
1021 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001022
Damien Miller95def091999-11-25 00:26:21 +11001023 if (packet_compression) {
1024 buffer_clear(&compression_buffer);
1025 buffer_uncompress(&incoming_packet, &compression_buffer);
1026 buffer_clear(&incoming_packet);
1027 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001028 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001029 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001030 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001031 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1032 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001033 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001034}
Damien Miller95def091999-11-25 00:26:21 +11001035
Ben Lindstrombba81212001-06-25 05:01:22 +00001036static int
Damien Millerdff50992002-01-22 23:16:32 +11001037packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001038{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001039 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001040 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001041 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001042 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001043 Enc *enc = NULL;
1044 Mac *mac = NULL;
1045 Comp *comp = NULL;
1046
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001047 if (newkeys[MODE_IN] != NULL) {
1048 enc = &newkeys[MODE_IN]->enc;
1049 mac = &newkeys[MODE_IN]->mac;
1050 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001051 }
1052 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001053 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001054
1055 if (packet_length == 0) {
1056 /*
1057 * check if input size is less than the cipher block size,
1058 * decrypt first block and extract length of incoming packet
1059 */
1060 if (buffer_len(&input) < block_size)
1061 return SSH_MSG_NONE;
1062 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001063 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001064 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001065 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001066 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001067 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001068 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001069#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001070 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001071#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001072 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001073 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001074 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001075 buffer_consume(&input, block_size);
1076 }
1077 /* we have a partial packet of block_size bytes */
1078 need = 4 + packet_length - block_size;
1079 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1080 need, maclen));
1081 if (need % block_size != 0)
1082 fatal("padding error: need %d block %d mod %d",
1083 need, block_size, need % block_size);
1084 /*
1085 * check if the entire packet has been received and
1086 * decrypt into incoming_packet
1087 */
1088 if (buffer_len(&input) < need + maclen)
1089 return SSH_MSG_NONE;
1090#ifdef PACKET_DEBUG
1091 fprintf(stderr, "read_poll enc/full: ");
1092 buffer_dump(&input);
1093#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001094 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001095 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001096 buffer_consume(&input, need);
1097 /*
1098 * compute MAC over seqnr and packet,
1099 * increment sequence number for incoming packet
1100 */
Damien Miller4af51302000-04-16 11:18:38 +10001101 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001102 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001103 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001104 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001105 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001106 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001107 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001108 buffer_consume(&input, mac->mac_len);
1109 }
Damien Miller278f9072001-12-21 15:00:19 +11001110 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001111 *seqnr_p = p_read.seqnr;
1112 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001113 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001114 if (++p_read.packets == 0)
1115 if (!(datafellows & SSH_BUG_NOREKEY))
1116 fatal("XXX too many packets with same key");
1117 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001118
1119 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001120 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001121 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001122 DBG(debug("input: padlen %d", padlen));
1123 if (padlen < 4)
1124 packet_disconnect("Corrupted padlen %d on input.", padlen);
1125
1126 /* skip packet size + padlen, discard padding */
1127 buffer_consume(&incoming_packet, 4 + 1);
1128 buffer_consume_end(&incoming_packet, padlen);
1129
1130 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1131 if (comp && comp->enabled) {
1132 buffer_clear(&compression_buffer);
1133 buffer_uncompress(&incoming_packet, &compression_buffer);
1134 buffer_clear(&incoming_packet);
1135 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1136 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001137 DBG(debug("input: len after de-compress %d",
1138 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001139 }
1140 /*
1141 * get packet type, implies consume.
1142 * return length of payload (without type field)
1143 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001144 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001145 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1146 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001147 if (type == SSH2_MSG_NEWKEYS)
1148 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001149 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1150 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001151#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001152 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001153 buffer_dump(&incoming_packet);
1154#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001155 /* reset for next packet */
1156 packet_length = 0;
1157 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001158}
1159
1160int
Damien Millerdff50992002-01-22 23:16:32 +11001161packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001162{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001163 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001164 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001165 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001166
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001167 for (;;) {
1168 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001169 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001170 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001171 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001172 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001173 case SSH2_MSG_IGNORE:
1174 break;
1175 case SSH2_MSG_DEBUG:
1176 packet_get_char();
1177 msg = packet_get_string(NULL);
1178 debug("Remote: %.900s", msg);
1179 xfree(msg);
1180 msg = packet_get_string(NULL);
1181 xfree(msg);
1182 break;
1183 case SSH2_MSG_DISCONNECT:
1184 reason = packet_get_int();
1185 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001186 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001187 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001188 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001189 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001190 break;
Damien Miller659811f2002-01-22 23:23:11 +11001191 case SSH2_MSG_UNIMPLEMENTED:
1192 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001193 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1194 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001195 break;
Damien Miller33b13562000-04-04 14:38:59 +10001196 default:
1197 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001198 }
Damien Miller33b13562000-04-04 14:38:59 +10001199 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001200 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001201 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001202 case SSH_MSG_IGNORE:
1203 break;
1204 case SSH_MSG_DEBUG:
1205 msg = packet_get_string(NULL);
1206 debug("Remote: %.900s", msg);
1207 xfree(msg);
1208 break;
1209 case SSH_MSG_DISCONNECT:
1210 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001211 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001212 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001213 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001214 xfree(msg);
1215 break;
1216 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001217 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001218 DBG(debug("received packet type %d", type));
1219 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001220 }
Damien Miller33b13562000-04-04 14:38:59 +10001221 }
1222 }
1223}
1224
Damien Miller278f9072001-12-21 15:00:19 +11001225int
Damien Millerdff50992002-01-22 23:16:32 +11001226packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001227{
Damien Millerdff50992002-01-22 23:16:32 +11001228 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001229}
1230
Damien Miller5428f641999-11-25 11:54:57 +11001231/*
1232 * Buffers the given amount of input characters. This is intended to be used
1233 * together with packet_read_poll.
1234 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001235
1236void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001237packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001238{
Damien Miller95def091999-11-25 00:26:21 +11001239 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240}
1241
1242/* Returns a character from the packet. */
1243
Ben Lindstrom46c16222000-12-22 01:43:59 +00001244u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001245packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246{
Damien Miller95def091999-11-25 00:26:21 +11001247 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001248
Damien Miller95def091999-11-25 00:26:21 +11001249 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001250 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251}
1252
1253/* Returns an integer from the packet data. */
1254
Ben Lindstrom46c16222000-12-22 01:43:59 +00001255u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001256packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001257{
Damien Miller95def091999-11-25 00:26:21 +11001258 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259}
1260
Damien Miller5428f641999-11-25 11:54:57 +11001261/*
1262 * Returns an arbitrary precision integer from the packet data. The integer
1263 * must have been initialized before this call.
1264 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001265
1266void
Damien Millerd432ccf2002-01-22 23:14:44 +11001267packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268{
Damien Miller76e1e362002-01-22 23:15:57 +11001269 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270}
1271
Damien Miller33b13562000-04-04 14:38:59 +10001272void
Damien Millerd432ccf2002-01-22 23:14:44 +11001273packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001274{
Damien Miller76e1e362002-01-22 23:15:57 +11001275 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001276}
1277
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001278void *
Damien Millereccb9de2005-06-17 12:59:34 +10001279packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001280{
Damien Millereccb9de2005-06-17 12:59:34 +10001281 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001282
Damien Miller33b13562000-04-04 14:38:59 +10001283 if (length_ptr != NULL)
1284 *length_ptr = bytes;
1285 return buffer_ptr(&incoming_packet);
1286}
1287
Damien Miller4af51302000-04-16 11:18:38 +10001288int
1289packet_remaining(void)
1290{
1291 return buffer_len(&incoming_packet);
1292}
1293
Damien Miller5428f641999-11-25 11:54:57 +11001294/*
1295 * Returns a string from the packet data. The string is allocated using
1296 * xmalloc; it is the responsibility of the calling program to free it when
1297 * no longer needed. The length_ptr argument may be NULL, or point to an
1298 * integer into which the length of the string is stored.
1299 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001300
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001301void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001302packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001303{
Damien Miller95def091999-11-25 00:26:21 +11001304 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001305}
1306
Damien Miller5428f641999-11-25 11:54:57 +11001307/*
1308 * Sends a diagnostic message from the server to the client. This message
1309 * can be sent at any time (but not while constructing another message). The
1310 * message is printed immediately, but only if the client is being executed
1311 * in verbose mode. These messages are primarily intended to ease debugging
1312 * authentication problems. The length of the formatted message must not
1313 * exceed 1024 bytes. This will automatically call packet_write_wait.
1314 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001315
1316void
Damien Miller95def091999-11-25 00:26:21 +11001317packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318{
Damien Miller95def091999-11-25 00:26:21 +11001319 char buf[1024];
1320 va_list args;
1321
Ben Lindstroma14ee472000-12-07 01:24:58 +00001322 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1323 return;
1324
Damien Miller95def091999-11-25 00:26:21 +11001325 va_start(args, fmt);
1326 vsnprintf(buf, sizeof(buf), fmt, args);
1327 va_end(args);
1328
Damien Miller7c8af4f2000-05-01 08:24:07 +10001329 if (compat20) {
1330 packet_start(SSH2_MSG_DEBUG);
1331 packet_put_char(0); /* bool: always display */
1332 packet_put_cstring(buf);
1333 packet_put_cstring("");
1334 } else {
1335 packet_start(SSH_MSG_DEBUG);
1336 packet_put_cstring(buf);
1337 }
Damien Miller95def091999-11-25 00:26:21 +11001338 packet_send();
1339 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340}
1341
Damien Miller5428f641999-11-25 11:54:57 +11001342/*
1343 * Logs the error plus constructs and sends a disconnect packet, closes the
1344 * connection, and exits. This function never returns. The error message
1345 * should not contain a newline. The length of the formatted message must
1346 * not exceed 1024 bytes.
1347 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001348
1349void
Damien Miller95def091999-11-25 00:26:21 +11001350packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001351{
Damien Miller95def091999-11-25 00:26:21 +11001352 char buf[1024];
1353 va_list args;
1354 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001355
Damien Miller95def091999-11-25 00:26:21 +11001356 if (disconnecting) /* Guard against recursive invocations. */
1357 fatal("packet_disconnect called recursively.");
1358 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359
Damien Miller5428f641999-11-25 11:54:57 +11001360 /*
1361 * Format the message. Note that the caller must make sure the
1362 * message is of limited size.
1363 */
Damien Miller95def091999-11-25 00:26:21 +11001364 va_start(args, fmt);
1365 vsnprintf(buf, sizeof(buf), fmt, args);
1366 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001367
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001368 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001369 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001370
Damien Miller95def091999-11-25 00:26:21 +11001371 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001372 if (compat20) {
1373 packet_start(SSH2_MSG_DISCONNECT);
1374 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1375 packet_put_cstring(buf);
1376 packet_put_cstring("");
1377 } else {
1378 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001379 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001380 }
Damien Miller95def091999-11-25 00:26:21 +11001381 packet_send();
1382 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001383
Damien Miller95def091999-11-25 00:26:21 +11001384 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001385 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001386
Damien Miller95def091999-11-25 00:26:21 +11001387 /* Close the connection. */
1388 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001389 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390}
1391
Damien Miller5428f641999-11-25 11:54:57 +11001392/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393
1394void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001395packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396{
Damien Miller95def091999-11-25 00:26:21 +11001397 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001398
Damien Miller95def091999-11-25 00:26:21 +11001399 if (len > 0) {
1400 len = write(connection_out, buffer_ptr(&output), len);
1401 if (len <= 0) {
1402 if (errno == EAGAIN)
1403 return;
1404 else
1405 fatal("Write failed: %.100s", strerror(errno));
1406 }
1407 buffer_consume(&output, len);
1408 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001409}
1410
Damien Miller5428f641999-11-25 11:54:57 +11001411/*
1412 * Calls packet_write_poll repeatedly until all pending output data has been
1413 * written.
1414 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001415
1416void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001417packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001419 fd_set *setp;
1420
Damien Miller07d86be2006-03-26 14:19:21 +11001421 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001422 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001423 packet_write_poll();
1424 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001425 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1426 sizeof(fd_mask));
1427 FD_SET(connection_out, setp);
1428 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001429 (errno == EAGAIN || errno == EINTR))
1430 ;
Damien Miller95def091999-11-25 00:26:21 +11001431 packet_write_poll();
1432 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001433 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434}
1435
1436/* Returns true if there is buffered data to write to the connection. */
1437
1438int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001439packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001440{
Damien Miller95def091999-11-25 00:26:21 +11001441 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001442}
1443
1444/* Returns true if there is not too much data to write to the connection. */
1445
1446int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001447packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001448{
Damien Miller95def091999-11-25 00:26:21 +11001449 if (interactive_mode)
1450 return buffer_len(&output) < 16384;
1451 else
1452 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453}
1454
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001455
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001456static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001457packet_set_tos(int interactive)
1458{
Damien Miller5924ceb2003-11-22 15:02:42 +11001459#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001460 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1461
1462 if (!packet_connection_is_on_socket() ||
1463 !packet_connection_is_ipv4())
1464 return;
1465 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1466 sizeof(tos)) < 0)
1467 error("setsockopt IP_TOS %d: %.100s:",
1468 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001469#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001470}
Ben Lindstroma7433982002-12-23 02:41:41 +00001471
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472/* Informs that the current session is interactive. Sets IP flags for that. */
1473
1474void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001475packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001477 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001478
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001479 if (called)
1480 return;
1481 called = 1;
1482
Damien Miller95def091999-11-25 00:26:21 +11001483 /* Record that we are in interactive mode. */
1484 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485
Damien Miller34132e52000-01-14 15:45:46 +11001486 /* Only set socket options if using a socket. */
1487 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001488 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001489 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001490 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491}
1492
1493/* Returns true if the current connection is interactive. */
1494
1495int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001496packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497{
Damien Miller95def091999-11-25 00:26:21 +11001498 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499}
Damien Miller6162d121999-11-21 13:23:52 +11001500
Darren Tucker1f8311c2004-05-13 16:39:33 +10001501int
Darren Tucker502d3842003-06-28 12:38:01 +10001502packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001503{
Damien Miller95def091999-11-25 00:26:21 +11001504 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001505
Damien Miller95def091999-11-25 00:26:21 +11001506 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001507 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001508 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001509 return -1;
1510 }
1511 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001512 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001513 return -1;
1514 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001515 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001516 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001517 max_packet_size = s;
1518 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001519}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001520
Damien Miller9f643902001-11-12 11:02:52 +11001521/* roundup current message to pad bytes */
1522void
1523packet_add_padding(u_char pad)
1524{
1525 extra_pad = pad;
1526}
1527
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001528/*
1529 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001530 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001531 * byte SSH_MSG_IGNORE
1532 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001533 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001534 * All implementations MUST understand (and ignore) this message at any
1535 * time (after receiving the protocol version). No implementation is
1536 * required to send them. This message can be used as an additional
1537 * protection measure against advanced traffic analysis techniques.
1538 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001539void
1540packet_send_ignore(int nbytes)
1541{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001542 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001543 int i;
1544
1545 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001546 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001547 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001548 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001549 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001550 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001551 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001552 }
1553}
Damien Millera5539d22003-04-09 20:50:06 +10001554
Darren Tucker1f8311c2004-05-13 16:39:33 +10001555#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001556int
1557packet_need_rekeying(void)
1558{
1559 if (datafellows & SSH_BUG_NOREKEY)
1560 return 0;
1561 return
1562 (p_send.packets > MAX_PACKETS) ||
1563 (p_read.packets > MAX_PACKETS) ||
1564 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1565 (max_blocks_in && (p_read.blocks > max_blocks_in));
1566}
1567
1568void
1569packet_set_rekey_limit(u_int32_t bytes)
1570{
1571 rekey_limit = bytes;
1572}
Damien Miller9786e6e2005-07-26 21:54:56 +10001573
1574void
1575packet_set_server(void)
1576{
1577 server_side = 1;
1578}
1579
1580void
1581packet_set_authenticated(void)
1582{
1583 after_authentication = 1;
1584}