blob: a5b2ab61a9c6c6d0673c17185ef99ae8f71f253f [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"
Ben Lindstrom3f584742002-06-23 21:49:25 +000040RCSID("$OpenBSD: packet.c,v 1.96 2002/06/23 21:10:02 deraadt 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"
Ben Lindstromc7637672001-06-09 00:36:26 +000051#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 Miller4d007762002-02-05 11:52:54 +110062#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000063#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100064
65#ifdef PACKET_DEBUG
66#define DBG(x) x
67#else
68#define DBG(x)
69#endif
70
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * This variable contains the file descriptors used for communicating with
73 * the other side. connection_in is used for reading; connection_out for
74 * writing. These can be the same descriptor, in which case it is assumed to
75 * be a socket.
76 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077static int connection_in = -1;
78static int connection_out = -1;
79
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000081static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
83/* Encryption context for receiving data. This is only used for decryption. */
84static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110085
86/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087static CipherContext send_context;
88
89/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000090Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
92/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000093Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
95/* Buffer for the partial outgoing packet being constructed. */
96static Buffer outgoing_packet;
97
98/* Buffer for the incoming packet currently being processed. */
99static Buffer incoming_packet;
100
101/* Scratch buffer for packet compression/decompression. */
102static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000103static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
105/* Flag indicating whether packet compression/decompression is enabled. */
106static int packet_compression = 0;
107
Damien Miller6162d121999-11-21 13:23:52 +1100108/* default maximum packet size */
109int max_packet_size = 32768;
110
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111/* Flag indicating whether this module has been initialized. */
112static int initialized = 0;
113
114/* Set to true if the connection is interactive. */
115static int interactive_mode = 0;
116
Damien Miller33b13562000-04-04 14:38:59 +1000117/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000118Newkeys *newkeys[MODE_MAX];
Ben Lindstromf6027d32002-03-22 01:42:04 +0000119static u_int32_t read_seqnr = 0;
120static u_int32_t send_seqnr = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000121
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000122/* Session key for protocol v1 */
123static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
124static u_int ssh1_keylen;
125
Damien Miller9f643902001-11-12 11:02:52 +1100126/* roundup current message to extra_pad bytes */
127static u_char extra_pad = 0;
128
Damien Miller5428f641999-11-25 11:54:57 +1100129/*
130 * Sets the descriptors used for communication. Disables encryption until
131 * packet_set_encryption_key is called.
132 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133void
134packet_set_connection(int fd_in, int fd_out)
135{
Damien Miller874d77b2000-10-14 16:23:11 +1100136 Cipher *none = cipher_by_name("none");
137 if (none == NULL)
138 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100139 connection_in = fd_in;
140 connection_out = fd_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100141 cipher_init(&send_context, none, "", 0, NULL, 0, CIPHER_ENCRYPT);
142 cipher_init(&receive_context, none, "", 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000143 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100144 if (!initialized) {
145 initialized = 1;
146 buffer_init(&input);
147 buffer_init(&output);
148 buffer_init(&outgoing_packet);
149 buffer_init(&incoming_packet);
150 }
151 /* Kludge: arrange the close function to be called from fatal(). */
152 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153}
154
Damien Miller34132e52000-01-14 15:45:46 +1100155/* Returns 1 if remote host is connected via socket, 0 if not. */
156
157int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000158packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100159{
160 struct sockaddr_storage from, to;
161 socklen_t fromlen, tolen;
162
163 /* filedescriptors in and out are the same, so it's a socket */
164 if (connection_in == connection_out)
165 return 1;
166 fromlen = sizeof(from);
167 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100168 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100169 return 0;
170 tolen = sizeof(to);
171 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100172 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100173 return 0;
174 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
175 return 0;
176 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
177 return 0;
178 return 1;
179}
180
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000181/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000182 * Exports an IV from the CipherContext required to export the key
183 * state back from the unprivileged child to the privileged parent
184 * process.
185 */
186
187void
188packet_get_keyiv(int mode, u_char *iv, u_int len)
189{
190 CipherContext *cc;
191
192 if (mode == MODE_OUT)
193 cc = &send_context;
194 else
195 cc = &receive_context;
196
197 cipher_get_keyiv(cc, iv, len);
198}
199
200int
201packet_get_keycontext(int mode, u_char *dat)
202{
203 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000204
Ben Lindstromf6027d32002-03-22 01:42:04 +0000205 if (mode == MODE_OUT)
206 cc = &send_context;
207 else
208 cc = &receive_context;
209
210 return (cipher_get_keycontext(cc, dat));
211}
212
213void
214packet_set_keycontext(int mode, u_char *dat)
215{
216 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000217
Ben Lindstromf6027d32002-03-22 01:42:04 +0000218 if (mode == MODE_OUT)
219 cc = &send_context;
220 else
221 cc = &receive_context;
222
223 cipher_set_keycontext(cc, dat);
224}
225
226int
227packet_get_keyiv_len(int mode)
228{
229 CipherContext *cc;
230
231 if (mode == MODE_OUT)
232 cc = &send_context;
233 else
234 cc = &receive_context;
235
236 return (cipher_get_keyiv_len(cc));
237}
238void
239packet_set_iv(int mode, u_char *dat)
240{
241 CipherContext *cc;
242
243 if (mode == MODE_OUT)
244 cc = &send_context;
245 else
246 cc = &receive_context;
247
248 cipher_set_keyiv(cc, dat);
249}
250int
251packet_get_ssh1_cipher()
252{
253 return (cipher_get_number(receive_context.cipher));
254}
255
256
257u_int32_t
258packet_get_seqnr(int mode)
259{
260 return (mode == MODE_IN ? read_seqnr : send_seqnr);
261}
262
263void
264packet_set_seqnr(int mode, u_int32_t seqnr)
265{
266 if (mode == MODE_IN)
267 read_seqnr = seqnr;
268 else if (mode == MODE_OUT)
269 send_seqnr = seqnr;
270 else
Ben Lindstrom3dca4f52002-06-06 20:59:25 +0000271 fatal("packet_set_seqnr: bad mode %d", mode);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000272}
273
Damien Miller34132e52000-01-14 15:45:46 +1100274/* returns 1 if connection is via ipv4 */
275
276int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000277packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100278{
279 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100280 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100281
282 memset(&to, 0, sizeof(to));
283 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
284 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000285 if (to.ss_family == AF_INET)
286 return 1;
287#ifdef IPV4_IN_IPV6
288 if (to.ss_family == AF_INET6 &&
289 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
290 return 1;
291#endif
292 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100293}
294
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295/* Sets the connection into non-blocking mode. */
296
297void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000298packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299{
Damien Miller95def091999-11-25 00:26:21 +1100300 /* Set the socket into non-blocking mode. */
301 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
302 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller95def091999-11-25 00:26:21 +1100304 if (connection_out != connection_in) {
305 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
306 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
307 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308}
309
310/* Returns the socket used for reading. */
311
312int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000313packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314{
Damien Miller95def091999-11-25 00:26:21 +1100315 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316}
317
318/* Returns the descriptor used for writing. */
319
320int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000321packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322{
Damien Miller95def091999-11-25 00:26:21 +1100323 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324}
325
326/* Closes the connection and clears and frees internal data structures. */
327
328void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000329packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330{
Damien Miller95def091999-11-25 00:26:21 +1100331 if (!initialized)
332 return;
333 initialized = 0;
334 if (connection_in == connection_out) {
335 shutdown(connection_out, SHUT_RDWR);
336 close(connection_out);
337 } else {
338 close(connection_in);
339 close(connection_out);
340 }
341 buffer_free(&input);
342 buffer_free(&output);
343 buffer_free(&outgoing_packet);
344 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000345 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100346 buffer_free(&compression_buffer);
347 buffer_compress_uninit();
348 }
Damien Miller963f6b22002-02-19 15:21:23 +1100349 cipher_cleanup(&send_context);
350 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351}
352
353/* Sets remote side protocol flags. */
354
355void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000356packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357{
Damien Miller95def091999-11-25 00:26:21 +1100358 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359}
360
361/* Returns the remote protocol flags set earlier by the above function. */
362
Ben Lindstrom46c16222000-12-22 01:43:59 +0000363u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000364packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365{
Damien Miller95def091999-11-25 00:26:21 +1100366 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367}
368
Damien Miller5428f641999-11-25 11:54:57 +1100369/*
370 * Starts packet compression from the next packet on in both directions.
371 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
372 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373
Ben Lindstrombba81212001-06-25 05:01:22 +0000374static void
375packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000376{
377 if (compression_buffer_ready == 1)
378 return;
379 compression_buffer_ready = 1;
380 buffer_init(&compression_buffer);
381}
382
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383void
384packet_start_compression(int level)
385{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000386 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100387 fatal("Compression already enabled.");
388 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000389 packet_init_compression();
390 buffer_compress_init_send(level);
391 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392}
393
Damien Miller5428f641999-11-25 11:54:57 +1100394/*
Damien Miller5428f641999-11-25 11:54:57 +1100395 * Causes any further packets to be encrypted using the given key. The same
396 * key is used for both sending and reception. However, both directions are
397 * encrypted independently of each other.
398 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000399
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000401packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100402 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403{
Damien Miller874d77b2000-10-14 16:23:11 +1100404 Cipher *cipher = cipher_by_number(number);
405 if (cipher == NULL)
406 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000407 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100408 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000409 if (keylen > SSH_SESSION_KEY_LENGTH)
410 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
411 memcpy(ssh1_key, key, keylen);
412 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100413 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
414 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415}
416
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000417u_int
418packet_get_encryption_key(u_char *key)
419{
420 if (key == NULL)
421 return (ssh1_keylen);
422 memcpy(key, ssh1_key, ssh1_keylen);
423 return (ssh1_keylen);
424}
425
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000426/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000428packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000430 u_char buf[9];
431 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432
Ben Lindstrom204e4882001-03-05 06:47:00 +0000433 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000434 len = compat20 ? 6 : 9;
435 memset(buf, 0, len - 1);
436 buf[len - 1] = type;
437 buffer_clear(&outgoing_packet);
438 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000439}
440
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000441/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442void
443packet_put_char(int value)
444{
Damien Miller95def091999-11-25 00:26:21 +1100445 char ch = value;
446 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000449packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450{
Damien Miller95def091999-11-25 00:26:21 +1100451 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100454packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455{
Damien Miller95def091999-11-25 00:26:21 +1100456 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457}
Damien Miller33b13562000-04-04 14:38:59 +1000458void
459packet_put_cstring(const char *str)
460{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000461 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000462}
Damien Miller33b13562000-04-04 14:38:59 +1000463void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100464packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000465{
466 buffer_append(&outgoing_packet, buf, len);
467}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468void
Damien Miller95def091999-11-25 00:26:21 +1100469packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470{
Damien Miller95def091999-11-25 00:26:21 +1100471 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472}
Damien Miller33b13562000-04-04 14:38:59 +1000473void
474packet_put_bignum2(BIGNUM * value)
475{
476 buffer_put_bignum2(&outgoing_packet, value);
477}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478
Damien Miller5428f641999-11-25 11:54:57 +1100479/*
480 * Finalizes and sends the packet. If the encryption key has been set,
481 * encrypts the packet before sending.
482 */
Damien Miller95def091999-11-25 00:26:21 +1100483
Ben Lindstrombba81212001-06-25 05:01:22 +0000484static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000485packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000487 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100488 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000489 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100490 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
Damien Miller5428f641999-11-25 11:54:57 +1100492 /*
493 * If using packet compression, compress the payload of the outgoing
494 * packet.
495 */
Damien Miller95def091999-11-25 00:26:21 +1100496 if (packet_compression) {
497 buffer_clear(&compression_buffer);
498 /* Skip padding. */
499 buffer_consume(&outgoing_packet, 8);
500 /* padding */
501 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
502 buffer_compress(&outgoing_packet, &compression_buffer);
503 buffer_clear(&outgoing_packet);
504 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100505 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100506 }
507 /* Compute packet length without padding (add checksum, remove padding). */
508 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509
Damien Millere247cc42000-05-07 12:03:14 +1000510 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100511 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100512 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100513 cp = buffer_ptr(&outgoing_packet);
514 for (i = 0; i < padding; i++) {
515 if (i % 4 == 0)
516 rand = arc4random();
517 cp[7 - i] = rand & 0xff;
518 rand >>= 8;
519 }
520 }
521 buffer_consume(&outgoing_packet, 8 - padding);
522
523 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100524 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000525 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100526 PUT_32BIT(buf, checksum);
527 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
529#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100530 fprintf(stderr, "packet_send plain: ");
531 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532#endif
533
Damien Miller95def091999-11-25 00:26:21 +1100534 /* Append to output. */
535 PUT_32BIT(buf, len);
536 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100537 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100538 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100539 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100540
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100542 fprintf(stderr, "encrypted: ");
543 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544#endif
545
Damien Miller95def091999-11-25 00:26:21 +1100546 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547
Damien Miller5428f641999-11-25 11:54:57 +1100548 /*
549 * Note that the packet is now only buffered in output. It won\'t be
550 * actually sent until packet_write_wait or packet_write_poll is
551 * called.
552 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553}
554
Ben Lindstromf6027d32002-03-22 01:42:04 +0000555void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000556set_newkeys(int mode)
557{
558 Enc *enc;
559 Mac *mac;
560 Comp *comp;
561 CipherContext *cc;
Damien Miller963f6b22002-02-19 15:21:23 +1100562 int encrypt;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000563
564 debug("newkeys: mode %d", mode);
565
Damien Miller963f6b22002-02-19 15:21:23 +1100566 if (mode == MODE_OUT) {
567 cc = &send_context;
568 encrypt = CIPHER_ENCRYPT;
569 } else {
570 cc = &receive_context;
571 encrypt = CIPHER_DECRYPT;
572 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000573 if (newkeys[mode] != NULL) {
574 debug("newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100575 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000576 enc = &newkeys[mode]->enc;
577 mac = &newkeys[mode]->mac;
578 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000579 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000580 xfree(enc->name);
581 xfree(enc->iv);
582 xfree(enc->key);
583 xfree(mac->name);
584 xfree(mac->key);
585 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000586 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000587 }
588 newkeys[mode] = kex_get_newkeys(mode);
589 if (newkeys[mode] == NULL)
590 fatal("newkeys: no keys for mode %d", mode);
591 enc = &newkeys[mode]->enc;
592 mac = &newkeys[mode]->mac;
593 comp = &newkeys[mode]->comp;
594 if (mac->md != NULL)
595 mac->enabled = 1;
596 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100597 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
598 enc->iv, enc->block_size, encrypt);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000599 /* Deleting the keys does not gain extra security */
600 /* memset(enc->iv, 0, enc->block_size);
601 memset(enc->key, 0, enc->key_len); */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000602 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000603 packet_init_compression();
604 if (mode == MODE_OUT)
605 buffer_compress_init_send(6);
606 else
607 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000608 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000609 }
610}
611
Damien Miller5428f641999-11-25 11:54:57 +1100612/*
Damien Miller33b13562000-04-04 14:38:59 +1000613 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
614 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000615static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000616packet_send2(void)
Damien Miller33b13562000-04-04 14:38:59 +1000617{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000618 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100619 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000620 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100621 u_int i, len;
Damien Miller33b13562000-04-04 14:38:59 +1000622 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000623 Enc *enc = NULL;
624 Mac *mac = NULL;
625 Comp *comp = NULL;
626 int block_size;
627
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000628 if (newkeys[MODE_OUT] != NULL) {
629 enc = &newkeys[MODE_OUT]->enc;
630 mac = &newkeys[MODE_OUT]->mac;
631 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000632 }
Damien Miller963f6b22002-02-19 15:21:23 +1100633 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000634
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000635 cp = buffer_ptr(&outgoing_packet);
636 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000637
638#ifdef PACKET_DEBUG
639 fprintf(stderr, "plain: ");
640 buffer_dump(&outgoing_packet);
641#endif
642
643 if (comp && comp->enabled) {
644 len = buffer_len(&outgoing_packet);
645 /* skip header, compress only payload */
646 buffer_consume(&outgoing_packet, 5);
647 buffer_clear(&compression_buffer);
648 buffer_compress(&outgoing_packet, &compression_buffer);
649 buffer_clear(&outgoing_packet);
650 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
651 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
652 buffer_len(&compression_buffer));
653 DBG(debug("compression: raw %d compressed %d", len,
654 buffer_len(&outgoing_packet)));
655 }
656
657 /* sizeof (packet_len + pad_len + payload) */
658 len = buffer_len(&outgoing_packet);
659
660 /*
661 * calc size of padding, alloc space, get random data,
662 * minimum padding is 4 bytes
663 */
664 padlen = block_size - (len % block_size);
665 if (padlen < 4)
666 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100667 if (extra_pad) {
668 /* will wrap if extra_pad+padlen > 255 */
669 extra_pad = roundup(extra_pad, block_size);
670 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000671 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100672 pad, len, padlen, extra_pad);
673 padlen += pad;
674 extra_pad = 0;
675 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100676 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100677 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000678 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000679 for (i = 0; i < padlen; i++) {
680 if (i % 4 == 0)
681 rand = arc4random();
682 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000683 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000684 }
Damien Millere247cc42000-05-07 12:03:14 +1000685 } else {
686 /* clear padding */
687 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000688 }
689 /* packet_length includes payload, padding and padding length field */
690 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000691 cp = buffer_ptr(&outgoing_packet);
692 PUT_32BIT(cp, packet_length);
693 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000694 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
695
696 /* compute MAC over seqnr and packet(length fields, payload, padding) */
697 if (mac && mac->enabled) {
Ben Lindstromf6027d32002-03-22 01:42:04 +0000698 macbuf = mac_compute(mac, send_seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100699 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000700 buffer_len(&outgoing_packet));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000701 DBG(debug("done calc MAC out #%d", send_seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000702 }
703 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100704 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100705 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000706 buffer_len(&outgoing_packet));
707 /* append unencrypted MAC */
708 if (mac && mac->enabled)
709 buffer_append(&output, (char *)macbuf, mac->mac_len);
710#ifdef PACKET_DEBUG
711 fprintf(stderr, "encrypted: ");
712 buffer_dump(&output);
713#endif
Damien Miller4af51302000-04-16 11:18:38 +1000714 /* increment sequence number for outgoing packets */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000715 if (++send_seqnr == 0)
Damien Miller4af51302000-04-16 11:18:38 +1000716 log("outgoing seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000717 buffer_clear(&outgoing_packet);
718
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000719 if (type == SSH2_MSG_NEWKEYS)
720 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000721}
722
723void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000724packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000725{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000726 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000727 packet_send2();
728 else
729 packet_send1();
730 DBG(debug("packet_send done"));
731}
732
Damien Miller33b13562000-04-04 14:38:59 +1000733/*
Damien Miller5428f641999-11-25 11:54:57 +1100734 * Waits until a packet has been received, and returns its type. Note that
735 * no other data is processed until this returns, so this function should not
736 * be used during the interactive session.
737 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000738
739int
Damien Millerdff50992002-01-22 23:16:32 +1100740packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741{
Damien Miller95def091999-11-25 00:26:21 +1100742 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000743 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100744 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000745 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000747 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
748 sizeof(fd_mask));
749
Damien Miller95def091999-11-25 00:26:21 +1100750 /* Since we are blocking, ensure that all written packets have been sent. */
751 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752
Damien Miller95def091999-11-25 00:26:21 +1100753 /* Stay in the loop until we have received a complete packet. */
754 for (;;) {
755 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100756 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000757 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000758 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100759 || type == SSH_SMSG_FAILURE
760 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000761 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100762 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100763 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000764 if (type != SSH_MSG_NONE) {
765 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100766 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000767 }
Damien Miller5428f641999-11-25 11:54:57 +1100768 /*
769 * Otherwise, wait for some data to arrive, add it to the
770 * buffer, and try again.
771 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000772 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
773 sizeof(fd_mask));
774 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100775
Damien Miller95def091999-11-25 00:26:21 +1100776 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000777 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000778 (errno == EAGAIN || errno == EINTR))
779 ;
Damien Miller5428f641999-11-25 11:54:57 +1100780
Damien Miller95def091999-11-25 00:26:21 +1100781 /* Read data from the socket. */
782 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100783 if (len == 0) {
784 log("Connection closed by %.200s", get_remote_ipaddr());
785 fatal_cleanup();
786 }
Damien Miller95def091999-11-25 00:26:21 +1100787 if (len < 0)
788 fatal("Read from socket failed: %.100s", strerror(errno));
789 /* Append it to the buffer. */
790 packet_process_incoming(buf, len);
791 }
792 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793}
794
Damien Miller278f9072001-12-21 15:00:19 +1100795int
Damien Millerdff50992002-01-22 23:16:32 +1100796packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100797{
Damien Millerdff50992002-01-22 23:16:32 +1100798 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100799}
800
Damien Miller5428f641999-11-25 11:54:57 +1100801/*
802 * Waits until a packet has been received, verifies that its type matches
803 * that given, and gives a fatal error and exits if there is a mismatch.
804 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805
806void
Damien Millerdff50992002-01-22 23:16:32 +1100807packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808{
Damien Miller95def091999-11-25 00:26:21 +1100809 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810
Damien Millerdff50992002-01-22 23:16:32 +1100811 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100812 if (type != expected_type)
813 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000814 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815}
816
817/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100818 * packet_process_incoming. If so, reads the packet; otherwise returns
819 * SSH_MSG_NONE. This does not wait for data from the connection.
820 *
821 * SSH_MSG_DISCONNECT is handled specially here. Also,
822 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
823 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100824 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825
Ben Lindstrombba81212001-06-25 05:01:22 +0000826static int
Damien Millerdff50992002-01-22 23:16:32 +1100827packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000829 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000830 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000831 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832
Damien Miller95def091999-11-25 00:26:21 +1100833 /* Check if input size is less than minimum packet size. */
834 if (buffer_len(&input) < 4 + 8)
835 return SSH_MSG_NONE;
836 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000837 cp = buffer_ptr(&input);
838 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100839 if (len < 1 + 2 + 2 || len > 256 * 1024)
840 packet_disconnect("Bad packet length %d.", len);
841 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000842
Damien Miller95def091999-11-25 00:26:21 +1100843 /* Check if the packet has been entirely received. */
844 if (buffer_len(&input) < 4 + padded_len)
845 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846
Damien Miller95def091999-11-25 00:26:21 +1100847 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848
Damien Miller95def091999-11-25 00:26:21 +1100849 /* Consume packet length. */
850 buffer_consume(&input, 4);
851
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000852 /*
853 * Cryptographic attack detector for ssh
854 * (C)1998 CORE-SDI, Buenos Aires Argentina
855 * Ariel Futoransky(futo@core-sdi.com)
856 */
Damien Miller963f6b22002-02-19 15:21:23 +1100857 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000858 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
859 packet_disconnect("crc32 compensation attack: network attack detected");
860
861 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100862 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100863 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100864 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000865
Damien Miller95def091999-11-25 00:26:21 +1100866 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867
868#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100869 fprintf(stderr, "read_poll plain: ");
870 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872
Damien Miller95def091999-11-25 00:26:21 +1100873 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100874 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000875 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876
Damien Miller95def091999-11-25 00:26:21 +1100877 /* Skip padding. */
878 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100879
Damien Miller95def091999-11-25 00:26:21 +1100880 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100881 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +1100882 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000883 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000885 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
886 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100887 if (checksum != stored_checksum)
888 packet_disconnect("Corrupted check bytes on input.");
889 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890
Damien Miller95def091999-11-25 00:26:21 +1100891 if (packet_compression) {
892 buffer_clear(&compression_buffer);
893 buffer_uncompress(&incoming_packet, &compression_buffer);
894 buffer_clear(&incoming_packet);
895 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000896 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100897 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000898 type = buffer_get_char(&incoming_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000899 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900}
Damien Miller95def091999-11-25 00:26:21 +1100901
Ben Lindstrombba81212001-06-25 05:01:22 +0000902static int
Damien Millerdff50992002-01-22 23:16:32 +1100903packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000904{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000905 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000906 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000907 u_char *macbuf, *cp, type;
Damien Miller33b13562000-04-04 14:38:59 +1000908 int maclen, block_size;
909 Enc *enc = NULL;
910 Mac *mac = NULL;
911 Comp *comp = NULL;
912
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000913 if (newkeys[MODE_IN] != NULL) {
914 enc = &newkeys[MODE_IN]->enc;
915 mac = &newkeys[MODE_IN]->mac;
916 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000917 }
918 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +1100919 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000920
921 if (packet_length == 0) {
922 /*
923 * check if input size is less than the cipher block size,
924 * decrypt first block and extract length of incoming packet
925 */
926 if (buffer_len(&input) < block_size)
927 return SSH_MSG_NONE;
928 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100929 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +1100930 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +1000931 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000932 cp = buffer_ptr(&incoming_packet);
933 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +1000934 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
935 buffer_dump(&incoming_packet);
936 packet_disconnect("Bad packet length %d.", packet_length);
937 }
938 DBG(debug("input: packet len %d", packet_length+4));
939 buffer_consume(&input, block_size);
940 }
941 /* we have a partial packet of block_size bytes */
942 need = 4 + packet_length - block_size;
943 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
944 need, maclen));
945 if (need % block_size != 0)
946 fatal("padding error: need %d block %d mod %d",
947 need, block_size, need % block_size);
948 /*
949 * check if the entire packet has been received and
950 * decrypt into incoming_packet
951 */
952 if (buffer_len(&input) < need + maclen)
953 return SSH_MSG_NONE;
954#ifdef PACKET_DEBUG
955 fprintf(stderr, "read_poll enc/full: ");
956 buffer_dump(&input);
957#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100958 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +1100959 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +1000960 buffer_consume(&input, need);
961 /*
962 * compute MAC over seqnr and packet,
963 * increment sequence number for incoming packet
964 */
Damien Miller4af51302000-04-16 11:18:38 +1000965 if (mac && mac->enabled) {
Ben Lindstromf6027d32002-03-22 01:42:04 +0000966 macbuf = mac_compute(mac, read_seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100967 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000968 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000969 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100970 packet_disconnect("Corrupted MAC on input.");
Ben Lindstromf6027d32002-03-22 01:42:04 +0000971 DBG(debug("MAC #%d ok", read_seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000972 buffer_consume(&input, mac->mac_len);
973 }
Damien Miller278f9072001-12-21 15:00:19 +1100974 if (seqnr_p != NULL)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000975 *seqnr_p = read_seqnr;
976 if (++read_seqnr == 0)
Damien Miller4af51302000-04-16 11:18:38 +1000977 log("incoming seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000978
979 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100980 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000981 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +1000982 DBG(debug("input: padlen %d", padlen));
983 if (padlen < 4)
984 packet_disconnect("Corrupted padlen %d on input.", padlen);
985
986 /* skip packet size + padlen, discard padding */
987 buffer_consume(&incoming_packet, 4 + 1);
988 buffer_consume_end(&incoming_packet, padlen);
989
990 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
991 if (comp && comp->enabled) {
992 buffer_clear(&compression_buffer);
993 buffer_uncompress(&incoming_packet, &compression_buffer);
994 buffer_clear(&incoming_packet);
995 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
996 buffer_len(&compression_buffer));
997 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
998 }
999 /*
1000 * get packet type, implies consume.
1001 * return length of payload (without type field)
1002 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001003 type = buffer_get_char(&incoming_packet);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001004 if (type == SSH2_MSG_NEWKEYS)
1005 set_newkeys(MODE_IN);
Damien Miller33b13562000-04-04 14:38:59 +10001006#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001007 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001008 buffer_dump(&incoming_packet);
1009#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001010 /* reset for next packet */
1011 packet_length = 0;
1012 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001013}
1014
1015int
Damien Millerdff50992002-01-22 23:16:32 +11001016packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001017{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001018 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001019 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001020 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001021
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001022 for (;;) {
1023 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001024 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001025 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001026 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001027 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001028 case SSH2_MSG_IGNORE:
1029 break;
1030 case SSH2_MSG_DEBUG:
1031 packet_get_char();
1032 msg = packet_get_string(NULL);
1033 debug("Remote: %.900s", msg);
1034 xfree(msg);
1035 msg = packet_get_string(NULL);
1036 xfree(msg);
1037 break;
1038 case SSH2_MSG_DISCONNECT:
1039 reason = packet_get_int();
1040 msg = packet_get_string(NULL);
Ben Lindstrom3f584742002-06-23 21:49:25 +00001041 log("Received disconnect from %s: %u: %.400s",
1042 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001043 xfree(msg);
1044 fatal_cleanup();
1045 break;
Damien Miller659811f2002-01-22 23:23:11 +11001046 case SSH2_MSG_UNIMPLEMENTED:
1047 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001048 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1049 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001050 break;
Damien Miller33b13562000-04-04 14:38:59 +10001051 default:
1052 return type;
1053 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001054 }
Damien Miller33b13562000-04-04 14:38:59 +10001055 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001056 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001057 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001058 case SSH_MSG_IGNORE:
1059 break;
1060 case SSH_MSG_DEBUG:
1061 msg = packet_get_string(NULL);
1062 debug("Remote: %.900s", msg);
1063 xfree(msg);
1064 break;
1065 case SSH_MSG_DISCONNECT:
1066 msg = packet_get_string(NULL);
Ben Lindstrom3f584742002-06-23 21:49:25 +00001067 log("Received disconnect from %s: %.400s",
1068 get_remote_ipaddr(), msg);
Damien Miller33b13562000-04-04 14:38:59 +10001069 fatal_cleanup();
1070 xfree(msg);
1071 break;
1072 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001073 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001074 DBG(debug("received packet type %d", type));
1075 return type;
1076 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001077 }
Damien Miller33b13562000-04-04 14:38:59 +10001078 }
1079 }
1080}
1081
Damien Miller278f9072001-12-21 15:00:19 +11001082int
Damien Millerdff50992002-01-22 23:16:32 +11001083packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001084{
Damien Millerdff50992002-01-22 23:16:32 +11001085 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001086}
1087
Damien Miller5428f641999-11-25 11:54:57 +11001088/*
1089 * Buffers the given amount of input characters. This is intended to be used
1090 * together with packet_read_poll.
1091 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001092
1093void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001094packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095{
Damien Miller95def091999-11-25 00:26:21 +11001096 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097}
1098
1099/* Returns a character from the packet. */
1100
Ben Lindstrom46c16222000-12-22 01:43:59 +00001101u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001102packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001103{
Damien Miller95def091999-11-25 00:26:21 +11001104 char ch;
1105 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001106 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107}
1108
1109/* Returns an integer from the packet data. */
1110
Ben Lindstrom46c16222000-12-22 01:43:59 +00001111u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001112packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113{
Damien Miller95def091999-11-25 00:26:21 +11001114 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115}
1116
Damien Miller5428f641999-11-25 11:54:57 +11001117/*
1118 * Returns an arbitrary precision integer from the packet data. The integer
1119 * must have been initialized before this call.
1120 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121
1122void
Damien Millerd432ccf2002-01-22 23:14:44 +11001123packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124{
Damien Miller76e1e362002-01-22 23:15:57 +11001125 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001126}
1127
Damien Miller33b13562000-04-04 14:38:59 +10001128void
Damien Millerd432ccf2002-01-22 23:14:44 +11001129packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001130{
Damien Miller76e1e362002-01-22 23:15:57 +11001131 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001132}
1133
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001134void *
Damien Miller33b13562000-04-04 14:38:59 +10001135packet_get_raw(int *length_ptr)
1136{
1137 int bytes = buffer_len(&incoming_packet);
1138 if (length_ptr != NULL)
1139 *length_ptr = bytes;
1140 return buffer_ptr(&incoming_packet);
1141}
1142
Damien Miller4af51302000-04-16 11:18:38 +10001143int
1144packet_remaining(void)
1145{
1146 return buffer_len(&incoming_packet);
1147}
1148
Damien Miller5428f641999-11-25 11:54:57 +11001149/*
1150 * Returns a string from the packet data. The string is allocated using
1151 * xmalloc; it is the responsibility of the calling program to free it when
1152 * no longer needed. The length_ptr argument may be NULL, or point to an
1153 * integer into which the length of the string is stored.
1154 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001156void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001157packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001158{
Damien Miller95def091999-11-25 00:26:21 +11001159 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160}
1161
Damien Miller5428f641999-11-25 11:54:57 +11001162/*
1163 * Sends a diagnostic message from the server to the client. This message
1164 * can be sent at any time (but not while constructing another message). The
1165 * message is printed immediately, but only if the client is being executed
1166 * in verbose mode. These messages are primarily intended to ease debugging
1167 * authentication problems. The length of the formatted message must not
1168 * exceed 1024 bytes. This will automatically call packet_write_wait.
1169 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170
1171void
Damien Miller95def091999-11-25 00:26:21 +11001172packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173{
Damien Miller95def091999-11-25 00:26:21 +11001174 char buf[1024];
1175 va_list args;
1176
Ben Lindstroma14ee472000-12-07 01:24:58 +00001177 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1178 return;
1179
Damien Miller95def091999-11-25 00:26:21 +11001180 va_start(args, fmt);
1181 vsnprintf(buf, sizeof(buf), fmt, args);
1182 va_end(args);
1183
Damien Miller7c8af4f2000-05-01 08:24:07 +10001184 if (compat20) {
1185 packet_start(SSH2_MSG_DEBUG);
1186 packet_put_char(0); /* bool: always display */
1187 packet_put_cstring(buf);
1188 packet_put_cstring("");
1189 } else {
1190 packet_start(SSH_MSG_DEBUG);
1191 packet_put_cstring(buf);
1192 }
Damien Miller95def091999-11-25 00:26:21 +11001193 packet_send();
1194 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001195}
1196
Damien Miller5428f641999-11-25 11:54:57 +11001197/*
1198 * Logs the error plus constructs and sends a disconnect packet, closes the
1199 * connection, and exits. This function never returns. The error message
1200 * should not contain a newline. The length of the formatted message must
1201 * not exceed 1024 bytes.
1202 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203
1204void
Damien Miller95def091999-11-25 00:26:21 +11001205packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206{
Damien Miller95def091999-11-25 00:26:21 +11001207 char buf[1024];
1208 va_list args;
1209 static int disconnecting = 0;
1210 if (disconnecting) /* Guard against recursive invocations. */
1211 fatal("packet_disconnect called recursively.");
1212 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213
Damien Miller5428f641999-11-25 11:54:57 +11001214 /*
1215 * Format the message. Note that the caller must make sure the
1216 * message is of limited size.
1217 */
Damien Miller95def091999-11-25 00:26:21 +11001218 va_start(args, fmt);
1219 vsnprintf(buf, sizeof(buf), fmt, args);
1220 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001221
Damien Miller95def091999-11-25 00:26:21 +11001222 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001223 if (compat20) {
1224 packet_start(SSH2_MSG_DISCONNECT);
1225 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1226 packet_put_cstring(buf);
1227 packet_put_cstring("");
1228 } else {
1229 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001230 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001231 }
Damien Miller95def091999-11-25 00:26:21 +11001232 packet_send();
1233 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234
Damien Miller95def091999-11-25 00:26:21 +11001235 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001236 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237
Damien Miller95def091999-11-25 00:26:21 +11001238 /* Close the connection. */
1239 packet_close();
1240
1241 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001242 log("Disconnecting: %.100s", buf);
1243 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001244}
1245
Damien Miller5428f641999-11-25 11:54:57 +11001246/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247
1248void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001249packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001250{
Damien Miller95def091999-11-25 00:26:21 +11001251 int len = buffer_len(&output);
1252 if (len > 0) {
1253 len = write(connection_out, buffer_ptr(&output), len);
1254 if (len <= 0) {
1255 if (errno == EAGAIN)
1256 return;
1257 else
1258 fatal("Write failed: %.100s", strerror(errno));
1259 }
1260 buffer_consume(&output, len);
1261 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001262}
1263
Damien Miller5428f641999-11-25 11:54:57 +11001264/*
1265 * Calls packet_write_poll repeatedly until all pending output data has been
1266 * written.
1267 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268
1269void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001270packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001271{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001272 fd_set *setp;
1273
1274 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1275 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001276 packet_write_poll();
1277 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001278 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1279 sizeof(fd_mask));
1280 FD_SET(connection_out, setp);
1281 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001282 (errno == EAGAIN || errno == EINTR))
1283 ;
Damien Miller95def091999-11-25 00:26:21 +11001284 packet_write_poll();
1285 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001286 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001287}
1288
1289/* Returns true if there is buffered data to write to the connection. */
1290
1291int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001292packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293{
Damien Miller95def091999-11-25 00:26:21 +11001294 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001295}
1296
1297/* Returns true if there is not too much data to write to the connection. */
1298
1299int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001300packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301{
Damien Miller95def091999-11-25 00:26:21 +11001302 if (interactive_mode)
1303 return buffer_len(&output) < 16384;
1304 else
1305 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001306}
1307
1308/* Informs that the current session is interactive. Sets IP flags for that. */
1309
1310void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001311packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001313 static int called = 0;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001314#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001315 int lowdelay = IPTOS_LOWDELAY;
1316 int throughput = IPTOS_THROUGHPUT;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001317#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001319 if (called)
1320 return;
1321 called = 1;
1322
Damien Miller95def091999-11-25 00:26:21 +11001323 /* Record that we are in interactive mode. */
1324 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001325
Damien Miller34132e52000-01-14 15:45:46 +11001326 /* Only set socket options if using a socket. */
1327 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001328 return;
Damien Miller34132e52000-01-14 15:45:46 +11001329 /*
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001330 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
Damien Miller34132e52000-01-14 15:45:46 +11001331 */
Damien Miller95def091999-11-25 00:26:21 +11001332 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001333 /*
1334 * Set IP options for an interactive connection. Use
1335 * IPTOS_LOWDELAY and TCP_NODELAY.
1336 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001337#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001338 if (packet_connection_is_ipv4()) {
Kevin Steves0c696152001-01-24 13:47:43 +00001339 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
Ben Lindstrom733a2352002-03-05 01:31:28 +00001340 &lowdelay, sizeof(lowdelay)) < 0)
Kevin Steves0c696152001-01-24 13:47:43 +00001341 error("setsockopt IPTOS_LOWDELAY: %.100s",
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001342 strerror(errno));
1343 }
Damien Miller615f9392000-05-17 22:53:33 +10001344#endif
Damien Miller398e1cf2002-02-05 11:52:13 +11001345 set_nodelay(connection_in);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001346 } else if (packet_connection_is_ipv4()) {
Damien Miller5428f641999-11-25 11:54:57 +11001347 /*
1348 * Set IP options for a non-interactive connection. Use
1349 * IPTOS_THROUGHPUT.
1350 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001351#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom733a2352002-03-05 01:31:28 +00001352 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001353 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001354 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001355#endif
Damien Miller95def091999-11-25 00:26:21 +11001356 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357}
1358
1359/* Returns true if the current connection is interactive. */
1360
1361int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001362packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363{
Damien Miller95def091999-11-25 00:26:21 +11001364 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365}
Damien Miller6162d121999-11-21 13:23:52 +11001366
1367int
1368packet_set_maxsize(int s)
1369{
Damien Miller95def091999-11-25 00:26:21 +11001370 static int called = 0;
1371 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001372 log("packet_set_maxsize: called twice: old %d new %d",
1373 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001374 return -1;
1375 }
1376 if (s < 4 * 1024 || s > 1024 * 1024) {
1377 log("packet_set_maxsize: bad size %d", s);
1378 return -1;
1379 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001380 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001381 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001382 max_packet_size = s;
1383 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001384}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001385
Damien Miller9f643902001-11-12 11:02:52 +11001386/* roundup current message to pad bytes */
1387void
1388packet_add_padding(u_char pad)
1389{
1390 extra_pad = pad;
1391}
1392
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001393/*
1394 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001395 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001396 * byte SSH_MSG_IGNORE
1397 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001398 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001399 * All implementations MUST understand (and ignore) this message at any
1400 * time (after receiving the protocol version). No implementation is
1401 * required to send them. This message can be used as an additional
1402 * protection measure against advanced traffic analysis techniques.
1403 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001404void
1405packet_send_ignore(int nbytes)
1406{
1407 u_int32_t rand = 0;
1408 int i;
1409
1410 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001411 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001412 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001413 if (i % 4 == 0)
1414 rand = arc4random();
1415 packet_put_char(rand & 0xff);
1416 rand >>= 8;
1417 }
1418}