blob: fe3eea0944fa6c286bfb8c2b279d945ae8631c89 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains code implementing the packet protocol and communication
6 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100013 *
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000016 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Darren Tucker1f8311c2004-05-13 16:39:33 +100040RCSID("$OpenBSD: packet.c,v 1.113 2004/05/11 19:01:43 deraadt Exp $");
Damien Millera5539d22003-04-09 20:50:06 +100041
Damien Millera0898b82003-04-09 21:05:52 +100042#include "openbsd-compat/sys-queue.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include "xmalloc.h"
45#include "buffer.h"
46#include "packet.h"
47#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "getput.h"
50
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 Miller33b13562000-04-04 14:38:59 +1000119/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000120Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000121static struct packet_state {
122 u_int32_t seqnr;
123 u_int32_t packets;
124 u_int64_t blocks;
125} p_read, p_send;
126
127static u_int64_t max_blocks_in, max_blocks_out;
128static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000129
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000130/* Session key for protocol v1 */
131static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
132static u_int ssh1_keylen;
133
Damien Miller9f643902001-11-12 11:02:52 +1100134/* roundup current message to extra_pad bytes */
135static u_char extra_pad = 0;
136
Damien Millera5539d22003-04-09 20:50:06 +1000137struct packet {
138 TAILQ_ENTRY(packet) next;
139 u_char type;
140 Buffer payload;
141};
142TAILQ_HEAD(, packet) outgoing;
143
Damien Miller5428f641999-11-25 11:54:57 +1100144/*
145 * Sets the descriptors used for communication. Disables encryption until
146 * packet_set_encryption_key is called.
147 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148void
149packet_set_connection(int fd_in, int fd_out)
150{
Damien Miller874d77b2000-10-14 16:23:11 +1100151 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000152
Damien Miller874d77b2000-10-14 16:23:11 +1100153 if (none == NULL)
154 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100155 connection_in = fd_in;
156 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000157 cipher_init(&send_context, none, (const u_char *)"",
158 0, NULL, 0, CIPHER_ENCRYPT);
159 cipher_init(&receive_context, none, (const u_char *)"",
160 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000161 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100162 if (!initialized) {
163 initialized = 1;
164 buffer_init(&input);
165 buffer_init(&output);
166 buffer_init(&outgoing_packet);
167 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000168 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100169 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170}
171
Damien Miller34132e52000-01-14 15:45:46 +1100172/* Returns 1 if remote host is connected via socket, 0 if not. */
173
174int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000175packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100176{
177 struct sockaddr_storage from, to;
178 socklen_t fromlen, tolen;
179
180 /* filedescriptors in and out are the same, so it's a socket */
181 if (connection_in == connection_out)
182 return 1;
183 fromlen = sizeof(from);
184 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100185 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100186 return 0;
187 tolen = sizeof(to);
188 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100189 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100190 return 0;
191 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
192 return 0;
193 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
194 return 0;
195 return 1;
196}
197
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000198/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000199 * Exports an IV from the CipherContext required to export the key
200 * state back from the unprivileged child to the privileged parent
201 * process.
202 */
203
204void
205packet_get_keyiv(int mode, u_char *iv, u_int len)
206{
207 CipherContext *cc;
208
209 if (mode == MODE_OUT)
210 cc = &send_context;
211 else
212 cc = &receive_context;
213
214 cipher_get_keyiv(cc, iv, len);
215}
216
217int
218packet_get_keycontext(int mode, u_char *dat)
219{
220 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000221
Ben Lindstromf6027d32002-03-22 01:42:04 +0000222 if (mode == MODE_OUT)
223 cc = &send_context;
224 else
225 cc = &receive_context;
226
227 return (cipher_get_keycontext(cc, dat));
228}
229
230void
231packet_set_keycontext(int mode, u_char *dat)
232{
233 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000234
Ben Lindstromf6027d32002-03-22 01:42:04 +0000235 if (mode == MODE_OUT)
236 cc = &send_context;
237 else
238 cc = &receive_context;
239
240 cipher_set_keycontext(cc, dat);
241}
242
243int
244packet_get_keyiv_len(int mode)
245{
246 CipherContext *cc;
247
248 if (mode == MODE_OUT)
249 cc = &send_context;
250 else
251 cc = &receive_context;
252
253 return (cipher_get_keyiv_len(cc));
254}
255void
256packet_set_iv(int mode, u_char *dat)
257{
258 CipherContext *cc;
259
260 if (mode == MODE_OUT)
261 cc = &send_context;
262 else
263 cc = &receive_context;
264
265 cipher_set_keyiv(cc, dat);
266}
267int
Damien Miller2b92d322003-06-11 22:05:06 +1000268packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000269{
270 return (cipher_get_number(receive_context.cipher));
271}
272
Damien Millera5539d22003-04-09 20:50:06 +1000273void
274packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000275{
Damien Millera5539d22003-04-09 20:50:06 +1000276 struct packet_state *state;
277
278 state = (mode == MODE_IN) ? &p_read : &p_send;
279 *seqnr = state->seqnr;
280 *blocks = state->blocks;
281 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000282}
283
284void
Damien Millera5539d22003-04-09 20:50:06 +1000285packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000286{
Damien Millera5539d22003-04-09 20:50:06 +1000287 struct packet_state *state;
288
289 state = (mode == MODE_IN) ? &p_read : &p_send;
290 state->seqnr = seqnr;
291 state->blocks = blocks;
292 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000293}
294
Damien Miller34132e52000-01-14 15:45:46 +1100295/* returns 1 if connection is via ipv4 */
296
297int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000298packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100299{
300 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100301 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100302
303 memset(&to, 0, sizeof(to));
304 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
305 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000306 if (to.ss_family == AF_INET)
307 return 1;
308#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100309 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000310 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
311 return 1;
312#endif
313 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100314}
315
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316/* Sets the connection into non-blocking mode. */
317
318void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000319packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320{
Damien Miller95def091999-11-25 00:26:21 +1100321 /* Set the socket into non-blocking mode. */
322 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
323 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324
Damien Miller95def091999-11-25 00:26:21 +1100325 if (connection_out != connection_in) {
326 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
327 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
328 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329}
330
331/* Returns the socket used for reading. */
332
333int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000334packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335{
Damien Miller95def091999-11-25 00:26:21 +1100336 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337}
338
339/* Returns the descriptor used for writing. */
340
341int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000342packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343{
Damien Miller95def091999-11-25 00:26:21 +1100344 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345}
346
347/* Closes the connection and clears and frees internal data structures. */
348
349void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000350packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351{
Damien Miller95def091999-11-25 00:26:21 +1100352 if (!initialized)
353 return;
354 initialized = 0;
355 if (connection_in == connection_out) {
356 shutdown(connection_out, SHUT_RDWR);
357 close(connection_out);
358 } else {
359 close(connection_in);
360 close(connection_out);
361 }
362 buffer_free(&input);
363 buffer_free(&output);
364 buffer_free(&outgoing_packet);
365 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000366 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100367 buffer_free(&compression_buffer);
368 buffer_compress_uninit();
369 }
Damien Miller963f6b22002-02-19 15:21:23 +1100370 cipher_cleanup(&send_context);
371 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372}
373
374/* Sets remote side protocol flags. */
375
376void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000377packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378{
Damien Miller95def091999-11-25 00:26:21 +1100379 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380}
381
382/* Returns the remote protocol flags set earlier by the above function. */
383
Ben Lindstrom46c16222000-12-22 01:43:59 +0000384u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000385packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386{
Damien Miller95def091999-11-25 00:26:21 +1100387 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388}
389
Damien Miller5428f641999-11-25 11:54:57 +1100390/*
391 * Starts packet compression from the next packet on in both directions.
392 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
393 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394
Ben Lindstrombba81212001-06-25 05:01:22 +0000395static void
396packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000397{
398 if (compression_buffer_ready == 1)
399 return;
400 compression_buffer_ready = 1;
401 buffer_init(&compression_buffer);
402}
403
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404void
405packet_start_compression(int level)
406{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000407 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100408 fatal("Compression already enabled.");
409 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000410 packet_init_compression();
411 buffer_compress_init_send(level);
412 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413}
414
Damien Miller5428f641999-11-25 11:54:57 +1100415/*
Damien Miller5428f641999-11-25 11:54:57 +1100416 * Causes any further packets to be encrypted using the given key. The same
417 * key is used for both sending and reception. However, both directions are
418 * encrypted independently of each other.
419 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000420
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000422packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100423 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424{
Damien Miller874d77b2000-10-14 16:23:11 +1100425 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000426
Damien Miller874d77b2000-10-14 16:23:11 +1100427 if (cipher == NULL)
428 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000429 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100430 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000431 if (keylen > SSH_SESSION_KEY_LENGTH)
432 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
433 memcpy(ssh1_key, key, keylen);
434 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100435 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
436 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437}
438
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000439u_int
440packet_get_encryption_key(u_char *key)
441{
442 if (key == NULL)
443 return (ssh1_keylen);
444 memcpy(key, ssh1_key, ssh1_keylen);
445 return (ssh1_keylen);
446}
447
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000448/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000450packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000452 u_char buf[9];
453 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454
Ben Lindstrom204e4882001-03-05 06:47:00 +0000455 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000456 len = compat20 ? 6 : 9;
457 memset(buf, 0, len - 1);
458 buf[len - 1] = type;
459 buffer_clear(&outgoing_packet);
460 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000461}
462
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000463/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464void
465packet_put_char(int value)
466{
Damien Miller95def091999-11-25 00:26:21 +1100467 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000468
Damien Miller95def091999-11-25 00:26:21 +1100469 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000472packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473{
Damien Miller95def091999-11-25 00:26:21 +1100474 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100477packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478{
Damien Miller95def091999-11-25 00:26:21 +1100479 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480}
Damien Miller33b13562000-04-04 14:38:59 +1000481void
482packet_put_cstring(const char *str)
483{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000484 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000485}
Damien Miller33b13562000-04-04 14:38:59 +1000486void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100487packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000488{
489 buffer_append(&outgoing_packet, buf, len);
490}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491void
Damien Miller95def091999-11-25 00:26:21 +1100492packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493{
Damien Miller95def091999-11-25 00:26:21 +1100494 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495}
Damien Miller33b13562000-04-04 14:38:59 +1000496void
497packet_put_bignum2(BIGNUM * value)
498{
499 buffer_put_bignum2(&outgoing_packet, value);
500}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501
Damien Miller5428f641999-11-25 11:54:57 +1100502/*
503 * Finalizes and sends the packet. If the encryption key has been set,
504 * encrypts the packet before sending.
505 */
Damien Miller95def091999-11-25 00:26:21 +1100506
Ben Lindstrombba81212001-06-25 05:01:22 +0000507static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000508packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000510 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100511 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000512 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100513 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Damien Miller5428f641999-11-25 11:54:57 +1100515 /*
516 * If using packet compression, compress the payload of the outgoing
517 * packet.
518 */
Damien Miller95def091999-11-25 00:26:21 +1100519 if (packet_compression) {
520 buffer_clear(&compression_buffer);
521 /* Skip padding. */
522 buffer_consume(&outgoing_packet, 8);
523 /* padding */
524 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
525 buffer_compress(&outgoing_packet, &compression_buffer);
526 buffer_clear(&outgoing_packet);
527 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100528 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100529 }
530 /* Compute packet length without padding (add checksum, remove padding). */
531 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532
Damien Millere247cc42000-05-07 12:03:14 +1000533 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100534 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100535 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100536 cp = buffer_ptr(&outgoing_packet);
537 for (i = 0; i < padding; i++) {
538 if (i % 4 == 0)
539 rand = arc4random();
540 cp[7 - i] = rand & 0xff;
541 rand >>= 8;
542 }
543 }
544 buffer_consume(&outgoing_packet, 8 - padding);
545
546 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100547 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000548 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100549 PUT_32BIT(buf, checksum);
550 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551
552#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100553 fprintf(stderr, "packet_send plain: ");
554 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555#endif
556
Damien Miller95def091999-11-25 00:26:21 +1100557 /* Append to output. */
558 PUT_32BIT(buf, len);
559 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100560 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100561 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100562 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100563
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100565 fprintf(stderr, "encrypted: ");
566 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567#endif
568
Damien Miller95def091999-11-25 00:26:21 +1100569 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Damien Miller5428f641999-11-25 11:54:57 +1100571 /*
572 * Note that the packet is now only buffered in output. It won\'t be
573 * actually sent until packet_write_wait or packet_write_poll is
574 * called.
575 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576}
577
Ben Lindstromf6027d32002-03-22 01:42:04 +0000578void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000579set_newkeys(int mode)
580{
581 Enc *enc;
582 Mac *mac;
583 Comp *comp;
584 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000585 u_int64_t *max_blocks;
Damien Miller963f6b22002-02-19 15:21:23 +1100586 int encrypt;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000587
Ben Lindstrom064496f2002-12-23 02:04:22 +0000588 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000589
Damien Miller963f6b22002-02-19 15:21:23 +1100590 if (mode == MODE_OUT) {
591 cc = &send_context;
592 encrypt = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000593 p_send.packets = p_send.blocks = 0;
594 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100595 } else {
596 cc = &receive_context;
597 encrypt = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000598 p_read.packets = p_read.blocks = 0;
599 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100600 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000601 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000602 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100603 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000604 enc = &newkeys[mode]->enc;
605 mac = &newkeys[mode]->mac;
606 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000607 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000608 xfree(enc->name);
609 xfree(enc->iv);
610 xfree(enc->key);
611 xfree(mac->name);
612 xfree(mac->key);
613 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000614 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000615 }
616 newkeys[mode] = kex_get_newkeys(mode);
617 if (newkeys[mode] == NULL)
618 fatal("newkeys: no keys for mode %d", mode);
619 enc = &newkeys[mode]->enc;
620 mac = &newkeys[mode]->mac;
621 comp = &newkeys[mode]->comp;
622 if (mac->md != NULL)
623 mac->enabled = 1;
624 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100625 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
626 enc->iv, enc->block_size, encrypt);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000627 /* Deleting the keys does not gain extra security */
628 /* memset(enc->iv, 0, enc->block_size);
629 memset(enc->key, 0, enc->key_len); */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000630 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000631 packet_init_compression();
632 if (mode == MODE_OUT)
633 buffer_compress_init_send(6);
634 else
635 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000636 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000637 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000638 /*
639 * The 2^(blocksize*2) limit is too expensive for 3DES,
640 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
641 */
642 if (enc->block_size >= 16)
643 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
644 else
645 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000646 if (rekey_limit)
647 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000648}
649
Damien Miller5428f641999-11-25 11:54:57 +1100650/*
Damien Miller33b13562000-04-04 14:38:59 +1000651 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
652 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000653static void
Damien Millera5539d22003-04-09 20:50:06 +1000654packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000655{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000656 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100657 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000658 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100659 u_int i, len;
Damien Miller33b13562000-04-04 14:38:59 +1000660 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000661 Enc *enc = NULL;
662 Mac *mac = NULL;
663 Comp *comp = NULL;
664 int block_size;
665
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000666 if (newkeys[MODE_OUT] != NULL) {
667 enc = &newkeys[MODE_OUT]->enc;
668 mac = &newkeys[MODE_OUT]->mac;
669 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000670 }
Damien Miller963f6b22002-02-19 15:21:23 +1100671 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000672
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000673 cp = buffer_ptr(&outgoing_packet);
674 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000675
676#ifdef PACKET_DEBUG
677 fprintf(stderr, "plain: ");
678 buffer_dump(&outgoing_packet);
679#endif
680
681 if (comp && comp->enabled) {
682 len = buffer_len(&outgoing_packet);
683 /* skip header, compress only payload */
684 buffer_consume(&outgoing_packet, 5);
685 buffer_clear(&compression_buffer);
686 buffer_compress(&outgoing_packet, &compression_buffer);
687 buffer_clear(&outgoing_packet);
688 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
689 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
690 buffer_len(&compression_buffer));
691 DBG(debug("compression: raw %d compressed %d", len,
692 buffer_len(&outgoing_packet)));
693 }
694
695 /* sizeof (packet_len + pad_len + payload) */
696 len = buffer_len(&outgoing_packet);
697
698 /*
699 * calc size of padding, alloc space, get random data,
700 * minimum padding is 4 bytes
701 */
702 padlen = block_size - (len % block_size);
703 if (padlen < 4)
704 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100705 if (extra_pad) {
706 /* will wrap if extra_pad+padlen > 255 */
707 extra_pad = roundup(extra_pad, block_size);
708 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000709 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100710 pad, len, padlen, extra_pad);
711 padlen += pad;
712 extra_pad = 0;
713 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100714 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100715 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000716 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000717 for (i = 0; i < padlen; i++) {
718 if (i % 4 == 0)
719 rand = arc4random();
720 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000721 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000722 }
Damien Millere247cc42000-05-07 12:03:14 +1000723 } else {
724 /* clear padding */
725 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000726 }
727 /* packet_length includes payload, padding and padding length field */
728 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000729 cp = buffer_ptr(&outgoing_packet);
730 PUT_32BIT(cp, packet_length);
731 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000732 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
733
734 /* compute MAC over seqnr and packet(length fields, payload, padding) */
735 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000736 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100737 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000738 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000739 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000740 }
741 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100742 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100743 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000744 buffer_len(&outgoing_packet));
745 /* append unencrypted MAC */
746 if (mac && mac->enabled)
747 buffer_append(&output, (char *)macbuf, mac->mac_len);
748#ifdef PACKET_DEBUG
749 fprintf(stderr, "encrypted: ");
750 buffer_dump(&output);
751#endif
Damien Miller4af51302000-04-16 11:18:38 +1000752 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000753 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000754 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000755 if (++p_send.packets == 0)
756 if (!(datafellows & SSH_BUG_NOREKEY))
757 fatal("XXX too many packets with same key");
758 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000759 buffer_clear(&outgoing_packet);
760
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000761 if (type == SSH2_MSG_NEWKEYS)
762 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000763}
764
Damien Millera5539d22003-04-09 20:50:06 +1000765static void
766packet_send2(void)
767{
768 static int rekeying = 0;
769 struct packet *p;
770 u_char type, *cp;
771
772 cp = buffer_ptr(&outgoing_packet);
773 type = cp[5];
774
775 /* during rekeying we can only send key exchange messages */
776 if (rekeying) {
777 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
778 (type <= SSH2_MSG_TRANSPORT_MAX))) {
779 debug("enqueue packet: %u", type);
780 p = xmalloc(sizeof(*p));
781 p->type = type;
782 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
783 buffer_init(&outgoing_packet);
784 TAILQ_INSERT_TAIL(&outgoing, p, next);
785 return;
786 }
787 }
788
789 /* rekeying starts with sending KEXINIT */
790 if (type == SSH2_MSG_KEXINIT)
791 rekeying = 1;
792
793 packet_send2_wrapped();
794
795 /* after a NEWKEYS message we can send the complete queue */
796 if (type == SSH2_MSG_NEWKEYS) {
797 rekeying = 0;
798 while ((p = TAILQ_FIRST(&outgoing))) {
799 type = p->type;
800 debug("dequeue packet: %u", type);
801 buffer_free(&outgoing_packet);
802 memcpy(&outgoing_packet, &p->payload,
803 sizeof(Buffer));
804 TAILQ_REMOVE(&outgoing, p, next);
805 xfree(p);
806 packet_send2_wrapped();
807 }
808 }
809}
810
Damien Miller33b13562000-04-04 14:38:59 +1000811void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000812packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000813{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000814 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000815 packet_send2();
816 else
817 packet_send1();
818 DBG(debug("packet_send done"));
819}
820
Damien Miller33b13562000-04-04 14:38:59 +1000821/*
Damien Miller5428f641999-11-25 11:54:57 +1100822 * Waits until a packet has been received, and returns its type. Note that
823 * no other data is processed until this returns, so this function should not
824 * be used during the interactive session.
825 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826
827int
Damien Millerdff50992002-01-22 23:16:32 +1100828packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000829{
Damien Miller95def091999-11-25 00:26:21 +1100830 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000831 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100832 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000833 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000834
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000835 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
836 sizeof(fd_mask));
837
Damien Miller95def091999-11-25 00:26:21 +1100838 /* Since we are blocking, ensure that all written packets have been sent. */
839 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000840
Damien Miller95def091999-11-25 00:26:21 +1100841 /* Stay in the loop until we have received a complete packet. */
842 for (;;) {
843 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100844 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000845 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000846 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100847 || type == SSH_SMSG_FAILURE
848 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000849 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100850 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100851 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000852 if (type != SSH_MSG_NONE) {
853 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100854 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000855 }
Damien Miller5428f641999-11-25 11:54:57 +1100856 /*
857 * Otherwise, wait for some data to arrive, add it to the
858 * buffer, and try again.
859 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000860 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
861 sizeof(fd_mask));
862 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100863
Damien Miller95def091999-11-25 00:26:21 +1100864 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000865 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000866 (errno == EAGAIN || errno == EINTR))
867 ;
Damien Miller5428f641999-11-25 11:54:57 +1100868
Damien Miller95def091999-11-25 00:26:21 +1100869 /* Read data from the socket. */
870 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100871 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000872 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000873 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100874 }
Damien Miller95def091999-11-25 00:26:21 +1100875 if (len < 0)
876 fatal("Read from socket failed: %.100s", strerror(errno));
877 /* Append it to the buffer. */
878 packet_process_incoming(buf, len);
879 }
880 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881}
882
Damien Miller278f9072001-12-21 15:00:19 +1100883int
Damien Millerdff50992002-01-22 23:16:32 +1100884packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100885{
Damien Millerdff50992002-01-22 23:16:32 +1100886 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100887}
888
Damien Miller5428f641999-11-25 11:54:57 +1100889/*
890 * Waits until a packet has been received, verifies that its type matches
891 * that given, and gives a fatal error and exits if there is a mismatch.
892 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
894void
Damien Millerdff50992002-01-22 23:16:32 +1100895packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896{
Damien Miller95def091999-11-25 00:26:21 +1100897 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898
Damien Millerdff50992002-01-22 23:16:32 +1100899 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100900 if (type != expected_type)
901 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000902 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903}
904
905/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100906 * packet_process_incoming. If so, reads the packet; otherwise returns
907 * SSH_MSG_NONE. This does not wait for data from the connection.
908 *
909 * SSH_MSG_DISCONNECT is handled specially here. Also,
910 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
911 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100912 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913
Ben Lindstrombba81212001-06-25 05:01:22 +0000914static int
Damien Millerdff50992002-01-22 23:16:32 +1100915packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000917 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000918 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000919 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920
Damien Miller95def091999-11-25 00:26:21 +1100921 /* Check if input size is less than minimum packet size. */
922 if (buffer_len(&input) < 4 + 8)
923 return SSH_MSG_NONE;
924 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000925 cp = buffer_ptr(&input);
926 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100927 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000928 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100929 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930
Damien Miller95def091999-11-25 00:26:21 +1100931 /* Check if the packet has been entirely received. */
932 if (buffer_len(&input) < 4 + padded_len)
933 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934
Damien Miller95def091999-11-25 00:26:21 +1100935 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936
Damien Miller95def091999-11-25 00:26:21 +1100937 /* Consume packet length. */
938 buffer_consume(&input, 4);
939
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000940 /*
941 * Cryptographic attack detector for ssh
942 * (C)1998 CORE-SDI, Buenos Aires Argentina
943 * Ariel Futoransky(futo@core-sdi.com)
944 */
Damien Miller963f6b22002-02-19 15:21:23 +1100945 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000946 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
947 packet_disconnect("crc32 compensation attack: network attack detected");
948
949 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100950 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100951 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100952 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000953
Damien Miller95def091999-11-25 00:26:21 +1100954 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955
956#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100957 fprintf(stderr, "read_poll plain: ");
958 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960
Damien Miller95def091999-11-25 00:26:21 +1100961 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100962 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000963 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964
Damien Miller95def091999-11-25 00:26:21 +1100965 /* Skip padding. */
966 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100967
Damien Miller95def091999-11-25 00:26:21 +1100968 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100969 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +1100970 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000971 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000973 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
974 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100975 if (checksum != stored_checksum)
976 packet_disconnect("Corrupted check bytes on input.");
977 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979 if (packet_compression) {
980 buffer_clear(&compression_buffer);
981 buffer_uncompress(&incoming_packet, &compression_buffer);
982 buffer_clear(&incoming_packet);
983 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000984 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100985 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000986 type = buffer_get_char(&incoming_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000987 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988}
Damien Miller95def091999-11-25 00:26:21 +1100989
Ben Lindstrombba81212001-06-25 05:01:22 +0000990static int
Damien Millerdff50992002-01-22 23:16:32 +1100991packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000992{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000993 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000994 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000995 u_char *macbuf, *cp, type;
Damien Miller33b13562000-04-04 14:38:59 +1000996 int maclen, block_size;
997 Enc *enc = NULL;
998 Mac *mac = NULL;
999 Comp *comp = NULL;
1000
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001001 if (newkeys[MODE_IN] != NULL) {
1002 enc = &newkeys[MODE_IN]->enc;
1003 mac = &newkeys[MODE_IN]->mac;
1004 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001005 }
1006 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001007 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001008
1009 if (packet_length == 0) {
1010 /*
1011 * check if input size is less than the cipher block size,
1012 * decrypt first block and extract length of incoming packet
1013 */
1014 if (buffer_len(&input) < block_size)
1015 return SSH_MSG_NONE;
1016 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001017 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001018 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001019 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001020 cp = buffer_ptr(&incoming_packet);
1021 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001022 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001023#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001024 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001025#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001026 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001027 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001028 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001029 buffer_consume(&input, block_size);
1030 }
1031 /* we have a partial packet of block_size bytes */
1032 need = 4 + packet_length - block_size;
1033 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1034 need, maclen));
1035 if (need % block_size != 0)
1036 fatal("padding error: need %d block %d mod %d",
1037 need, block_size, need % block_size);
1038 /*
1039 * check if the entire packet has been received and
1040 * decrypt into incoming_packet
1041 */
1042 if (buffer_len(&input) < need + maclen)
1043 return SSH_MSG_NONE;
1044#ifdef PACKET_DEBUG
1045 fprintf(stderr, "read_poll enc/full: ");
1046 buffer_dump(&input);
1047#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001048 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001049 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001050 buffer_consume(&input, need);
1051 /*
1052 * compute MAC over seqnr and packet,
1053 * increment sequence number for incoming packet
1054 */
Damien Miller4af51302000-04-16 11:18:38 +10001055 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001056 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001057 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001058 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001059 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001060 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001061 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001062 buffer_consume(&input, mac->mac_len);
1063 }
Damien Miller278f9072001-12-21 15:00:19 +11001064 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001065 *seqnr_p = p_read.seqnr;
1066 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001067 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001068 if (++p_read.packets == 0)
1069 if (!(datafellows & SSH_BUG_NOREKEY))
1070 fatal("XXX too many packets with same key");
1071 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001072
1073 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001074 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001075 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001076 DBG(debug("input: padlen %d", padlen));
1077 if (padlen < 4)
1078 packet_disconnect("Corrupted padlen %d on input.", padlen);
1079
1080 /* skip packet size + padlen, discard padding */
1081 buffer_consume(&incoming_packet, 4 + 1);
1082 buffer_consume_end(&incoming_packet, padlen);
1083
1084 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1085 if (comp && comp->enabled) {
1086 buffer_clear(&compression_buffer);
1087 buffer_uncompress(&incoming_packet, &compression_buffer);
1088 buffer_clear(&incoming_packet);
1089 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1090 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001091 DBG(debug("input: len after de-compress %d",
1092 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001093 }
1094 /*
1095 * get packet type, implies consume.
1096 * return length of payload (without type field)
1097 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001098 type = buffer_get_char(&incoming_packet);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001099 if (type == SSH2_MSG_NEWKEYS)
1100 set_newkeys(MODE_IN);
Damien Miller33b13562000-04-04 14:38:59 +10001101#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001102 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001103 buffer_dump(&incoming_packet);
1104#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001105 /* reset for next packet */
1106 packet_length = 0;
1107 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001108}
1109
1110int
Damien Millerdff50992002-01-22 23:16:32 +11001111packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001112{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001113 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001114 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001115 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001116
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001117 for (;;) {
1118 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001119 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001120 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001121 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001122 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001123 case SSH2_MSG_IGNORE:
1124 break;
1125 case SSH2_MSG_DEBUG:
1126 packet_get_char();
1127 msg = packet_get_string(NULL);
1128 debug("Remote: %.900s", msg);
1129 xfree(msg);
1130 msg = packet_get_string(NULL);
1131 xfree(msg);
1132 break;
1133 case SSH2_MSG_DISCONNECT:
1134 reason = packet_get_int();
1135 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001136 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001137 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001138 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001139 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001140 break;
Damien Miller659811f2002-01-22 23:23:11 +11001141 case SSH2_MSG_UNIMPLEMENTED:
1142 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001143 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1144 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001145 break;
Damien Miller33b13562000-04-04 14:38:59 +10001146 default:
1147 return type;
1148 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001149 }
Damien Miller33b13562000-04-04 14:38:59 +10001150 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001151 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001152 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001153 case SSH_MSG_IGNORE:
1154 break;
1155 case SSH_MSG_DEBUG:
1156 msg = packet_get_string(NULL);
1157 debug("Remote: %.900s", msg);
1158 xfree(msg);
1159 break;
1160 case SSH_MSG_DISCONNECT:
1161 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001162 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001163 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001164 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001165 xfree(msg);
1166 break;
1167 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001168 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001169 DBG(debug("received packet type %d", type));
1170 return type;
1171 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001172 }
Damien Miller33b13562000-04-04 14:38:59 +10001173 }
1174 }
1175}
1176
Damien Miller278f9072001-12-21 15:00:19 +11001177int
Damien Millerdff50992002-01-22 23:16:32 +11001178packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001179{
Damien Millerdff50992002-01-22 23:16:32 +11001180 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001181}
1182
Damien Miller5428f641999-11-25 11:54:57 +11001183/*
1184 * Buffers the given amount of input characters. This is intended to be used
1185 * together with packet_read_poll.
1186 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187
1188void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001189packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190{
Damien Miller95def091999-11-25 00:26:21 +11001191 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192}
1193
1194/* Returns a character from the packet. */
1195
Ben Lindstrom46c16222000-12-22 01:43:59 +00001196u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001197packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198{
Damien Miller95def091999-11-25 00:26:21 +11001199 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001200
Damien Miller95def091999-11-25 00:26:21 +11001201 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001202 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203}
1204
1205/* Returns an integer from the packet data. */
1206
Ben Lindstrom46c16222000-12-22 01:43:59 +00001207u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001208packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209{
Damien Miller95def091999-11-25 00:26:21 +11001210 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211}
1212
Damien Miller5428f641999-11-25 11:54:57 +11001213/*
1214 * Returns an arbitrary precision integer from the packet data. The integer
1215 * must have been initialized before this call.
1216 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001217
1218void
Damien Millerd432ccf2002-01-22 23:14:44 +11001219packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220{
Damien Miller76e1e362002-01-22 23:15:57 +11001221 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001222}
1223
Damien Miller33b13562000-04-04 14:38:59 +10001224void
Damien Millerd432ccf2002-01-22 23:14:44 +11001225packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001226{
Damien Miller76e1e362002-01-22 23:15:57 +11001227 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001228}
1229
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001230void *
Damien Miller33b13562000-04-04 14:38:59 +10001231packet_get_raw(int *length_ptr)
1232{
1233 int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001234
Damien Miller33b13562000-04-04 14:38:59 +10001235 if (length_ptr != NULL)
1236 *length_ptr = bytes;
1237 return buffer_ptr(&incoming_packet);
1238}
1239
Damien Miller4af51302000-04-16 11:18:38 +10001240int
1241packet_remaining(void)
1242{
1243 return buffer_len(&incoming_packet);
1244}
1245
Damien Miller5428f641999-11-25 11:54:57 +11001246/*
1247 * Returns a string from the packet data. The string is allocated using
1248 * xmalloc; it is the responsibility of the calling program to free it when
1249 * no longer needed. The length_ptr argument may be NULL, or point to an
1250 * integer into which the length of the string is stored.
1251 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001253void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001254packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255{
Damien Miller95def091999-11-25 00:26:21 +11001256 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001257}
1258
Damien Miller5428f641999-11-25 11:54:57 +11001259/*
1260 * Sends a diagnostic message from the server to the client. This message
1261 * can be sent at any time (but not while constructing another message). The
1262 * message is printed immediately, but only if the client is being executed
1263 * in verbose mode. These messages are primarily intended to ease debugging
1264 * authentication problems. The length of the formatted message must not
1265 * exceed 1024 bytes. This will automatically call packet_write_wait.
1266 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267
1268void
Damien Miller95def091999-11-25 00:26:21 +11001269packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270{
Damien Miller95def091999-11-25 00:26:21 +11001271 char buf[1024];
1272 va_list args;
1273
Ben Lindstroma14ee472000-12-07 01:24:58 +00001274 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1275 return;
1276
Damien Miller95def091999-11-25 00:26:21 +11001277 va_start(args, fmt);
1278 vsnprintf(buf, sizeof(buf), fmt, args);
1279 va_end(args);
1280
Damien Miller7c8af4f2000-05-01 08:24:07 +10001281 if (compat20) {
1282 packet_start(SSH2_MSG_DEBUG);
1283 packet_put_char(0); /* bool: always display */
1284 packet_put_cstring(buf);
1285 packet_put_cstring("");
1286 } else {
1287 packet_start(SSH_MSG_DEBUG);
1288 packet_put_cstring(buf);
1289 }
Damien Miller95def091999-11-25 00:26:21 +11001290 packet_send();
1291 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001292}
1293
Damien Miller5428f641999-11-25 11:54:57 +11001294/*
1295 * Logs the error plus constructs and sends a disconnect packet, closes the
1296 * connection, and exits. This function never returns. The error message
1297 * should not contain a newline. The length of the formatted message must
1298 * not exceed 1024 bytes.
1299 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001300
1301void
Damien Miller95def091999-11-25 00:26:21 +11001302packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001303{
Damien Miller95def091999-11-25 00:26:21 +11001304 char buf[1024];
1305 va_list args;
1306 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001307
Damien Miller95def091999-11-25 00:26:21 +11001308 if (disconnecting) /* Guard against recursive invocations. */
1309 fatal("packet_disconnect called recursively.");
1310 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001311
Damien Miller5428f641999-11-25 11:54:57 +11001312 /*
1313 * Format the message. Note that the caller must make sure the
1314 * message is of limited size.
1315 */
Damien Miller95def091999-11-25 00:26:21 +11001316 va_start(args, fmt);
1317 vsnprintf(buf, sizeof(buf), fmt, args);
1318 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001320 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001321 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001322
Damien Miller95def091999-11-25 00:26:21 +11001323 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001324 if (compat20) {
1325 packet_start(SSH2_MSG_DISCONNECT);
1326 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1327 packet_put_cstring(buf);
1328 packet_put_cstring("");
1329 } else {
1330 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001331 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001332 }
Damien Miller95def091999-11-25 00:26:21 +11001333 packet_send();
1334 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001335
Damien Miller95def091999-11-25 00:26:21 +11001336 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001337 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338
Damien Miller95def091999-11-25 00:26:21 +11001339 /* Close the connection. */
1340 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001341 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001342}
1343
Damien Miller5428f641999-11-25 11:54:57 +11001344/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001345
1346void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001347packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001348{
Damien Miller95def091999-11-25 00:26:21 +11001349 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001350
Damien Miller95def091999-11-25 00:26:21 +11001351 if (len > 0) {
1352 len = write(connection_out, buffer_ptr(&output), len);
1353 if (len <= 0) {
1354 if (errno == EAGAIN)
1355 return;
1356 else
1357 fatal("Write failed: %.100s", strerror(errno));
1358 }
1359 buffer_consume(&output, len);
1360 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361}
1362
Damien Miller5428f641999-11-25 11:54:57 +11001363/*
1364 * Calls packet_write_poll repeatedly until all pending output data has been
1365 * written.
1366 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001367
1368void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001369packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001370{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001371 fd_set *setp;
1372
1373 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1374 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001375 packet_write_poll();
1376 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001377 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1378 sizeof(fd_mask));
1379 FD_SET(connection_out, setp);
1380 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001381 (errno == EAGAIN || errno == EINTR))
1382 ;
Damien Miller95def091999-11-25 00:26:21 +11001383 packet_write_poll();
1384 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001385 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001386}
1387
1388/* Returns true if there is buffered data to write to the connection. */
1389
1390int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001391packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392{
Damien Miller95def091999-11-25 00:26:21 +11001393 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394}
1395
1396/* Returns true if there is not too much data to write to the connection. */
1397
1398int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001399packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400{
Damien Miller95def091999-11-25 00:26:21 +11001401 if (interactive_mode)
1402 return buffer_len(&output) < 16384;
1403 else
1404 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405}
1406
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001407
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001408static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001409packet_set_tos(int interactive)
1410{
Damien Miller5924ceb2003-11-22 15:02:42 +11001411#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001412 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1413
1414 if (!packet_connection_is_on_socket() ||
1415 !packet_connection_is_ipv4())
1416 return;
1417 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1418 sizeof(tos)) < 0)
1419 error("setsockopt IP_TOS %d: %.100s:",
1420 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001421#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001422}
Ben Lindstroma7433982002-12-23 02:41:41 +00001423
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001424/* Informs that the current session is interactive. Sets IP flags for that. */
1425
1426void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001427packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001429 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001431 if (called)
1432 return;
1433 called = 1;
1434
Damien Miller95def091999-11-25 00:26:21 +11001435 /* Record that we are in interactive mode. */
1436 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437
Damien Miller34132e52000-01-14 15:45:46 +11001438 /* Only set socket options if using a socket. */
1439 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001440 return;
Ben Lindstroma7433982002-12-23 02:41:41 +00001441 if (interactive)
Damien Miller398e1cf2002-02-05 11:52:13 +11001442 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001443 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444}
1445
1446/* Returns true if the current connection is interactive. */
1447
1448int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001449packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001450{
Damien Miller95def091999-11-25 00:26:21 +11001451 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001452}
Damien Miller6162d121999-11-21 13:23:52 +11001453
Darren Tucker1f8311c2004-05-13 16:39:33 +10001454int
Darren Tucker502d3842003-06-28 12:38:01 +10001455packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001456{
Damien Miller95def091999-11-25 00:26:21 +11001457 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001458
Damien Miller95def091999-11-25 00:26:21 +11001459 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001460 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001461 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001462 return -1;
1463 }
1464 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001465 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001466 return -1;
1467 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001468 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001469 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001470 max_packet_size = s;
1471 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001472}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001473
Damien Miller9f643902001-11-12 11:02:52 +11001474/* roundup current message to pad bytes */
1475void
1476packet_add_padding(u_char pad)
1477{
1478 extra_pad = pad;
1479}
1480
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001481/*
1482 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001483 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001484 * byte SSH_MSG_IGNORE
1485 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001486 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001487 * All implementations MUST understand (and ignore) this message at any
1488 * time (after receiving the protocol version). No implementation is
1489 * required to send them. This message can be used as an additional
1490 * protection measure against advanced traffic analysis techniques.
1491 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001492void
1493packet_send_ignore(int nbytes)
1494{
1495 u_int32_t rand = 0;
1496 int i;
1497
1498 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001499 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001500 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001501 if (i % 4 == 0)
1502 rand = arc4random();
1503 packet_put_char(rand & 0xff);
1504 rand >>= 8;
1505 }
1506}
Damien Millera5539d22003-04-09 20:50:06 +10001507
Darren Tucker1f8311c2004-05-13 16:39:33 +10001508#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001509int
1510packet_need_rekeying(void)
1511{
1512 if (datafellows & SSH_BUG_NOREKEY)
1513 return 0;
1514 return
1515 (p_send.packets > MAX_PACKETS) ||
1516 (p_read.packets > MAX_PACKETS) ||
1517 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1518 (max_blocks_in && (p_read.blocks > max_blocks_in));
1519}
1520
1521void
1522packet_set_rekey_limit(u_int32_t bytes)
1523{
1524 rekey_limit = bytes;
1525}