blob: bd347ef0f4ff90a87bc5944c934abd2717c39c37 [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 Lindstrom8b2eecd2002-07-07 22:11:51 +000040RCSID("$OpenBSD: packet.c,v 1.97 2002/07/04 08:12:15 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");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000137
Damien Miller874d77b2000-10-14 16:23:11 +1100138 if (none == NULL)
139 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100140 connection_in = fd_in;
141 connection_out = fd_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100142 cipher_init(&send_context, none, "", 0, NULL, 0, CIPHER_ENCRYPT);
143 cipher_init(&receive_context, none, "", 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000144 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100145 if (!initialized) {
146 initialized = 1;
147 buffer_init(&input);
148 buffer_init(&output);
149 buffer_init(&outgoing_packet);
150 buffer_init(&incoming_packet);
151 }
152 /* Kludge: arrange the close function to be called from fatal(). */
153 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154}
155
Damien Miller34132e52000-01-14 15:45:46 +1100156/* Returns 1 if remote host is connected via socket, 0 if not. */
157
158int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000159packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100160{
161 struct sockaddr_storage from, to;
162 socklen_t fromlen, tolen;
163
164 /* filedescriptors in and out are the same, so it's a socket */
165 if (connection_in == connection_out)
166 return 1;
167 fromlen = sizeof(from);
168 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100169 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100170 return 0;
171 tolen = sizeof(to);
172 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100173 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100174 return 0;
175 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
176 return 0;
177 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
178 return 0;
179 return 1;
180}
181
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000182/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000183 * Exports an IV from the CipherContext required to export the key
184 * state back from the unprivileged child to the privileged parent
185 * process.
186 */
187
188void
189packet_get_keyiv(int mode, u_char *iv, u_int len)
190{
191 CipherContext *cc;
192
193 if (mode == MODE_OUT)
194 cc = &send_context;
195 else
196 cc = &receive_context;
197
198 cipher_get_keyiv(cc, iv, len);
199}
200
201int
202packet_get_keycontext(int mode, u_char *dat)
203{
204 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000205
Ben Lindstromf6027d32002-03-22 01:42:04 +0000206 if (mode == MODE_OUT)
207 cc = &send_context;
208 else
209 cc = &receive_context;
210
211 return (cipher_get_keycontext(cc, dat));
212}
213
214void
215packet_set_keycontext(int mode, u_char *dat)
216{
217 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000218
Ben Lindstromf6027d32002-03-22 01:42:04 +0000219 if (mode == MODE_OUT)
220 cc = &send_context;
221 else
222 cc = &receive_context;
223
224 cipher_set_keycontext(cc, dat);
225}
226
227int
228packet_get_keyiv_len(int mode)
229{
230 CipherContext *cc;
231
232 if (mode == MODE_OUT)
233 cc = &send_context;
234 else
235 cc = &receive_context;
236
237 return (cipher_get_keyiv_len(cc));
238}
239void
240packet_set_iv(int mode, u_char *dat)
241{
242 CipherContext *cc;
243
244 if (mode == MODE_OUT)
245 cc = &send_context;
246 else
247 cc = &receive_context;
248
249 cipher_set_keyiv(cc, dat);
250}
251int
252packet_get_ssh1_cipher()
253{
254 return (cipher_get_number(receive_context.cipher));
255}
256
257
258u_int32_t
259packet_get_seqnr(int mode)
260{
261 return (mode == MODE_IN ? read_seqnr : send_seqnr);
262}
263
264void
265packet_set_seqnr(int mode, u_int32_t seqnr)
266{
267 if (mode == MODE_IN)
268 read_seqnr = seqnr;
269 else if (mode == MODE_OUT)
270 send_seqnr = seqnr;
271 else
Ben Lindstrom3dca4f52002-06-06 20:59:25 +0000272 fatal("packet_set_seqnr: bad mode %d", mode);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000273}
274
Damien Miller34132e52000-01-14 15:45:46 +1100275/* returns 1 if connection is via ipv4 */
276
277int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000278packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100279{
280 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100281 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100282
283 memset(&to, 0, sizeof(to));
284 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
285 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000286 if (to.ss_family == AF_INET)
287 return 1;
288#ifdef IPV4_IN_IPV6
289 if (to.ss_family == AF_INET6 &&
290 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
291 return 1;
292#endif
293 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100294}
295
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296/* Sets the connection into non-blocking mode. */
297
298void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000299packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300{
Damien Miller95def091999-11-25 00:26:21 +1100301 /* Set the socket into non-blocking mode. */
302 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
303 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304
Damien Miller95def091999-11-25 00:26:21 +1100305 if (connection_out != connection_in) {
306 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
307 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
308 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309}
310
311/* Returns the socket used for reading. */
312
313int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000314packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315{
Damien Miller95def091999-11-25 00:26:21 +1100316 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317}
318
319/* Returns the descriptor used for writing. */
320
321int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000322packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323{
Damien Miller95def091999-11-25 00:26:21 +1100324 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325}
326
327/* Closes the connection and clears and frees internal data structures. */
328
329void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000330packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331{
Damien Miller95def091999-11-25 00:26:21 +1100332 if (!initialized)
333 return;
334 initialized = 0;
335 if (connection_in == connection_out) {
336 shutdown(connection_out, SHUT_RDWR);
337 close(connection_out);
338 } else {
339 close(connection_in);
340 close(connection_out);
341 }
342 buffer_free(&input);
343 buffer_free(&output);
344 buffer_free(&outgoing_packet);
345 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000346 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100347 buffer_free(&compression_buffer);
348 buffer_compress_uninit();
349 }
Damien Miller963f6b22002-02-19 15:21:23 +1100350 cipher_cleanup(&send_context);
351 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352}
353
354/* Sets remote side protocol flags. */
355
356void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000357packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358{
Damien Miller95def091999-11-25 00:26:21 +1100359 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360}
361
362/* Returns the remote protocol flags set earlier by the above function. */
363
Ben Lindstrom46c16222000-12-22 01:43:59 +0000364u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000365packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366{
Damien Miller95def091999-11-25 00:26:21 +1100367 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368}
369
Damien Miller5428f641999-11-25 11:54:57 +1100370/*
371 * Starts packet compression from the next packet on in both directions.
372 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
373 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374
Ben Lindstrombba81212001-06-25 05:01:22 +0000375static void
376packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000377{
378 if (compression_buffer_ready == 1)
379 return;
380 compression_buffer_ready = 1;
381 buffer_init(&compression_buffer);
382}
383
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384void
385packet_start_compression(int level)
386{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000387 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100388 fatal("Compression already enabled.");
389 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000390 packet_init_compression();
391 buffer_compress_init_send(level);
392 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393}
394
Damien Miller5428f641999-11-25 11:54:57 +1100395/*
Damien Miller5428f641999-11-25 11:54:57 +1100396 * Causes any further packets to be encrypted using the given key. The same
397 * key is used for both sending and reception. However, both directions are
398 * encrypted independently of each other.
399 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000400
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000402packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100403 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404{
Damien Miller874d77b2000-10-14 16:23:11 +1100405 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000406
Damien Miller874d77b2000-10-14 16:23:11 +1100407 if (cipher == NULL)
408 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000409 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100410 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000411 if (keylen > SSH_SESSION_KEY_LENGTH)
412 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
413 memcpy(ssh1_key, key, keylen);
414 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100415 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
416 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417}
418
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000419u_int
420packet_get_encryption_key(u_char *key)
421{
422 if (key == NULL)
423 return (ssh1_keylen);
424 memcpy(key, ssh1_key, ssh1_keylen);
425 return (ssh1_keylen);
426}
427
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000428/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000430packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000432 u_char buf[9];
433 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434
Ben Lindstrom204e4882001-03-05 06:47:00 +0000435 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000436 len = compat20 ? 6 : 9;
437 memset(buf, 0, len - 1);
438 buf[len - 1] = type;
439 buffer_clear(&outgoing_packet);
440 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000441}
442
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000443/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444void
445packet_put_char(int value)
446{
Damien Miller95def091999-11-25 00:26:21 +1100447 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000448
Damien Miller95def091999-11-25 00:26:21 +1100449 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000452packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453{
Damien Miller95def091999-11-25 00:26:21 +1100454 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100457packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458{
Damien Miller95def091999-11-25 00:26:21 +1100459 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460}
Damien Miller33b13562000-04-04 14:38:59 +1000461void
462packet_put_cstring(const char *str)
463{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000464 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000465}
Damien Miller33b13562000-04-04 14:38:59 +1000466void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100467packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000468{
469 buffer_append(&outgoing_packet, buf, len);
470}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471void
Damien Miller95def091999-11-25 00:26:21 +1100472packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473{
Damien Miller95def091999-11-25 00:26:21 +1100474 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
Damien Miller33b13562000-04-04 14:38:59 +1000476void
477packet_put_bignum2(BIGNUM * value)
478{
479 buffer_put_bignum2(&outgoing_packet, value);
480}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481
Damien Miller5428f641999-11-25 11:54:57 +1100482/*
483 * Finalizes and sends the packet. If the encryption key has been set,
484 * encrypts the packet before sending.
485 */
Damien Miller95def091999-11-25 00:26:21 +1100486
Ben Lindstrombba81212001-06-25 05:01:22 +0000487static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000488packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000490 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100491 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000492 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100493 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494
Damien Miller5428f641999-11-25 11:54:57 +1100495 /*
496 * If using packet compression, compress the payload of the outgoing
497 * packet.
498 */
Damien Miller95def091999-11-25 00:26:21 +1100499 if (packet_compression) {
500 buffer_clear(&compression_buffer);
501 /* Skip padding. */
502 buffer_consume(&outgoing_packet, 8);
503 /* padding */
504 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
505 buffer_compress(&outgoing_packet, &compression_buffer);
506 buffer_clear(&outgoing_packet);
507 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100508 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100509 }
510 /* Compute packet length without padding (add checksum, remove padding). */
511 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Damien Millere247cc42000-05-07 12:03:14 +1000513 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100514 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100515 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100516 cp = buffer_ptr(&outgoing_packet);
517 for (i = 0; i < padding; i++) {
518 if (i % 4 == 0)
519 rand = arc4random();
520 cp[7 - i] = rand & 0xff;
521 rand >>= 8;
522 }
523 }
524 buffer_consume(&outgoing_packet, 8 - padding);
525
526 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100527 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000528 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100529 PUT_32BIT(buf, checksum);
530 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531
532#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100533 fprintf(stderr, "packet_send plain: ");
534 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535#endif
536
Damien Miller95def091999-11-25 00:26:21 +1100537 /* Append to output. */
538 PUT_32BIT(buf, len);
539 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100540 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100541 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100542 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100543
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100545 fprintf(stderr, "encrypted: ");
546 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547#endif
548
Damien Miller95def091999-11-25 00:26:21 +1100549 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000550
Damien Miller5428f641999-11-25 11:54:57 +1100551 /*
552 * Note that the packet is now only buffered in output. It won\'t be
553 * actually sent until packet_write_wait or packet_write_poll is
554 * called.
555 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556}
557
Ben Lindstromf6027d32002-03-22 01:42:04 +0000558void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000559set_newkeys(int mode)
560{
561 Enc *enc;
562 Mac *mac;
563 Comp *comp;
564 CipherContext *cc;
Damien Miller963f6b22002-02-19 15:21:23 +1100565 int encrypt;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000566
567 debug("newkeys: mode %d", mode);
568
Damien Miller963f6b22002-02-19 15:21:23 +1100569 if (mode == MODE_OUT) {
570 cc = &send_context;
571 encrypt = CIPHER_ENCRYPT;
572 } else {
573 cc = &receive_context;
574 encrypt = CIPHER_DECRYPT;
575 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000576 if (newkeys[mode] != NULL) {
577 debug("newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100578 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000579 enc = &newkeys[mode]->enc;
580 mac = &newkeys[mode]->mac;
581 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000582 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000583 xfree(enc->name);
584 xfree(enc->iv);
585 xfree(enc->key);
586 xfree(mac->name);
587 xfree(mac->key);
588 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000589 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000590 }
591 newkeys[mode] = kex_get_newkeys(mode);
592 if (newkeys[mode] == NULL)
593 fatal("newkeys: no keys for mode %d", mode);
594 enc = &newkeys[mode]->enc;
595 mac = &newkeys[mode]->mac;
596 comp = &newkeys[mode]->comp;
597 if (mac->md != NULL)
598 mac->enabled = 1;
599 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100600 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
601 enc->iv, enc->block_size, encrypt);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000602 /* Deleting the keys does not gain extra security */
603 /* memset(enc->iv, 0, enc->block_size);
604 memset(enc->key, 0, enc->key_len); */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000605 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000606 packet_init_compression();
607 if (mode == MODE_OUT)
608 buffer_compress_init_send(6);
609 else
610 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000611 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000612 }
613}
614
Damien Miller5428f641999-11-25 11:54:57 +1100615/*
Damien Miller33b13562000-04-04 14:38:59 +1000616 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
617 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000618static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000619packet_send2(void)
Damien Miller33b13562000-04-04 14:38:59 +1000620{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000621 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100622 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000623 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100624 u_int i, len;
Damien Miller33b13562000-04-04 14:38:59 +1000625 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000626 Enc *enc = NULL;
627 Mac *mac = NULL;
628 Comp *comp = NULL;
629 int block_size;
630
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000631 if (newkeys[MODE_OUT] != NULL) {
632 enc = &newkeys[MODE_OUT]->enc;
633 mac = &newkeys[MODE_OUT]->mac;
634 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000635 }
Damien Miller963f6b22002-02-19 15:21:23 +1100636 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000637
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000638 cp = buffer_ptr(&outgoing_packet);
639 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000640
641#ifdef PACKET_DEBUG
642 fprintf(stderr, "plain: ");
643 buffer_dump(&outgoing_packet);
644#endif
645
646 if (comp && comp->enabled) {
647 len = buffer_len(&outgoing_packet);
648 /* skip header, compress only payload */
649 buffer_consume(&outgoing_packet, 5);
650 buffer_clear(&compression_buffer);
651 buffer_compress(&outgoing_packet, &compression_buffer);
652 buffer_clear(&outgoing_packet);
653 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
654 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
655 buffer_len(&compression_buffer));
656 DBG(debug("compression: raw %d compressed %d", len,
657 buffer_len(&outgoing_packet)));
658 }
659
660 /* sizeof (packet_len + pad_len + payload) */
661 len = buffer_len(&outgoing_packet);
662
663 /*
664 * calc size of padding, alloc space, get random data,
665 * minimum padding is 4 bytes
666 */
667 padlen = block_size - (len % block_size);
668 if (padlen < 4)
669 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100670 if (extra_pad) {
671 /* will wrap if extra_pad+padlen > 255 */
672 extra_pad = roundup(extra_pad, block_size);
673 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000674 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100675 pad, len, padlen, extra_pad);
676 padlen += pad;
677 extra_pad = 0;
678 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100679 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100680 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000681 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000682 for (i = 0; i < padlen; i++) {
683 if (i % 4 == 0)
684 rand = arc4random();
685 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000686 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000687 }
Damien Millere247cc42000-05-07 12:03:14 +1000688 } else {
689 /* clear padding */
690 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000691 }
692 /* packet_length includes payload, padding and padding length field */
693 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000694 cp = buffer_ptr(&outgoing_packet);
695 PUT_32BIT(cp, packet_length);
696 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000697 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
698
699 /* compute MAC over seqnr and packet(length fields, payload, padding) */
700 if (mac && mac->enabled) {
Ben Lindstromf6027d32002-03-22 01:42:04 +0000701 macbuf = mac_compute(mac, send_seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100702 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000703 buffer_len(&outgoing_packet));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000704 DBG(debug("done calc MAC out #%d", send_seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000705 }
706 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100707 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100708 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000709 buffer_len(&outgoing_packet));
710 /* append unencrypted MAC */
711 if (mac && mac->enabled)
712 buffer_append(&output, (char *)macbuf, mac->mac_len);
713#ifdef PACKET_DEBUG
714 fprintf(stderr, "encrypted: ");
715 buffer_dump(&output);
716#endif
Damien Miller4af51302000-04-16 11:18:38 +1000717 /* increment sequence number for outgoing packets */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000718 if (++send_seqnr == 0)
Damien Miller4af51302000-04-16 11:18:38 +1000719 log("outgoing seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000720 buffer_clear(&outgoing_packet);
721
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000722 if (type == SSH2_MSG_NEWKEYS)
723 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000724}
725
726void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000727packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000728{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000729 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000730 packet_send2();
731 else
732 packet_send1();
733 DBG(debug("packet_send done"));
734}
735
Damien Miller33b13562000-04-04 14:38:59 +1000736/*
Damien Miller5428f641999-11-25 11:54:57 +1100737 * Waits until a packet has been received, and returns its type. Note that
738 * no other data is processed until this returns, so this function should not
739 * be used during the interactive session.
740 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741
742int
Damien Millerdff50992002-01-22 23:16:32 +1100743packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000744{
Damien Miller95def091999-11-25 00:26:21 +1100745 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000746 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100747 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000748 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000749
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000750 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
751 sizeof(fd_mask));
752
Damien Miller95def091999-11-25 00:26:21 +1100753 /* Since we are blocking, ensure that all written packets have been sent. */
754 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000755
Damien Miller95def091999-11-25 00:26:21 +1100756 /* Stay in the loop until we have received a complete packet. */
757 for (;;) {
758 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100759 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000760 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000761 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100762 || type == SSH_SMSG_FAILURE
763 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000764 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100765 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100766 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000767 if (type != SSH_MSG_NONE) {
768 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100769 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000770 }
Damien Miller5428f641999-11-25 11:54:57 +1100771 /*
772 * Otherwise, wait for some data to arrive, add it to the
773 * buffer, and try again.
774 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000775 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
776 sizeof(fd_mask));
777 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100778
Damien Miller95def091999-11-25 00:26:21 +1100779 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000780 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000781 (errno == EAGAIN || errno == EINTR))
782 ;
Damien Miller5428f641999-11-25 11:54:57 +1100783
Damien Miller95def091999-11-25 00:26:21 +1100784 /* Read data from the socket. */
785 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100786 if (len == 0) {
787 log("Connection closed by %.200s", get_remote_ipaddr());
788 fatal_cleanup();
789 }
Damien Miller95def091999-11-25 00:26:21 +1100790 if (len < 0)
791 fatal("Read from socket failed: %.100s", strerror(errno));
792 /* Append it to the buffer. */
793 packet_process_incoming(buf, len);
794 }
795 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796}
797
Damien Miller278f9072001-12-21 15:00:19 +1100798int
Damien Millerdff50992002-01-22 23:16:32 +1100799packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100800{
Damien Millerdff50992002-01-22 23:16:32 +1100801 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100802}
803
Damien Miller5428f641999-11-25 11:54:57 +1100804/*
805 * Waits until a packet has been received, verifies that its type matches
806 * that given, and gives a fatal error and exits if there is a mismatch.
807 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808
809void
Damien Millerdff50992002-01-22 23:16:32 +1100810packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811{
Damien Miller95def091999-11-25 00:26:21 +1100812 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000813
Damien Millerdff50992002-01-22 23:16:32 +1100814 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100815 if (type != expected_type)
816 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000817 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818}
819
820/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100821 * packet_process_incoming. If so, reads the packet; otherwise returns
822 * SSH_MSG_NONE. This does not wait for data from the connection.
823 *
824 * SSH_MSG_DISCONNECT is handled specially here. Also,
825 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
826 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100827 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828
Ben Lindstrombba81212001-06-25 05:01:22 +0000829static int
Damien Millerdff50992002-01-22 23:16:32 +1100830packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000832 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000833 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000834 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000835
Damien Miller95def091999-11-25 00:26:21 +1100836 /* Check if input size is less than minimum packet size. */
837 if (buffer_len(&input) < 4 + 8)
838 return SSH_MSG_NONE;
839 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000840 cp = buffer_ptr(&input);
841 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100842 if (len < 1 + 2 + 2 || len > 256 * 1024)
843 packet_disconnect("Bad packet length %d.", len);
844 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000845
Damien Miller95def091999-11-25 00:26:21 +1100846 /* Check if the packet has been entirely received. */
847 if (buffer_len(&input) < 4 + padded_len)
848 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849
Damien Miller95def091999-11-25 00:26:21 +1100850 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851
Damien Miller95def091999-11-25 00:26:21 +1100852 /* Consume packet length. */
853 buffer_consume(&input, 4);
854
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000855 /*
856 * Cryptographic attack detector for ssh
857 * (C)1998 CORE-SDI, Buenos Aires Argentina
858 * Ariel Futoransky(futo@core-sdi.com)
859 */
Damien Miller963f6b22002-02-19 15:21:23 +1100860 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000861 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
862 packet_disconnect("crc32 compensation attack: network attack detected");
863
864 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100865 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100866 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100867 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000868
Damien Miller95def091999-11-25 00:26:21 +1100869 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870
871#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100872 fprintf(stderr, "read_poll plain: ");
873 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875
Damien Miller95def091999-11-25 00:26:21 +1100876 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100877 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000878 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879
Damien Miller95def091999-11-25 00:26:21 +1100880 /* Skip padding. */
881 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100882
Damien Miller95def091999-11-25 00:26:21 +1100883 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100884 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +1100885 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000886 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000888 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
889 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100890 if (checksum != stored_checksum)
891 packet_disconnect("Corrupted check bytes on input.");
892 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Miller95def091999-11-25 00:26:21 +1100894 if (packet_compression) {
895 buffer_clear(&compression_buffer);
896 buffer_uncompress(&incoming_packet, &compression_buffer);
897 buffer_clear(&incoming_packet);
898 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000899 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100900 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000901 type = buffer_get_char(&incoming_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000902 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903}
Damien Miller95def091999-11-25 00:26:21 +1100904
Ben Lindstrombba81212001-06-25 05:01:22 +0000905static int
Damien Millerdff50992002-01-22 23:16:32 +1100906packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000907{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000908 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000909 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000910 u_char *macbuf, *cp, type;
Damien Miller33b13562000-04-04 14:38:59 +1000911 int maclen, block_size;
912 Enc *enc = NULL;
913 Mac *mac = NULL;
914 Comp *comp = NULL;
915
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000916 if (newkeys[MODE_IN] != NULL) {
917 enc = &newkeys[MODE_IN]->enc;
918 mac = &newkeys[MODE_IN]->mac;
919 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000920 }
921 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +1100922 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000923
924 if (packet_length == 0) {
925 /*
926 * check if input size is less than the cipher block size,
927 * decrypt first block and extract length of incoming packet
928 */
929 if (buffer_len(&input) < block_size)
930 return SSH_MSG_NONE;
931 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100932 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +1100933 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +1000934 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000935 cp = buffer_ptr(&incoming_packet);
936 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +1000937 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
938 buffer_dump(&incoming_packet);
939 packet_disconnect("Bad packet length %d.", packet_length);
940 }
941 DBG(debug("input: packet len %d", packet_length+4));
942 buffer_consume(&input, block_size);
943 }
944 /* we have a partial packet of block_size bytes */
945 need = 4 + packet_length - block_size;
946 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
947 need, maclen));
948 if (need % block_size != 0)
949 fatal("padding error: need %d block %d mod %d",
950 need, block_size, need % block_size);
951 /*
952 * check if the entire packet has been received and
953 * decrypt into incoming_packet
954 */
955 if (buffer_len(&input) < need + maclen)
956 return SSH_MSG_NONE;
957#ifdef PACKET_DEBUG
958 fprintf(stderr, "read_poll enc/full: ");
959 buffer_dump(&input);
960#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100961 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +1100962 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +1000963 buffer_consume(&input, need);
964 /*
965 * compute MAC over seqnr and packet,
966 * increment sequence number for incoming packet
967 */
Damien Miller4af51302000-04-16 11:18:38 +1000968 if (mac && mac->enabled) {
Ben Lindstromf6027d32002-03-22 01:42:04 +0000969 macbuf = mac_compute(mac, read_seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100970 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000971 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000972 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100973 packet_disconnect("Corrupted MAC on input.");
Ben Lindstromf6027d32002-03-22 01:42:04 +0000974 DBG(debug("MAC #%d ok", read_seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000975 buffer_consume(&input, mac->mac_len);
976 }
Damien Miller278f9072001-12-21 15:00:19 +1100977 if (seqnr_p != NULL)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000978 *seqnr_p = read_seqnr;
979 if (++read_seqnr == 0)
Damien Miller4af51302000-04-16 11:18:38 +1000980 log("incoming seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000981
982 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100983 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000984 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +1000985 DBG(debug("input: padlen %d", padlen));
986 if (padlen < 4)
987 packet_disconnect("Corrupted padlen %d on input.", padlen);
988
989 /* skip packet size + padlen, discard padding */
990 buffer_consume(&incoming_packet, 4 + 1);
991 buffer_consume_end(&incoming_packet, padlen);
992
993 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
994 if (comp && comp->enabled) {
995 buffer_clear(&compression_buffer);
996 buffer_uncompress(&incoming_packet, &compression_buffer);
997 buffer_clear(&incoming_packet);
998 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
999 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001000 DBG(debug("input: len after de-compress %d",
1001 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001002 }
1003 /*
1004 * get packet type, implies consume.
1005 * return length of payload (without type field)
1006 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001007 type = buffer_get_char(&incoming_packet);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001008 if (type == SSH2_MSG_NEWKEYS)
1009 set_newkeys(MODE_IN);
Damien Miller33b13562000-04-04 14:38:59 +10001010#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001011 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001012 buffer_dump(&incoming_packet);
1013#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001014 /* reset for next packet */
1015 packet_length = 0;
1016 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001017}
1018
1019int
Damien Millerdff50992002-01-22 23:16:32 +11001020packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001021{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001022 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001023 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001024 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001025
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001026 for (;;) {
1027 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001028 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001029 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001030 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001031 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001032 case SSH2_MSG_IGNORE:
1033 break;
1034 case SSH2_MSG_DEBUG:
1035 packet_get_char();
1036 msg = packet_get_string(NULL);
1037 debug("Remote: %.900s", msg);
1038 xfree(msg);
1039 msg = packet_get_string(NULL);
1040 xfree(msg);
1041 break;
1042 case SSH2_MSG_DISCONNECT:
1043 reason = packet_get_int();
1044 msg = packet_get_string(NULL);
Ben Lindstrom3f584742002-06-23 21:49:25 +00001045 log("Received disconnect from %s: %u: %.400s",
1046 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001047 xfree(msg);
1048 fatal_cleanup();
1049 break;
Damien Miller659811f2002-01-22 23:23:11 +11001050 case SSH2_MSG_UNIMPLEMENTED:
1051 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001052 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1053 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001054 break;
Damien Miller33b13562000-04-04 14:38:59 +10001055 default:
1056 return type;
1057 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001058 }
Damien Miller33b13562000-04-04 14:38:59 +10001059 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001060 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001061 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001062 case SSH_MSG_IGNORE:
1063 break;
1064 case SSH_MSG_DEBUG:
1065 msg = packet_get_string(NULL);
1066 debug("Remote: %.900s", msg);
1067 xfree(msg);
1068 break;
1069 case SSH_MSG_DISCONNECT:
1070 msg = packet_get_string(NULL);
Ben Lindstrom3f584742002-06-23 21:49:25 +00001071 log("Received disconnect from %s: %.400s",
1072 get_remote_ipaddr(), msg);
Damien Miller33b13562000-04-04 14:38:59 +10001073 fatal_cleanup();
1074 xfree(msg);
1075 break;
1076 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001077 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001078 DBG(debug("received packet type %d", type));
1079 return type;
1080 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001081 }
Damien Miller33b13562000-04-04 14:38:59 +10001082 }
1083 }
1084}
1085
Damien Miller278f9072001-12-21 15:00:19 +11001086int
Damien Millerdff50992002-01-22 23:16:32 +11001087packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001088{
Damien Millerdff50992002-01-22 23:16:32 +11001089 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001090}
1091
Damien Miller5428f641999-11-25 11:54:57 +11001092/*
1093 * Buffers the given amount of input characters. This is intended to be used
1094 * together with packet_read_poll.
1095 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001096
1097void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001098packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001099{
Damien Miller95def091999-11-25 00:26:21 +11001100 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101}
1102
1103/* Returns a character from the packet. */
1104
Ben Lindstrom46c16222000-12-22 01:43:59 +00001105u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001106packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107{
Damien Miller95def091999-11-25 00:26:21 +11001108 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001109
Damien Miller95def091999-11-25 00:26:21 +11001110 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001111 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112}
1113
1114/* Returns an integer from the packet data. */
1115
Ben Lindstrom46c16222000-12-22 01:43:59 +00001116u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001117packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001118{
Damien Miller95def091999-11-25 00:26:21 +11001119 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120}
1121
Damien Miller5428f641999-11-25 11:54:57 +11001122/*
1123 * Returns an arbitrary precision integer from the packet data. The integer
1124 * must have been initialized before this call.
1125 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001126
1127void
Damien Millerd432ccf2002-01-22 23:14:44 +11001128packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129{
Damien Miller76e1e362002-01-22 23:15:57 +11001130 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131}
1132
Damien Miller33b13562000-04-04 14:38:59 +10001133void
Damien Millerd432ccf2002-01-22 23:14:44 +11001134packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001135{
Damien Miller76e1e362002-01-22 23:15:57 +11001136 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001137}
1138
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001139void *
Damien Miller33b13562000-04-04 14:38:59 +10001140packet_get_raw(int *length_ptr)
1141{
1142 int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001143
Damien Miller33b13562000-04-04 14:38:59 +10001144 if (length_ptr != NULL)
1145 *length_ptr = bytes;
1146 return buffer_ptr(&incoming_packet);
1147}
1148
Damien Miller4af51302000-04-16 11:18:38 +10001149int
1150packet_remaining(void)
1151{
1152 return buffer_len(&incoming_packet);
1153}
1154
Damien Miller5428f641999-11-25 11:54:57 +11001155/*
1156 * Returns a string from the packet data. The string is allocated using
1157 * xmalloc; it is the responsibility of the calling program to free it when
1158 * no longer needed. The length_ptr argument may be NULL, or point to an
1159 * integer into which the length of the string is stored.
1160 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001161
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001162void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001163packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164{
Damien Miller95def091999-11-25 00:26:21 +11001165 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001166}
1167
Damien Miller5428f641999-11-25 11:54:57 +11001168/*
1169 * Sends a diagnostic message from the server to the client. This message
1170 * can be sent at any time (but not while constructing another message). The
1171 * message is printed immediately, but only if the client is being executed
1172 * in verbose mode. These messages are primarily intended to ease debugging
1173 * authentication problems. The length of the formatted message must not
1174 * exceed 1024 bytes. This will automatically call packet_write_wait.
1175 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176
1177void
Damien Miller95def091999-11-25 00:26:21 +11001178packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001179{
Damien Miller95def091999-11-25 00:26:21 +11001180 char buf[1024];
1181 va_list args;
1182
Ben Lindstroma14ee472000-12-07 01:24:58 +00001183 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1184 return;
1185
Damien Miller95def091999-11-25 00:26:21 +11001186 va_start(args, fmt);
1187 vsnprintf(buf, sizeof(buf), fmt, args);
1188 va_end(args);
1189
Damien Miller7c8af4f2000-05-01 08:24:07 +10001190 if (compat20) {
1191 packet_start(SSH2_MSG_DEBUG);
1192 packet_put_char(0); /* bool: always display */
1193 packet_put_cstring(buf);
1194 packet_put_cstring("");
1195 } else {
1196 packet_start(SSH_MSG_DEBUG);
1197 packet_put_cstring(buf);
1198 }
Damien Miller95def091999-11-25 00:26:21 +11001199 packet_send();
1200 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201}
1202
Damien Miller5428f641999-11-25 11:54:57 +11001203/*
1204 * Logs the error plus constructs and sends a disconnect packet, closes the
1205 * connection, and exits. This function never returns. The error message
1206 * should not contain a newline. The length of the formatted message must
1207 * not exceed 1024 bytes.
1208 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209
1210void
Damien Miller95def091999-11-25 00:26:21 +11001211packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212{
Damien Miller95def091999-11-25 00:26:21 +11001213 char buf[1024];
1214 va_list args;
1215 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001216
Damien Miller95def091999-11-25 00:26:21 +11001217 if (disconnecting) /* Guard against recursive invocations. */
1218 fatal("packet_disconnect called recursively.");
1219 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220
Damien Miller5428f641999-11-25 11:54:57 +11001221 /*
1222 * Format the message. Note that the caller must make sure the
1223 * message is of limited size.
1224 */
Damien Miller95def091999-11-25 00:26:21 +11001225 va_start(args, fmt);
1226 vsnprintf(buf, sizeof(buf), fmt, args);
1227 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228
Damien Miller95def091999-11-25 00:26:21 +11001229 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001230 if (compat20) {
1231 packet_start(SSH2_MSG_DISCONNECT);
1232 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1233 packet_put_cstring(buf);
1234 packet_put_cstring("");
1235 } else {
1236 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001237 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001238 }
Damien Miller95def091999-11-25 00:26:21 +11001239 packet_send();
1240 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241
Damien Miller95def091999-11-25 00:26:21 +11001242 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001243 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001244
Damien Miller95def091999-11-25 00:26:21 +11001245 /* Close the connection. */
1246 packet_close();
1247
1248 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001249 log("Disconnecting: %.100s", buf);
1250 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251}
1252
Damien Miller5428f641999-11-25 11:54:57 +11001253/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254
1255void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001256packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001257{
Damien Miller95def091999-11-25 00:26:21 +11001258 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001259
Damien Miller95def091999-11-25 00:26:21 +11001260 if (len > 0) {
1261 len = write(connection_out, buffer_ptr(&output), len);
1262 if (len <= 0) {
1263 if (errno == EAGAIN)
1264 return;
1265 else
1266 fatal("Write failed: %.100s", strerror(errno));
1267 }
1268 buffer_consume(&output, len);
1269 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270}
1271
Damien Miller5428f641999-11-25 11:54:57 +11001272/*
1273 * Calls packet_write_poll repeatedly until all pending output data has been
1274 * written.
1275 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001276
1277void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001278packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001279{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001280 fd_set *setp;
1281
1282 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1283 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001284 packet_write_poll();
1285 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001286 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1287 sizeof(fd_mask));
1288 FD_SET(connection_out, setp);
1289 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001290 (errno == EAGAIN || errno == EINTR))
1291 ;
Damien Miller95def091999-11-25 00:26:21 +11001292 packet_write_poll();
1293 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001294 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001295}
1296
1297/* Returns true if there is buffered data to write to the connection. */
1298
1299int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001300packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301{
Damien Miller95def091999-11-25 00:26:21 +11001302 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001303}
1304
1305/* Returns true if there is not too much data to write to the connection. */
1306
1307int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001308packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309{
Damien Miller95def091999-11-25 00:26:21 +11001310 if (interactive_mode)
1311 return buffer_len(&output) < 16384;
1312 else
1313 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001314}
1315
1316/* Informs that the current session is interactive. Sets IP flags for that. */
1317
1318void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001319packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001321 static int called = 0;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001322#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001323 int lowdelay = IPTOS_LOWDELAY;
1324 int throughput = IPTOS_THROUGHPUT;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001325#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001326
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001327 if (called)
1328 return;
1329 called = 1;
1330
Damien Miller95def091999-11-25 00:26:21 +11001331 /* Record that we are in interactive mode. */
1332 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001333
Damien Miller34132e52000-01-14 15:45:46 +11001334 /* Only set socket options if using a socket. */
1335 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001336 return;
Damien Miller34132e52000-01-14 15:45:46 +11001337 /*
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001338 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
Damien Miller34132e52000-01-14 15:45:46 +11001339 */
Damien Miller95def091999-11-25 00:26:21 +11001340 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001341 /*
1342 * Set IP options for an interactive connection. Use
1343 * IPTOS_LOWDELAY and TCP_NODELAY.
1344 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001345#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001346 if (packet_connection_is_ipv4()) {
Kevin Steves0c696152001-01-24 13:47:43 +00001347 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
Ben Lindstrom733a2352002-03-05 01:31:28 +00001348 &lowdelay, sizeof(lowdelay)) < 0)
Kevin Steves0c696152001-01-24 13:47:43 +00001349 error("setsockopt IPTOS_LOWDELAY: %.100s",
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001350 strerror(errno));
1351 }
Damien Miller615f9392000-05-17 22:53:33 +10001352#endif
Damien Miller398e1cf2002-02-05 11:52:13 +11001353 set_nodelay(connection_in);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001354 } else if (packet_connection_is_ipv4()) {
Damien Miller5428f641999-11-25 11:54:57 +11001355 /*
1356 * Set IP options for a non-interactive connection. Use
1357 * IPTOS_THROUGHPUT.
1358 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001359#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom733a2352002-03-05 01:31:28 +00001360 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001361 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001362 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001363#endif
Damien Miller95def091999-11-25 00:26:21 +11001364 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365}
1366
1367/* Returns true if the current connection is interactive. */
1368
1369int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001370packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001371{
Damien Miller95def091999-11-25 00:26:21 +11001372 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001373}
Damien Miller6162d121999-11-21 13:23:52 +11001374
1375int
1376packet_set_maxsize(int s)
1377{
Damien Miller95def091999-11-25 00:26:21 +11001378 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001379
Damien Miller95def091999-11-25 00:26:21 +11001380 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001381 log("packet_set_maxsize: called twice: old %d new %d",
1382 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001383 return -1;
1384 }
1385 if (s < 4 * 1024 || s > 1024 * 1024) {
1386 log("packet_set_maxsize: bad size %d", s);
1387 return -1;
1388 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001389 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001390 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001391 max_packet_size = s;
1392 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001393}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001394
Damien Miller9f643902001-11-12 11:02:52 +11001395/* roundup current message to pad bytes */
1396void
1397packet_add_padding(u_char pad)
1398{
1399 extra_pad = pad;
1400}
1401
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001402/*
1403 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001404 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001405 * byte SSH_MSG_IGNORE
1406 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001407 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001408 * All implementations MUST understand (and ignore) this message at any
1409 * time (after receiving the protocol version). No implementation is
1410 * required to send them. This message can be used as an additional
1411 * protection measure against advanced traffic analysis techniques.
1412 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001413void
1414packet_send_ignore(int nbytes)
1415{
1416 u_int32_t rand = 0;
1417 int i;
1418
1419 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001420 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001421 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001422 if (i % 4 == 0)
1423 rand = arc4random();
1424 packet_put_char(rand & 0xff);
1425 rand >>= 8;
1426 }
1427}