blob: 1a634cff45e35b7af5e2517f59860040df446455 [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.
16 * Copyright (c) 2000 Markus Friedl. All rights reserved.
17 *
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"
Ben Lindstromcb978aa2001-03-05 07:07:49 +000040RCSID("$OpenBSD: packet.c,v 1.56 2001/03/03 21:41:07 millert Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "xmalloc.h"
43#include "buffer.h"
44#include "packet.h"
45#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "getput.h"
48
49#include "compress.h"
50#include "deattack.h"
Damien Millerb38eff82000-04-01 11:09:21 +100051#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Miller33b13562000-04-04 14:38:59 +100053#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100055#include "ssh2.h"
56
Damien Miller874d77b2000-10-14 16:23:11 +110057#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100058#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000059#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "log.h"
61#include "canohost.h"
Damien Miller33b13562000-04-04 14:38:59 +100062
63#ifdef PACKET_DEBUG
64#define DBG(x) x
65#else
66#define DBG(x)
67#endif
68
Damien Miller5428f641999-11-25 11:54:57 +110069/*
70 * This variable contains the file descriptors used for communicating with
71 * the other side. connection_in is used for reading; connection_out for
72 * writing. These can be the same descriptor, in which case it is assumed to
73 * be a socket.
74 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075static int connection_in = -1;
76static int connection_out = -1;
77
Damien Miller5428f641999-11-25 11:54:57 +110078/*
79 * Cipher type. This value is only used to determine whether to pad the
80 * packets with zeroes or random data.
81 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082static int cipher_type = SSH_CIPHER_NONE;
83
84/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000085static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
87/* Encryption context for receiving data. This is only used for decryption. */
88static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110089
90/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091static CipherContext send_context;
92
93/* Buffer for raw input data from the socket. */
94static Buffer input;
95
96/* Buffer for raw output data going to the socket. */
97static Buffer output;
98
99/* Buffer for the partial outgoing packet being constructed. */
100static Buffer outgoing_packet;
101
102/* Buffer for the incoming packet currently being processed. */
103static Buffer incoming_packet;
104
105/* Scratch buffer for packet compression/decompression. */
106static Buffer compression_buffer;
107
108/* Flag indicating whether packet compression/decompression is enabled. */
109static int packet_compression = 0;
110
Damien Miller6162d121999-11-21 13:23:52 +1100111/* default maximum packet size */
112int max_packet_size = 32768;
113
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114/* Flag indicating whether this module has been initialized. */
115static int initialized = 0;
116
117/* Set to true if the connection is interactive. */
118static int interactive_mode = 0;
119
Damien Miller33b13562000-04-04 14:38:59 +1000120/* True if SSH2 packet format is used */
121int use_ssh2_packet_format = 0;
122
123/* Session key information for Encryption and MAC */
124Kex *kex = NULL;
125
126void
127packet_set_kex(Kex *k)
128{
129 if( k->mac[MODE_IN ].key == NULL ||
130 k->enc[MODE_IN ].key == NULL ||
131 k->enc[MODE_IN ].iv == NULL ||
132 k->mac[MODE_OUT].key == NULL ||
133 k->enc[MODE_OUT].key == NULL ||
134 k->enc[MODE_OUT].iv == NULL)
135 fatal("bad KEX");
136 kex = k;
137}
138void
139clear_enc_keys(Enc *enc, int len)
140{
141 memset(enc->iv, 0, len);
142 memset(enc->key, 0, len);
143 xfree(enc->iv);
144 xfree(enc->key);
145 enc->iv = NULL;
146 enc->key = NULL;
147}
148void
149packet_set_ssh2_format(void)
150{
Damien Miller35dabd02000-05-01 21:10:33 +1000151 DBG(debug("use_ssh2_packet_format"));
Damien Miller33b13562000-04-04 14:38:59 +1000152 use_ssh2_packet_format = 1;
153}
154
Damien Miller5428f641999-11-25 11:54:57 +1100155/*
156 * Sets the descriptors used for communication. Disables encryption until
157 * packet_set_encryption_key is called.
158 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159void
160packet_set_connection(int fd_in, int fd_out)
161{
Damien Miller874d77b2000-10-14 16:23:11 +1100162 Cipher *none = cipher_by_name("none");
163 if (none == NULL)
164 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100165 connection_in = fd_in;
166 connection_out = fd_out;
167 cipher_type = SSH_CIPHER_NONE;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000168 cipher_init(&send_context, none, (u_char *) "", 0, NULL, 0);
169 cipher_init(&receive_context, none, (u_char *) "", 0, NULL, 0);
Damien Miller95def091999-11-25 00:26:21 +1100170 if (!initialized) {
171 initialized = 1;
172 buffer_init(&input);
173 buffer_init(&output);
174 buffer_init(&outgoing_packet);
175 buffer_init(&incoming_packet);
176 }
177 /* Kludge: arrange the close function to be called from fatal(). */
178 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179}
180
Damien Miller34132e52000-01-14 15:45:46 +1100181/* Returns 1 if remote host is connected via socket, 0 if not. */
182
183int
184packet_connection_is_on_socket()
185{
186 struct sockaddr_storage from, to;
187 socklen_t fromlen, tolen;
188
189 /* filedescriptors in and out are the same, so it's a socket */
190 if (connection_in == connection_out)
191 return 1;
192 fromlen = sizeof(from);
193 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100194 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100195 return 0;
196 tolen = sizeof(to);
197 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100198 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100199 return 0;
200 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
201 return 0;
202 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
203 return 0;
204 return 1;
205}
206
207/* returns 1 if connection is via ipv4 */
208
209int
210packet_connection_is_ipv4()
211{
212 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100213 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100214
215 memset(&to, 0, sizeof(to));
216 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
217 return 0;
218 if (to.ss_family != AF_INET)
219 return 0;
220 return 1;
221}
222
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223/* Sets the connection into non-blocking mode. */
224
225void
226packet_set_nonblocking()
227{
Damien Miller95def091999-11-25 00:26:21 +1100228 /* Set the socket into non-blocking mode. */
229 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
230 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231
Damien Miller95def091999-11-25 00:26:21 +1100232 if (connection_out != connection_in) {
233 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
234 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
235 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236}
237
238/* Returns the socket used for reading. */
239
240int
241packet_get_connection_in()
242{
Damien Miller95def091999-11-25 00:26:21 +1100243 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244}
245
246/* Returns the descriptor used for writing. */
247
248int
249packet_get_connection_out()
250{
Damien Miller95def091999-11-25 00:26:21 +1100251 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252}
253
254/* Closes the connection and clears and frees internal data structures. */
255
256void
257packet_close()
258{
Damien Miller95def091999-11-25 00:26:21 +1100259 if (!initialized)
260 return;
261 initialized = 0;
262 if (connection_in == connection_out) {
263 shutdown(connection_out, SHUT_RDWR);
264 close(connection_out);
265 } else {
266 close(connection_in);
267 close(connection_out);
268 }
269 buffer_free(&input);
270 buffer_free(&output);
271 buffer_free(&outgoing_packet);
272 buffer_free(&incoming_packet);
273 if (packet_compression) {
274 buffer_free(&compression_buffer);
275 buffer_compress_uninit();
276 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277}
278
279/* Sets remote side protocol flags. */
280
281void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000282packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283{
Damien Miller95def091999-11-25 00:26:21 +1100284 remote_protocol_flags = protocol_flags;
285 channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286}
287
288/* Returns the remote protocol flags set earlier by the above function. */
289
Ben Lindstrom46c16222000-12-22 01:43:59 +0000290u_int
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291packet_get_protocol_flags()
292{
Damien Miller95def091999-11-25 00:26:21 +1100293 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294}
295
Damien Miller5428f641999-11-25 11:54:57 +1100296/*
297 * Starts packet compression from the next packet on in both directions.
298 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
299 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300
Damien Miller33b13562000-04-04 14:38:59 +1000301/*** XXXXX todo: kex means re-init */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302void
303packet_start_compression(int level)
304{
Damien Miller95def091999-11-25 00:26:21 +1100305 if (packet_compression)
306 fatal("Compression already enabled.");
307 packet_compression = 1;
308 buffer_init(&compression_buffer);
309 buffer_compress_init(level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310}
311
Damien Miller5428f641999-11-25 11:54:57 +1100312/*
313 * Encrypts the given number of bytes, copying from src to dest. bytes is
314 * known to be a multiple of 8.
315 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316
317void
Damien Miller95def091999-11-25 00:26:21 +1100318packet_encrypt(CipherContext * cc, void *dest, void *src,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000319 u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320{
Damien Miller95def091999-11-25 00:26:21 +1100321 cipher_encrypt(cc, dest, src, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322}
323
Damien Miller5428f641999-11-25 11:54:57 +1100324/*
325 * Decrypts the given number of bytes, copying from src to dest. bytes is
326 * known to be a multiple of 8.
327 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328
329void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000330packet_decrypt(CipherContext *context, void *dest, void *src, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331{
Damien Miller5428f641999-11-25 11:54:57 +1100332 /*
333 * Cryptographic attack detector for ssh - Modifications for packet.c
334 * (C)1998 CORE-SDI, Buenos Aires Argentina Ariel Futoransky(futo@core-sdi.com)
335 */
Damien Miller874d77b2000-10-14 16:23:11 +1100336 if (!compat20 &&
337 context->cipher->number != SSH_CIPHER_NONE &&
338 detect_attack(src, bytes, NULL) == DEATTACK_DETECTED)
Damien Miller95def091999-11-25 00:26:21 +1100339 packet_disconnect("crc32 compensation attack: network attack detected");
340
Damien Miller874d77b2000-10-14 16:23:11 +1100341 cipher_decrypt(context, dest, src, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342}
343
Damien Miller5428f641999-11-25 11:54:57 +1100344/*
345 * Causes any further packets to be encrypted using the given key. The same
346 * key is used for both sending and reception. However, both directions are
347 * encrypted independently of each other.
348 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
350void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000351packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100352 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353{
Damien Miller874d77b2000-10-14 16:23:11 +1100354 Cipher *cipher = cipher_by_number(number);
355 if (cipher == NULL)
356 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000357 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100358 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
359 cipher_init(&receive_context, cipher, key, keylen, NULL, 0);
360 cipher_init(&send_context, cipher, key, keylen, NULL, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361}
362
363/* Starts constructing a packet to send. */
364
365void
Damien Miller33b13562000-04-04 14:38:59 +1000366packet_start1(int type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367{
Damien Miller95def091999-11-25 00:26:21 +1100368 char buf[9];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369
Damien Miller95def091999-11-25 00:26:21 +1100370 buffer_clear(&outgoing_packet);
371 memset(buf, 0, 8);
372 buf[8] = type;
373 buffer_append(&outgoing_packet, buf, 9);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374}
375
Damien Miller33b13562000-04-04 14:38:59 +1000376void
377packet_start2(int type)
378{
379 char buf[4+1+1];
380
381 buffer_clear(&outgoing_packet);
382 memset(buf, 0, sizeof buf);
383 /* buf[0..3] = payload_len; */
384 /* buf[4] = pad_len; */
385 buf[5] = type & 0xff;
386 buffer_append(&outgoing_packet, buf, sizeof buf);
387}
388
389void
390packet_start(int type)
391{
Ben Lindstrom204e4882001-03-05 06:47:00 +0000392 DBG(debug("packet_start[%d]", type));
Damien Miller33b13562000-04-04 14:38:59 +1000393 if (use_ssh2_packet_format)
394 packet_start2(type);
395 else
396 packet_start1(type);
397}
398
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399/* Appends a character to the packet data. */
400
401void
402packet_put_char(int value)
403{
Damien Miller95def091999-11-25 00:26:21 +1100404 char ch = value;
405 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406}
407
408/* Appends an integer to the packet data. */
409
410void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000411packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412{
Damien Miller95def091999-11-25 00:26:21 +1100413 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414}
415
416/* Appends a string to packet data. */
417
418void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000419packet_put_string(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420{
Damien Miller95def091999-11-25 00:26:21 +1100421 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422}
Damien Miller33b13562000-04-04 14:38:59 +1000423void
424packet_put_cstring(const char *str)
425{
426 buffer_put_string(&outgoing_packet, str, strlen(str));
427}
428
429void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000430packet_put_raw(const char *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000431{
432 buffer_append(&outgoing_packet, buf, len);
433}
434
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435
436/* Appends an arbitrary precision integer to packet data. */
437
438void
Damien Miller95def091999-11-25 00:26:21 +1100439packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440{
Damien Miller95def091999-11-25 00:26:21 +1100441 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442}
Damien Miller33b13562000-04-04 14:38:59 +1000443void
444packet_put_bignum2(BIGNUM * value)
445{
446 buffer_put_bignum2(&outgoing_packet, value);
447}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448
Damien Miller5428f641999-11-25 11:54:57 +1100449/*
450 * Finalizes and sends the packet. If the encryption key has been set,
451 * encrypts the packet before sending.
452 */
Damien Miller95def091999-11-25 00:26:21 +1100453
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000455packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456{
Damien Miller95def091999-11-25 00:26:21 +1100457 char buf[8], *cp;
458 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000459 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100460 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461
Damien Miller5428f641999-11-25 11:54:57 +1100462 /*
463 * If using packet compression, compress the payload of the outgoing
464 * packet.
465 */
Damien Miller95def091999-11-25 00:26:21 +1100466 if (packet_compression) {
467 buffer_clear(&compression_buffer);
468 /* Skip padding. */
469 buffer_consume(&outgoing_packet, 8);
470 /* padding */
471 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
472 buffer_compress(&outgoing_packet, &compression_buffer);
473 buffer_clear(&outgoing_packet);
474 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
475 buffer_len(&compression_buffer));
476 }
477 /* Compute packet length without padding (add checksum, remove padding). */
478 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479
Damien Millere247cc42000-05-07 12:03:14 +1000480 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100481 padding = 8 - len % 8;
482 if (cipher_type != SSH_CIPHER_NONE) {
483 cp = buffer_ptr(&outgoing_packet);
484 for (i = 0; i < padding; i++) {
485 if (i % 4 == 0)
486 rand = arc4random();
487 cp[7 - i] = rand & 0xff;
488 rand >>= 8;
489 }
490 }
491 buffer_consume(&outgoing_packet, 8 - padding);
492
493 /* Add check bytes. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000494 checksum = ssh_crc32((u_char *) buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000495 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100496 PUT_32BIT(buf, checksum);
497 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498
499#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100500 fprintf(stderr, "packet_send plain: ");
501 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502#endif
503
Damien Miller95def091999-11-25 00:26:21 +1100504 /* Append to output. */
505 PUT_32BIT(buf, len);
506 buffer_append(&output, buf, 4);
507 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
508 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
509 buffer_len(&outgoing_packet));
510
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100512 fprintf(stderr, "encrypted: ");
513 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514#endif
515
Damien Miller95def091999-11-25 00:26:21 +1100516 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517
Damien Miller5428f641999-11-25 11:54:57 +1100518 /*
519 * Note that the packet is now only buffered in output. It won\'t be
520 * actually sent until packet_write_wait or packet_write_poll is
521 * called.
522 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523}
524
Damien Miller5428f641999-11-25 11:54:57 +1100525/*
Damien Miller33b13562000-04-04 14:38:59 +1000526 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
527 */
528void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000529packet_send2(void)
Damien Miller33b13562000-04-04 14:38:59 +1000530{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000531 static u_int32_t seqnr = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000532 u_char *macbuf = NULL;
Damien Miller33b13562000-04-04 14:38:59 +1000533 char *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000534 u_int packet_length = 0;
535 u_int i, padlen, len;
Damien Miller33b13562000-04-04 14:38:59 +1000536 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000537 int type;
538 Enc *enc = NULL;
539 Mac *mac = NULL;
540 Comp *comp = NULL;
541 int block_size;
542
543 if (kex != NULL) {
544 enc = &kex->enc[MODE_OUT];
545 mac = &kex->mac[MODE_OUT];
546 comp = &kex->comp[MODE_OUT];
547 }
Damien Miller874d77b2000-10-14 16:23:11 +1100548 block_size = enc ? enc->cipher->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000549
550 cp = buffer_ptr(&outgoing_packet);
551 type = cp[5] & 0xff;
552
553#ifdef PACKET_DEBUG
554 fprintf(stderr, "plain: ");
555 buffer_dump(&outgoing_packet);
556#endif
557
558 if (comp && comp->enabled) {
559 len = buffer_len(&outgoing_packet);
560 /* skip header, compress only payload */
561 buffer_consume(&outgoing_packet, 5);
562 buffer_clear(&compression_buffer);
563 buffer_compress(&outgoing_packet, &compression_buffer);
564 buffer_clear(&outgoing_packet);
565 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
566 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
567 buffer_len(&compression_buffer));
568 DBG(debug("compression: raw %d compressed %d", len,
569 buffer_len(&outgoing_packet)));
570 }
571
572 /* sizeof (packet_len + pad_len + payload) */
573 len = buffer_len(&outgoing_packet);
574
575 /*
576 * calc size of padding, alloc space, get random data,
577 * minimum padding is 4 bytes
578 */
579 padlen = block_size - (len % block_size);
580 if (padlen < 4)
581 padlen += block_size;
582 buffer_append_space(&outgoing_packet, &cp, padlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100583 if (enc && enc->cipher->number != SSH_CIPHER_NONE) {
Damien Millere247cc42000-05-07 12:03:14 +1000584 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000585 for (i = 0; i < padlen; i++) {
586 if (i % 4 == 0)
587 rand = arc4random();
588 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000589 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000590 }
Damien Millere247cc42000-05-07 12:03:14 +1000591 } else {
592 /* clear padding */
593 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000594 }
595 /* packet_length includes payload, padding and padding length field */
596 packet_length = buffer_len(&outgoing_packet) - 4;
597 cp = buffer_ptr(&outgoing_packet);
598 PUT_32BIT(cp, packet_length);
599 cp[4] = padlen & 0xff;
600 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
601
602 /* compute MAC over seqnr and packet(length fields, payload, padding) */
603 if (mac && mac->enabled) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000604 macbuf = mac_compute(mac, seqnr,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000605 (u_char *) buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000606 buffer_len(&outgoing_packet));
Damien Miller874d77b2000-10-14 16:23:11 +1100607 DBG(debug("done calc MAC out #%d", seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000608 }
609 /* encrypt packet and append to output buffer. */
610 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
611 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
612 buffer_len(&outgoing_packet));
613 /* append unencrypted MAC */
614 if (mac && mac->enabled)
615 buffer_append(&output, (char *)macbuf, mac->mac_len);
616#ifdef PACKET_DEBUG
617 fprintf(stderr, "encrypted: ");
618 buffer_dump(&output);
619#endif
Damien Miller4af51302000-04-16 11:18:38 +1000620 /* increment sequence number for outgoing packets */
621 if (++seqnr == 0)
622 log("outgoing seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000623 buffer_clear(&outgoing_packet);
624
625 if (type == SSH2_MSG_NEWKEYS) {
626 if (kex==NULL || mac==NULL || enc==NULL || comp==NULL)
627 fatal("packet_send2: no KEX");
628 if (mac->md != NULL)
629 mac->enabled = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100630 DBG(debug("cipher_init send_context"));
631 cipher_init(&send_context, enc->cipher,
632 enc->key, enc->cipher->key_len,
633 enc->iv, enc->cipher->block_size);
Damien Miller33b13562000-04-04 14:38:59 +1000634 clear_enc_keys(enc, kex->we_need);
635 if (comp->type != 0 && comp->enabled == 0) {
636 comp->enabled = 1;
637 if (! packet_compression)
638 packet_start_compression(6);
639 }
640 }
641}
642
643void
644packet_send()
645{
646 if (use_ssh2_packet_format)
647 packet_send2();
648 else
649 packet_send1();
650 DBG(debug("packet_send done"));
651}
652
Damien Miller33b13562000-04-04 14:38:59 +1000653/*
Damien Miller5428f641999-11-25 11:54:57 +1100654 * Waits until a packet has been received, and returns its type. Note that
655 * no other data is processed until this returns, so this function should not
656 * be used during the interactive session.
657 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658
659int
660packet_read(int *payload_len_ptr)
661{
Damien Miller95def091999-11-25 00:26:21 +1100662 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000663 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100664 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000665 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000666
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000667 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
668 sizeof(fd_mask));
669
Damien Miller95def091999-11-25 00:26:21 +1100670 /* Since we are blocking, ensure that all written packets have been sent. */
671 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672
Damien Miller95def091999-11-25 00:26:21 +1100673 /* Stay in the loop until we have received a complete packet. */
674 for (;;) {
675 /* Try to read a packet from the buffer. */
676 type = packet_read_poll(payload_len_ptr);
Damien Millere247cc42000-05-07 12:03:14 +1000677 if (!use_ssh2_packet_format && (
678 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100679 || type == SSH_SMSG_FAILURE
680 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000681 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller95def091999-11-25 00:26:21 +1100682 packet_integrity_check(*payload_len_ptr, 0, type);
683 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000684 if (type != SSH_MSG_NONE) {
685 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100686 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000687 }
Damien Miller5428f641999-11-25 11:54:57 +1100688 /*
689 * Otherwise, wait for some data to arrive, add it to the
690 * buffer, and try again.
691 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000692 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
693 sizeof(fd_mask));
694 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100695
Damien Miller95def091999-11-25 00:26:21 +1100696 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000697 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000698 (errno == EAGAIN || errno == EINTR))
699 ;
Damien Miller5428f641999-11-25 11:54:57 +1100700
Damien Miller95def091999-11-25 00:26:21 +1100701 /* Read data from the socket. */
702 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100703 if (len == 0) {
704 log("Connection closed by %.200s", get_remote_ipaddr());
705 fatal_cleanup();
706 }
Damien Miller95def091999-11-25 00:26:21 +1100707 if (len < 0)
708 fatal("Read from socket failed: %.100s", strerror(errno));
709 /* Append it to the buffer. */
710 packet_process_incoming(buf, len);
711 }
712 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713}
714
Damien Miller5428f641999-11-25 11:54:57 +1100715/*
716 * Waits until a packet has been received, verifies that its type matches
717 * that given, and gives a fatal error and exits if there is a mismatch.
718 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719
720void
721packet_read_expect(int *payload_len_ptr, int expected_type)
722{
Damien Miller95def091999-11-25 00:26:21 +1100723 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724
Damien Miller95def091999-11-25 00:26:21 +1100725 type = packet_read(payload_len_ptr);
726 if (type != expected_type)
727 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000728 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729}
730
731/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100732 * packet_process_incoming. If so, reads the packet; otherwise returns
733 * SSH_MSG_NONE. This does not wait for data from the connection.
734 *
735 * SSH_MSG_DISCONNECT is handled specially here. Also,
736 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
737 * to higher levels.
738 *
739 * The returned payload_len does include space consumed by:
740 * Packet length
741 * Padding
742 * Packet type
743 * Check bytes
744 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745
746int
Damien Miller33b13562000-04-04 14:38:59 +1000747packet_read_poll1(int *payload_len_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000748{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000749 u_int len, padded_len;
750 u_char *ucp;
Damien Miller33b13562000-04-04 14:38:59 +1000751 char buf[8], *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000752 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753
Damien Miller95def091999-11-25 00:26:21 +1100754 /* Check if input size is less than minimum packet size. */
755 if (buffer_len(&input) < 4 + 8)
756 return SSH_MSG_NONE;
757 /* Get length of incoming packet. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000758 ucp = (u_char *) buffer_ptr(&input);
Damien Miller95def091999-11-25 00:26:21 +1100759 len = GET_32BIT(ucp);
760 if (len < 1 + 2 + 2 || len > 256 * 1024)
761 packet_disconnect("Bad packet length %d.", len);
762 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000763
Damien Miller95def091999-11-25 00:26:21 +1100764 /* Check if the packet has been entirely received. */
765 if (buffer_len(&input) < 4 + padded_len)
766 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767
Damien Miller95def091999-11-25 00:26:21 +1100768 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000769
Damien Miller95def091999-11-25 00:26:21 +1100770 /* Consume packet length. */
771 buffer_consume(&input, 4);
772
773 /* Copy data to incoming_packet. */
774 buffer_clear(&incoming_packet);
775 buffer_append_space(&incoming_packet, &cp, padded_len);
776 packet_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len);
777 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778
779#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100780 fprintf(stderr, "read_poll plain: ");
781 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783
Damien Miller95def091999-11-25 00:26:21 +1100784 /* Compute packet checksum. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000785 checksum = ssh_crc32((u_char *) buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000786 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787
Damien Miller95def091999-11-25 00:26:21 +1100788 /* Skip padding. */
789 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100790
Damien Miller95def091999-11-25 00:26:21 +1100791 /* Test check bytes. */
Damien Millerfd7c9111999-11-08 16:15:55 +1100792
Damien Miller95def091999-11-25 00:26:21 +1100793 if (len != buffer_len(&incoming_packet))
794 packet_disconnect("packet_read_poll: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000795 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796
Ben Lindstrom46c16222000-12-22 01:43:59 +0000797 ucp = (u_char *) buffer_ptr(&incoming_packet) + len - 4;
Damien Miller95def091999-11-25 00:26:21 +1100798 stored_checksum = GET_32BIT(ucp);
799 if (checksum != stored_checksum)
800 packet_disconnect("Corrupted check bytes on input.");
801 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000802
Damien Miller95def091999-11-25 00:26:21 +1100803 /* If using packet compression, decompress the packet. */
804 if (packet_compression) {
805 buffer_clear(&compression_buffer);
806 buffer_uncompress(&incoming_packet, &compression_buffer);
807 buffer_clear(&incoming_packet);
808 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000809 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100810 }
811 /* Get packet type. */
812 buffer_get(&incoming_packet, &buf[0], 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000813
Damien Miller95def091999-11-25 00:26:21 +1100814 /* Return length of payload (without type field). */
815 *payload_len_ptr = buffer_len(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816
Damien Miller95def091999-11-25 00:26:21 +1100817 /* Return type. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000818 return (u_char) buf[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819}
Damien Miller95def091999-11-25 00:26:21 +1100820
Damien Miller33b13562000-04-04 14:38:59 +1000821int
822packet_read_poll2(int *payload_len_ptr)
823{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000824 static u_int32_t seqnr = 0;
825 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000826 u_int padlen, need;
827 u_char buf[8], *macbuf;
828 u_char *ucp;
Damien Miller33b13562000-04-04 14:38:59 +1000829 char *cp;
Damien Miller33b13562000-04-04 14:38:59 +1000830 int type;
831 int maclen, block_size;
832 Enc *enc = NULL;
833 Mac *mac = NULL;
834 Comp *comp = NULL;
835
836 if (kex != NULL) {
837 enc = &kex->enc[MODE_IN];
838 mac = &kex->mac[MODE_IN];
839 comp = &kex->comp[MODE_IN];
840 }
841 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100842 block_size = enc ? enc->cipher->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000843
844 if (packet_length == 0) {
845 /*
846 * check if input size is less than the cipher block size,
847 * decrypt first block and extract length of incoming packet
848 */
849 if (buffer_len(&input) < block_size)
850 return SSH_MSG_NONE;
851 buffer_clear(&incoming_packet);
852 buffer_append_space(&incoming_packet, &cp, block_size);
853 packet_decrypt(&receive_context, cp, buffer_ptr(&input),
854 block_size);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000855 ucp = (u_char *) buffer_ptr(&incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000856 packet_length = GET_32BIT(ucp);
857 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
858 buffer_dump(&incoming_packet);
859 packet_disconnect("Bad packet length %d.", packet_length);
860 }
861 DBG(debug("input: packet len %d", packet_length+4));
862 buffer_consume(&input, block_size);
863 }
864 /* we have a partial packet of block_size bytes */
865 need = 4 + packet_length - block_size;
866 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
867 need, maclen));
868 if (need % block_size != 0)
869 fatal("padding error: need %d block %d mod %d",
870 need, block_size, need % block_size);
871 /*
872 * check if the entire packet has been received and
873 * decrypt into incoming_packet
874 */
875 if (buffer_len(&input) < need + maclen)
876 return SSH_MSG_NONE;
877#ifdef PACKET_DEBUG
878 fprintf(stderr, "read_poll enc/full: ");
879 buffer_dump(&input);
880#endif
881 buffer_append_space(&incoming_packet, &cp, need);
882 packet_decrypt(&receive_context, cp, buffer_ptr(&input), need);
883 buffer_consume(&input, need);
884 /*
885 * compute MAC over seqnr and packet,
886 * increment sequence number for incoming packet
887 */
Damien Miller4af51302000-04-16 11:18:38 +1000888 if (mac && mac->enabled) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000889 macbuf = mac_compute(mac, seqnr,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000890 (u_char *) buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000891 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000892 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100893 packet_disconnect("Corrupted MAC on input.");
894 DBG(debug("MAC #%d ok", seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000895 buffer_consume(&input, mac->mac_len);
896 }
Damien Miller4af51302000-04-16 11:18:38 +1000897 if (++seqnr == 0)
898 log("incoming seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000899
900 /* get padlen */
901 cp = buffer_ptr(&incoming_packet) + 4;
902 padlen = *cp & 0xff;
903 DBG(debug("input: padlen %d", padlen));
904 if (padlen < 4)
905 packet_disconnect("Corrupted padlen %d on input.", padlen);
906
907 /* skip packet size + padlen, discard padding */
908 buffer_consume(&incoming_packet, 4 + 1);
909 buffer_consume_end(&incoming_packet, padlen);
910
911 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
912 if (comp && comp->enabled) {
913 buffer_clear(&compression_buffer);
914 buffer_uncompress(&incoming_packet, &compression_buffer);
915 buffer_clear(&incoming_packet);
916 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
917 buffer_len(&compression_buffer));
918 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
919 }
920 /*
921 * get packet type, implies consume.
922 * return length of payload (without type field)
923 */
924 buffer_get(&incoming_packet, (char *)&buf[0], 1);
925 *payload_len_ptr = buffer_len(&incoming_packet);
926
927 /* reset for next packet */
928 packet_length = 0;
929
930 /* extract packet type */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000931 type = (u_char)buf[0];
Damien Miller33b13562000-04-04 14:38:59 +1000932
933 if (type == SSH2_MSG_NEWKEYS) {
934 if (kex==NULL || mac==NULL || enc==NULL || comp==NULL)
935 fatal("packet_read_poll2: no KEX");
936 if (mac->md != NULL)
937 mac->enabled = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100938 DBG(debug("cipher_init receive_context"));
939 cipher_init(&receive_context, enc->cipher,
940 enc->key, enc->cipher->key_len,
941 enc->iv, enc->cipher->block_size);
Damien Miller33b13562000-04-04 14:38:59 +1000942 clear_enc_keys(enc, kex->we_need);
943 if (comp->type != 0 && comp->enabled == 0) {
944 comp->enabled = 1;
945 if (! packet_compression)
946 packet_start_compression(6);
947 }
948 }
949
950#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +0000951 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +1000952 buffer_dump(&incoming_packet);
953#endif
Ben Lindstrom46c16222000-12-22 01:43:59 +0000954 return (u_char)type;
Damien Miller33b13562000-04-04 14:38:59 +1000955}
956
957int
958packet_read_poll(int *payload_len_ptr)
959{
960 char *msg;
961 for (;;) {
962 int type = use_ssh2_packet_format ?
963 packet_read_poll2(payload_len_ptr):
964 packet_read_poll1(payload_len_ptr);
965
966 if(compat20) {
967 int reason;
968 if (type != 0)
969 DBG(debug("received packet type %d", type));
970 switch(type) {
971 case SSH2_MSG_IGNORE:
972 break;
973 case SSH2_MSG_DEBUG:
974 packet_get_char();
975 msg = packet_get_string(NULL);
976 debug("Remote: %.900s", msg);
977 xfree(msg);
978 msg = packet_get_string(NULL);
979 xfree(msg);
980 break;
981 case SSH2_MSG_DISCONNECT:
982 reason = packet_get_int();
983 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000984 log("Received disconnect from %s: %d: %.400s", get_remote_ipaddr(),
985 reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +1000986 xfree(msg);
987 fatal_cleanup();
988 break;
989 default:
990 return type;
991 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000992 }
Damien Miller33b13562000-04-04 14:38:59 +1000993 } else {
994 switch(type) {
995 case SSH_MSG_IGNORE:
996 break;
997 case SSH_MSG_DEBUG:
998 msg = packet_get_string(NULL);
999 debug("Remote: %.900s", msg);
1000 xfree(msg);
1001 break;
1002 case SSH_MSG_DISCONNECT:
1003 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001004 log("Received disconnect from %s: %.400s", get_remote_ipaddr(),
1005 msg);
Damien Miller33b13562000-04-04 14:38:59 +10001006 fatal_cleanup();
1007 xfree(msg);
1008 break;
1009 default:
1010 if (type != 0)
1011 DBG(debug("received packet type %d", type));
1012 return type;
1013 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001014 }
Damien Miller33b13562000-04-04 14:38:59 +10001015 }
1016 }
1017}
1018
Damien Miller5428f641999-11-25 11:54:57 +11001019/*
1020 * Buffers the given amount of input characters. This is intended to be used
1021 * together with packet_read_poll.
1022 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
1024void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001025packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001026{
Damien Miller95def091999-11-25 00:26:21 +11001027 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028}
1029
1030/* Returns a character from the packet. */
1031
Ben Lindstrom46c16222000-12-22 01:43:59 +00001032u_int
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001033packet_get_char()
1034{
Damien Miller95def091999-11-25 00:26:21 +11001035 char ch;
1036 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001037 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001038}
1039
1040/* Returns an integer from the packet data. */
1041
Ben Lindstrom46c16222000-12-22 01:43:59 +00001042u_int
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043packet_get_int()
1044{
Damien Miller95def091999-11-25 00:26:21 +11001045 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001046}
1047
Damien Miller5428f641999-11-25 11:54:57 +11001048/*
1049 * Returns an arbitrary precision integer from the packet data. The integer
1050 * must have been initialized before this call.
1051 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052
1053void
Damien Miller95def091999-11-25 00:26:21 +11001054packet_get_bignum(BIGNUM * value, int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055{
Damien Miller95def091999-11-25 00:26:21 +11001056 *length_ptr = buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001057}
1058
Damien Miller33b13562000-04-04 14:38:59 +10001059void
1060packet_get_bignum2(BIGNUM * value, int *length_ptr)
1061{
1062 *length_ptr = buffer_get_bignum2(&incoming_packet, value);
1063}
1064
1065char *
1066packet_get_raw(int *length_ptr)
1067{
1068 int bytes = buffer_len(&incoming_packet);
1069 if (length_ptr != NULL)
1070 *length_ptr = bytes;
1071 return buffer_ptr(&incoming_packet);
1072}
1073
Damien Miller4af51302000-04-16 11:18:38 +10001074int
1075packet_remaining(void)
1076{
1077 return buffer_len(&incoming_packet);
1078}
1079
Damien Miller5428f641999-11-25 11:54:57 +11001080/*
1081 * Returns a string from the packet data. The string is allocated using
1082 * xmalloc; it is the responsibility of the calling program to free it when
1083 * no longer needed. The length_ptr argument may be NULL, or point to an
1084 * integer into which the length of the string is stored.
1085 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001086
Damien Miller5428f641999-11-25 11:54:57 +11001087char *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001088packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001089{
Damien Miller95def091999-11-25 00:26:21 +11001090 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001091}
1092
Damien Miller5428f641999-11-25 11:54:57 +11001093/*
1094 * Sends a diagnostic message from the server to the client. This message
1095 * can be sent at any time (but not while constructing another message). The
1096 * message is printed immediately, but only if the client is being executed
1097 * in verbose mode. These messages are primarily intended to ease debugging
1098 * authentication problems. The length of the formatted message must not
1099 * exceed 1024 bytes. This will automatically call packet_write_wait.
1100 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101
1102void
Damien Miller95def091999-11-25 00:26:21 +11001103packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104{
Damien Miller95def091999-11-25 00:26:21 +11001105 char buf[1024];
1106 va_list args;
1107
Ben Lindstroma14ee472000-12-07 01:24:58 +00001108 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1109 return;
1110
Damien Miller95def091999-11-25 00:26:21 +11001111 va_start(args, fmt);
1112 vsnprintf(buf, sizeof(buf), fmt, args);
1113 va_end(args);
1114
Damien Miller7c8af4f2000-05-01 08:24:07 +10001115 if (compat20) {
1116 packet_start(SSH2_MSG_DEBUG);
1117 packet_put_char(0); /* bool: always display */
1118 packet_put_cstring(buf);
1119 packet_put_cstring("");
1120 } else {
1121 packet_start(SSH_MSG_DEBUG);
1122 packet_put_cstring(buf);
1123 }
Damien Miller95def091999-11-25 00:26:21 +11001124 packet_send();
1125 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001126}
1127
Damien Miller5428f641999-11-25 11:54:57 +11001128/*
1129 * Logs the error plus constructs and sends a disconnect packet, closes the
1130 * connection, and exits. This function never returns. The error message
1131 * should not contain a newline. The length of the formatted message must
1132 * not exceed 1024 bytes.
1133 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001134
1135void
Damien Miller95def091999-11-25 00:26:21 +11001136packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137{
Damien Miller95def091999-11-25 00:26:21 +11001138 char buf[1024];
1139 va_list args;
1140 static int disconnecting = 0;
1141 if (disconnecting) /* Guard against recursive invocations. */
1142 fatal("packet_disconnect called recursively.");
1143 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144
Damien Miller5428f641999-11-25 11:54:57 +11001145 /*
1146 * Format the message. Note that the caller must make sure the
1147 * message is of limited size.
1148 */
Damien Miller95def091999-11-25 00:26:21 +11001149 va_start(args, fmt);
1150 vsnprintf(buf, sizeof(buf), fmt, args);
1151 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001152
Damien Miller95def091999-11-25 00:26:21 +11001153 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001154 if (compat20) {
1155 packet_start(SSH2_MSG_DISCONNECT);
1156 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1157 packet_put_cstring(buf);
1158 packet_put_cstring("");
1159 } else {
1160 packet_start(SSH_MSG_DISCONNECT);
1161 packet_put_string(buf, strlen(buf));
1162 }
Damien Miller95def091999-11-25 00:26:21 +11001163 packet_send();
1164 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165
Damien Miller95def091999-11-25 00:26:21 +11001166 /* Stop listening for connections. */
1167 channel_stop_listening();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168
Damien Miller95def091999-11-25 00:26:21 +11001169 /* Close the connection. */
1170 packet_close();
1171
1172 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001173 log("Disconnecting: %.100s", buf);
1174 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175}
1176
Damien Miller5428f641999-11-25 11:54:57 +11001177/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178
1179void
1180packet_write_poll()
1181{
Damien Miller95def091999-11-25 00:26:21 +11001182 int len = buffer_len(&output);
1183 if (len > 0) {
1184 len = write(connection_out, buffer_ptr(&output), len);
1185 if (len <= 0) {
1186 if (errno == EAGAIN)
1187 return;
1188 else
1189 fatal("Write failed: %.100s", strerror(errno));
1190 }
1191 buffer_consume(&output, len);
1192 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193}
1194
Damien Miller5428f641999-11-25 11:54:57 +11001195/*
1196 * Calls packet_write_poll repeatedly until all pending output data has been
1197 * written.
1198 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199
1200void
1201packet_write_wait()
1202{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001203 fd_set *setp;
1204
1205 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1206 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001207 packet_write_poll();
1208 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001209 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1210 sizeof(fd_mask));
1211 FD_SET(connection_out, setp);
1212 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001213 (errno == EAGAIN || errno == EINTR))
1214 ;
Damien Miller95def091999-11-25 00:26:21 +11001215 packet_write_poll();
1216 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001217 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218}
1219
1220/* Returns true if there is buffered data to write to the connection. */
1221
1222int
1223packet_have_data_to_write()
1224{
Damien Miller95def091999-11-25 00:26:21 +11001225 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226}
1227
1228/* Returns true if there is not too much data to write to the connection. */
1229
1230int
1231packet_not_very_much_data_to_write()
1232{
Damien Miller95def091999-11-25 00:26:21 +11001233 if (interactive_mode)
1234 return buffer_len(&output) < 16384;
1235 else
1236 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237}
1238
1239/* Informs that the current session is interactive. Sets IP flags for that. */
1240
1241void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001242packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001244 static int called = 0;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001245#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001246 int lowdelay = IPTOS_LOWDELAY;
1247 int throughput = IPTOS_THROUGHPUT;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001248#endif
Damien Miller95def091999-11-25 00:26:21 +11001249 int on = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001250
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001251 if (called)
1252 return;
1253 called = 1;
1254
Damien Miller95def091999-11-25 00:26:21 +11001255 /* Record that we are in interactive mode. */
1256 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001257
Damien Miller34132e52000-01-14 15:45:46 +11001258 /* Only set socket options if using a socket. */
1259 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001260 return;
Damien Miller34132e52000-01-14 15:45:46 +11001261 /*
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001262 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
Damien Miller34132e52000-01-14 15:45:46 +11001263 */
Damien Miller95def091999-11-25 00:26:21 +11001264 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001265 /*
1266 * Set IP options for an interactive connection. Use
1267 * IPTOS_LOWDELAY and TCP_NODELAY.
1268 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001269#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001270 if (packet_connection_is_ipv4()) {
Kevin Steves0c696152001-01-24 13:47:43 +00001271 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001272 (void *) &lowdelay, sizeof(lowdelay)) < 0)
Kevin Steves0c696152001-01-24 13:47:43 +00001273 error("setsockopt IPTOS_LOWDELAY: %.100s",
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001274 strerror(errno));
1275 }
Damien Miller615f9392000-05-17 22:53:33 +10001276#endif
Damien Miller95def091999-11-25 00:26:21 +11001277 if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on,
Damien Miller34132e52000-01-14 15:45:46 +11001278 sizeof(on)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001279 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001280 } else if (packet_connection_is_ipv4()) {
Damien Miller5428f641999-11-25 11:54:57 +11001281 /*
1282 * Set IP options for a non-interactive connection. Use
1283 * IPTOS_THROUGHPUT.
1284 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001285#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Damien Miller95def091999-11-25 00:26:21 +11001286 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001287 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001288 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001289#endif
Damien Miller95def091999-11-25 00:26:21 +11001290 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001291}
1292
1293/* Returns true if the current connection is interactive. */
1294
1295int
1296packet_is_interactive()
1297{
Damien Miller95def091999-11-25 00:26:21 +11001298 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299}
Damien Miller6162d121999-11-21 13:23:52 +11001300
1301int
1302packet_set_maxsize(int s)
1303{
Damien Miller95def091999-11-25 00:26:21 +11001304 static int called = 0;
1305 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001306 log("packet_set_maxsize: called twice: old %d new %d",
1307 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001308 return -1;
1309 }
1310 if (s < 4 * 1024 || s > 1024 * 1024) {
1311 log("packet_set_maxsize: bad size %d", s);
1312 return -1;
1313 }
1314 log("packet_set_maxsize: setting to %d", s);
1315 max_packet_size = s;
1316 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001317}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001318
1319/*
1320 * 9.2. Ignored Data Message
1321 *
1322 * byte SSH_MSG_IGNORE
1323 * string data
1324 *
1325 * All implementations MUST understand (and ignore) this message at any
1326 * time (after receiving the protocol version). No implementation is
1327 * required to send them. This message can be used as an additional
1328 * protection measure against advanced traffic analysis techniques.
1329 */
1330/* size of current + ignore message should be n*sumlen bytes (w/o mac) */
1331void
1332packet_inject_ignore(int sumlen)
1333{
Ben Lindstrome229b252001-03-05 06:28:06 +00001334 int blocksize, padlen, have, need, nb, mini, nbytes;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001335 Enc *enc = NULL;
1336
1337 if (use_ssh2_packet_format == 0)
1338 return;
1339
1340 have = buffer_len(&outgoing_packet);
1341 debug2("packet_inject_ignore: current %d", have);
1342 if (kex != NULL)
1343 enc = &kex->enc[MODE_OUT];
1344 blocksize = enc ? enc->cipher->block_size : 8;
1345 padlen = blocksize - (have % blocksize);
1346 if (padlen < 4)
1347 padlen += blocksize;
1348 have += padlen;
1349 have /= blocksize; /* # of blocks for current message */
1350
1351 nb = roundup(sumlen, blocksize) / blocksize; /* blocks for both */
1352 mini = roundup(5+1+4+4, blocksize) / blocksize; /* minsize ignore msg */
1353 need = nb - (have % nb); /* blocks for ignore */
1354 if (need <= mini)
1355 need += nb;
1356 nbytes = (need - mini) * blocksize; /* size of ignore payload */
1357 debug2("packet_inject_ignore: block %d have %d nb %d mini %d need %d",
1358 blocksize, have, nb, mini, need);
1359
1360 /* enqueue current message and append a ignore message */
1361 packet_send();
Ben Lindstrome229b252001-03-05 06:28:06 +00001362 packet_send_ignore(nbytes);
1363}
1364
1365void
1366packet_send_ignore(int nbytes)
1367{
1368 u_int32_t rand = 0;
1369 int i;
1370
1371 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001372 packet_put_int(nbytes);
1373 for(i = 0; i < nbytes; i++) {
1374 if (i % 4 == 0)
1375 rand = arc4random();
1376 packet_put_char(rand & 0xff);
1377 rand >>= 8;
1378 }
1379}