blob: d5b50f2f4ff7cb076be18b13b12e02ecd538d6f9 [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"
Damien Millereccb9de2005-06-17 12:59:34 +100040RCSID("$OpenBSD: packet.c,v 1.117 2005/06/17 02:44:32 djm 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. */
Damien Miller232711f2004-06-15 10:35:30 +1000322 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323
Damien Miller232711f2004-06-15 10:35:30 +1000324 if (connection_out != connection_in)
325 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326}
327
328/* Returns the socket used for reading. */
329
330int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000331packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332{
Damien Miller95def091999-11-25 00:26:21 +1100333 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334}
335
336/* Returns the descriptor used for writing. */
337
338int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000339packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340{
Damien Miller95def091999-11-25 00:26:21 +1100341 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342}
343
344/* Closes the connection and clears and frees internal data structures. */
345
346void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000347packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348{
Damien Miller95def091999-11-25 00:26:21 +1100349 if (!initialized)
350 return;
351 initialized = 0;
352 if (connection_in == connection_out) {
353 shutdown(connection_out, SHUT_RDWR);
354 close(connection_out);
355 } else {
356 close(connection_in);
357 close(connection_out);
358 }
359 buffer_free(&input);
360 buffer_free(&output);
361 buffer_free(&outgoing_packet);
362 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000363 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100364 buffer_free(&compression_buffer);
365 buffer_compress_uninit();
366 }
Damien Miller963f6b22002-02-19 15:21:23 +1100367 cipher_cleanup(&send_context);
368 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369}
370
371/* Sets remote side protocol flags. */
372
373void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000374packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375{
Damien Miller95def091999-11-25 00:26:21 +1100376 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377}
378
379/* Returns the remote protocol flags set earlier by the above function. */
380
Ben Lindstrom46c16222000-12-22 01:43:59 +0000381u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000382packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383{
Damien Miller95def091999-11-25 00:26:21 +1100384 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385}
386
Damien Miller5428f641999-11-25 11:54:57 +1100387/*
388 * Starts packet compression from the next packet on in both directions.
389 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
390 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391
Ben Lindstrombba81212001-06-25 05:01:22 +0000392static void
393packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000394{
395 if (compression_buffer_ready == 1)
396 return;
397 compression_buffer_ready = 1;
398 buffer_init(&compression_buffer);
399}
400
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401void
402packet_start_compression(int level)
403{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000404 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100405 fatal("Compression already enabled.");
406 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000407 packet_init_compression();
408 buffer_compress_init_send(level);
409 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410}
411
Damien Miller5428f641999-11-25 11:54:57 +1100412/*
Damien Miller5428f641999-11-25 11:54:57 +1100413 * Causes any further packets to be encrypted using the given key. The same
414 * key is used for both sending and reception. However, both directions are
415 * encrypted independently of each other.
416 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000417
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000419packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100420 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421{
Damien Miller874d77b2000-10-14 16:23:11 +1100422 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000423
Damien Miller874d77b2000-10-14 16:23:11 +1100424 if (cipher == NULL)
425 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000426 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100427 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000428 if (keylen > SSH_SESSION_KEY_LENGTH)
429 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
430 memcpy(ssh1_key, key, keylen);
431 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100432 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
433 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434}
435
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000436u_int
437packet_get_encryption_key(u_char *key)
438{
439 if (key == NULL)
440 return (ssh1_keylen);
441 memcpy(key, ssh1_key, ssh1_keylen);
442 return (ssh1_keylen);
443}
444
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000445/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000447packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000449 u_char buf[9];
450 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451
Ben Lindstrom204e4882001-03-05 06:47:00 +0000452 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000453 len = compat20 ? 6 : 9;
454 memset(buf, 0, len - 1);
455 buf[len - 1] = type;
456 buffer_clear(&outgoing_packet);
457 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000458}
459
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000460/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461void
462packet_put_char(int value)
463{
Damien Miller95def091999-11-25 00:26:21 +1100464 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000465
Damien Miller95def091999-11-25 00:26:21 +1100466 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000469packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470{
Damien Miller95def091999-11-25 00:26:21 +1100471 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100474packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475{
Damien Miller95def091999-11-25 00:26:21 +1100476 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477}
Damien Miller33b13562000-04-04 14:38:59 +1000478void
479packet_put_cstring(const char *str)
480{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000481 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000482}
Damien Miller33b13562000-04-04 14:38:59 +1000483void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100484packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000485{
486 buffer_append(&outgoing_packet, buf, len);
487}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488void
Damien Miller95def091999-11-25 00:26:21 +1100489packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490{
Damien Miller95def091999-11-25 00:26:21 +1100491 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492}
Damien Miller33b13562000-04-04 14:38:59 +1000493void
494packet_put_bignum2(BIGNUM * value)
495{
496 buffer_put_bignum2(&outgoing_packet, value);
497}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498
Damien Miller5428f641999-11-25 11:54:57 +1100499/*
500 * Finalizes and sends the packet. If the encryption key has been set,
501 * encrypts the packet before sending.
502 */
Damien Miller95def091999-11-25 00:26:21 +1100503
Ben Lindstrombba81212001-06-25 05:01:22 +0000504static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000505packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000507 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100508 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000509 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000510 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511
Damien Miller5428f641999-11-25 11:54:57 +1100512 /*
513 * If using packet compression, compress the payload of the outgoing
514 * packet.
515 */
Damien Miller95def091999-11-25 00:26:21 +1100516 if (packet_compression) {
517 buffer_clear(&compression_buffer);
518 /* Skip padding. */
519 buffer_consume(&outgoing_packet, 8);
520 /* padding */
521 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
522 buffer_compress(&outgoing_packet, &compression_buffer);
523 buffer_clear(&outgoing_packet);
524 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100525 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100526 }
527 /* Compute packet length without padding (add checksum, remove padding). */
528 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529
Damien Millere247cc42000-05-07 12:03:14 +1000530 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100531 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100532 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100533 cp = buffer_ptr(&outgoing_packet);
534 for (i = 0; i < padding; i++) {
535 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000536 rnd = arc4random();
537 cp[7 - i] = rnd & 0xff;
538 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100539 }
540 }
541 buffer_consume(&outgoing_packet, 8 - padding);
542
543 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100544 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000545 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100546 PUT_32BIT(buf, checksum);
547 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000548
549#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100550 fprintf(stderr, "packet_send plain: ");
551 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552#endif
553
Damien Miller95def091999-11-25 00:26:21 +1100554 /* Append to output. */
555 PUT_32BIT(buf, len);
556 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100557 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100558 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100559 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100560
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100562 fprintf(stderr, "encrypted: ");
563 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564#endif
565
Damien Miller95def091999-11-25 00:26:21 +1100566 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567
Damien Miller5428f641999-11-25 11:54:57 +1100568 /*
569 * Note that the packet is now only buffered in output. It won\'t be
570 * actually sent until packet_write_wait or packet_write_poll is
571 * called.
572 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573}
574
Ben Lindstromf6027d32002-03-22 01:42:04 +0000575void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000576set_newkeys(int mode)
577{
578 Enc *enc;
579 Mac *mac;
580 Comp *comp;
581 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000582 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000583 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000584
Ben Lindstrom064496f2002-12-23 02:04:22 +0000585 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000586
Damien Miller963f6b22002-02-19 15:21:23 +1100587 if (mode == MODE_OUT) {
588 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000589 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000590 p_send.packets = p_send.blocks = 0;
591 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100592 } else {
593 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000594 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000595 p_read.packets = p_read.blocks = 0;
596 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100597 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000598 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000599 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100600 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000601 enc = &newkeys[mode]->enc;
602 mac = &newkeys[mode]->mac;
603 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000604 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000605 xfree(enc->name);
606 xfree(enc->iv);
607 xfree(enc->key);
608 xfree(mac->name);
609 xfree(mac->key);
610 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000611 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000612 }
613 newkeys[mode] = kex_get_newkeys(mode);
614 if (newkeys[mode] == NULL)
615 fatal("newkeys: no keys for mode %d", mode);
616 enc = &newkeys[mode]->enc;
617 mac = &newkeys[mode]->mac;
618 comp = &newkeys[mode]->comp;
619 if (mac->md != NULL)
620 mac->enabled = 1;
621 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100622 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000623 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000624 /* Deleting the keys does not gain extra security */
625 /* memset(enc->iv, 0, enc->block_size);
626 memset(enc->key, 0, enc->key_len); */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000627 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000628 packet_init_compression();
629 if (mode == MODE_OUT)
630 buffer_compress_init_send(6);
631 else
632 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000633 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000634 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000635 /*
636 * The 2^(blocksize*2) limit is too expensive for 3DES,
637 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
638 */
639 if (enc->block_size >= 16)
640 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
641 else
642 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000643 if (rekey_limit)
644 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000645}
646
Damien Miller5428f641999-11-25 11:54:57 +1100647/*
Damien Miller33b13562000-04-04 14:38:59 +1000648 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
649 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000650static void
Damien Millera5539d22003-04-09 20:50:06 +1000651packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000652{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000653 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100654 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000655 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100656 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000657 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000658 Enc *enc = NULL;
659 Mac *mac = NULL;
660 Comp *comp = NULL;
661 int block_size;
662
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000663 if (newkeys[MODE_OUT] != NULL) {
664 enc = &newkeys[MODE_OUT]->enc;
665 mac = &newkeys[MODE_OUT]->mac;
666 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000667 }
Damien Miller963f6b22002-02-19 15:21:23 +1100668 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000669
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000670 cp = buffer_ptr(&outgoing_packet);
671 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000672
673#ifdef PACKET_DEBUG
674 fprintf(stderr, "plain: ");
675 buffer_dump(&outgoing_packet);
676#endif
677
678 if (comp && comp->enabled) {
679 len = buffer_len(&outgoing_packet);
680 /* skip header, compress only payload */
681 buffer_consume(&outgoing_packet, 5);
682 buffer_clear(&compression_buffer);
683 buffer_compress(&outgoing_packet, &compression_buffer);
684 buffer_clear(&outgoing_packet);
685 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
686 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
687 buffer_len(&compression_buffer));
688 DBG(debug("compression: raw %d compressed %d", len,
689 buffer_len(&outgoing_packet)));
690 }
691
692 /* sizeof (packet_len + pad_len + payload) */
693 len = buffer_len(&outgoing_packet);
694
695 /*
696 * calc size of padding, alloc space, get random data,
697 * minimum padding is 4 bytes
698 */
699 padlen = block_size - (len % block_size);
700 if (padlen < 4)
701 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100702 if (extra_pad) {
703 /* will wrap if extra_pad+padlen > 255 */
704 extra_pad = roundup(extra_pad, block_size);
705 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000706 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100707 pad, len, padlen, extra_pad);
708 padlen += pad;
709 extra_pad = 0;
710 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100711 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100712 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000713 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000714 for (i = 0; i < padlen; i++) {
715 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000716 rnd = arc4random();
717 cp[i] = rnd & 0xff;
718 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000719 }
Damien Millere247cc42000-05-07 12:03:14 +1000720 } else {
721 /* clear padding */
722 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000723 }
724 /* packet_length includes payload, padding and padding length field */
725 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000726 cp = buffer_ptr(&outgoing_packet);
727 PUT_32BIT(cp, packet_length);
728 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000729 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
730
731 /* compute MAC over seqnr and packet(length fields, payload, padding) */
732 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000733 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100734 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000735 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000736 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000737 }
738 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100739 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100740 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000741 buffer_len(&outgoing_packet));
742 /* append unencrypted MAC */
743 if (mac && mac->enabled)
744 buffer_append(&output, (char *)macbuf, mac->mac_len);
745#ifdef PACKET_DEBUG
746 fprintf(stderr, "encrypted: ");
747 buffer_dump(&output);
748#endif
Damien Miller4af51302000-04-16 11:18:38 +1000749 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000750 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000751 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000752 if (++p_send.packets == 0)
753 if (!(datafellows & SSH_BUG_NOREKEY))
754 fatal("XXX too many packets with same key");
755 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000756 buffer_clear(&outgoing_packet);
757
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000758 if (type == SSH2_MSG_NEWKEYS)
759 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000760}
761
Damien Millera5539d22003-04-09 20:50:06 +1000762static void
763packet_send2(void)
764{
765 static int rekeying = 0;
766 struct packet *p;
767 u_char type, *cp;
768
769 cp = buffer_ptr(&outgoing_packet);
770 type = cp[5];
771
772 /* during rekeying we can only send key exchange messages */
773 if (rekeying) {
774 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
775 (type <= SSH2_MSG_TRANSPORT_MAX))) {
776 debug("enqueue packet: %u", type);
777 p = xmalloc(sizeof(*p));
778 p->type = type;
779 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
780 buffer_init(&outgoing_packet);
781 TAILQ_INSERT_TAIL(&outgoing, p, next);
782 return;
783 }
784 }
785
786 /* rekeying starts with sending KEXINIT */
787 if (type == SSH2_MSG_KEXINIT)
788 rekeying = 1;
789
790 packet_send2_wrapped();
791
792 /* after a NEWKEYS message we can send the complete queue */
793 if (type == SSH2_MSG_NEWKEYS) {
794 rekeying = 0;
795 while ((p = TAILQ_FIRST(&outgoing))) {
796 type = p->type;
797 debug("dequeue packet: %u", type);
798 buffer_free(&outgoing_packet);
799 memcpy(&outgoing_packet, &p->payload,
800 sizeof(Buffer));
801 TAILQ_REMOVE(&outgoing, p, next);
802 xfree(p);
803 packet_send2_wrapped();
804 }
805 }
806}
807
Damien Miller33b13562000-04-04 14:38:59 +1000808void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000809packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000810{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000811 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000812 packet_send2();
813 else
814 packet_send1();
815 DBG(debug("packet_send done"));
816}
817
Damien Miller33b13562000-04-04 14:38:59 +1000818/*
Damien Miller5428f641999-11-25 11:54:57 +1100819 * Waits until a packet has been received, and returns its type. Note that
820 * no other data is processed until this returns, so this function should not
821 * be used during the interactive session.
822 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823
824int
Damien Millerdff50992002-01-22 23:16:32 +1100825packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826{
Damien Miller95def091999-11-25 00:26:21 +1100827 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000828 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100829 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000830 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000832 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
833 sizeof(fd_mask));
834
Damien Miller95def091999-11-25 00:26:21 +1100835 /* Since we are blocking, ensure that all written packets have been sent. */
836 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000837
Damien Miller95def091999-11-25 00:26:21 +1100838 /* Stay in the loop until we have received a complete packet. */
839 for (;;) {
840 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100841 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000842 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000843 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100844 || type == SSH_SMSG_FAILURE
845 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000846 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100847 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100848 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000849 if (type != SSH_MSG_NONE) {
850 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100851 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000852 }
Damien Miller5428f641999-11-25 11:54:57 +1100853 /*
854 * Otherwise, wait for some data to arrive, add it to the
855 * buffer, and try again.
856 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000857 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
858 sizeof(fd_mask));
859 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100860
Damien Miller95def091999-11-25 00:26:21 +1100861 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000862 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000863 (errno == EAGAIN || errno == EINTR))
864 ;
Damien Miller5428f641999-11-25 11:54:57 +1100865
Damien Miller95def091999-11-25 00:26:21 +1100866 /* Read data from the socket. */
867 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100868 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000869 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000870 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100871 }
Damien Miller95def091999-11-25 00:26:21 +1100872 if (len < 0)
873 fatal("Read from socket failed: %.100s", strerror(errno));
874 /* Append it to the buffer. */
875 packet_process_incoming(buf, len);
876 }
877 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878}
879
Damien Miller278f9072001-12-21 15:00:19 +1100880int
Damien Millerdff50992002-01-22 23:16:32 +1100881packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100882{
Damien Millerdff50992002-01-22 23:16:32 +1100883 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100884}
885
Damien Miller5428f641999-11-25 11:54:57 +1100886/*
887 * Waits until a packet has been received, verifies that its type matches
888 * that given, and gives a fatal error and exits if there is a mismatch.
889 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890
891void
Damien Millerdff50992002-01-22 23:16:32 +1100892packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893{
Damien Miller95def091999-11-25 00:26:21 +1100894 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895
Damien Millerdff50992002-01-22 23:16:32 +1100896 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100897 if (type != expected_type)
898 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000899 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900}
901
902/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100903 * packet_process_incoming. If so, reads the packet; otherwise returns
904 * SSH_MSG_NONE. This does not wait for data from the connection.
905 *
906 * SSH_MSG_DISCONNECT is handled specially here. Also,
907 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
908 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100909 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910
Ben Lindstrombba81212001-06-25 05:01:22 +0000911static int
Damien Millerdff50992002-01-22 23:16:32 +1100912packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000914 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000915 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000916 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917
Damien Miller95def091999-11-25 00:26:21 +1100918 /* Check if input size is less than minimum packet size. */
919 if (buffer_len(&input) < 4 + 8)
920 return SSH_MSG_NONE;
921 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000922 cp = buffer_ptr(&input);
923 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100924 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000925 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100926 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927
Damien Miller95def091999-11-25 00:26:21 +1100928 /* Check if the packet has been entirely received. */
929 if (buffer_len(&input) < 4 + padded_len)
930 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931
Damien Miller95def091999-11-25 00:26:21 +1100932 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933
Damien Miller95def091999-11-25 00:26:21 +1100934 /* Consume packet length. */
935 buffer_consume(&input, 4);
936
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000937 /*
938 * Cryptographic attack detector for ssh
939 * (C)1998 CORE-SDI, Buenos Aires Argentina
940 * Ariel Futoransky(futo@core-sdi.com)
941 */
Damien Miller963f6b22002-02-19 15:21:23 +1100942 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000943 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
944 packet_disconnect("crc32 compensation attack: network attack detected");
945
946 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100947 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100948 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100949 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000950
Damien Miller95def091999-11-25 00:26:21 +1100951 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
953#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100954 fprintf(stderr, "read_poll plain: ");
955 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
Damien Miller95def091999-11-25 00:26:21 +1100958 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100959 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000960 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961
Damien Miller95def091999-11-25 00:26:21 +1100962 /* Skip padding. */
963 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100964
Damien Miller95def091999-11-25 00:26:21 +1100965 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100966 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +1100967 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000968 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000970 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
971 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100972 if (checksum != stored_checksum)
973 packet_disconnect("Corrupted check bytes on input.");
974 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975
Damien Miller95def091999-11-25 00:26:21 +1100976 if (packet_compression) {
977 buffer_clear(&compression_buffer);
978 buffer_uncompress(&incoming_packet, &compression_buffer);
979 buffer_clear(&incoming_packet);
980 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000981 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100982 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000983 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +1100984 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
985 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000986 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987}
Damien Miller95def091999-11-25 00:26:21 +1100988
Ben Lindstrombba81212001-06-25 05:01:22 +0000989static int
Damien Millerdff50992002-01-22 23:16:32 +1100990packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000991{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000992 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000993 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000994 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +1000995 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000996 Enc *enc = NULL;
997 Mac *mac = NULL;
998 Comp *comp = NULL;
999
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001000 if (newkeys[MODE_IN] != NULL) {
1001 enc = &newkeys[MODE_IN]->enc;
1002 mac = &newkeys[MODE_IN]->mac;
1003 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001004 }
1005 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001006 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001007
1008 if (packet_length == 0) {
1009 /*
1010 * check if input size is less than the cipher block size,
1011 * decrypt first block and extract length of incoming packet
1012 */
1013 if (buffer_len(&input) < block_size)
1014 return SSH_MSG_NONE;
1015 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001016 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001017 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001018 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001019 cp = buffer_ptr(&incoming_packet);
1020 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001021 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001022#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001023 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001024#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001025 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001026 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001027 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001028 buffer_consume(&input, block_size);
1029 }
1030 /* we have a partial packet of block_size bytes */
1031 need = 4 + packet_length - block_size;
1032 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1033 need, maclen));
1034 if (need % block_size != 0)
1035 fatal("padding error: need %d block %d mod %d",
1036 need, block_size, need % block_size);
1037 /*
1038 * check if the entire packet has been received and
1039 * decrypt into incoming_packet
1040 */
1041 if (buffer_len(&input) < need + maclen)
1042 return SSH_MSG_NONE;
1043#ifdef PACKET_DEBUG
1044 fprintf(stderr, "read_poll enc/full: ");
1045 buffer_dump(&input);
1046#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001047 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001048 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001049 buffer_consume(&input, need);
1050 /*
1051 * compute MAC over seqnr and packet,
1052 * increment sequence number for incoming packet
1053 */
Damien Miller4af51302000-04-16 11:18:38 +10001054 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001055 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001056 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001057 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001058 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001059 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001060 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001061 buffer_consume(&input, mac->mac_len);
1062 }
Damien Miller278f9072001-12-21 15:00:19 +11001063 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001064 *seqnr_p = p_read.seqnr;
1065 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001066 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001067 if (++p_read.packets == 0)
1068 if (!(datafellows & SSH_BUG_NOREKEY))
1069 fatal("XXX too many packets with same key");
1070 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001071
1072 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001073 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001074 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001075 DBG(debug("input: padlen %d", padlen));
1076 if (padlen < 4)
1077 packet_disconnect("Corrupted padlen %d on input.", padlen);
1078
1079 /* skip packet size + padlen, discard padding */
1080 buffer_consume(&incoming_packet, 4 + 1);
1081 buffer_consume_end(&incoming_packet, padlen);
1082
1083 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1084 if (comp && comp->enabled) {
1085 buffer_clear(&compression_buffer);
1086 buffer_uncompress(&incoming_packet, &compression_buffer);
1087 buffer_clear(&incoming_packet);
1088 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1089 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001090 DBG(debug("input: len after de-compress %d",
1091 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001092 }
1093 /*
1094 * get packet type, implies consume.
1095 * return length of payload (without type field)
1096 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001097 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001098 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1099 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001100 if (type == SSH2_MSG_NEWKEYS)
1101 set_newkeys(MODE_IN);
Damien Miller33b13562000-04-04 14:38:59 +10001102#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001103 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001104 buffer_dump(&incoming_packet);
1105#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001106 /* reset for next packet */
1107 packet_length = 0;
1108 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001109}
1110
1111int
Damien Millerdff50992002-01-22 23:16:32 +11001112packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001113{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001114 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001115 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001116 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001117
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001118 for (;;) {
1119 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001120 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001121 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001122 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001123 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001124 case SSH2_MSG_IGNORE:
1125 break;
1126 case SSH2_MSG_DEBUG:
1127 packet_get_char();
1128 msg = packet_get_string(NULL);
1129 debug("Remote: %.900s", msg);
1130 xfree(msg);
1131 msg = packet_get_string(NULL);
1132 xfree(msg);
1133 break;
1134 case SSH2_MSG_DISCONNECT:
1135 reason = packet_get_int();
1136 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001137 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001138 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001139 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001140 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001141 break;
Damien Miller659811f2002-01-22 23:23:11 +11001142 case SSH2_MSG_UNIMPLEMENTED:
1143 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001144 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1145 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001146 break;
Damien Miller33b13562000-04-04 14:38:59 +10001147 default:
1148 return type;
1149 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001150 }
Damien Miller33b13562000-04-04 14:38:59 +10001151 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001152 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001153 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001154 case SSH_MSG_IGNORE:
1155 break;
1156 case SSH_MSG_DEBUG:
1157 msg = packet_get_string(NULL);
1158 debug("Remote: %.900s", msg);
1159 xfree(msg);
1160 break;
1161 case SSH_MSG_DISCONNECT:
1162 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001163 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001164 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001165 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001166 xfree(msg);
1167 break;
1168 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001169 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001170 DBG(debug("received packet type %d", type));
1171 return type;
1172 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001173 }
Damien Miller33b13562000-04-04 14:38:59 +10001174 }
1175 }
1176}
1177
Damien Miller278f9072001-12-21 15:00:19 +11001178int
Damien Millerdff50992002-01-22 23:16:32 +11001179packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001180{
Damien Millerdff50992002-01-22 23:16:32 +11001181 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001182}
1183
Damien Miller5428f641999-11-25 11:54:57 +11001184/*
1185 * Buffers the given amount of input characters. This is intended to be used
1186 * together with packet_read_poll.
1187 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188
1189void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001190packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001191{
Damien Miller95def091999-11-25 00:26:21 +11001192 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193}
1194
1195/* Returns a character from the packet. */
1196
Ben Lindstrom46c16222000-12-22 01:43:59 +00001197u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001198packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199{
Damien Miller95def091999-11-25 00:26:21 +11001200 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001201
Damien Miller95def091999-11-25 00:26:21 +11001202 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001203 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204}
1205
1206/* Returns an integer from the packet data. */
1207
Ben Lindstrom46c16222000-12-22 01:43:59 +00001208u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001209packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001210{
Damien Miller95def091999-11-25 00:26:21 +11001211 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212}
1213
Damien Miller5428f641999-11-25 11:54:57 +11001214/*
1215 * Returns an arbitrary precision integer from the packet data. The integer
1216 * must have been initialized before this call.
1217 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218
1219void
Damien Millerd432ccf2002-01-22 23:14:44 +11001220packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001221{
Damien Miller76e1e362002-01-22 23:15:57 +11001222 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001223}
1224
Damien Miller33b13562000-04-04 14:38:59 +10001225void
Damien Millerd432ccf2002-01-22 23:14:44 +11001226packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001227{
Damien Miller76e1e362002-01-22 23:15:57 +11001228 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001229}
1230
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001231void *
Damien Millereccb9de2005-06-17 12:59:34 +10001232packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001233{
Damien Millereccb9de2005-06-17 12:59:34 +10001234 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001235
Damien Miller33b13562000-04-04 14:38:59 +10001236 if (length_ptr != NULL)
1237 *length_ptr = bytes;
1238 return buffer_ptr(&incoming_packet);
1239}
1240
Damien Miller4af51302000-04-16 11:18:38 +10001241int
1242packet_remaining(void)
1243{
1244 return buffer_len(&incoming_packet);
1245}
1246
Damien Miller5428f641999-11-25 11:54:57 +11001247/*
1248 * Returns a string from the packet data. The string is allocated using
1249 * xmalloc; it is the responsibility of the calling program to free it when
1250 * no longer needed. The length_ptr argument may be NULL, or point to an
1251 * integer into which the length of the string is stored.
1252 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001254void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001255packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256{
Damien Miller95def091999-11-25 00:26:21 +11001257 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258}
1259
Damien Miller5428f641999-11-25 11:54:57 +11001260/*
1261 * Sends a diagnostic message from the server to the client. This message
1262 * can be sent at any time (but not while constructing another message). The
1263 * message is printed immediately, but only if the client is being executed
1264 * in verbose mode. These messages are primarily intended to ease debugging
1265 * authentication problems. The length of the formatted message must not
1266 * exceed 1024 bytes. This will automatically call packet_write_wait.
1267 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268
1269void
Damien Miller95def091999-11-25 00:26:21 +11001270packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001271{
Damien Miller95def091999-11-25 00:26:21 +11001272 char buf[1024];
1273 va_list args;
1274
Ben Lindstroma14ee472000-12-07 01:24:58 +00001275 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1276 return;
1277
Damien Miller95def091999-11-25 00:26:21 +11001278 va_start(args, fmt);
1279 vsnprintf(buf, sizeof(buf), fmt, args);
1280 va_end(args);
1281
Damien Miller7c8af4f2000-05-01 08:24:07 +10001282 if (compat20) {
1283 packet_start(SSH2_MSG_DEBUG);
1284 packet_put_char(0); /* bool: always display */
1285 packet_put_cstring(buf);
1286 packet_put_cstring("");
1287 } else {
1288 packet_start(SSH_MSG_DEBUG);
1289 packet_put_cstring(buf);
1290 }
Damien Miller95def091999-11-25 00:26:21 +11001291 packet_send();
1292 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293}
1294
Damien Miller5428f641999-11-25 11:54:57 +11001295/*
1296 * Logs the error plus constructs and sends a disconnect packet, closes the
1297 * connection, and exits. This function never returns. The error message
1298 * should not contain a newline. The length of the formatted message must
1299 * not exceed 1024 bytes.
1300 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301
1302void
Damien Miller95def091999-11-25 00:26:21 +11001303packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001304{
Damien Miller95def091999-11-25 00:26:21 +11001305 char buf[1024];
1306 va_list args;
1307 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001308
Damien Miller95def091999-11-25 00:26:21 +11001309 if (disconnecting) /* Guard against recursive invocations. */
1310 fatal("packet_disconnect called recursively.");
1311 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312
Damien Miller5428f641999-11-25 11:54:57 +11001313 /*
1314 * Format the message. Note that the caller must make sure the
1315 * message is of limited size.
1316 */
Damien Miller95def091999-11-25 00:26:21 +11001317 va_start(args, fmt);
1318 vsnprintf(buf, sizeof(buf), fmt, args);
1319 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001321 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001322 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001323
Damien Miller95def091999-11-25 00:26:21 +11001324 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001325 if (compat20) {
1326 packet_start(SSH2_MSG_DISCONNECT);
1327 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1328 packet_put_cstring(buf);
1329 packet_put_cstring("");
1330 } else {
1331 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001332 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001333 }
Damien Miller95def091999-11-25 00:26:21 +11001334 packet_send();
1335 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001336
Damien Miller95def091999-11-25 00:26:21 +11001337 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001338 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001339
Damien Miller95def091999-11-25 00:26:21 +11001340 /* Close the connection. */
1341 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001342 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343}
1344
Damien Miller5428f641999-11-25 11:54:57 +11001345/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346
1347void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001348packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001349{
Damien Miller95def091999-11-25 00:26:21 +11001350 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001351
Damien Miller95def091999-11-25 00:26:21 +11001352 if (len > 0) {
1353 len = write(connection_out, buffer_ptr(&output), len);
1354 if (len <= 0) {
1355 if (errno == EAGAIN)
1356 return;
1357 else
1358 fatal("Write failed: %.100s", strerror(errno));
1359 }
1360 buffer_consume(&output, len);
1361 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001362}
1363
Damien Miller5428f641999-11-25 11:54:57 +11001364/*
1365 * Calls packet_write_poll repeatedly until all pending output data has been
1366 * written.
1367 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368
1369void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001370packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001371{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001372 fd_set *setp;
1373
1374 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1375 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001376 packet_write_poll();
1377 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001378 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1379 sizeof(fd_mask));
1380 FD_SET(connection_out, setp);
1381 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001382 (errno == EAGAIN || errno == EINTR))
1383 ;
Damien Miller95def091999-11-25 00:26:21 +11001384 packet_write_poll();
1385 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001386 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387}
1388
1389/* Returns true if there is buffered data to write to the connection. */
1390
1391int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001392packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393{
Damien Miller95def091999-11-25 00:26:21 +11001394 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001395}
1396
1397/* Returns true if there is not too much data to write to the connection. */
1398
1399int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001400packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001401{
Damien Miller95def091999-11-25 00:26:21 +11001402 if (interactive_mode)
1403 return buffer_len(&output) < 16384;
1404 else
1405 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406}
1407
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001408
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001409static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001410packet_set_tos(int interactive)
1411{
Damien Miller5924ceb2003-11-22 15:02:42 +11001412#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001413 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1414
1415 if (!packet_connection_is_on_socket() ||
1416 !packet_connection_is_ipv4())
1417 return;
1418 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1419 sizeof(tos)) < 0)
1420 error("setsockopt IP_TOS %d: %.100s:",
1421 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001422#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001423}
Ben Lindstroma7433982002-12-23 02:41:41 +00001424
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425/* Informs that the current session is interactive. Sets IP flags for that. */
1426
1427void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001428packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001430 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001432 if (called)
1433 return;
1434 called = 1;
1435
Damien Miller95def091999-11-25 00:26:21 +11001436 /* Record that we are in interactive mode. */
1437 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438
Damien Miller34132e52000-01-14 15:45:46 +11001439 /* Only set socket options if using a socket. */
1440 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001441 return;
Ben Lindstroma7433982002-12-23 02:41:41 +00001442 if (interactive)
Damien Miller398e1cf2002-02-05 11:52:13 +11001443 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001444 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445}
1446
1447/* Returns true if the current connection is interactive. */
1448
1449int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001450packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001451{
Damien Miller95def091999-11-25 00:26:21 +11001452 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453}
Damien Miller6162d121999-11-21 13:23:52 +11001454
Darren Tucker1f8311c2004-05-13 16:39:33 +10001455int
Darren Tucker502d3842003-06-28 12:38:01 +10001456packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001457{
Damien Miller95def091999-11-25 00:26:21 +11001458 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001459
Damien Miller95def091999-11-25 00:26:21 +11001460 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001461 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001462 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001463 return -1;
1464 }
1465 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001466 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001467 return -1;
1468 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001469 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001470 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001471 max_packet_size = s;
1472 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001473}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001474
Damien Miller9f643902001-11-12 11:02:52 +11001475/* roundup current message to pad bytes */
1476void
1477packet_add_padding(u_char pad)
1478{
1479 extra_pad = pad;
1480}
1481
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001482/*
1483 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001484 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001485 * byte SSH_MSG_IGNORE
1486 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001487 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001488 * All implementations MUST understand (and ignore) this message at any
1489 * time (after receiving the protocol version). No implementation is
1490 * required to send them. This message can be used as an additional
1491 * protection measure against advanced traffic analysis techniques.
1492 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001493void
1494packet_send_ignore(int nbytes)
1495{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001496 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001497 int i;
1498
1499 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001500 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001501 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001502 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001503 rnd = arc4random();
1504 packet_put_char(rnd & 0xff);
1505 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001506 }
1507}
Damien Millera5539d22003-04-09 20:50:06 +10001508
Darren Tucker1f8311c2004-05-13 16:39:33 +10001509#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001510int
1511packet_need_rekeying(void)
1512{
1513 if (datafellows & SSH_BUG_NOREKEY)
1514 return 0;
1515 return
1516 (p_send.packets > MAX_PACKETS) ||
1517 (p_read.packets > MAX_PACKETS) ||
1518 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1519 (max_blocks_in && (p_read.blocks > max_blocks_in));
1520}
1521
1522void
1523packet_set_rekey_limit(u_int32_t bytes)
1524{
1525 rekey_limit = bytes;
1526}