blob: a20e4ed02b85c662200ac9530724d249aabb7713 [file] [log] [blame]
Damien Miller9aec9192006-08-05 10:57:45 +10001/* $OpenBSD: packet.c,v 1.138 2006/07/25 02:59:21 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>
Damien Miller9aec9192006-08-05 10:57:45 +100045#ifdef HAVE_SYS_TIME_H
46# include <sys/time.h>
47#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100048
49#include <netinet/in_systm.h>
50#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110051#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Darren Tucker39972492006-07-12 22:22:46 +100053#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100054#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100055#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100056#include <unistd.h>
Darren Tucker5d196262006-07-12 22:15:16 +100057
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058#include "xmalloc.h"
59#include "buffer.h"
60#include "packet.h"
61#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
64#include "compress.h"
65#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000066#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Damien Miller33b13562000-04-04 14:38:59 +100068#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100070#include "ssh2.h"
71
Damien Miller874d77b2000-10-14 16:23:11 +110072#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100073#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000074#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "log.h"
76#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110077#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000078#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100079
80#ifdef PACKET_DEBUG
81#define DBG(x) x
82#else
83#define DBG(x)
84#endif
85
Damien Miller5428f641999-11-25 11:54:57 +110086/*
87 * This variable contains the file descriptors used for communicating with
88 * the other side. connection_in is used for reading; connection_out for
89 * writing. These can be the same descriptor, in which case it is assumed to
90 * be a socket.
91 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092static int connection_in = -1;
93static int connection_out = -1;
94
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000096static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
98/* Encryption context for receiving data. This is only used for decryption. */
99static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +1100100
101/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102static CipherContext send_context;
103
104/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000105Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
107/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000108Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
110/* Buffer for the partial outgoing packet being constructed. */
111static Buffer outgoing_packet;
112
113/* Buffer for the incoming packet currently being processed. */
114static Buffer incoming_packet;
115
116/* Scratch buffer for packet compression/decompression. */
117static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000118static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
120/* Flag indicating whether packet compression/decompression is enabled. */
121static int packet_compression = 0;
122
Damien Miller6162d121999-11-21 13:23:52 +1100123/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000124u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100125
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126/* Flag indicating whether this module has been initialized. */
127static int initialized = 0;
128
129/* Set to true if the connection is interactive. */
130static int interactive_mode = 0;
131
Damien Miller9786e6e2005-07-26 21:54:56 +1000132/* Set to true if we are the server side. */
133static int server_side = 0;
134
135/* Set to true if we are authenticated. */
136static int after_authentication = 0;
137
Damien Miller33b13562000-04-04 14:38:59 +1000138/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000139Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000140static struct packet_state {
141 u_int32_t seqnr;
142 u_int32_t packets;
143 u_int64_t blocks;
144} p_read, p_send;
145
146static u_int64_t max_blocks_in, max_blocks_out;
147static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000148
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000149/* Session key for protocol v1 */
150static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
151static u_int ssh1_keylen;
152
Damien Miller9f643902001-11-12 11:02:52 +1100153/* roundup current message to extra_pad bytes */
154static u_char extra_pad = 0;
155
Damien Millera5539d22003-04-09 20:50:06 +1000156struct packet {
157 TAILQ_ENTRY(packet) next;
158 u_char type;
159 Buffer payload;
160};
161TAILQ_HEAD(, packet) outgoing;
162
Damien Miller5428f641999-11-25 11:54:57 +1100163/*
164 * Sets the descriptors used for communication. Disables encryption until
165 * packet_set_encryption_key is called.
166 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167void
168packet_set_connection(int fd_in, int fd_out)
169{
Damien Miller874d77b2000-10-14 16:23:11 +1100170 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000171
Damien Miller874d77b2000-10-14 16:23:11 +1100172 if (none == NULL)
173 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100174 connection_in = fd_in;
175 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000176 cipher_init(&send_context, none, (const u_char *)"",
177 0, NULL, 0, CIPHER_ENCRYPT);
178 cipher_init(&receive_context, none, (const u_char *)"",
179 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000180 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100181 if (!initialized) {
182 initialized = 1;
183 buffer_init(&input);
184 buffer_init(&output);
185 buffer_init(&outgoing_packet);
186 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000187 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100188 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189}
190
Damien Miller34132e52000-01-14 15:45:46 +1100191/* Returns 1 if remote host is connected via socket, 0 if not. */
192
193int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000194packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100195{
196 struct sockaddr_storage from, to;
197 socklen_t fromlen, tolen;
198
199 /* filedescriptors in and out are the same, so it's a socket */
200 if (connection_in == connection_out)
201 return 1;
202 fromlen = sizeof(from);
203 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100204 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100205 return 0;
206 tolen = sizeof(to);
207 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100208 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100209 return 0;
210 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
211 return 0;
212 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
213 return 0;
214 return 1;
215}
216
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000217/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000218 * Exports an IV from the CipherContext required to export the key
219 * state back from the unprivileged child to the privileged parent
220 * process.
221 */
222
223void
224packet_get_keyiv(int mode, u_char *iv, u_int len)
225{
226 CipherContext *cc;
227
228 if (mode == MODE_OUT)
229 cc = &send_context;
230 else
231 cc = &receive_context;
232
233 cipher_get_keyiv(cc, iv, len);
234}
235
236int
237packet_get_keycontext(int mode, u_char *dat)
238{
239 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000240
Ben Lindstromf6027d32002-03-22 01:42:04 +0000241 if (mode == MODE_OUT)
242 cc = &send_context;
243 else
244 cc = &receive_context;
245
246 return (cipher_get_keycontext(cc, dat));
247}
248
249void
250packet_set_keycontext(int mode, u_char *dat)
251{
252 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000253
Ben Lindstromf6027d32002-03-22 01:42:04 +0000254 if (mode == MODE_OUT)
255 cc = &send_context;
256 else
257 cc = &receive_context;
258
259 cipher_set_keycontext(cc, dat);
260}
261
262int
263packet_get_keyiv_len(int mode)
264{
265 CipherContext *cc;
266
267 if (mode == MODE_OUT)
268 cc = &send_context;
269 else
270 cc = &receive_context;
271
272 return (cipher_get_keyiv_len(cc));
273}
Damien Miller4f7becb2006-03-26 14:10:14 +1100274
Ben Lindstromf6027d32002-03-22 01:42:04 +0000275void
276packet_set_iv(int mode, u_char *dat)
277{
278 CipherContext *cc;
279
280 if (mode == MODE_OUT)
281 cc = &send_context;
282 else
283 cc = &receive_context;
284
285 cipher_set_keyiv(cc, dat);
286}
Damien Miller4f7becb2006-03-26 14:10:14 +1100287
Ben Lindstromf6027d32002-03-22 01:42:04 +0000288int
Damien Miller2b92d322003-06-11 22:05:06 +1000289packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000290{
291 return (cipher_get_number(receive_context.cipher));
292}
293
Damien Millera5539d22003-04-09 20:50:06 +1000294void
295packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000296{
Damien Millera5539d22003-04-09 20:50:06 +1000297 struct packet_state *state;
298
299 state = (mode == MODE_IN) ? &p_read : &p_send;
300 *seqnr = state->seqnr;
301 *blocks = state->blocks;
302 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000303}
304
305void
Damien Millera5539d22003-04-09 20:50:06 +1000306packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000307{
Damien Millera5539d22003-04-09 20:50:06 +1000308 struct packet_state *state;
309
310 state = (mode == MODE_IN) ? &p_read : &p_send;
311 state->seqnr = seqnr;
312 state->blocks = blocks;
313 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000314}
315
Damien Miller34132e52000-01-14 15:45:46 +1100316/* returns 1 if connection is via ipv4 */
317
318int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000319packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100320{
321 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100322 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100323
324 memset(&to, 0, sizeof(to));
325 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
326 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000327 if (to.ss_family == AF_INET)
328 return 1;
329#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100330 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000331 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
332 return 1;
333#endif
334 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100335}
336
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337/* Sets the connection into non-blocking mode. */
338
339void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000340packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341{
Damien Miller95def091999-11-25 00:26:21 +1100342 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000343 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344
Damien Miller232711f2004-06-15 10:35:30 +1000345 if (connection_out != connection_in)
346 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347}
348
349/* Returns the socket used for reading. */
350
351int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000352packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353{
Damien Miller95def091999-11-25 00:26:21 +1100354 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355}
356
357/* Returns the descriptor used for writing. */
358
359int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000360packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361{
Damien Miller95def091999-11-25 00:26:21 +1100362 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363}
364
365/* Closes the connection and clears and frees internal data structures. */
366
367void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000368packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369{
Damien Miller95def091999-11-25 00:26:21 +1100370 if (!initialized)
371 return;
372 initialized = 0;
373 if (connection_in == connection_out) {
374 shutdown(connection_out, SHUT_RDWR);
375 close(connection_out);
376 } else {
377 close(connection_in);
378 close(connection_out);
379 }
380 buffer_free(&input);
381 buffer_free(&output);
382 buffer_free(&outgoing_packet);
383 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000384 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100385 buffer_free(&compression_buffer);
386 buffer_compress_uninit();
387 }
Damien Miller963f6b22002-02-19 15:21:23 +1100388 cipher_cleanup(&send_context);
389 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390}
391
392/* Sets remote side protocol flags. */
393
394void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000395packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396{
Damien Miller95def091999-11-25 00:26:21 +1100397 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398}
399
400/* Returns the remote protocol flags set earlier by the above function. */
401
Ben Lindstrom46c16222000-12-22 01:43:59 +0000402u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000403packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404{
Damien Miller95def091999-11-25 00:26:21 +1100405 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406}
407
Damien Miller5428f641999-11-25 11:54:57 +1100408/*
409 * Starts packet compression from the next packet on in both directions.
410 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
411 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412
Ben Lindstrombba81212001-06-25 05:01:22 +0000413static void
414packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000415{
416 if (compression_buffer_ready == 1)
417 return;
418 compression_buffer_ready = 1;
419 buffer_init(&compression_buffer);
420}
421
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422void
423packet_start_compression(int level)
424{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000425 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100426 fatal("Compression already enabled.");
427 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000428 packet_init_compression();
429 buffer_compress_init_send(level);
430 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431}
432
Damien Miller5428f641999-11-25 11:54:57 +1100433/*
Damien Miller5428f641999-11-25 11:54:57 +1100434 * Causes any further packets to be encrypted using the given key. The same
435 * key is used for both sending and reception. However, both directions are
436 * encrypted independently of each other.
437 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000438
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000440packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100441 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442{
Damien Miller874d77b2000-10-14 16:23:11 +1100443 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000444
Damien Miller874d77b2000-10-14 16:23:11 +1100445 if (cipher == NULL)
446 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000447 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100448 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000449 if (keylen > SSH_SESSION_KEY_LENGTH)
450 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
451 memcpy(ssh1_key, key, keylen);
452 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100453 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
454 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455}
456
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000457u_int
458packet_get_encryption_key(u_char *key)
459{
460 if (key == NULL)
461 return (ssh1_keylen);
462 memcpy(key, ssh1_key, ssh1_keylen);
463 return (ssh1_keylen);
464}
465
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000466/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000468packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000470 u_char buf[9];
471 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472
Ben Lindstrom204e4882001-03-05 06:47:00 +0000473 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000474 len = compat20 ? 6 : 9;
475 memset(buf, 0, len - 1);
476 buf[len - 1] = type;
477 buffer_clear(&outgoing_packet);
478 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000479}
480
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000481/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482void
483packet_put_char(int value)
484{
Damien Miller95def091999-11-25 00:26:21 +1100485 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000486
Damien Miller95def091999-11-25 00:26:21 +1100487 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488}
Damien Miller4f7becb2006-03-26 14:10:14 +1100489
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000491packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492{
Damien Miller95def091999-11-25 00:26:21 +1100493 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494}
Damien Miller4f7becb2006-03-26 14:10:14 +1100495
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100497packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498{
Damien Miller95def091999-11-25 00:26:21 +1100499 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500}
Damien Miller4f7becb2006-03-26 14:10:14 +1100501
Damien Miller33b13562000-04-04 14:38:59 +1000502void
503packet_put_cstring(const char *str)
504{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000505 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000506}
Damien Miller4f7becb2006-03-26 14:10:14 +1100507
Damien Miller33b13562000-04-04 14:38:59 +1000508void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100509packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000510{
511 buffer_append(&outgoing_packet, buf, len);
512}
Damien Miller4f7becb2006-03-26 14:10:14 +1100513
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514void
Damien Miller95def091999-11-25 00:26:21 +1100515packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516{
Damien Miller95def091999-11-25 00:26:21 +1100517 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518}
Damien Miller4f7becb2006-03-26 14:10:14 +1100519
Damien Miller33b13562000-04-04 14:38:59 +1000520void
521packet_put_bignum2(BIGNUM * value)
522{
523 buffer_put_bignum2(&outgoing_packet, value);
524}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525
Damien Miller5428f641999-11-25 11:54:57 +1100526/*
527 * Finalizes and sends the packet. If the encryption key has been set,
528 * encrypts the packet before sending.
529 */
Damien Miller95def091999-11-25 00:26:21 +1100530
Ben Lindstrombba81212001-06-25 05:01:22 +0000531static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000532packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000534 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100535 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000536 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000537 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538
Damien Miller5428f641999-11-25 11:54:57 +1100539 /*
540 * If using packet compression, compress the payload of the outgoing
541 * packet.
542 */
Damien Miller95def091999-11-25 00:26:21 +1100543 if (packet_compression) {
544 buffer_clear(&compression_buffer);
545 /* Skip padding. */
546 buffer_consume(&outgoing_packet, 8);
547 /* padding */
548 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
549 buffer_compress(&outgoing_packet, &compression_buffer);
550 buffer_clear(&outgoing_packet);
551 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100552 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100553 }
554 /* Compute packet length without padding (add checksum, remove padding). */
555 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556
Damien Millere247cc42000-05-07 12:03:14 +1000557 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100558 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100559 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100560 cp = buffer_ptr(&outgoing_packet);
561 for (i = 0; i < padding; i++) {
562 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000563 rnd = arc4random();
564 cp[7 - i] = rnd & 0xff;
565 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100566 }
567 }
568 buffer_consume(&outgoing_packet, 8 - padding);
569
570 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100571 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000572 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100573 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100574 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
576#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100577 fprintf(stderr, "packet_send plain: ");
578 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579#endif
580
Damien Miller95def091999-11-25 00:26:21 +1100581 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100582 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100583 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100584 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100585 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100586 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100587
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100589 fprintf(stderr, "encrypted: ");
590 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591#endif
592
Damien Miller95def091999-11-25 00:26:21 +1100593 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594
Damien Miller5428f641999-11-25 11:54:57 +1100595 /*
Damien Miller788f2122005-11-05 15:14:59 +1100596 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100597 * actually sent until packet_write_wait or packet_write_poll is
598 * called.
599 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600}
601
Ben Lindstromf6027d32002-03-22 01:42:04 +0000602void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000603set_newkeys(int mode)
604{
605 Enc *enc;
606 Mac *mac;
607 Comp *comp;
608 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000609 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000610 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000611
Ben Lindstrom064496f2002-12-23 02:04:22 +0000612 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000613
Damien Miller963f6b22002-02-19 15:21:23 +1100614 if (mode == MODE_OUT) {
615 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000616 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000617 p_send.packets = p_send.blocks = 0;
618 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100619 } else {
620 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000621 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000622 p_read.packets = p_read.blocks = 0;
623 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100624 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000625 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000626 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100627 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000628 enc = &newkeys[mode]->enc;
629 mac = &newkeys[mode]->mac;
630 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000631 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000632 xfree(enc->name);
633 xfree(enc->iv);
634 xfree(enc->key);
635 xfree(mac->name);
636 xfree(mac->key);
637 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000638 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000639 }
640 newkeys[mode] = kex_get_newkeys(mode);
641 if (newkeys[mode] == NULL)
642 fatal("newkeys: no keys for mode %d", mode);
643 enc = &newkeys[mode]->enc;
644 mac = &newkeys[mode]->mac;
645 comp = &newkeys[mode]->comp;
646 if (mac->md != NULL)
647 mac->enabled = 1;
648 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100649 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000650 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000651 /* Deleting the keys does not gain extra security */
652 /* memset(enc->iv, 0, enc->block_size);
653 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000654 if ((comp->type == COMP_ZLIB ||
655 (comp->type == COMP_DELAYED && after_authentication)) &&
656 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000657 packet_init_compression();
658 if (mode == MODE_OUT)
659 buffer_compress_init_send(6);
660 else
661 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000662 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000663 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000664 /*
665 * The 2^(blocksize*2) limit is too expensive for 3DES,
666 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
667 */
668 if (enc->block_size >= 16)
669 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
670 else
671 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000672 if (rekey_limit)
673 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000674}
675
Damien Miller5428f641999-11-25 11:54:57 +1100676/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000677 * Delayed compression for SSH2 is enabled after authentication:
678 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
679 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
680 */
681static void
682packet_enable_delayed_compress(void)
683{
684 Comp *comp = NULL;
685 int mode;
686
687 /*
688 * Remember that we are past the authentication step, so rekeying
689 * with COMP_DELAYED will turn on compression immediately.
690 */
691 after_authentication = 1;
692 for (mode = 0; mode < MODE_MAX; mode++) {
693 comp = &newkeys[mode]->comp;
694 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000695 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000696 if (mode == MODE_OUT)
697 buffer_compress_init_send(6);
698 else
699 buffer_compress_init_recv();
700 comp->enabled = 1;
701 }
702 }
703}
704
705/*
Damien Miller33b13562000-04-04 14:38:59 +1000706 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
707 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000708static void
Damien Millera5539d22003-04-09 20:50:06 +1000709packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000710{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000711 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100712 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000713 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100714 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000715 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000716 Enc *enc = NULL;
717 Mac *mac = NULL;
718 Comp *comp = NULL;
719 int block_size;
720
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000721 if (newkeys[MODE_OUT] != NULL) {
722 enc = &newkeys[MODE_OUT]->enc;
723 mac = &newkeys[MODE_OUT]->mac;
724 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000725 }
Damien Miller963f6b22002-02-19 15:21:23 +1100726 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000727
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000728 cp = buffer_ptr(&outgoing_packet);
729 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000730
731#ifdef PACKET_DEBUG
732 fprintf(stderr, "plain: ");
733 buffer_dump(&outgoing_packet);
734#endif
735
736 if (comp && comp->enabled) {
737 len = buffer_len(&outgoing_packet);
738 /* skip header, compress only payload */
739 buffer_consume(&outgoing_packet, 5);
740 buffer_clear(&compression_buffer);
741 buffer_compress(&outgoing_packet, &compression_buffer);
742 buffer_clear(&outgoing_packet);
743 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
744 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
745 buffer_len(&compression_buffer));
746 DBG(debug("compression: raw %d compressed %d", len,
747 buffer_len(&outgoing_packet)));
748 }
749
750 /* sizeof (packet_len + pad_len + payload) */
751 len = buffer_len(&outgoing_packet);
752
753 /*
754 * calc size of padding, alloc space, get random data,
755 * minimum padding is 4 bytes
756 */
757 padlen = block_size - (len % block_size);
758 if (padlen < 4)
759 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100760 if (extra_pad) {
761 /* will wrap if extra_pad+padlen > 255 */
762 extra_pad = roundup(extra_pad, block_size);
763 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000764 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100765 pad, len, padlen, extra_pad);
766 padlen += pad;
767 extra_pad = 0;
768 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100769 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100770 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000771 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000772 for (i = 0; i < padlen; i++) {
773 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000774 rnd = arc4random();
775 cp[i] = rnd & 0xff;
776 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000777 }
Damien Millere247cc42000-05-07 12:03:14 +1000778 } else {
779 /* clear padding */
780 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000781 }
782 /* packet_length includes payload, padding and padding length field */
783 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000784 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100785 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000786 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000787 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
788
789 /* compute MAC over seqnr and packet(length fields, payload, padding) */
790 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000791 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100792 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000793 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000794 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000795 }
796 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100797 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100798 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000799 buffer_len(&outgoing_packet));
800 /* append unencrypted MAC */
801 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100802 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000803#ifdef PACKET_DEBUG
804 fprintf(stderr, "encrypted: ");
805 buffer_dump(&output);
806#endif
Damien Miller4af51302000-04-16 11:18:38 +1000807 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000808 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000809 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000810 if (++p_send.packets == 0)
811 if (!(datafellows & SSH_BUG_NOREKEY))
812 fatal("XXX too many packets with same key");
813 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000814 buffer_clear(&outgoing_packet);
815
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000816 if (type == SSH2_MSG_NEWKEYS)
817 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000818 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
819 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000820}
821
Damien Millera5539d22003-04-09 20:50:06 +1000822static void
823packet_send2(void)
824{
825 static int rekeying = 0;
826 struct packet *p;
827 u_char type, *cp;
828
829 cp = buffer_ptr(&outgoing_packet);
830 type = cp[5];
831
832 /* during rekeying we can only send key exchange messages */
833 if (rekeying) {
834 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
835 (type <= SSH2_MSG_TRANSPORT_MAX))) {
836 debug("enqueue packet: %u", type);
837 p = xmalloc(sizeof(*p));
838 p->type = type;
839 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
840 buffer_init(&outgoing_packet);
841 TAILQ_INSERT_TAIL(&outgoing, p, next);
842 return;
843 }
844 }
845
846 /* rekeying starts with sending KEXINIT */
847 if (type == SSH2_MSG_KEXINIT)
848 rekeying = 1;
849
850 packet_send2_wrapped();
851
852 /* after a NEWKEYS message we can send the complete queue */
853 if (type == SSH2_MSG_NEWKEYS) {
854 rekeying = 0;
855 while ((p = TAILQ_FIRST(&outgoing))) {
856 type = p->type;
857 debug("dequeue packet: %u", type);
858 buffer_free(&outgoing_packet);
859 memcpy(&outgoing_packet, &p->payload,
860 sizeof(Buffer));
861 TAILQ_REMOVE(&outgoing, p, next);
862 xfree(p);
863 packet_send2_wrapped();
864 }
865 }
866}
867
Damien Miller33b13562000-04-04 14:38:59 +1000868void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000869packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000870{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000871 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000872 packet_send2();
873 else
874 packet_send1();
875 DBG(debug("packet_send done"));
876}
877
Damien Miller33b13562000-04-04 14:38:59 +1000878/*
Damien Miller5428f641999-11-25 11:54:57 +1100879 * Waits until a packet has been received, and returns its type. Note that
880 * no other data is processed until this returns, so this function should not
881 * be used during the interactive session.
882 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883
884int
Damien Millerdff50992002-01-22 23:16:32 +1100885packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886{
Damien Miller95def091999-11-25 00:26:21 +1100887 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000888 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100889 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000890 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891
Damien Miller07d86be2006-03-26 14:19:21 +1100892 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000893 sizeof(fd_mask));
894
Damien Miller95def091999-11-25 00:26:21 +1100895 /* Since we are blocking, ensure that all written packets have been sent. */
896 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897
Damien Miller95def091999-11-25 00:26:21 +1100898 /* Stay in the loop until we have received a complete packet. */
899 for (;;) {
900 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100901 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000902 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000903 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100904 || type == SSH_SMSG_FAILURE
905 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000906 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100907 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100908 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000909 if (type != SSH_MSG_NONE) {
910 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100911 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000912 }
Damien Miller5428f641999-11-25 11:54:57 +1100913 /*
914 * Otherwise, wait for some data to arrive, add it to the
915 * buffer, and try again.
916 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000917 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
918 sizeof(fd_mask));
919 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100920
Damien Miller95def091999-11-25 00:26:21 +1100921 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000922 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000923 (errno == EAGAIN || errno == EINTR))
924 ;
Damien Miller5428f641999-11-25 11:54:57 +1100925
Damien Miller95def091999-11-25 00:26:21 +1100926 /* Read data from the socket. */
927 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100928 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000929 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000930 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100931 }
Damien Miller95def091999-11-25 00:26:21 +1100932 if (len < 0)
933 fatal("Read from socket failed: %.100s", strerror(errno));
934 /* Append it to the buffer. */
935 packet_process_incoming(buf, len);
936 }
937 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938}
939
Damien Miller278f9072001-12-21 15:00:19 +1100940int
Damien Millerdff50992002-01-22 23:16:32 +1100941packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100942{
Damien Millerdff50992002-01-22 23:16:32 +1100943 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100944}
945
Damien Miller5428f641999-11-25 11:54:57 +1100946/*
947 * Waits until a packet has been received, verifies that its type matches
948 * that given, and gives a fatal error and exits if there is a mismatch.
949 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950
951void
Damien Millerdff50992002-01-22 23:16:32 +1100952packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953{
Damien Miller95def091999-11-25 00:26:21 +1100954 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955
Damien Millerdff50992002-01-22 23:16:32 +1100956 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100957 if (type != expected_type)
958 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000959 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960}
961
962/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100963 * packet_process_incoming. If so, reads the packet; otherwise returns
964 * SSH_MSG_NONE. This does not wait for data from the connection.
965 *
966 * SSH_MSG_DISCONNECT is handled specially here. Also,
967 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
968 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100969 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970
Ben Lindstrombba81212001-06-25 05:01:22 +0000971static int
Damien Millerdff50992002-01-22 23:16:32 +1100972packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000974 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000975 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000976 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000977
Damien Miller95def091999-11-25 00:26:21 +1100978 /* Check if input size is less than minimum packet size. */
979 if (buffer_len(&input) < 4 + 8)
980 return SSH_MSG_NONE;
981 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000982 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100983 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100984 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000985 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100986 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987
Damien Miller95def091999-11-25 00:26:21 +1100988 /* Check if the packet has been entirely received. */
989 if (buffer_len(&input) < 4 + padded_len)
990 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991
Damien Miller95def091999-11-25 00:26:21 +1100992 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993
Damien Miller95def091999-11-25 00:26:21 +1100994 /* Consume packet length. */
995 buffer_consume(&input, 4);
996
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000997 /*
998 * Cryptographic attack detector for ssh
999 * (C)1998 CORE-SDI, Buenos Aires Argentina
1000 * Ariel Futoransky(futo@core-sdi.com)
1001 */
Damien Miller963f6b22002-02-19 15:21:23 +11001002 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +11001003 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001004 packet_disconnect("crc32 compensation attack: network attack detected");
1005
1006 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001007 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001008 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001009 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001010
Damien Miller95def091999-11-25 00:26:21 +11001011 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
1013#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001014 fprintf(stderr, "read_poll plain: ");
1015 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017
Damien Miller95def091999-11-25 00:26:21 +11001018 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001019 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001020 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001021
Damien Miller95def091999-11-25 00:26:21 +11001022 /* Skip padding. */
1023 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001024
Damien Miller95def091999-11-25 00:26:21 +11001025 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001026 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001027 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001028 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001029
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001030 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001031 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001032 if (checksum != stored_checksum)
1033 packet_disconnect("Corrupted check bytes on input.");
1034 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035
Damien Miller95def091999-11-25 00:26:21 +11001036 if (packet_compression) {
1037 buffer_clear(&compression_buffer);
1038 buffer_uncompress(&incoming_packet, &compression_buffer);
1039 buffer_clear(&incoming_packet);
1040 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001041 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001042 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001043 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001044 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1045 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001046 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047}
Damien Miller95def091999-11-25 00:26:21 +11001048
Ben Lindstrombba81212001-06-25 05:01:22 +00001049static int
Damien Millerdff50992002-01-22 23:16:32 +11001050packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001051{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001052 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001053 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001054 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001055 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001056 Enc *enc = NULL;
1057 Mac *mac = NULL;
1058 Comp *comp = NULL;
1059
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001060 if (newkeys[MODE_IN] != NULL) {
1061 enc = &newkeys[MODE_IN]->enc;
1062 mac = &newkeys[MODE_IN]->mac;
1063 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001064 }
1065 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001066 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001067
1068 if (packet_length == 0) {
1069 /*
1070 * check if input size is less than the cipher block size,
1071 * decrypt first block and extract length of incoming packet
1072 */
1073 if (buffer_len(&input) < block_size)
1074 return SSH_MSG_NONE;
1075 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001076 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001077 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001078 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001079 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001080 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001081 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001082#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001083 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001084#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001085 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001086 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001087 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001088 buffer_consume(&input, block_size);
1089 }
1090 /* we have a partial packet of block_size bytes */
1091 need = 4 + packet_length - block_size;
1092 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1093 need, maclen));
1094 if (need % block_size != 0)
1095 fatal("padding error: need %d block %d mod %d",
1096 need, block_size, need % block_size);
1097 /*
1098 * check if the entire packet has been received and
1099 * decrypt into incoming_packet
1100 */
1101 if (buffer_len(&input) < need + maclen)
1102 return SSH_MSG_NONE;
1103#ifdef PACKET_DEBUG
1104 fprintf(stderr, "read_poll enc/full: ");
1105 buffer_dump(&input);
1106#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001107 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001108 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001109 buffer_consume(&input, need);
1110 /*
1111 * compute MAC over seqnr and packet,
1112 * increment sequence number for incoming packet
1113 */
Damien Miller4af51302000-04-16 11:18:38 +10001114 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001115 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001116 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001117 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001118 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001119 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001120 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001121 buffer_consume(&input, mac->mac_len);
1122 }
Damien Miller278f9072001-12-21 15:00:19 +11001123 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001124 *seqnr_p = p_read.seqnr;
1125 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001126 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001127 if (++p_read.packets == 0)
1128 if (!(datafellows & SSH_BUG_NOREKEY))
1129 fatal("XXX too many packets with same key");
1130 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001131
1132 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001133 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001134 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001135 DBG(debug("input: padlen %d", padlen));
1136 if (padlen < 4)
1137 packet_disconnect("Corrupted padlen %d on input.", padlen);
1138
1139 /* skip packet size + padlen, discard padding */
1140 buffer_consume(&incoming_packet, 4 + 1);
1141 buffer_consume_end(&incoming_packet, padlen);
1142
1143 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1144 if (comp && comp->enabled) {
1145 buffer_clear(&compression_buffer);
1146 buffer_uncompress(&incoming_packet, &compression_buffer);
1147 buffer_clear(&incoming_packet);
1148 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1149 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001150 DBG(debug("input: len after de-compress %d",
1151 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001152 }
1153 /*
1154 * get packet type, implies consume.
1155 * return length of payload (without type field)
1156 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001157 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001158 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1159 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001160 if (type == SSH2_MSG_NEWKEYS)
1161 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001162 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1163 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001164#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001165 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001166 buffer_dump(&incoming_packet);
1167#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001168 /* reset for next packet */
1169 packet_length = 0;
1170 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001171}
1172
1173int
Damien Millerdff50992002-01-22 23:16:32 +11001174packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001175{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001176 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001177 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001178 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001179
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001180 for (;;) {
1181 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001182 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001183 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001184 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001185 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001186 case SSH2_MSG_IGNORE:
1187 break;
1188 case SSH2_MSG_DEBUG:
1189 packet_get_char();
1190 msg = packet_get_string(NULL);
1191 debug("Remote: %.900s", msg);
1192 xfree(msg);
1193 msg = packet_get_string(NULL);
1194 xfree(msg);
1195 break;
1196 case SSH2_MSG_DISCONNECT:
1197 reason = packet_get_int();
1198 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001199 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001200 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001201 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001202 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001203 break;
Damien Miller659811f2002-01-22 23:23:11 +11001204 case SSH2_MSG_UNIMPLEMENTED:
1205 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001206 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1207 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001208 break;
Damien Miller33b13562000-04-04 14:38:59 +10001209 default:
1210 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001211 }
Damien Miller33b13562000-04-04 14:38:59 +10001212 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001213 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001214 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001215 case SSH_MSG_IGNORE:
1216 break;
1217 case SSH_MSG_DEBUG:
1218 msg = packet_get_string(NULL);
1219 debug("Remote: %.900s", msg);
1220 xfree(msg);
1221 break;
1222 case SSH_MSG_DISCONNECT:
1223 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001224 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001225 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001226 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001227 xfree(msg);
1228 break;
1229 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001230 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001231 DBG(debug("received packet type %d", type));
1232 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001233 }
Damien Miller33b13562000-04-04 14:38:59 +10001234 }
1235 }
1236}
1237
Damien Miller278f9072001-12-21 15:00:19 +11001238int
Damien Millerdff50992002-01-22 23:16:32 +11001239packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001240{
Damien Millerdff50992002-01-22 23:16:32 +11001241 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001242}
1243
Damien Miller5428f641999-11-25 11:54:57 +11001244/*
1245 * Buffers the given amount of input characters. This is intended to be used
1246 * together with packet_read_poll.
1247 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248
1249void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001250packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251{
Damien Miller95def091999-11-25 00:26:21 +11001252 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253}
1254
1255/* Returns a character from the packet. */
1256
Ben Lindstrom46c16222000-12-22 01:43:59 +00001257u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001258packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259{
Damien Miller95def091999-11-25 00:26:21 +11001260 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001261
Damien Miller95def091999-11-25 00:26:21 +11001262 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001263 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264}
1265
1266/* Returns an integer from the packet data. */
1267
Ben Lindstrom46c16222000-12-22 01:43:59 +00001268u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001269packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270{
Damien Miller95def091999-11-25 00:26:21 +11001271 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272}
1273
Damien Miller5428f641999-11-25 11:54:57 +11001274/*
1275 * Returns an arbitrary precision integer from the packet data. The integer
1276 * must have been initialized before this call.
1277 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001278
1279void
Damien Millerd432ccf2002-01-22 23:14:44 +11001280packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281{
Damien Miller76e1e362002-01-22 23:15:57 +11001282 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001283}
1284
Damien Miller33b13562000-04-04 14:38:59 +10001285void
Damien Millerd432ccf2002-01-22 23:14:44 +11001286packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001287{
Damien Miller76e1e362002-01-22 23:15:57 +11001288 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001289}
1290
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001291void *
Damien Millereccb9de2005-06-17 12:59:34 +10001292packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001293{
Damien Millereccb9de2005-06-17 12:59:34 +10001294 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001295
Damien Miller33b13562000-04-04 14:38:59 +10001296 if (length_ptr != NULL)
1297 *length_ptr = bytes;
1298 return buffer_ptr(&incoming_packet);
1299}
1300
Damien Miller4af51302000-04-16 11:18:38 +10001301int
1302packet_remaining(void)
1303{
1304 return buffer_len(&incoming_packet);
1305}
1306
Damien Miller5428f641999-11-25 11:54:57 +11001307/*
1308 * Returns a string from the packet data. The string is allocated using
1309 * xmalloc; it is the responsibility of the calling program to free it when
1310 * no longer needed. The length_ptr argument may be NULL, or point to an
1311 * integer into which the length of the string is stored.
1312 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001313
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001314void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001315packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316{
Damien Miller95def091999-11-25 00:26:21 +11001317 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318}
1319
Damien Miller5428f641999-11-25 11:54:57 +11001320/*
1321 * Sends a diagnostic message from the server to the client. This message
1322 * can be sent at any time (but not while constructing another message). The
1323 * message is printed immediately, but only if the client is being executed
1324 * in verbose mode. These messages are primarily intended to ease debugging
1325 * authentication problems. The length of the formatted message must not
1326 * exceed 1024 bytes. This will automatically call packet_write_wait.
1327 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001328
1329void
Damien Miller95def091999-11-25 00:26:21 +11001330packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331{
Damien Miller95def091999-11-25 00:26:21 +11001332 char buf[1024];
1333 va_list args;
1334
Ben Lindstroma14ee472000-12-07 01:24:58 +00001335 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1336 return;
1337
Damien Miller95def091999-11-25 00:26:21 +11001338 va_start(args, fmt);
1339 vsnprintf(buf, sizeof(buf), fmt, args);
1340 va_end(args);
1341
Damien Miller7c8af4f2000-05-01 08:24:07 +10001342 if (compat20) {
1343 packet_start(SSH2_MSG_DEBUG);
1344 packet_put_char(0); /* bool: always display */
1345 packet_put_cstring(buf);
1346 packet_put_cstring("");
1347 } else {
1348 packet_start(SSH_MSG_DEBUG);
1349 packet_put_cstring(buf);
1350 }
Damien Miller95def091999-11-25 00:26:21 +11001351 packet_send();
1352 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001353}
1354
Damien Miller5428f641999-11-25 11:54:57 +11001355/*
1356 * Logs the error plus constructs and sends a disconnect packet, closes the
1357 * connection, and exits. This function never returns. The error message
1358 * should not contain a newline. The length of the formatted message must
1359 * not exceed 1024 bytes.
1360 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361
1362void
Damien Miller95def091999-11-25 00:26:21 +11001363packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364{
Damien Miller95def091999-11-25 00:26:21 +11001365 char buf[1024];
1366 va_list args;
1367 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001368
Damien Miller95def091999-11-25 00:26:21 +11001369 if (disconnecting) /* Guard against recursive invocations. */
1370 fatal("packet_disconnect called recursively.");
1371 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001372
Damien Miller5428f641999-11-25 11:54:57 +11001373 /*
1374 * Format the message. Note that the caller must make sure the
1375 * message is of limited size.
1376 */
Damien Miller95def091999-11-25 00:26:21 +11001377 va_start(args, fmt);
1378 vsnprintf(buf, sizeof(buf), fmt, args);
1379 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001380
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001381 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001382 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001383
Damien Miller95def091999-11-25 00:26:21 +11001384 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001385 if (compat20) {
1386 packet_start(SSH2_MSG_DISCONNECT);
1387 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1388 packet_put_cstring(buf);
1389 packet_put_cstring("");
1390 } else {
1391 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001392 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001393 }
Damien Miller95def091999-11-25 00:26:21 +11001394 packet_send();
1395 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396
Damien Miller95def091999-11-25 00:26:21 +11001397 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001398 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001399
Damien Miller95def091999-11-25 00:26:21 +11001400 /* Close the connection. */
1401 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001402 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403}
1404
Damien Miller5428f641999-11-25 11:54:57 +11001405/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406
1407void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001408packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001409{
Damien Miller95def091999-11-25 00:26:21 +11001410 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001411
Damien Miller95def091999-11-25 00:26:21 +11001412 if (len > 0) {
1413 len = write(connection_out, buffer_ptr(&output), len);
1414 if (len <= 0) {
1415 if (errno == EAGAIN)
1416 return;
1417 else
1418 fatal("Write failed: %.100s", strerror(errno));
1419 }
1420 buffer_consume(&output, len);
1421 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001422}
1423
Damien Miller5428f641999-11-25 11:54:57 +11001424/*
1425 * Calls packet_write_poll repeatedly until all pending output data has been
1426 * written.
1427 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428
1429void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001430packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001432 fd_set *setp;
1433
Damien Miller07d86be2006-03-26 14:19:21 +11001434 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001435 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001436 packet_write_poll();
1437 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001438 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1439 sizeof(fd_mask));
1440 FD_SET(connection_out, setp);
1441 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001442 (errno == EAGAIN || errno == EINTR))
1443 ;
Damien Miller95def091999-11-25 00:26:21 +11001444 packet_write_poll();
1445 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001446 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447}
1448
1449/* Returns true if there is buffered data to write to the connection. */
1450
1451int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001452packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453{
Damien Miller95def091999-11-25 00:26:21 +11001454 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455}
1456
1457/* Returns true if there is not too much data to write to the connection. */
1458
1459int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001460packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461{
Damien Miller95def091999-11-25 00:26:21 +11001462 if (interactive_mode)
1463 return buffer_len(&output) < 16384;
1464 else
1465 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466}
1467
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001468
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001469static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001470packet_set_tos(int interactive)
1471{
Damien Miller5924ceb2003-11-22 15:02:42 +11001472#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001473 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1474
1475 if (!packet_connection_is_on_socket() ||
1476 !packet_connection_is_ipv4())
1477 return;
1478 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1479 sizeof(tos)) < 0)
1480 error("setsockopt IP_TOS %d: %.100s:",
1481 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001482#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001483}
Ben Lindstroma7433982002-12-23 02:41:41 +00001484
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485/* Informs that the current session is interactive. Sets IP flags for that. */
1486
1487void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001488packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001489{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001490 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001492 if (called)
1493 return;
1494 called = 1;
1495
Damien Miller95def091999-11-25 00:26:21 +11001496 /* Record that we are in interactive mode. */
1497 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498
Damien Miller34132e52000-01-14 15:45:46 +11001499 /* Only set socket options if using a socket. */
1500 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001501 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001502 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001503 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504}
1505
1506/* Returns true if the current connection is interactive. */
1507
1508int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001509packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001510{
Damien Miller95def091999-11-25 00:26:21 +11001511 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001512}
Damien Miller6162d121999-11-21 13:23:52 +11001513
Darren Tucker1f8311c2004-05-13 16:39:33 +10001514int
Darren Tucker502d3842003-06-28 12:38:01 +10001515packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001516{
Damien Miller95def091999-11-25 00:26:21 +11001517 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001518
Damien Miller95def091999-11-25 00:26:21 +11001519 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001520 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001521 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001522 return -1;
1523 }
1524 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001525 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001526 return -1;
1527 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001528 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001529 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001530 max_packet_size = s;
1531 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001532}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001533
Damien Miller9f643902001-11-12 11:02:52 +11001534/* roundup current message to pad bytes */
1535void
1536packet_add_padding(u_char pad)
1537{
1538 extra_pad = pad;
1539}
1540
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001541/*
1542 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001543 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001544 * byte SSH_MSG_IGNORE
1545 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001546 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001547 * All implementations MUST understand (and ignore) this message at any
1548 * time (after receiving the protocol version). No implementation is
1549 * required to send them. This message can be used as an additional
1550 * protection measure against advanced traffic analysis techniques.
1551 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001552void
1553packet_send_ignore(int nbytes)
1554{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001555 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001556 int i;
1557
1558 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001559 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001560 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001561 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001562 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001563 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001564 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001565 }
1566}
Damien Millera5539d22003-04-09 20:50:06 +10001567
Darren Tucker1f8311c2004-05-13 16:39:33 +10001568#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001569int
1570packet_need_rekeying(void)
1571{
1572 if (datafellows & SSH_BUG_NOREKEY)
1573 return 0;
1574 return
1575 (p_send.packets > MAX_PACKETS) ||
1576 (p_read.packets > MAX_PACKETS) ||
1577 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1578 (max_blocks_in && (p_read.blocks > max_blocks_in));
1579}
1580
1581void
1582packet_set_rekey_limit(u_int32_t bytes)
1583{
1584 rekey_limit = bytes;
1585}
Damien Miller9786e6e2005-07-26 21:54:56 +10001586
1587void
1588packet_set_server(void)
1589{
1590 server_side = 1;
1591}
1592
1593void
1594packet_set_authenticated(void)
1595{
1596 after_authentication = 1;
1597}