blob: b4f3aea1424af4736c23dbc106b7505ccd24562f [file] [log] [blame]
Darren Tucker5d196262006-07-12 22:15:16 +10001/* $OpenBSD: packet.c,v 1.134 2006/07/10 16:37:36 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
Damien Millere3b60b52006-07-10 21:08:03 +100042#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100043#include "openbsd-compat/sys-queue.h"
Damien Miller8ec8c3e2006-07-10 20:35:38 +100044#include <sys/socket.h>
45
46#include <netinet/in_systm.h>
47#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110048#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
Darren Tucker5d196262006-07-12 22:15:16 +100050#include <stdarg.h>
51
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052#include "xmalloc.h"
53#include "buffer.h"
54#include "packet.h"
55#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
58#include "compress.h"
59#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000060#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller33b13562000-04-04 14:38:59 +100062#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100064#include "ssh2.h"
65
Damien Miller874d77b2000-10-14 16:23:11 +110066#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100067#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000068#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "log.h"
70#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110071#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000072#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100073
74#ifdef PACKET_DEBUG
75#define DBG(x) x
76#else
77#define DBG(x)
78#endif
79
Damien Miller5428f641999-11-25 11:54:57 +110080/*
81 * This variable contains the file descriptors used for communicating with
82 * the other side. connection_in is used for reading; connection_out for
83 * writing. These can be the same descriptor, in which case it is assumed to
84 * be a socket.
85 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086static int connection_in = -1;
87static int connection_out = -1;
88
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000090static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
92/* Encryption context for receiving data. This is only used for decryption. */
93static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110094
95/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096static CipherContext send_context;
97
98/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000099Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
101/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000102Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
104/* Buffer for the partial outgoing packet being constructed. */
105static Buffer outgoing_packet;
106
107/* Buffer for the incoming packet currently being processed. */
108static Buffer incoming_packet;
109
110/* Scratch buffer for packet compression/decompression. */
111static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000112static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
114/* Flag indicating whether packet compression/decompression is enabled. */
115static int packet_compression = 0;
116
Damien Miller6162d121999-11-21 13:23:52 +1100117/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000118u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100119
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120/* Flag indicating whether this module has been initialized. */
121static int initialized = 0;
122
123/* Set to true if the connection is interactive. */
124static int interactive_mode = 0;
125
Damien Miller9786e6e2005-07-26 21:54:56 +1000126/* Set to true if we are the server side. */
127static int server_side = 0;
128
129/* Set to true if we are authenticated. */
130static int after_authentication = 0;
131
Damien Miller33b13562000-04-04 14:38:59 +1000132/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000133Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000134static struct packet_state {
135 u_int32_t seqnr;
136 u_int32_t packets;
137 u_int64_t blocks;
138} p_read, p_send;
139
140static u_int64_t max_blocks_in, max_blocks_out;
141static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000142
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000143/* Session key for protocol v1 */
144static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
145static u_int ssh1_keylen;
146
Damien Miller9f643902001-11-12 11:02:52 +1100147/* roundup current message to extra_pad bytes */
148static u_char extra_pad = 0;
149
Damien Millera5539d22003-04-09 20:50:06 +1000150struct packet {
151 TAILQ_ENTRY(packet) next;
152 u_char type;
153 Buffer payload;
154};
155TAILQ_HEAD(, packet) outgoing;
156
Damien Miller5428f641999-11-25 11:54:57 +1100157/*
158 * Sets the descriptors used for communication. Disables encryption until
159 * packet_set_encryption_key is called.
160 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161void
162packet_set_connection(int fd_in, int fd_out)
163{
Damien Miller874d77b2000-10-14 16:23:11 +1100164 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000165
Damien Miller874d77b2000-10-14 16:23:11 +1100166 if (none == NULL)
167 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100168 connection_in = fd_in;
169 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000170 cipher_init(&send_context, none, (const u_char *)"",
171 0, NULL, 0, CIPHER_ENCRYPT);
172 cipher_init(&receive_context, none, (const u_char *)"",
173 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000174 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100175 if (!initialized) {
176 initialized = 1;
177 buffer_init(&input);
178 buffer_init(&output);
179 buffer_init(&outgoing_packet);
180 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000181 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100182 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183}
184
Damien Miller34132e52000-01-14 15:45:46 +1100185/* Returns 1 if remote host is connected via socket, 0 if not. */
186
187int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000188packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100189{
190 struct sockaddr_storage from, to;
191 socklen_t fromlen, tolen;
192
193 /* filedescriptors in and out are the same, so it's a socket */
194 if (connection_in == connection_out)
195 return 1;
196 fromlen = sizeof(from);
197 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100198 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100199 return 0;
200 tolen = sizeof(to);
201 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100202 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100203 return 0;
204 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
205 return 0;
206 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
207 return 0;
208 return 1;
209}
210
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000211/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000212 * Exports an IV from the CipherContext required to export the key
213 * state back from the unprivileged child to the privileged parent
214 * process.
215 */
216
217void
218packet_get_keyiv(int mode, u_char *iv, u_int len)
219{
220 CipherContext *cc;
221
222 if (mode == MODE_OUT)
223 cc = &send_context;
224 else
225 cc = &receive_context;
226
227 cipher_get_keyiv(cc, iv, len);
228}
229
230int
231packet_get_keycontext(int mode, u_char *dat)
232{
233 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000234
Ben Lindstromf6027d32002-03-22 01:42:04 +0000235 if (mode == MODE_OUT)
236 cc = &send_context;
237 else
238 cc = &receive_context;
239
240 return (cipher_get_keycontext(cc, dat));
241}
242
243void
244packet_set_keycontext(int mode, u_char *dat)
245{
246 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000247
Ben Lindstromf6027d32002-03-22 01:42:04 +0000248 if (mode == MODE_OUT)
249 cc = &send_context;
250 else
251 cc = &receive_context;
252
253 cipher_set_keycontext(cc, dat);
254}
255
256int
257packet_get_keyiv_len(int mode)
258{
259 CipherContext *cc;
260
261 if (mode == MODE_OUT)
262 cc = &send_context;
263 else
264 cc = &receive_context;
265
266 return (cipher_get_keyiv_len(cc));
267}
Damien Miller4f7becb2006-03-26 14:10:14 +1100268
Ben Lindstromf6027d32002-03-22 01:42:04 +0000269void
270packet_set_iv(int mode, u_char *dat)
271{
272 CipherContext *cc;
273
274 if (mode == MODE_OUT)
275 cc = &send_context;
276 else
277 cc = &receive_context;
278
279 cipher_set_keyiv(cc, dat);
280}
Damien Miller4f7becb2006-03-26 14:10:14 +1100281
Ben Lindstromf6027d32002-03-22 01:42:04 +0000282int
Damien Miller2b92d322003-06-11 22:05:06 +1000283packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000284{
285 return (cipher_get_number(receive_context.cipher));
286}
287
Damien Millera5539d22003-04-09 20:50:06 +1000288void
289packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000290{
Damien Millera5539d22003-04-09 20:50:06 +1000291 struct packet_state *state;
292
293 state = (mode == MODE_IN) ? &p_read : &p_send;
294 *seqnr = state->seqnr;
295 *blocks = state->blocks;
296 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000297}
298
299void
Damien Millera5539d22003-04-09 20:50:06 +1000300packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000301{
Damien Millera5539d22003-04-09 20:50:06 +1000302 struct packet_state *state;
303
304 state = (mode == MODE_IN) ? &p_read : &p_send;
305 state->seqnr = seqnr;
306 state->blocks = blocks;
307 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000308}
309
Damien Miller34132e52000-01-14 15:45:46 +1100310/* returns 1 if connection is via ipv4 */
311
312int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000313packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100314{
315 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100316 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100317
318 memset(&to, 0, sizeof(to));
319 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
320 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000321 if (to.ss_family == AF_INET)
322 return 1;
323#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100324 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000325 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
326 return 1;
327#endif
328 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100329}
330
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331/* Sets the connection into non-blocking mode. */
332
333void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000334packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335{
Damien Miller95def091999-11-25 00:26:21 +1100336 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000337 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338
Damien Miller232711f2004-06-15 10:35:30 +1000339 if (connection_out != connection_in)
340 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341}
342
343/* Returns the socket used for reading. */
344
345int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000346packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347{
Damien Miller95def091999-11-25 00:26:21 +1100348 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349}
350
351/* Returns the descriptor used for writing. */
352
353int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000354packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355{
Damien Miller95def091999-11-25 00:26:21 +1100356 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357}
358
359/* Closes the connection and clears and frees internal data structures. */
360
361void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000362packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363{
Damien Miller95def091999-11-25 00:26:21 +1100364 if (!initialized)
365 return;
366 initialized = 0;
367 if (connection_in == connection_out) {
368 shutdown(connection_out, SHUT_RDWR);
369 close(connection_out);
370 } else {
371 close(connection_in);
372 close(connection_out);
373 }
374 buffer_free(&input);
375 buffer_free(&output);
376 buffer_free(&outgoing_packet);
377 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000378 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100379 buffer_free(&compression_buffer);
380 buffer_compress_uninit();
381 }
Damien Miller963f6b22002-02-19 15:21:23 +1100382 cipher_cleanup(&send_context);
383 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384}
385
386/* Sets remote side protocol flags. */
387
388void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000389packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390{
Damien Miller95def091999-11-25 00:26:21 +1100391 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392}
393
394/* Returns the remote protocol flags set earlier by the above function. */
395
Ben Lindstrom46c16222000-12-22 01:43:59 +0000396u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000397packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398{
Damien Miller95def091999-11-25 00:26:21 +1100399 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400}
401
Damien Miller5428f641999-11-25 11:54:57 +1100402/*
403 * Starts packet compression from the next packet on in both directions.
404 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
405 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406
Ben Lindstrombba81212001-06-25 05:01:22 +0000407static void
408packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000409{
410 if (compression_buffer_ready == 1)
411 return;
412 compression_buffer_ready = 1;
413 buffer_init(&compression_buffer);
414}
415
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416void
417packet_start_compression(int level)
418{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000419 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100420 fatal("Compression already enabled.");
421 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000422 packet_init_compression();
423 buffer_compress_init_send(level);
424 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425}
426
Damien Miller5428f641999-11-25 11:54:57 +1100427/*
Damien Miller5428f641999-11-25 11:54:57 +1100428 * Causes any further packets to be encrypted using the given key. The same
429 * key is used for both sending and reception. However, both directions are
430 * encrypted independently of each other.
431 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000432
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000434packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100435 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436{
Damien Miller874d77b2000-10-14 16:23:11 +1100437 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000438
Damien Miller874d77b2000-10-14 16:23:11 +1100439 if (cipher == NULL)
440 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000441 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100442 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000443 if (keylen > SSH_SESSION_KEY_LENGTH)
444 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
445 memcpy(ssh1_key, key, keylen);
446 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100447 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
448 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449}
450
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000451u_int
452packet_get_encryption_key(u_char *key)
453{
454 if (key == NULL)
455 return (ssh1_keylen);
456 memcpy(key, ssh1_key, ssh1_keylen);
457 return (ssh1_keylen);
458}
459
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000460/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000462packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000464 u_char buf[9];
465 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466
Ben Lindstrom204e4882001-03-05 06:47:00 +0000467 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000468 len = compat20 ? 6 : 9;
469 memset(buf, 0, len - 1);
470 buf[len - 1] = type;
471 buffer_clear(&outgoing_packet);
472 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000473}
474
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000475/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476void
477packet_put_char(int value)
478{
Damien Miller95def091999-11-25 00:26:21 +1100479 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000480
Damien Miller95def091999-11-25 00:26:21 +1100481 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482}
Damien Miller4f7becb2006-03-26 14:10:14 +1100483
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000485packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486{
Damien Miller95def091999-11-25 00:26:21 +1100487 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488}
Damien Miller4f7becb2006-03-26 14:10:14 +1100489
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100491packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492{
Damien Miller95def091999-11-25 00:26:21 +1100493 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494}
Damien Miller4f7becb2006-03-26 14:10:14 +1100495
Damien Miller33b13562000-04-04 14:38:59 +1000496void
497packet_put_cstring(const char *str)
498{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000499 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000500}
Damien Miller4f7becb2006-03-26 14:10:14 +1100501
Damien Miller33b13562000-04-04 14:38:59 +1000502void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100503packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000504{
505 buffer_append(&outgoing_packet, buf, len);
506}
Damien Miller4f7becb2006-03-26 14:10:14 +1100507
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508void
Damien Miller95def091999-11-25 00:26:21 +1100509packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510{
Damien Miller95def091999-11-25 00:26:21 +1100511 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512}
Damien Miller4f7becb2006-03-26 14:10:14 +1100513
Damien Miller33b13562000-04-04 14:38:59 +1000514void
515packet_put_bignum2(BIGNUM * value)
516{
517 buffer_put_bignum2(&outgoing_packet, value);
518}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Damien Miller5428f641999-11-25 11:54:57 +1100520/*
521 * Finalizes and sends the packet. If the encryption key has been set,
522 * encrypts the packet before sending.
523 */
Damien Miller95def091999-11-25 00:26:21 +1100524
Ben Lindstrombba81212001-06-25 05:01:22 +0000525static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000526packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000528 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100529 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000530 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000531 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532
Damien Miller5428f641999-11-25 11:54:57 +1100533 /*
534 * If using packet compression, compress the payload of the outgoing
535 * packet.
536 */
Damien Miller95def091999-11-25 00:26:21 +1100537 if (packet_compression) {
538 buffer_clear(&compression_buffer);
539 /* Skip padding. */
540 buffer_consume(&outgoing_packet, 8);
541 /* padding */
542 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
543 buffer_compress(&outgoing_packet, &compression_buffer);
544 buffer_clear(&outgoing_packet);
545 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100546 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100547 }
548 /* Compute packet length without padding (add checksum, remove padding). */
549 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000550
Damien Millere247cc42000-05-07 12:03:14 +1000551 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100552 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100553 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100554 cp = buffer_ptr(&outgoing_packet);
555 for (i = 0; i < padding; i++) {
556 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000557 rnd = arc4random();
558 cp[7 - i] = rnd & 0xff;
559 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100560 }
561 }
562 buffer_consume(&outgoing_packet, 8 - padding);
563
564 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100565 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000566 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100567 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100568 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569
570#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100571 fprintf(stderr, "packet_send plain: ");
572 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573#endif
574
Damien Miller95def091999-11-25 00:26:21 +1100575 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100576 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100577 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100578 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100579 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100580 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100581
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100583 fprintf(stderr, "encrypted: ");
584 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585#endif
586
Damien Miller95def091999-11-25 00:26:21 +1100587 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588
Damien Miller5428f641999-11-25 11:54:57 +1100589 /*
Damien Miller788f2122005-11-05 15:14:59 +1100590 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100591 * actually sent until packet_write_wait or packet_write_poll is
592 * called.
593 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594}
595
Ben Lindstromf6027d32002-03-22 01:42:04 +0000596void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000597set_newkeys(int mode)
598{
599 Enc *enc;
600 Mac *mac;
601 Comp *comp;
602 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000603 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000604 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000605
Ben Lindstrom064496f2002-12-23 02:04:22 +0000606 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000607
Damien Miller963f6b22002-02-19 15:21:23 +1100608 if (mode == MODE_OUT) {
609 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000610 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000611 p_send.packets = p_send.blocks = 0;
612 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100613 } else {
614 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000615 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000616 p_read.packets = p_read.blocks = 0;
617 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100618 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000619 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000620 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100621 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000622 enc = &newkeys[mode]->enc;
623 mac = &newkeys[mode]->mac;
624 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000625 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000626 xfree(enc->name);
627 xfree(enc->iv);
628 xfree(enc->key);
629 xfree(mac->name);
630 xfree(mac->key);
631 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000632 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000633 }
634 newkeys[mode] = kex_get_newkeys(mode);
635 if (newkeys[mode] == NULL)
636 fatal("newkeys: no keys for mode %d", mode);
637 enc = &newkeys[mode]->enc;
638 mac = &newkeys[mode]->mac;
639 comp = &newkeys[mode]->comp;
640 if (mac->md != NULL)
641 mac->enabled = 1;
642 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100643 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000644 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000645 /* Deleting the keys does not gain extra security */
646 /* memset(enc->iv, 0, enc->block_size);
647 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000648 if ((comp->type == COMP_ZLIB ||
649 (comp->type == COMP_DELAYED && after_authentication)) &&
650 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000651 packet_init_compression();
652 if (mode == MODE_OUT)
653 buffer_compress_init_send(6);
654 else
655 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000656 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000657 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000658 /*
659 * The 2^(blocksize*2) limit is too expensive for 3DES,
660 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
661 */
662 if (enc->block_size >= 16)
663 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
664 else
665 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000666 if (rekey_limit)
667 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000668}
669
Damien Miller5428f641999-11-25 11:54:57 +1100670/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000671 * Delayed compression for SSH2 is enabled after authentication:
672 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
673 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
674 */
675static void
676packet_enable_delayed_compress(void)
677{
678 Comp *comp = NULL;
679 int mode;
680
681 /*
682 * Remember that we are past the authentication step, so rekeying
683 * with COMP_DELAYED will turn on compression immediately.
684 */
685 after_authentication = 1;
686 for (mode = 0; mode < MODE_MAX; mode++) {
687 comp = &newkeys[mode]->comp;
688 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000689 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000690 if (mode == MODE_OUT)
691 buffer_compress_init_send(6);
692 else
693 buffer_compress_init_recv();
694 comp->enabled = 1;
695 }
696 }
697}
698
699/*
Damien Miller33b13562000-04-04 14:38:59 +1000700 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
701 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000702static void
Damien Millera5539d22003-04-09 20:50:06 +1000703packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000704{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000705 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100706 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000707 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100708 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000709 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000710 Enc *enc = NULL;
711 Mac *mac = NULL;
712 Comp *comp = NULL;
713 int block_size;
714
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000715 if (newkeys[MODE_OUT] != NULL) {
716 enc = &newkeys[MODE_OUT]->enc;
717 mac = &newkeys[MODE_OUT]->mac;
718 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000719 }
Damien Miller963f6b22002-02-19 15:21:23 +1100720 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000721
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000722 cp = buffer_ptr(&outgoing_packet);
723 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000724
725#ifdef PACKET_DEBUG
726 fprintf(stderr, "plain: ");
727 buffer_dump(&outgoing_packet);
728#endif
729
730 if (comp && comp->enabled) {
731 len = buffer_len(&outgoing_packet);
732 /* skip header, compress only payload */
733 buffer_consume(&outgoing_packet, 5);
734 buffer_clear(&compression_buffer);
735 buffer_compress(&outgoing_packet, &compression_buffer);
736 buffer_clear(&outgoing_packet);
737 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
738 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
739 buffer_len(&compression_buffer));
740 DBG(debug("compression: raw %d compressed %d", len,
741 buffer_len(&outgoing_packet)));
742 }
743
744 /* sizeof (packet_len + pad_len + payload) */
745 len = buffer_len(&outgoing_packet);
746
747 /*
748 * calc size of padding, alloc space, get random data,
749 * minimum padding is 4 bytes
750 */
751 padlen = block_size - (len % block_size);
752 if (padlen < 4)
753 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100754 if (extra_pad) {
755 /* will wrap if extra_pad+padlen > 255 */
756 extra_pad = roundup(extra_pad, block_size);
757 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000758 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100759 pad, len, padlen, extra_pad);
760 padlen += pad;
761 extra_pad = 0;
762 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100763 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100764 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000765 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000766 for (i = 0; i < padlen; i++) {
767 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000768 rnd = arc4random();
769 cp[i] = rnd & 0xff;
770 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000771 }
Damien Millere247cc42000-05-07 12:03:14 +1000772 } else {
773 /* clear padding */
774 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000775 }
776 /* packet_length includes payload, padding and padding length field */
777 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000778 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100779 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000780 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000781 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
782
783 /* compute MAC over seqnr and packet(length fields, payload, padding) */
784 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000785 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100786 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000787 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000788 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000789 }
790 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100791 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100792 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000793 buffer_len(&outgoing_packet));
794 /* append unencrypted MAC */
795 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100796 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000797#ifdef PACKET_DEBUG
798 fprintf(stderr, "encrypted: ");
799 buffer_dump(&output);
800#endif
Damien Miller4af51302000-04-16 11:18:38 +1000801 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000802 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000803 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000804 if (++p_send.packets == 0)
805 if (!(datafellows & SSH_BUG_NOREKEY))
806 fatal("XXX too many packets with same key");
807 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000808 buffer_clear(&outgoing_packet);
809
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000810 if (type == SSH2_MSG_NEWKEYS)
811 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000812 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
813 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000814}
815
Damien Millera5539d22003-04-09 20:50:06 +1000816static void
817packet_send2(void)
818{
819 static int rekeying = 0;
820 struct packet *p;
821 u_char type, *cp;
822
823 cp = buffer_ptr(&outgoing_packet);
824 type = cp[5];
825
826 /* during rekeying we can only send key exchange messages */
827 if (rekeying) {
828 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
829 (type <= SSH2_MSG_TRANSPORT_MAX))) {
830 debug("enqueue packet: %u", type);
831 p = xmalloc(sizeof(*p));
832 p->type = type;
833 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
834 buffer_init(&outgoing_packet);
835 TAILQ_INSERT_TAIL(&outgoing, p, next);
836 return;
837 }
838 }
839
840 /* rekeying starts with sending KEXINIT */
841 if (type == SSH2_MSG_KEXINIT)
842 rekeying = 1;
843
844 packet_send2_wrapped();
845
846 /* after a NEWKEYS message we can send the complete queue */
847 if (type == SSH2_MSG_NEWKEYS) {
848 rekeying = 0;
849 while ((p = TAILQ_FIRST(&outgoing))) {
850 type = p->type;
851 debug("dequeue packet: %u", type);
852 buffer_free(&outgoing_packet);
853 memcpy(&outgoing_packet, &p->payload,
854 sizeof(Buffer));
855 TAILQ_REMOVE(&outgoing, p, next);
856 xfree(p);
857 packet_send2_wrapped();
858 }
859 }
860}
861
Damien Miller33b13562000-04-04 14:38:59 +1000862void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000863packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000864{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000865 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000866 packet_send2();
867 else
868 packet_send1();
869 DBG(debug("packet_send done"));
870}
871
Damien Miller33b13562000-04-04 14:38:59 +1000872/*
Damien Miller5428f641999-11-25 11:54:57 +1100873 * Waits until a packet has been received, and returns its type. Note that
874 * no other data is processed until this returns, so this function should not
875 * be used during the interactive session.
876 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877
878int
Damien Millerdff50992002-01-22 23:16:32 +1100879packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880{
Damien Miller95def091999-11-25 00:26:21 +1100881 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000882 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100883 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000884 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885
Damien Miller07d86be2006-03-26 14:19:21 +1100886 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000887 sizeof(fd_mask));
888
Damien Miller95def091999-11-25 00:26:21 +1100889 /* Since we are blocking, ensure that all written packets have been sent. */
890 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891
Damien Miller95def091999-11-25 00:26:21 +1100892 /* Stay in the loop until we have received a complete packet. */
893 for (;;) {
894 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100895 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000896 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000897 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100898 || type == SSH_SMSG_FAILURE
899 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000900 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100901 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100902 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000903 if (type != SSH_MSG_NONE) {
904 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100905 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000906 }
Damien Miller5428f641999-11-25 11:54:57 +1100907 /*
908 * Otherwise, wait for some data to arrive, add it to the
909 * buffer, and try again.
910 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000911 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
912 sizeof(fd_mask));
913 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100914
Damien Miller95def091999-11-25 00:26:21 +1100915 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000916 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000917 (errno == EAGAIN || errno == EINTR))
918 ;
Damien Miller5428f641999-11-25 11:54:57 +1100919
Damien Miller95def091999-11-25 00:26:21 +1100920 /* Read data from the socket. */
921 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100922 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000923 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000924 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100925 }
Damien Miller95def091999-11-25 00:26:21 +1100926 if (len < 0)
927 fatal("Read from socket failed: %.100s", strerror(errno));
928 /* Append it to the buffer. */
929 packet_process_incoming(buf, len);
930 }
931 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000932}
933
Damien Miller278f9072001-12-21 15:00:19 +1100934int
Damien Millerdff50992002-01-22 23:16:32 +1100935packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100936{
Damien Millerdff50992002-01-22 23:16:32 +1100937 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100938}
939
Damien Miller5428f641999-11-25 11:54:57 +1100940/*
941 * Waits until a packet has been received, verifies that its type matches
942 * that given, and gives a fatal error and exits if there is a mismatch.
943 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944
945void
Damien Millerdff50992002-01-22 23:16:32 +1100946packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947{
Damien Miller95def091999-11-25 00:26:21 +1100948 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949
Damien Millerdff50992002-01-22 23:16:32 +1100950 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100951 if (type != expected_type)
952 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000953 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954}
955
956/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100957 * packet_process_incoming. If so, reads the packet; otherwise returns
958 * SSH_MSG_NONE. This does not wait for data from the connection.
959 *
960 * SSH_MSG_DISCONNECT is handled specially here. Also,
961 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
962 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100963 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964
Ben Lindstrombba81212001-06-25 05:01:22 +0000965static int
Damien Millerdff50992002-01-22 23:16:32 +1100966packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000968 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000969 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000970 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Damien Miller95def091999-11-25 00:26:21 +1100972 /* Check if input size is less than minimum packet size. */
973 if (buffer_len(&input) < 4 + 8)
974 return SSH_MSG_NONE;
975 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000976 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100977 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100978 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000979 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100980 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Damien Miller95def091999-11-25 00:26:21 +1100982 /* Check if the packet has been entirely received. */
983 if (buffer_len(&input) < 4 + padded_len)
984 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000985
Damien Miller95def091999-11-25 00:26:21 +1100986 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987
Damien Miller95def091999-11-25 00:26:21 +1100988 /* Consume packet length. */
989 buffer_consume(&input, 4);
990
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000991 /*
992 * Cryptographic attack detector for ssh
993 * (C)1998 CORE-SDI, Buenos Aires Argentina
994 * Ariel Futoransky(futo@core-sdi.com)
995 */
Damien Miller963f6b22002-02-19 15:21:23 +1100996 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +1100997 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000998 packet_disconnect("crc32 compensation attack: network attack detected");
999
1000 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001001 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001002 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001003 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001004
Damien Miller95def091999-11-25 00:26:21 +11001005 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006
1007#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001008 fprintf(stderr, "read_poll plain: ");
1009 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001010#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011
Damien Miller95def091999-11-25 00:26:21 +11001012 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001013 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001014 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015
Damien Miller95def091999-11-25 00:26:21 +11001016 /* Skip padding. */
1017 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001018
Damien Miller95def091999-11-25 00:26:21 +11001019 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001020 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001021 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001022 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001024 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001025 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001026 if (checksum != stored_checksum)
1027 packet_disconnect("Corrupted check bytes on input.");
1028 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001029
Damien Miller95def091999-11-25 00:26:21 +11001030 if (packet_compression) {
1031 buffer_clear(&compression_buffer);
1032 buffer_uncompress(&incoming_packet, &compression_buffer);
1033 buffer_clear(&incoming_packet);
1034 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001035 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001036 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001037 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001038 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1039 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001040 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041}
Damien Miller95def091999-11-25 00:26:21 +11001042
Ben Lindstrombba81212001-06-25 05:01:22 +00001043static int
Damien Millerdff50992002-01-22 23:16:32 +11001044packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001045{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001046 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001047 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001048 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001049 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001050 Enc *enc = NULL;
1051 Mac *mac = NULL;
1052 Comp *comp = NULL;
1053
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001054 if (newkeys[MODE_IN] != NULL) {
1055 enc = &newkeys[MODE_IN]->enc;
1056 mac = &newkeys[MODE_IN]->mac;
1057 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001058 }
1059 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001060 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001061
1062 if (packet_length == 0) {
1063 /*
1064 * check if input size is less than the cipher block size,
1065 * decrypt first block and extract length of incoming packet
1066 */
1067 if (buffer_len(&input) < block_size)
1068 return SSH_MSG_NONE;
1069 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001070 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001071 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001072 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001073 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001074 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001075 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001076#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001077 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001078#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001079 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001080 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001081 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001082 buffer_consume(&input, block_size);
1083 }
1084 /* we have a partial packet of block_size bytes */
1085 need = 4 + packet_length - block_size;
1086 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1087 need, maclen));
1088 if (need % block_size != 0)
1089 fatal("padding error: need %d block %d mod %d",
1090 need, block_size, need % block_size);
1091 /*
1092 * check if the entire packet has been received and
1093 * decrypt into incoming_packet
1094 */
1095 if (buffer_len(&input) < need + maclen)
1096 return SSH_MSG_NONE;
1097#ifdef PACKET_DEBUG
1098 fprintf(stderr, "read_poll enc/full: ");
1099 buffer_dump(&input);
1100#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001101 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001102 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001103 buffer_consume(&input, need);
1104 /*
1105 * compute MAC over seqnr and packet,
1106 * increment sequence number for incoming packet
1107 */
Damien Miller4af51302000-04-16 11:18:38 +10001108 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001109 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001110 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001111 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001112 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001113 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001114 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001115 buffer_consume(&input, mac->mac_len);
1116 }
Damien Miller278f9072001-12-21 15:00:19 +11001117 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001118 *seqnr_p = p_read.seqnr;
1119 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001120 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001121 if (++p_read.packets == 0)
1122 if (!(datafellows & SSH_BUG_NOREKEY))
1123 fatal("XXX too many packets with same key");
1124 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001125
1126 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001127 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001128 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001129 DBG(debug("input: padlen %d", padlen));
1130 if (padlen < 4)
1131 packet_disconnect("Corrupted padlen %d on input.", padlen);
1132
1133 /* skip packet size + padlen, discard padding */
1134 buffer_consume(&incoming_packet, 4 + 1);
1135 buffer_consume_end(&incoming_packet, padlen);
1136
1137 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1138 if (comp && comp->enabled) {
1139 buffer_clear(&compression_buffer);
1140 buffer_uncompress(&incoming_packet, &compression_buffer);
1141 buffer_clear(&incoming_packet);
1142 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1143 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001144 DBG(debug("input: len after de-compress %d",
1145 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001146 }
1147 /*
1148 * get packet type, implies consume.
1149 * return length of payload (without type field)
1150 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001151 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001152 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1153 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001154 if (type == SSH2_MSG_NEWKEYS)
1155 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001156 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1157 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001158#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001159 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001160 buffer_dump(&incoming_packet);
1161#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001162 /* reset for next packet */
1163 packet_length = 0;
1164 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001165}
1166
1167int
Damien Millerdff50992002-01-22 23:16:32 +11001168packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001169{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001170 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001171 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001172 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001173
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001174 for (;;) {
1175 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001176 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001177 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001178 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001179 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001180 case SSH2_MSG_IGNORE:
1181 break;
1182 case SSH2_MSG_DEBUG:
1183 packet_get_char();
1184 msg = packet_get_string(NULL);
1185 debug("Remote: %.900s", msg);
1186 xfree(msg);
1187 msg = packet_get_string(NULL);
1188 xfree(msg);
1189 break;
1190 case SSH2_MSG_DISCONNECT:
1191 reason = packet_get_int();
1192 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001193 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001194 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001195 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001196 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001197 break;
Damien Miller659811f2002-01-22 23:23:11 +11001198 case SSH2_MSG_UNIMPLEMENTED:
1199 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001200 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1201 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001202 break;
Damien Miller33b13562000-04-04 14:38:59 +10001203 default:
1204 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001205 }
Damien Miller33b13562000-04-04 14:38:59 +10001206 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001207 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001208 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001209 case SSH_MSG_IGNORE:
1210 break;
1211 case SSH_MSG_DEBUG:
1212 msg = packet_get_string(NULL);
1213 debug("Remote: %.900s", msg);
1214 xfree(msg);
1215 break;
1216 case SSH_MSG_DISCONNECT:
1217 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001218 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001219 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001220 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001221 xfree(msg);
1222 break;
1223 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001224 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001225 DBG(debug("received packet type %d", type));
1226 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001227 }
Damien Miller33b13562000-04-04 14:38:59 +10001228 }
1229 }
1230}
1231
Damien Miller278f9072001-12-21 15:00:19 +11001232int
Damien Millerdff50992002-01-22 23:16:32 +11001233packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001234{
Damien Millerdff50992002-01-22 23:16:32 +11001235 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001236}
1237
Damien Miller5428f641999-11-25 11:54:57 +11001238/*
1239 * Buffers the given amount of input characters. This is intended to be used
1240 * together with packet_read_poll.
1241 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242
1243void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001244packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245{
Damien Miller95def091999-11-25 00:26:21 +11001246 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247}
1248
1249/* Returns a character from the packet. */
1250
Ben Lindstrom46c16222000-12-22 01:43:59 +00001251u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001252packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253{
Damien Miller95def091999-11-25 00:26:21 +11001254 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001255
Damien Miller95def091999-11-25 00:26:21 +11001256 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001257 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258}
1259
1260/* Returns an integer from the packet data. */
1261
Ben Lindstrom46c16222000-12-22 01:43:59 +00001262u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001263packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264{
Damien Miller95def091999-11-25 00:26:21 +11001265 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266}
1267
Damien Miller5428f641999-11-25 11:54:57 +11001268/*
1269 * Returns an arbitrary precision integer from the packet data. The integer
1270 * must have been initialized before this call.
1271 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272
1273void
Damien Millerd432ccf2002-01-22 23:14:44 +11001274packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001275{
Damien Miller76e1e362002-01-22 23:15:57 +11001276 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001277}
1278
Damien Miller33b13562000-04-04 14:38:59 +10001279void
Damien Millerd432ccf2002-01-22 23:14:44 +11001280packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001281{
Damien Miller76e1e362002-01-22 23:15:57 +11001282 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001283}
1284
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001285void *
Damien Millereccb9de2005-06-17 12:59:34 +10001286packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001287{
Damien Millereccb9de2005-06-17 12:59:34 +10001288 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001289
Damien Miller33b13562000-04-04 14:38:59 +10001290 if (length_ptr != NULL)
1291 *length_ptr = bytes;
1292 return buffer_ptr(&incoming_packet);
1293}
1294
Damien Miller4af51302000-04-16 11:18:38 +10001295int
1296packet_remaining(void)
1297{
1298 return buffer_len(&incoming_packet);
1299}
1300
Damien Miller5428f641999-11-25 11:54:57 +11001301/*
1302 * Returns a string from the packet data. The string is allocated using
1303 * xmalloc; it is the responsibility of the calling program to free it when
1304 * no longer needed. The length_ptr argument may be NULL, or point to an
1305 * integer into which the length of the string is stored.
1306 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001307
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001308void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001309packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310{
Damien Miller95def091999-11-25 00:26:21 +11001311 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312}
1313
Damien Miller5428f641999-11-25 11:54:57 +11001314/*
1315 * Sends a diagnostic message from the server to the client. This message
1316 * can be sent at any time (but not while constructing another message). The
1317 * message is printed immediately, but only if the client is being executed
1318 * in verbose mode. These messages are primarily intended to ease debugging
1319 * authentication problems. The length of the formatted message must not
1320 * exceed 1024 bytes. This will automatically call packet_write_wait.
1321 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001322
1323void
Damien Miller95def091999-11-25 00:26:21 +11001324packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001325{
Damien Miller95def091999-11-25 00:26:21 +11001326 char buf[1024];
1327 va_list args;
1328
Ben Lindstroma14ee472000-12-07 01:24:58 +00001329 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1330 return;
1331
Damien Miller95def091999-11-25 00:26:21 +11001332 va_start(args, fmt);
1333 vsnprintf(buf, sizeof(buf), fmt, args);
1334 va_end(args);
1335
Damien Miller7c8af4f2000-05-01 08:24:07 +10001336 if (compat20) {
1337 packet_start(SSH2_MSG_DEBUG);
1338 packet_put_char(0); /* bool: always display */
1339 packet_put_cstring(buf);
1340 packet_put_cstring("");
1341 } else {
1342 packet_start(SSH_MSG_DEBUG);
1343 packet_put_cstring(buf);
1344 }
Damien Miller95def091999-11-25 00:26:21 +11001345 packet_send();
1346 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001347}
1348
Damien Miller5428f641999-11-25 11:54:57 +11001349/*
1350 * Logs the error plus constructs and sends a disconnect packet, closes the
1351 * connection, and exits. This function never returns. The error message
1352 * should not contain a newline. The length of the formatted message must
1353 * not exceed 1024 bytes.
1354 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355
1356void
Damien Miller95def091999-11-25 00:26:21 +11001357packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001358{
Damien Miller95def091999-11-25 00:26:21 +11001359 char buf[1024];
1360 va_list args;
1361 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001362
Damien Miller95def091999-11-25 00:26:21 +11001363 if (disconnecting) /* Guard against recursive invocations. */
1364 fatal("packet_disconnect called recursively.");
1365 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366
Damien Miller5428f641999-11-25 11:54:57 +11001367 /*
1368 * Format the message. Note that the caller must make sure the
1369 * message is of limited size.
1370 */
Damien Miller95def091999-11-25 00:26:21 +11001371 va_start(args, fmt);
1372 vsnprintf(buf, sizeof(buf), fmt, args);
1373 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001374
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001375 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001376 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001377
Damien Miller95def091999-11-25 00:26:21 +11001378 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001379 if (compat20) {
1380 packet_start(SSH2_MSG_DISCONNECT);
1381 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1382 packet_put_cstring(buf);
1383 packet_put_cstring("");
1384 } else {
1385 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001386 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001387 }
Damien Miller95def091999-11-25 00:26:21 +11001388 packet_send();
1389 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390
Damien Miller95def091999-11-25 00:26:21 +11001391 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001392 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393
Damien Miller95def091999-11-25 00:26:21 +11001394 /* Close the connection. */
1395 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001396 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001397}
1398
Damien Miller5428f641999-11-25 11:54:57 +11001399/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400
1401void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001402packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403{
Damien Miller95def091999-11-25 00:26:21 +11001404 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001405
Damien Miller95def091999-11-25 00:26:21 +11001406 if (len > 0) {
1407 len = write(connection_out, buffer_ptr(&output), len);
1408 if (len <= 0) {
1409 if (errno == EAGAIN)
1410 return;
1411 else
1412 fatal("Write failed: %.100s", strerror(errno));
1413 }
1414 buffer_consume(&output, len);
1415 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416}
1417
Damien Miller5428f641999-11-25 11:54:57 +11001418/*
1419 * Calls packet_write_poll repeatedly until all pending output data has been
1420 * written.
1421 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001422
1423void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001424packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001426 fd_set *setp;
1427
Damien Miller07d86be2006-03-26 14:19:21 +11001428 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001429 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001430 packet_write_poll();
1431 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001432 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1433 sizeof(fd_mask));
1434 FD_SET(connection_out, setp);
1435 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001436 (errno == EAGAIN || errno == EINTR))
1437 ;
Damien Miller95def091999-11-25 00:26:21 +11001438 packet_write_poll();
1439 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001440 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441}
1442
1443/* Returns true if there is buffered data to write to the connection. */
1444
1445int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001446packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447{
Damien Miller95def091999-11-25 00:26:21 +11001448 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449}
1450
1451/* Returns true if there is not too much data to write to the connection. */
1452
1453int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001454packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455{
Damien Miller95def091999-11-25 00:26:21 +11001456 if (interactive_mode)
1457 return buffer_len(&output) < 16384;
1458 else
1459 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001460}
1461
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001462
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001463static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001464packet_set_tos(int interactive)
1465{
Damien Miller5924ceb2003-11-22 15:02:42 +11001466#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001467 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1468
1469 if (!packet_connection_is_on_socket() ||
1470 !packet_connection_is_ipv4())
1471 return;
1472 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1473 sizeof(tos)) < 0)
1474 error("setsockopt IP_TOS %d: %.100s:",
1475 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001476#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001477}
Ben Lindstroma7433982002-12-23 02:41:41 +00001478
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479/* Informs that the current session is interactive. Sets IP flags for that. */
1480
1481void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001482packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001484 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001486 if (called)
1487 return;
1488 called = 1;
1489
Damien Miller95def091999-11-25 00:26:21 +11001490 /* Record that we are in interactive mode. */
1491 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492
Damien Miller34132e52000-01-14 15:45:46 +11001493 /* Only set socket options if using a socket. */
1494 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001495 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001496 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001497 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498}
1499
1500/* Returns true if the current connection is interactive. */
1501
1502int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001503packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504{
Damien Miller95def091999-11-25 00:26:21 +11001505 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506}
Damien Miller6162d121999-11-21 13:23:52 +11001507
Darren Tucker1f8311c2004-05-13 16:39:33 +10001508int
Darren Tucker502d3842003-06-28 12:38:01 +10001509packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001510{
Damien Miller95def091999-11-25 00:26:21 +11001511 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001512
Damien Miller95def091999-11-25 00:26:21 +11001513 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001514 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001515 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001516 return -1;
1517 }
1518 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001519 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001520 return -1;
1521 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001522 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001523 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001524 max_packet_size = s;
1525 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001526}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001527
Damien Miller9f643902001-11-12 11:02:52 +11001528/* roundup current message to pad bytes */
1529void
1530packet_add_padding(u_char pad)
1531{
1532 extra_pad = pad;
1533}
1534
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001535/*
1536 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001537 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001538 * byte SSH_MSG_IGNORE
1539 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001540 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001541 * All implementations MUST understand (and ignore) this message at any
1542 * time (after receiving the protocol version). No implementation is
1543 * required to send them. This message can be used as an additional
1544 * protection measure against advanced traffic analysis techniques.
1545 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001546void
1547packet_send_ignore(int nbytes)
1548{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001549 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001550 int i;
1551
1552 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001553 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001554 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001555 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001556 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001557 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001558 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001559 }
1560}
Damien Millera5539d22003-04-09 20:50:06 +10001561
Darren Tucker1f8311c2004-05-13 16:39:33 +10001562#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001563int
1564packet_need_rekeying(void)
1565{
1566 if (datafellows & SSH_BUG_NOREKEY)
1567 return 0;
1568 return
1569 (p_send.packets > MAX_PACKETS) ||
1570 (p_read.packets > MAX_PACKETS) ||
1571 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1572 (max_blocks_in && (p_read.blocks > max_blocks_in));
1573}
1574
1575void
1576packet_set_rekey_limit(u_int32_t bytes)
1577{
1578 rekey_limit = bytes;
1579}
Damien Miller9786e6e2005-07-26 21:54:56 +10001580
1581void
1582packet_set_server(void)
1583{
1584 server_side = 1;
1585}
1586
1587void
1588packet_set_authenticated(void)
1589{
1590 after_authentication = 1;
1591}