blob: ce652cfd80dc483c0128e4fa63f3acc9e345aa1f [file] [log] [blame]
Damien Millere6b3b612006-07-24 14:01:23 +10001/* $OpenBSD: packet.c,v 1.136 2006/07/17 01:31:09 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 Tucker39972492006-07-12 22:22:46 +100050#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100051#include <stdarg.h>
Damien Millere6b3b612006-07-24 14:01:23 +100052#include <unistd.h>
Darren Tucker5d196262006-07-12 22:15:16 +100053
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "xmalloc.h"
55#include "buffer.h"
56#include "packet.h"
57#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
60#include "compress.h"
61#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000062#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Damien Miller33b13562000-04-04 14:38:59 +100064#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000065#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100066#include "ssh2.h"
67
Damien Miller874d77b2000-10-14 16:23:11 +110068#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100069#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000070#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071#include "log.h"
72#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110073#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000074#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100075
76#ifdef PACKET_DEBUG
77#define DBG(x) x
78#else
79#define DBG(x)
80#endif
81
Damien Miller5428f641999-11-25 11:54:57 +110082/*
83 * This variable contains the file descriptors used for communicating with
84 * the other side. connection_in is used for reading; connection_out for
85 * writing. These can be the same descriptor, in which case it is assumed to
86 * be a socket.
87 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088static int connection_in = -1;
89static int connection_out = -1;
90
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000092static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* Encryption context for receiving data. This is only used for decryption. */
95static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110096
97/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098static CipherContext send_context;
99
100/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000101Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
103/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000104Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
106/* Buffer for the partial outgoing packet being constructed. */
107static Buffer outgoing_packet;
108
109/* Buffer for the incoming packet currently being processed. */
110static Buffer incoming_packet;
111
112/* Scratch buffer for packet compression/decompression. */
113static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000114static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
116/* Flag indicating whether packet compression/decompression is enabled. */
117static int packet_compression = 0;
118
Damien Miller6162d121999-11-21 13:23:52 +1100119/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000120u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100121
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122/* Flag indicating whether this module has been initialized. */
123static int initialized = 0;
124
125/* Set to true if the connection is interactive. */
126static int interactive_mode = 0;
127
Damien Miller9786e6e2005-07-26 21:54:56 +1000128/* Set to true if we are the server side. */
129static int server_side = 0;
130
131/* Set to true if we are authenticated. */
132static int after_authentication = 0;
133
Damien Miller33b13562000-04-04 14:38:59 +1000134/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000135Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000136static struct packet_state {
137 u_int32_t seqnr;
138 u_int32_t packets;
139 u_int64_t blocks;
140} p_read, p_send;
141
142static u_int64_t max_blocks_in, max_blocks_out;
143static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000144
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000145/* Session key for protocol v1 */
146static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
147static u_int ssh1_keylen;
148
Damien Miller9f643902001-11-12 11:02:52 +1100149/* roundup current message to extra_pad bytes */
150static u_char extra_pad = 0;
151
Damien Millera5539d22003-04-09 20:50:06 +1000152struct packet {
153 TAILQ_ENTRY(packet) next;
154 u_char type;
155 Buffer payload;
156};
157TAILQ_HEAD(, packet) outgoing;
158
Damien Miller5428f641999-11-25 11:54:57 +1100159/*
160 * Sets the descriptors used for communication. Disables encryption until
161 * packet_set_encryption_key is called.
162 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163void
164packet_set_connection(int fd_in, int fd_out)
165{
Damien Miller874d77b2000-10-14 16:23:11 +1100166 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000167
Damien Miller874d77b2000-10-14 16:23:11 +1100168 if (none == NULL)
169 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100170 connection_in = fd_in;
171 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000172 cipher_init(&send_context, none, (const u_char *)"",
173 0, NULL, 0, CIPHER_ENCRYPT);
174 cipher_init(&receive_context, none, (const u_char *)"",
175 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000176 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100177 if (!initialized) {
178 initialized = 1;
179 buffer_init(&input);
180 buffer_init(&output);
181 buffer_init(&outgoing_packet);
182 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000183 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100184 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185}
186
Damien Miller34132e52000-01-14 15:45:46 +1100187/* Returns 1 if remote host is connected via socket, 0 if not. */
188
189int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000190packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100191{
192 struct sockaddr_storage from, to;
193 socklen_t fromlen, tolen;
194
195 /* filedescriptors in and out are the same, so it's a socket */
196 if (connection_in == connection_out)
197 return 1;
198 fromlen = sizeof(from);
199 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100200 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100201 return 0;
202 tolen = sizeof(to);
203 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100204 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100205 return 0;
206 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
207 return 0;
208 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
209 return 0;
210 return 1;
211}
212
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000213/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000214 * Exports an IV from the CipherContext required to export the key
215 * state back from the unprivileged child to the privileged parent
216 * process.
217 */
218
219void
220packet_get_keyiv(int mode, u_char *iv, u_int len)
221{
222 CipherContext *cc;
223
224 if (mode == MODE_OUT)
225 cc = &send_context;
226 else
227 cc = &receive_context;
228
229 cipher_get_keyiv(cc, iv, len);
230}
231
232int
233packet_get_keycontext(int mode, u_char *dat)
234{
235 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000236
Ben Lindstromf6027d32002-03-22 01:42:04 +0000237 if (mode == MODE_OUT)
238 cc = &send_context;
239 else
240 cc = &receive_context;
241
242 return (cipher_get_keycontext(cc, dat));
243}
244
245void
246packet_set_keycontext(int mode, u_char *dat)
247{
248 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000249
Ben Lindstromf6027d32002-03-22 01:42:04 +0000250 if (mode == MODE_OUT)
251 cc = &send_context;
252 else
253 cc = &receive_context;
254
255 cipher_set_keycontext(cc, dat);
256}
257
258int
259packet_get_keyiv_len(int mode)
260{
261 CipherContext *cc;
262
263 if (mode == MODE_OUT)
264 cc = &send_context;
265 else
266 cc = &receive_context;
267
268 return (cipher_get_keyiv_len(cc));
269}
Damien Miller4f7becb2006-03-26 14:10:14 +1100270
Ben Lindstromf6027d32002-03-22 01:42:04 +0000271void
272packet_set_iv(int mode, u_char *dat)
273{
274 CipherContext *cc;
275
276 if (mode == MODE_OUT)
277 cc = &send_context;
278 else
279 cc = &receive_context;
280
281 cipher_set_keyiv(cc, dat);
282}
Damien Miller4f7becb2006-03-26 14:10:14 +1100283
Ben Lindstromf6027d32002-03-22 01:42:04 +0000284int
Damien Miller2b92d322003-06-11 22:05:06 +1000285packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000286{
287 return (cipher_get_number(receive_context.cipher));
288}
289
Damien Millera5539d22003-04-09 20:50:06 +1000290void
291packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000292{
Damien Millera5539d22003-04-09 20:50:06 +1000293 struct packet_state *state;
294
295 state = (mode == MODE_IN) ? &p_read : &p_send;
296 *seqnr = state->seqnr;
297 *blocks = state->blocks;
298 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000299}
300
301void
Damien Millera5539d22003-04-09 20:50:06 +1000302packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000303{
Damien Millera5539d22003-04-09 20:50:06 +1000304 struct packet_state *state;
305
306 state = (mode == MODE_IN) ? &p_read : &p_send;
307 state->seqnr = seqnr;
308 state->blocks = blocks;
309 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000310}
311
Damien Miller34132e52000-01-14 15:45:46 +1100312/* returns 1 if connection is via ipv4 */
313
314int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000315packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100316{
317 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100318 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100319
320 memset(&to, 0, sizeof(to));
321 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
322 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000323 if (to.ss_family == AF_INET)
324 return 1;
325#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100326 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000327 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
328 return 1;
329#endif
330 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100331}
332
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333/* Sets the connection into non-blocking mode. */
334
335void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000336packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337{
Damien Miller95def091999-11-25 00:26:21 +1100338 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000339 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340
Damien Miller232711f2004-06-15 10:35:30 +1000341 if (connection_out != connection_in)
342 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343}
344
345/* Returns the socket used for reading. */
346
347int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000348packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349{
Damien Miller95def091999-11-25 00:26:21 +1100350 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351}
352
353/* Returns the descriptor used for writing. */
354
355int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000356packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357{
Damien Miller95def091999-11-25 00:26:21 +1100358 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359}
360
361/* Closes the connection and clears and frees internal data structures. */
362
363void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000364packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365{
Damien Miller95def091999-11-25 00:26:21 +1100366 if (!initialized)
367 return;
368 initialized = 0;
369 if (connection_in == connection_out) {
370 shutdown(connection_out, SHUT_RDWR);
371 close(connection_out);
372 } else {
373 close(connection_in);
374 close(connection_out);
375 }
376 buffer_free(&input);
377 buffer_free(&output);
378 buffer_free(&outgoing_packet);
379 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000380 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100381 buffer_free(&compression_buffer);
382 buffer_compress_uninit();
383 }
Damien Miller963f6b22002-02-19 15:21:23 +1100384 cipher_cleanup(&send_context);
385 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386}
387
388/* Sets remote side protocol flags. */
389
390void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000391packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392{
Damien Miller95def091999-11-25 00:26:21 +1100393 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394}
395
396/* Returns the remote protocol flags set earlier by the above function. */
397
Ben Lindstrom46c16222000-12-22 01:43:59 +0000398u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000399packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400{
Damien Miller95def091999-11-25 00:26:21 +1100401 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402}
403
Damien Miller5428f641999-11-25 11:54:57 +1100404/*
405 * Starts packet compression from the next packet on in both directions.
406 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
407 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
Ben Lindstrombba81212001-06-25 05:01:22 +0000409static void
410packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000411{
412 if (compression_buffer_ready == 1)
413 return;
414 compression_buffer_ready = 1;
415 buffer_init(&compression_buffer);
416}
417
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418void
419packet_start_compression(int level)
420{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000421 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100422 fatal("Compression already enabled.");
423 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000424 packet_init_compression();
425 buffer_compress_init_send(level);
426 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427}
428
Damien Miller5428f641999-11-25 11:54:57 +1100429/*
Damien Miller5428f641999-11-25 11:54:57 +1100430 * Causes any further packets to be encrypted using the given key. The same
431 * key is used for both sending and reception. However, both directions are
432 * encrypted independently of each other.
433 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000434
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000436packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100437 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438{
Damien Miller874d77b2000-10-14 16:23:11 +1100439 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000440
Damien Miller874d77b2000-10-14 16:23:11 +1100441 if (cipher == NULL)
442 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000443 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100444 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000445 if (keylen > SSH_SESSION_KEY_LENGTH)
446 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
447 memcpy(ssh1_key, key, keylen);
448 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100449 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
450 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451}
452
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000453u_int
454packet_get_encryption_key(u_char *key)
455{
456 if (key == NULL)
457 return (ssh1_keylen);
458 memcpy(key, ssh1_key, ssh1_keylen);
459 return (ssh1_keylen);
460}
461
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000462/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000464packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000466 u_char buf[9];
467 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Ben Lindstrom204e4882001-03-05 06:47:00 +0000469 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000470 len = compat20 ? 6 : 9;
471 memset(buf, 0, len - 1);
472 buf[len - 1] = type;
473 buffer_clear(&outgoing_packet);
474 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000475}
476
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000477/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478void
479packet_put_char(int value)
480{
Damien Miller95def091999-11-25 00:26:21 +1100481 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000482
Damien Miller95def091999-11-25 00:26:21 +1100483 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484}
Damien Miller4f7becb2006-03-26 14:10:14 +1100485
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000487packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488{
Damien Miller95def091999-11-25 00:26:21 +1100489 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490}
Damien Miller4f7becb2006-03-26 14:10:14 +1100491
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100493packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494{
Damien Miller95def091999-11-25 00:26:21 +1100495 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496}
Damien Miller4f7becb2006-03-26 14:10:14 +1100497
Damien Miller33b13562000-04-04 14:38:59 +1000498void
499packet_put_cstring(const char *str)
500{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000501 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000502}
Damien Miller4f7becb2006-03-26 14:10:14 +1100503
Damien Miller33b13562000-04-04 14:38:59 +1000504void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100505packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000506{
507 buffer_append(&outgoing_packet, buf, len);
508}
Damien Miller4f7becb2006-03-26 14:10:14 +1100509
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510void
Damien Miller95def091999-11-25 00:26:21 +1100511packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512{
Damien Miller95def091999-11-25 00:26:21 +1100513 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514}
Damien Miller4f7becb2006-03-26 14:10:14 +1100515
Damien Miller33b13562000-04-04 14:38:59 +1000516void
517packet_put_bignum2(BIGNUM * value)
518{
519 buffer_put_bignum2(&outgoing_packet, value);
520}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Damien Miller5428f641999-11-25 11:54:57 +1100522/*
523 * Finalizes and sends the packet. If the encryption key has been set,
524 * encrypts the packet before sending.
525 */
Damien Miller95def091999-11-25 00:26:21 +1100526
Ben Lindstrombba81212001-06-25 05:01:22 +0000527static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000528packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000530 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100531 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000532 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000533 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534
Damien Miller5428f641999-11-25 11:54:57 +1100535 /*
536 * If using packet compression, compress the payload of the outgoing
537 * packet.
538 */
Damien Miller95def091999-11-25 00:26:21 +1100539 if (packet_compression) {
540 buffer_clear(&compression_buffer);
541 /* Skip padding. */
542 buffer_consume(&outgoing_packet, 8);
543 /* padding */
544 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
545 buffer_compress(&outgoing_packet, &compression_buffer);
546 buffer_clear(&outgoing_packet);
547 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100548 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100549 }
550 /* Compute packet length without padding (add checksum, remove padding). */
551 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552
Damien Millere247cc42000-05-07 12:03:14 +1000553 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100554 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100555 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100556 cp = buffer_ptr(&outgoing_packet);
557 for (i = 0; i < padding; i++) {
558 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000559 rnd = arc4random();
560 cp[7 - i] = rnd & 0xff;
561 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100562 }
563 }
564 buffer_consume(&outgoing_packet, 8 - padding);
565
566 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100567 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000568 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100569 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100570 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571
572#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100573 fprintf(stderr, "packet_send plain: ");
574 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575#endif
576
Damien Miller95def091999-11-25 00:26:21 +1100577 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100578 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100579 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100580 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100581 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100582 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100583
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100585 fprintf(stderr, "encrypted: ");
586 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587#endif
588
Damien Miller95def091999-11-25 00:26:21 +1100589 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590
Damien Miller5428f641999-11-25 11:54:57 +1100591 /*
Damien Miller788f2122005-11-05 15:14:59 +1100592 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100593 * actually sent until packet_write_wait or packet_write_poll is
594 * called.
595 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596}
597
Ben Lindstromf6027d32002-03-22 01:42:04 +0000598void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000599set_newkeys(int mode)
600{
601 Enc *enc;
602 Mac *mac;
603 Comp *comp;
604 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000605 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000606 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000607
Ben Lindstrom064496f2002-12-23 02:04:22 +0000608 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000609
Damien Miller963f6b22002-02-19 15:21:23 +1100610 if (mode == MODE_OUT) {
611 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000612 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000613 p_send.packets = p_send.blocks = 0;
614 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100615 } else {
616 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000617 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000618 p_read.packets = p_read.blocks = 0;
619 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100620 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000621 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000622 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100623 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000624 enc = &newkeys[mode]->enc;
625 mac = &newkeys[mode]->mac;
626 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000627 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000628 xfree(enc->name);
629 xfree(enc->iv);
630 xfree(enc->key);
631 xfree(mac->name);
632 xfree(mac->key);
633 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000634 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000635 }
636 newkeys[mode] = kex_get_newkeys(mode);
637 if (newkeys[mode] == NULL)
638 fatal("newkeys: no keys for mode %d", mode);
639 enc = &newkeys[mode]->enc;
640 mac = &newkeys[mode]->mac;
641 comp = &newkeys[mode]->comp;
642 if (mac->md != NULL)
643 mac->enabled = 1;
644 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100645 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000646 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000647 /* Deleting the keys does not gain extra security */
648 /* memset(enc->iv, 0, enc->block_size);
649 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000650 if ((comp->type == COMP_ZLIB ||
651 (comp->type == COMP_DELAYED && after_authentication)) &&
652 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000653 packet_init_compression();
654 if (mode == MODE_OUT)
655 buffer_compress_init_send(6);
656 else
657 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000658 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000659 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000660 /*
661 * The 2^(blocksize*2) limit is too expensive for 3DES,
662 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
663 */
664 if (enc->block_size >= 16)
665 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
666 else
667 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000668 if (rekey_limit)
669 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000670}
671
Damien Miller5428f641999-11-25 11:54:57 +1100672/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000673 * Delayed compression for SSH2 is enabled after authentication:
674 * This happans on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
675 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
676 */
677static void
678packet_enable_delayed_compress(void)
679{
680 Comp *comp = NULL;
681 int mode;
682
683 /*
684 * Remember that we are past the authentication step, so rekeying
685 * with COMP_DELAYED will turn on compression immediately.
686 */
687 after_authentication = 1;
688 for (mode = 0; mode < MODE_MAX; mode++) {
689 comp = &newkeys[mode]->comp;
690 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000691 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000692 if (mode == MODE_OUT)
693 buffer_compress_init_send(6);
694 else
695 buffer_compress_init_recv();
696 comp->enabled = 1;
697 }
698 }
699}
700
701/*
Damien Miller33b13562000-04-04 14:38:59 +1000702 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
703 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000704static void
Damien Millera5539d22003-04-09 20:50:06 +1000705packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000706{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000707 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100708 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000709 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100710 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000711 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000712 Enc *enc = NULL;
713 Mac *mac = NULL;
714 Comp *comp = NULL;
715 int block_size;
716
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000717 if (newkeys[MODE_OUT] != NULL) {
718 enc = &newkeys[MODE_OUT]->enc;
719 mac = &newkeys[MODE_OUT]->mac;
720 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000721 }
Damien Miller963f6b22002-02-19 15:21:23 +1100722 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000723
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000724 cp = buffer_ptr(&outgoing_packet);
725 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000726
727#ifdef PACKET_DEBUG
728 fprintf(stderr, "plain: ");
729 buffer_dump(&outgoing_packet);
730#endif
731
732 if (comp && comp->enabled) {
733 len = buffer_len(&outgoing_packet);
734 /* skip header, compress only payload */
735 buffer_consume(&outgoing_packet, 5);
736 buffer_clear(&compression_buffer);
737 buffer_compress(&outgoing_packet, &compression_buffer);
738 buffer_clear(&outgoing_packet);
739 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
740 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
741 buffer_len(&compression_buffer));
742 DBG(debug("compression: raw %d compressed %d", len,
743 buffer_len(&outgoing_packet)));
744 }
745
746 /* sizeof (packet_len + pad_len + payload) */
747 len = buffer_len(&outgoing_packet);
748
749 /*
750 * calc size of padding, alloc space, get random data,
751 * minimum padding is 4 bytes
752 */
753 padlen = block_size - (len % block_size);
754 if (padlen < 4)
755 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100756 if (extra_pad) {
757 /* will wrap if extra_pad+padlen > 255 */
758 extra_pad = roundup(extra_pad, block_size);
759 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000760 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100761 pad, len, padlen, extra_pad);
762 padlen += pad;
763 extra_pad = 0;
764 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100765 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100766 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000767 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000768 for (i = 0; i < padlen; i++) {
769 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000770 rnd = arc4random();
771 cp[i] = rnd & 0xff;
772 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000773 }
Damien Millere247cc42000-05-07 12:03:14 +1000774 } else {
775 /* clear padding */
776 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000777 }
778 /* packet_length includes payload, padding and padding length field */
779 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000780 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100781 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000782 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000783 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
784
785 /* compute MAC over seqnr and packet(length fields, payload, padding) */
786 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000787 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100788 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000789 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000790 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000791 }
792 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100793 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100794 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000795 buffer_len(&outgoing_packet));
796 /* append unencrypted MAC */
797 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100798 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000799#ifdef PACKET_DEBUG
800 fprintf(stderr, "encrypted: ");
801 buffer_dump(&output);
802#endif
Damien Miller4af51302000-04-16 11:18:38 +1000803 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000804 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000805 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000806 if (++p_send.packets == 0)
807 if (!(datafellows & SSH_BUG_NOREKEY))
808 fatal("XXX too many packets with same key");
809 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000810 buffer_clear(&outgoing_packet);
811
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000812 if (type == SSH2_MSG_NEWKEYS)
813 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000814 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
815 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000816}
817
Damien Millera5539d22003-04-09 20:50:06 +1000818static void
819packet_send2(void)
820{
821 static int rekeying = 0;
822 struct packet *p;
823 u_char type, *cp;
824
825 cp = buffer_ptr(&outgoing_packet);
826 type = cp[5];
827
828 /* during rekeying we can only send key exchange messages */
829 if (rekeying) {
830 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
831 (type <= SSH2_MSG_TRANSPORT_MAX))) {
832 debug("enqueue packet: %u", type);
833 p = xmalloc(sizeof(*p));
834 p->type = type;
835 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
836 buffer_init(&outgoing_packet);
837 TAILQ_INSERT_TAIL(&outgoing, p, next);
838 return;
839 }
840 }
841
842 /* rekeying starts with sending KEXINIT */
843 if (type == SSH2_MSG_KEXINIT)
844 rekeying = 1;
845
846 packet_send2_wrapped();
847
848 /* after a NEWKEYS message we can send the complete queue */
849 if (type == SSH2_MSG_NEWKEYS) {
850 rekeying = 0;
851 while ((p = TAILQ_FIRST(&outgoing))) {
852 type = p->type;
853 debug("dequeue packet: %u", type);
854 buffer_free(&outgoing_packet);
855 memcpy(&outgoing_packet, &p->payload,
856 sizeof(Buffer));
857 TAILQ_REMOVE(&outgoing, p, next);
858 xfree(p);
859 packet_send2_wrapped();
860 }
861 }
862}
863
Damien Miller33b13562000-04-04 14:38:59 +1000864void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000865packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000866{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000867 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000868 packet_send2();
869 else
870 packet_send1();
871 DBG(debug("packet_send done"));
872}
873
Damien Miller33b13562000-04-04 14:38:59 +1000874/*
Damien Miller5428f641999-11-25 11:54:57 +1100875 * Waits until a packet has been received, and returns its type. Note that
876 * no other data is processed until this returns, so this function should not
877 * be used during the interactive session.
878 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879
880int
Damien Millerdff50992002-01-22 23:16:32 +1100881packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882{
Damien Miller95def091999-11-25 00:26:21 +1100883 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000884 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100885 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000886 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887
Damien Miller07d86be2006-03-26 14:19:21 +1100888 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000889 sizeof(fd_mask));
890
Damien Miller95def091999-11-25 00:26:21 +1100891 /* Since we are blocking, ensure that all written packets have been sent. */
892 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Miller95def091999-11-25 00:26:21 +1100894 /* Stay in the loop until we have received a complete packet. */
895 for (;;) {
896 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100897 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000898 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000899 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100900 || type == SSH_SMSG_FAILURE
901 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000902 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100903 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100904 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000905 if (type != SSH_MSG_NONE) {
906 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100907 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000908 }
Damien Miller5428f641999-11-25 11:54:57 +1100909 /*
910 * Otherwise, wait for some data to arrive, add it to the
911 * buffer, and try again.
912 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000913 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
914 sizeof(fd_mask));
915 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100916
Damien Miller95def091999-11-25 00:26:21 +1100917 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000918 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000919 (errno == EAGAIN || errno == EINTR))
920 ;
Damien Miller5428f641999-11-25 11:54:57 +1100921
Damien Miller95def091999-11-25 00:26:21 +1100922 /* Read data from the socket. */
923 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100924 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000925 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000926 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100927 }
Damien Miller95def091999-11-25 00:26:21 +1100928 if (len < 0)
929 fatal("Read from socket failed: %.100s", strerror(errno));
930 /* Append it to the buffer. */
931 packet_process_incoming(buf, len);
932 }
933 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934}
935
Damien Miller278f9072001-12-21 15:00:19 +1100936int
Damien Millerdff50992002-01-22 23:16:32 +1100937packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100938{
Damien Millerdff50992002-01-22 23:16:32 +1100939 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100940}
941
Damien Miller5428f641999-11-25 11:54:57 +1100942/*
943 * Waits until a packet has been received, verifies that its type matches
944 * that given, and gives a fatal error and exits if there is a mismatch.
945 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946
947void
Damien Millerdff50992002-01-22 23:16:32 +1100948packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949{
Damien Miller95def091999-11-25 00:26:21 +1100950 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Damien Millerdff50992002-01-22 23:16:32 +1100952 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100953 if (type != expected_type)
954 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000955 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956}
957
958/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100959 * packet_process_incoming. If so, reads the packet; otherwise returns
960 * SSH_MSG_NONE. This does not wait for data from the connection.
961 *
962 * SSH_MSG_DISCONNECT is handled specially here. Also,
963 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
964 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100965 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966
Ben Lindstrombba81212001-06-25 05:01:22 +0000967static int
Damien Millerdff50992002-01-22 23:16:32 +1100968packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000970 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000971 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000972 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973
Damien Miller95def091999-11-25 00:26:21 +1100974 /* Check if input size is less than minimum packet size. */
975 if (buffer_len(&input) < 4 + 8)
976 return SSH_MSG_NONE;
977 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000978 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100979 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100980 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000981 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100982 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983
Damien Miller95def091999-11-25 00:26:21 +1100984 /* Check if the packet has been entirely received. */
985 if (buffer_len(&input) < 4 + padded_len)
986 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987
Damien Miller95def091999-11-25 00:26:21 +1100988 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000989
Damien Miller95def091999-11-25 00:26:21 +1100990 /* Consume packet length. */
991 buffer_consume(&input, 4);
992
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000993 /*
994 * Cryptographic attack detector for ssh
995 * (C)1998 CORE-SDI, Buenos Aires Argentina
996 * Ariel Futoransky(futo@core-sdi.com)
997 */
Damien Miller963f6b22002-02-19 15:21:23 +1100998 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +1100999 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001000 packet_disconnect("crc32 compensation attack: network attack detected");
1001
1002 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001003 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001004 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001005 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001006
Damien Miller95def091999-11-25 00:26:21 +11001007 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
1009#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001010 fprintf(stderr, "read_poll plain: ");
1011 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013
Damien Miller95def091999-11-25 00:26:21 +11001014 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001015 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001016 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017
Damien Miller95def091999-11-25 00:26:21 +11001018 /* Skip padding. */
1019 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001020
Damien Miller95def091999-11-25 00:26:21 +11001021 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001022 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001023 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001024 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001025
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001026 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001027 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001028 if (checksum != stored_checksum)
1029 packet_disconnect("Corrupted check bytes on input.");
1030 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031
Damien Miller95def091999-11-25 00:26:21 +11001032 if (packet_compression) {
1033 buffer_clear(&compression_buffer);
1034 buffer_uncompress(&incoming_packet, &compression_buffer);
1035 buffer_clear(&incoming_packet);
1036 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001037 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001038 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001039 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001040 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1041 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001042 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043}
Damien Miller95def091999-11-25 00:26:21 +11001044
Ben Lindstrombba81212001-06-25 05:01:22 +00001045static int
Damien Millerdff50992002-01-22 23:16:32 +11001046packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001047{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001048 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001049 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001050 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001051 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001052 Enc *enc = NULL;
1053 Mac *mac = NULL;
1054 Comp *comp = NULL;
1055
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001056 if (newkeys[MODE_IN] != NULL) {
1057 enc = &newkeys[MODE_IN]->enc;
1058 mac = &newkeys[MODE_IN]->mac;
1059 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001060 }
1061 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001062 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001063
1064 if (packet_length == 0) {
1065 /*
1066 * check if input size is less than the cipher block size,
1067 * decrypt first block and extract length of incoming packet
1068 */
1069 if (buffer_len(&input) < block_size)
1070 return SSH_MSG_NONE;
1071 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001072 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001073 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001074 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001075 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001076 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001077 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001078#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001079 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001080#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001081 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001082 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001083 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001084 buffer_consume(&input, block_size);
1085 }
1086 /* we have a partial packet of block_size bytes */
1087 need = 4 + packet_length - block_size;
1088 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1089 need, maclen));
1090 if (need % block_size != 0)
1091 fatal("padding error: need %d block %d mod %d",
1092 need, block_size, need % block_size);
1093 /*
1094 * check if the entire packet has been received and
1095 * decrypt into incoming_packet
1096 */
1097 if (buffer_len(&input) < need + maclen)
1098 return SSH_MSG_NONE;
1099#ifdef PACKET_DEBUG
1100 fprintf(stderr, "read_poll enc/full: ");
1101 buffer_dump(&input);
1102#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001103 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001104 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001105 buffer_consume(&input, need);
1106 /*
1107 * compute MAC over seqnr and packet,
1108 * increment sequence number for incoming packet
1109 */
Damien Miller4af51302000-04-16 11:18:38 +10001110 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001111 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001112 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001113 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001114 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001115 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001116 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001117 buffer_consume(&input, mac->mac_len);
1118 }
Damien Miller278f9072001-12-21 15:00:19 +11001119 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001120 *seqnr_p = p_read.seqnr;
1121 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001122 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001123 if (++p_read.packets == 0)
1124 if (!(datafellows & SSH_BUG_NOREKEY))
1125 fatal("XXX too many packets with same key");
1126 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001127
1128 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001129 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001130 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001131 DBG(debug("input: padlen %d", padlen));
1132 if (padlen < 4)
1133 packet_disconnect("Corrupted padlen %d on input.", padlen);
1134
1135 /* skip packet size + padlen, discard padding */
1136 buffer_consume(&incoming_packet, 4 + 1);
1137 buffer_consume_end(&incoming_packet, padlen);
1138
1139 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1140 if (comp && comp->enabled) {
1141 buffer_clear(&compression_buffer);
1142 buffer_uncompress(&incoming_packet, &compression_buffer);
1143 buffer_clear(&incoming_packet);
1144 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1145 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001146 DBG(debug("input: len after de-compress %d",
1147 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001148 }
1149 /*
1150 * get packet type, implies consume.
1151 * return length of payload (without type field)
1152 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001153 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001154 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1155 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001156 if (type == SSH2_MSG_NEWKEYS)
1157 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001158 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1159 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001160#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001161 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001162 buffer_dump(&incoming_packet);
1163#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001164 /* reset for next packet */
1165 packet_length = 0;
1166 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001167}
1168
1169int
Damien Millerdff50992002-01-22 23:16:32 +11001170packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001171{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001172 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001173 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001174 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001175
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001176 for (;;) {
1177 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001178 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001179 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001180 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001181 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001182 case SSH2_MSG_IGNORE:
1183 break;
1184 case SSH2_MSG_DEBUG:
1185 packet_get_char();
1186 msg = packet_get_string(NULL);
1187 debug("Remote: %.900s", msg);
1188 xfree(msg);
1189 msg = packet_get_string(NULL);
1190 xfree(msg);
1191 break;
1192 case SSH2_MSG_DISCONNECT:
1193 reason = packet_get_int();
1194 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001195 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001196 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001197 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001198 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001199 break;
Damien Miller659811f2002-01-22 23:23:11 +11001200 case SSH2_MSG_UNIMPLEMENTED:
1201 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001202 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1203 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001204 break;
Damien Miller33b13562000-04-04 14:38:59 +10001205 default:
1206 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001207 }
Damien Miller33b13562000-04-04 14:38:59 +10001208 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001209 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001210 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001211 case SSH_MSG_IGNORE:
1212 break;
1213 case SSH_MSG_DEBUG:
1214 msg = packet_get_string(NULL);
1215 debug("Remote: %.900s", msg);
1216 xfree(msg);
1217 break;
1218 case SSH_MSG_DISCONNECT:
1219 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001220 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001221 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001222 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001223 xfree(msg);
1224 break;
1225 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001226 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001227 DBG(debug("received packet type %d", type));
1228 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001229 }
Damien Miller33b13562000-04-04 14:38:59 +10001230 }
1231 }
1232}
1233
Damien Miller278f9072001-12-21 15:00:19 +11001234int
Damien Millerdff50992002-01-22 23:16:32 +11001235packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001236{
Damien Millerdff50992002-01-22 23:16:32 +11001237 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001238}
1239
Damien Miller5428f641999-11-25 11:54:57 +11001240/*
1241 * Buffers the given amount of input characters. This is intended to be used
1242 * together with packet_read_poll.
1243 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001244
1245void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001246packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247{
Damien Miller95def091999-11-25 00:26:21 +11001248 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249}
1250
1251/* Returns a character from the packet. */
1252
Ben Lindstrom46c16222000-12-22 01:43:59 +00001253u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001254packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255{
Damien Miller95def091999-11-25 00:26:21 +11001256 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001257
Damien Miller95def091999-11-25 00:26:21 +11001258 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001259 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260}
1261
1262/* Returns an integer from the packet data. */
1263
Ben Lindstrom46c16222000-12-22 01:43:59 +00001264u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001265packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266{
Damien Miller95def091999-11-25 00:26:21 +11001267 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268}
1269
Damien Miller5428f641999-11-25 11:54:57 +11001270/*
1271 * Returns an arbitrary precision integer from the packet data. The integer
1272 * must have been initialized before this call.
1273 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001274
1275void
Damien Millerd432ccf2002-01-22 23:14:44 +11001276packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001277{
Damien Miller76e1e362002-01-22 23:15:57 +11001278 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001279}
1280
Damien Miller33b13562000-04-04 14:38:59 +10001281void
Damien Millerd432ccf2002-01-22 23:14:44 +11001282packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001283{
Damien Miller76e1e362002-01-22 23:15:57 +11001284 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001285}
1286
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001287void *
Damien Millereccb9de2005-06-17 12:59:34 +10001288packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001289{
Damien Millereccb9de2005-06-17 12:59:34 +10001290 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001291
Damien Miller33b13562000-04-04 14:38:59 +10001292 if (length_ptr != NULL)
1293 *length_ptr = bytes;
1294 return buffer_ptr(&incoming_packet);
1295}
1296
Damien Miller4af51302000-04-16 11:18:38 +10001297int
1298packet_remaining(void)
1299{
1300 return buffer_len(&incoming_packet);
1301}
1302
Damien Miller5428f641999-11-25 11:54:57 +11001303/*
1304 * Returns a string from the packet data. The string is allocated using
1305 * xmalloc; it is the responsibility of the calling program to free it when
1306 * no longer needed. The length_ptr argument may be NULL, or point to an
1307 * integer into which the length of the string is stored.
1308 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001310void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001311packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312{
Damien Miller95def091999-11-25 00:26:21 +11001313 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001314}
1315
Damien Miller5428f641999-11-25 11:54:57 +11001316/*
1317 * Sends a diagnostic message from the server to the client. This message
1318 * can be sent at any time (but not while constructing another message). The
1319 * message is printed immediately, but only if the client is being executed
1320 * in verbose mode. These messages are primarily intended to ease debugging
1321 * authentication problems. The length of the formatted message must not
1322 * exceed 1024 bytes. This will automatically call packet_write_wait.
1323 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001324
1325void
Damien Miller95def091999-11-25 00:26:21 +11001326packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001327{
Damien Miller95def091999-11-25 00:26:21 +11001328 char buf[1024];
1329 va_list args;
1330
Ben Lindstroma14ee472000-12-07 01:24:58 +00001331 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1332 return;
1333
Damien Miller95def091999-11-25 00:26:21 +11001334 va_start(args, fmt);
1335 vsnprintf(buf, sizeof(buf), fmt, args);
1336 va_end(args);
1337
Damien Miller7c8af4f2000-05-01 08:24:07 +10001338 if (compat20) {
1339 packet_start(SSH2_MSG_DEBUG);
1340 packet_put_char(0); /* bool: always display */
1341 packet_put_cstring(buf);
1342 packet_put_cstring("");
1343 } else {
1344 packet_start(SSH_MSG_DEBUG);
1345 packet_put_cstring(buf);
1346 }
Damien Miller95def091999-11-25 00:26:21 +11001347 packet_send();
1348 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001349}
1350
Damien Miller5428f641999-11-25 11:54:57 +11001351/*
1352 * Logs the error plus constructs and sends a disconnect packet, closes the
1353 * connection, and exits. This function never returns. The error message
1354 * should not contain a newline. The length of the formatted message must
1355 * not exceed 1024 bytes.
1356 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357
1358void
Damien Miller95def091999-11-25 00:26:21 +11001359packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001360{
Damien Miller95def091999-11-25 00:26:21 +11001361 char buf[1024];
1362 va_list args;
1363 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001364
Damien Miller95def091999-11-25 00:26:21 +11001365 if (disconnecting) /* Guard against recursive invocations. */
1366 fatal("packet_disconnect called recursively.");
1367 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368
Damien Miller5428f641999-11-25 11:54:57 +11001369 /*
1370 * Format the message. Note that the caller must make sure the
1371 * message is of limited size.
1372 */
Damien Miller95def091999-11-25 00:26:21 +11001373 va_start(args, fmt);
1374 vsnprintf(buf, sizeof(buf), fmt, args);
1375 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001376
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001377 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001378 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001379
Damien Miller95def091999-11-25 00:26:21 +11001380 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001381 if (compat20) {
1382 packet_start(SSH2_MSG_DISCONNECT);
1383 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1384 packet_put_cstring(buf);
1385 packet_put_cstring("");
1386 } else {
1387 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001388 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001389 }
Damien Miller95def091999-11-25 00:26:21 +11001390 packet_send();
1391 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392
Damien Miller95def091999-11-25 00:26:21 +11001393 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001394 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001395
Damien Miller95def091999-11-25 00:26:21 +11001396 /* Close the connection. */
1397 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001398 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001399}
1400
Damien Miller5428f641999-11-25 11:54:57 +11001401/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402
1403void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001404packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405{
Damien Miller95def091999-11-25 00:26:21 +11001406 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001407
Damien Miller95def091999-11-25 00:26:21 +11001408 if (len > 0) {
1409 len = write(connection_out, buffer_ptr(&output), len);
1410 if (len <= 0) {
1411 if (errno == EAGAIN)
1412 return;
1413 else
1414 fatal("Write failed: %.100s", strerror(errno));
1415 }
1416 buffer_consume(&output, len);
1417 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418}
1419
Damien Miller5428f641999-11-25 11:54:57 +11001420/*
1421 * Calls packet_write_poll repeatedly until all pending output data has been
1422 * written.
1423 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001424
1425void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001426packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001427{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001428 fd_set *setp;
1429
Damien Miller07d86be2006-03-26 14:19:21 +11001430 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001431 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001432 packet_write_poll();
1433 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001434 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1435 sizeof(fd_mask));
1436 FD_SET(connection_out, setp);
1437 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001438 (errno == EAGAIN || errno == EINTR))
1439 ;
Damien Miller95def091999-11-25 00:26:21 +11001440 packet_write_poll();
1441 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001442 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443}
1444
1445/* Returns true if there is buffered data to write to the connection. */
1446
1447int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001448packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449{
Damien Miller95def091999-11-25 00:26:21 +11001450 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001451}
1452
1453/* Returns true if there is not too much data to write to the connection. */
1454
1455int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001456packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001457{
Damien Miller95def091999-11-25 00:26:21 +11001458 if (interactive_mode)
1459 return buffer_len(&output) < 16384;
1460 else
1461 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462}
1463
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001464
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001465static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001466packet_set_tos(int interactive)
1467{
Damien Miller5924ceb2003-11-22 15:02:42 +11001468#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001469 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1470
1471 if (!packet_connection_is_on_socket() ||
1472 !packet_connection_is_ipv4())
1473 return;
1474 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1475 sizeof(tos)) < 0)
1476 error("setsockopt IP_TOS %d: %.100s:",
1477 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001478#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001479}
Ben Lindstroma7433982002-12-23 02:41:41 +00001480
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481/* Informs that the current session is interactive. Sets IP flags for that. */
1482
1483void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001484packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001486 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001487
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001488 if (called)
1489 return;
1490 called = 1;
1491
Damien Miller95def091999-11-25 00:26:21 +11001492 /* Record that we are in interactive mode. */
1493 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494
Damien Miller34132e52000-01-14 15:45:46 +11001495 /* Only set socket options if using a socket. */
1496 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001497 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001498 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001499 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500}
1501
1502/* Returns true if the current connection is interactive. */
1503
1504int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001505packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506{
Damien Miller95def091999-11-25 00:26:21 +11001507 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001508}
Damien Miller6162d121999-11-21 13:23:52 +11001509
Darren Tucker1f8311c2004-05-13 16:39:33 +10001510int
Darren Tucker502d3842003-06-28 12:38:01 +10001511packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001512{
Damien Miller95def091999-11-25 00:26:21 +11001513 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001514
Damien Miller95def091999-11-25 00:26:21 +11001515 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001516 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001517 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001518 return -1;
1519 }
1520 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001521 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001522 return -1;
1523 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001524 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001525 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001526 max_packet_size = s;
1527 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001528}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001529
Damien Miller9f643902001-11-12 11:02:52 +11001530/* roundup current message to pad bytes */
1531void
1532packet_add_padding(u_char pad)
1533{
1534 extra_pad = pad;
1535}
1536
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001537/*
1538 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001539 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001540 * byte SSH_MSG_IGNORE
1541 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001542 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001543 * All implementations MUST understand (and ignore) this message at any
1544 * time (after receiving the protocol version). No implementation is
1545 * required to send them. This message can be used as an additional
1546 * protection measure against advanced traffic analysis techniques.
1547 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001548void
1549packet_send_ignore(int nbytes)
1550{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001551 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001552 int i;
1553
1554 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001555 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001556 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001557 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001558 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001559 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001560 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001561 }
1562}
Damien Millera5539d22003-04-09 20:50:06 +10001563
Darren Tucker1f8311c2004-05-13 16:39:33 +10001564#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001565int
1566packet_need_rekeying(void)
1567{
1568 if (datafellows & SSH_BUG_NOREKEY)
1569 return 0;
1570 return
1571 (p_send.packets > MAX_PACKETS) ||
1572 (p_read.packets > MAX_PACKETS) ||
1573 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1574 (max_blocks_in && (p_read.blocks > max_blocks_in));
1575}
1576
1577void
1578packet_set_rekey_limit(u_int32_t bytes)
1579{
1580 rekey_limit = bytes;
1581}
Damien Miller9786e6e2005-07-26 21:54:56 +10001582
1583void
1584packet_set_server(void)
1585{
1586 server_side = 1;
1587}
1588
1589void
1590packet_set_authenticated(void)
1591{
1592 after_authentication = 1;
1593}