blob: a4cb3324e400388b4ca49039050bd4e8c792d368 [file] [log] [blame]
Darren Tuckerf676c572006-08-05 18:51:08 +10001/* $OpenBSD: packet.c,v 1.143 2006/08/05 08:34:04 dtucker 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 Miller8dbffe72006-08-05 11:02:17 +100044#include <sys/param.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100045#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100046#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100049
50#include <netinet/in_systm.h>
51#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110052#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Darren Tucker39972492006-07-12 22:22:46 +100054#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100055#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100056#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100057#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100058#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100059#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100060#include <signal.h>
Darren Tucker5d196262006-07-12 22:15:16 +100061
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "xmalloc.h"
63#include "buffer.h"
64#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "compress.h"
67#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000068#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100069#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100071#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110072#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100073#include "key.h"
Damien Miller33b13562000-04-04 14:38:59 +100074#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000075#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
77#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110078#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000079#include "ssh.h"
Damien Miller33b13562000-04-04 14:38:59 +100080
81#ifdef PACKET_DEBUG
82#define DBG(x) x
83#else
84#define DBG(x)
85#endif
86
Damien Miller5428f641999-11-25 11:54:57 +110087/*
88 * This variable contains the file descriptors used for communicating with
89 * the other side. connection_in is used for reading; connection_out for
90 * writing. These can be the same descriptor, in which case it is assumed to
91 * be a socket.
92 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093static int connection_in = -1;
94static int connection_out = -1;
95
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000097static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99/* Encryption context for receiving data. This is only used for decryption. */
100static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +1100101
102/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103static CipherContext send_context;
104
105/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000106Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
108/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000109Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
111/* Buffer for the partial outgoing packet being constructed. */
112static Buffer outgoing_packet;
113
114/* Buffer for the incoming packet currently being processed. */
115static Buffer incoming_packet;
116
117/* Scratch buffer for packet compression/decompression. */
118static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000119static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
121/* Flag indicating whether packet compression/decompression is enabled. */
122static int packet_compression = 0;
123
Damien Miller6162d121999-11-21 13:23:52 +1100124/* default maximum packet size */
Darren Tucker502d3842003-06-28 12:38:01 +1000125u_int max_packet_size = 32768;
Damien Miller6162d121999-11-21 13:23:52 +1100126
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127/* Flag indicating whether this module has been initialized. */
128static int initialized = 0;
129
130/* Set to true if the connection is interactive. */
131static int interactive_mode = 0;
132
Damien Miller9786e6e2005-07-26 21:54:56 +1000133/* Set to true if we are the server side. */
134static int server_side = 0;
135
136/* Set to true if we are authenticated. */
137static int after_authentication = 0;
138
Damien Miller33b13562000-04-04 14:38:59 +1000139/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000140Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000141static struct packet_state {
142 u_int32_t seqnr;
143 u_int32_t packets;
144 u_int64_t blocks;
145} p_read, p_send;
146
147static u_int64_t max_blocks_in, max_blocks_out;
148static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000149
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000150/* Session key for protocol v1 */
151static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
152static u_int ssh1_keylen;
153
Damien Miller9f643902001-11-12 11:02:52 +1100154/* roundup current message to extra_pad bytes */
155static u_char extra_pad = 0;
156
Damien Millera5539d22003-04-09 20:50:06 +1000157struct packet {
158 TAILQ_ENTRY(packet) next;
159 u_char type;
160 Buffer payload;
161};
162TAILQ_HEAD(, packet) outgoing;
163
Damien Miller5428f641999-11-25 11:54:57 +1100164/*
165 * Sets the descriptors used for communication. Disables encryption until
166 * packet_set_encryption_key is called.
167 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168void
169packet_set_connection(int fd_in, int fd_out)
170{
Damien Miller874d77b2000-10-14 16:23:11 +1100171 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000172
Damien Miller874d77b2000-10-14 16:23:11 +1100173 if (none == NULL)
174 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100175 connection_in = fd_in;
176 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000177 cipher_init(&send_context, none, (const u_char *)"",
178 0, NULL, 0, CIPHER_ENCRYPT);
179 cipher_init(&receive_context, none, (const u_char *)"",
180 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000181 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100182 if (!initialized) {
183 initialized = 1;
184 buffer_init(&input);
185 buffer_init(&output);
186 buffer_init(&outgoing_packet);
187 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000188 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100189 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
Damien Miller34132e52000-01-14 15:45:46 +1100192/* Returns 1 if remote host is connected via socket, 0 if not. */
193
194int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000195packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100196{
197 struct sockaddr_storage from, to;
198 socklen_t fromlen, tolen;
199
200 /* filedescriptors in and out are the same, so it's a socket */
201 if (connection_in == connection_out)
202 return 1;
203 fromlen = sizeof(from);
204 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100205 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100206 return 0;
207 tolen = sizeof(to);
208 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100209 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100210 return 0;
211 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
212 return 0;
213 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
214 return 0;
215 return 1;
216}
217
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000218/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000219 * Exports an IV from the CipherContext required to export the key
220 * state back from the unprivileged child to the privileged parent
221 * process.
222 */
223
224void
225packet_get_keyiv(int mode, u_char *iv, u_int len)
226{
227 CipherContext *cc;
228
229 if (mode == MODE_OUT)
230 cc = &send_context;
231 else
232 cc = &receive_context;
233
234 cipher_get_keyiv(cc, iv, len);
235}
236
237int
238packet_get_keycontext(int mode, u_char *dat)
239{
240 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000241
Ben Lindstromf6027d32002-03-22 01:42:04 +0000242 if (mode == MODE_OUT)
243 cc = &send_context;
244 else
245 cc = &receive_context;
246
247 return (cipher_get_keycontext(cc, dat));
248}
249
250void
251packet_set_keycontext(int mode, u_char *dat)
252{
253 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000254
Ben Lindstromf6027d32002-03-22 01:42:04 +0000255 if (mode == MODE_OUT)
256 cc = &send_context;
257 else
258 cc = &receive_context;
259
260 cipher_set_keycontext(cc, dat);
261}
262
263int
264packet_get_keyiv_len(int mode)
265{
266 CipherContext *cc;
267
268 if (mode == MODE_OUT)
269 cc = &send_context;
270 else
271 cc = &receive_context;
272
273 return (cipher_get_keyiv_len(cc));
274}
Damien Miller4f7becb2006-03-26 14:10:14 +1100275
Ben Lindstromf6027d32002-03-22 01:42:04 +0000276void
277packet_set_iv(int mode, u_char *dat)
278{
279 CipherContext *cc;
280
281 if (mode == MODE_OUT)
282 cc = &send_context;
283 else
284 cc = &receive_context;
285
286 cipher_set_keyiv(cc, dat);
287}
Damien Miller4f7becb2006-03-26 14:10:14 +1100288
Ben Lindstromf6027d32002-03-22 01:42:04 +0000289int
Damien Miller2b92d322003-06-11 22:05:06 +1000290packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000291{
292 return (cipher_get_number(receive_context.cipher));
293}
294
Damien Millera5539d22003-04-09 20:50:06 +1000295void
296packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000297{
Damien Millera5539d22003-04-09 20:50:06 +1000298 struct packet_state *state;
299
300 state = (mode == MODE_IN) ? &p_read : &p_send;
301 *seqnr = state->seqnr;
302 *blocks = state->blocks;
303 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000304}
305
306void
Damien Millera5539d22003-04-09 20:50:06 +1000307packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000308{
Damien Millera5539d22003-04-09 20:50:06 +1000309 struct packet_state *state;
310
311 state = (mode == MODE_IN) ? &p_read : &p_send;
312 state->seqnr = seqnr;
313 state->blocks = blocks;
314 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000315}
316
Damien Miller34132e52000-01-14 15:45:46 +1100317/* returns 1 if connection is via ipv4 */
318
319int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000320packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100321{
322 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100323 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100324
325 memset(&to, 0, sizeof(to));
326 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
327 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000328 if (to.ss_family == AF_INET)
329 return 1;
330#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100331 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000332 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
333 return 1;
334#endif
335 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100336}
337
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338/* Sets the connection into non-blocking mode. */
339
340void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000341packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342{
Damien Miller95def091999-11-25 00:26:21 +1100343 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000344 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Damien Miller232711f2004-06-15 10:35:30 +1000346 if (connection_out != connection_in)
347 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348}
349
350/* Returns the socket used for reading. */
351
352int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000353packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354{
Damien Miller95def091999-11-25 00:26:21 +1100355 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356}
357
358/* Returns the descriptor used for writing. */
359
360int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000361packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362{
Damien Miller95def091999-11-25 00:26:21 +1100363 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364}
365
366/* Closes the connection and clears and frees internal data structures. */
367
368void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000369packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370{
Damien Miller95def091999-11-25 00:26:21 +1100371 if (!initialized)
372 return;
373 initialized = 0;
374 if (connection_in == connection_out) {
375 shutdown(connection_out, SHUT_RDWR);
376 close(connection_out);
377 } else {
378 close(connection_in);
379 close(connection_out);
380 }
381 buffer_free(&input);
382 buffer_free(&output);
383 buffer_free(&outgoing_packet);
384 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000385 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100386 buffer_free(&compression_buffer);
387 buffer_compress_uninit();
388 }
Damien Miller963f6b22002-02-19 15:21:23 +1100389 cipher_cleanup(&send_context);
390 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391}
392
393/* Sets remote side protocol flags. */
394
395void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000396packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397{
Damien Miller95def091999-11-25 00:26:21 +1100398 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399}
400
401/* Returns the remote protocol flags set earlier by the above function. */
402
Ben Lindstrom46c16222000-12-22 01:43:59 +0000403u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000404packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405{
Damien Miller95def091999-11-25 00:26:21 +1100406 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407}
408
Damien Miller5428f641999-11-25 11:54:57 +1100409/*
410 * Starts packet compression from the next packet on in both directions.
411 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
412 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
Ben Lindstrombba81212001-06-25 05:01:22 +0000414static void
415packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000416{
417 if (compression_buffer_ready == 1)
418 return;
419 compression_buffer_ready = 1;
420 buffer_init(&compression_buffer);
421}
422
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423void
424packet_start_compression(int level)
425{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000426 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100427 fatal("Compression already enabled.");
428 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000429 packet_init_compression();
430 buffer_compress_init_send(level);
431 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432}
433
Damien Miller5428f641999-11-25 11:54:57 +1100434/*
Damien Miller5428f641999-11-25 11:54:57 +1100435 * Causes any further packets to be encrypted using the given key. The same
436 * key is used for both sending and reception. However, both directions are
437 * encrypted independently of each other.
438 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000439
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000441packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100442 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443{
Damien Miller874d77b2000-10-14 16:23:11 +1100444 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000445
Damien Miller874d77b2000-10-14 16:23:11 +1100446 if (cipher == NULL)
447 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000448 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100449 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000450 if (keylen > SSH_SESSION_KEY_LENGTH)
451 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
452 memcpy(ssh1_key, key, keylen);
453 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100454 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
455 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456}
457
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000458u_int
459packet_get_encryption_key(u_char *key)
460{
461 if (key == NULL)
462 return (ssh1_keylen);
463 memcpy(key, ssh1_key, ssh1_keylen);
464 return (ssh1_keylen);
465}
466
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000467/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000469packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000471 u_char buf[9];
472 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473
Ben Lindstrom204e4882001-03-05 06:47:00 +0000474 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000475 len = compat20 ? 6 : 9;
476 memset(buf, 0, len - 1);
477 buf[len - 1] = type;
478 buffer_clear(&outgoing_packet);
479 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000480}
481
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000482/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483void
484packet_put_char(int value)
485{
Damien Miller95def091999-11-25 00:26:21 +1100486 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000487
Damien Miller95def091999-11-25 00:26:21 +1100488 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489}
Damien Miller4f7becb2006-03-26 14:10:14 +1100490
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000492packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493{
Damien Miller95def091999-11-25 00:26:21 +1100494 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495}
Damien Miller4f7becb2006-03-26 14:10:14 +1100496
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100498packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499{
Damien Miller95def091999-11-25 00:26:21 +1100500 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501}
Damien Miller4f7becb2006-03-26 14:10:14 +1100502
Damien Miller33b13562000-04-04 14:38:59 +1000503void
504packet_put_cstring(const char *str)
505{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000506 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000507}
Damien Miller4f7becb2006-03-26 14:10:14 +1100508
Damien Miller33b13562000-04-04 14:38:59 +1000509void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100510packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000511{
512 buffer_append(&outgoing_packet, buf, len);
513}
Damien Miller4f7becb2006-03-26 14:10:14 +1100514
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515void
Damien Miller95def091999-11-25 00:26:21 +1100516packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517{
Damien Miller95def091999-11-25 00:26:21 +1100518 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519}
Damien Miller4f7becb2006-03-26 14:10:14 +1100520
Damien Miller33b13562000-04-04 14:38:59 +1000521void
522packet_put_bignum2(BIGNUM * value)
523{
524 buffer_put_bignum2(&outgoing_packet, value);
525}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Damien Miller5428f641999-11-25 11:54:57 +1100527/*
528 * Finalizes and sends the packet. If the encryption key has been set,
529 * encrypts the packet before sending.
530 */
Damien Miller95def091999-11-25 00:26:21 +1100531
Ben Lindstrombba81212001-06-25 05:01:22 +0000532static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000533packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000535 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100536 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000537 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000538 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539
Damien Miller5428f641999-11-25 11:54:57 +1100540 /*
541 * If using packet compression, compress the payload of the outgoing
542 * packet.
543 */
Damien Miller95def091999-11-25 00:26:21 +1100544 if (packet_compression) {
545 buffer_clear(&compression_buffer);
546 /* Skip padding. */
547 buffer_consume(&outgoing_packet, 8);
548 /* padding */
549 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
550 buffer_compress(&outgoing_packet, &compression_buffer);
551 buffer_clear(&outgoing_packet);
552 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100553 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100554 }
555 /* Compute packet length without padding (add checksum, remove padding). */
556 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557
Damien Millere247cc42000-05-07 12:03:14 +1000558 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100559 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100560 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100561 cp = buffer_ptr(&outgoing_packet);
562 for (i = 0; i < padding; i++) {
563 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000564 rnd = arc4random();
565 cp[7 - i] = rnd & 0xff;
566 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100567 }
568 }
569 buffer_consume(&outgoing_packet, 8 - padding);
570
571 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100572 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000573 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100574 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100575 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576
577#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100578 fprintf(stderr, "packet_send plain: ");
579 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580#endif
581
Damien Miller95def091999-11-25 00:26:21 +1100582 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100583 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100584 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100585 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100586 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100587 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100588
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100590 fprintf(stderr, "encrypted: ");
591 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592#endif
593
Damien Miller95def091999-11-25 00:26:21 +1100594 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595
Damien Miller5428f641999-11-25 11:54:57 +1100596 /*
Damien Miller788f2122005-11-05 15:14:59 +1100597 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100598 * actually sent until packet_write_wait or packet_write_poll is
599 * called.
600 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000601}
602
Ben Lindstromf6027d32002-03-22 01:42:04 +0000603void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000604set_newkeys(int mode)
605{
606 Enc *enc;
607 Mac *mac;
608 Comp *comp;
609 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000610 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000611 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000612
Ben Lindstrom064496f2002-12-23 02:04:22 +0000613 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000614
Damien Miller963f6b22002-02-19 15:21:23 +1100615 if (mode == MODE_OUT) {
616 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000617 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000618 p_send.packets = p_send.blocks = 0;
619 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100620 } else {
621 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000622 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000623 p_read.packets = p_read.blocks = 0;
624 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100625 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000626 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000627 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100628 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000629 enc = &newkeys[mode]->enc;
630 mac = &newkeys[mode]->mac;
631 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000632 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000633 xfree(enc->name);
634 xfree(enc->iv);
635 xfree(enc->key);
636 xfree(mac->name);
637 xfree(mac->key);
638 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000639 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000640 }
641 newkeys[mode] = kex_get_newkeys(mode);
642 if (newkeys[mode] == NULL)
643 fatal("newkeys: no keys for mode %d", mode);
644 enc = &newkeys[mode]->enc;
645 mac = &newkeys[mode]->mac;
646 comp = &newkeys[mode]->comp;
647 if (mac->md != NULL)
648 mac->enabled = 1;
649 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100650 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000651 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000652 /* Deleting the keys does not gain extra security */
653 /* memset(enc->iv, 0, enc->block_size);
654 memset(enc->key, 0, enc->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000655 if ((comp->type == COMP_ZLIB ||
656 (comp->type == COMP_DELAYED && after_authentication)) &&
657 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000658 packet_init_compression();
659 if (mode == MODE_OUT)
660 buffer_compress_init_send(6);
661 else
662 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000663 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000664 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000665 /*
666 * The 2^(blocksize*2) limit is too expensive for 3DES,
667 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
668 */
669 if (enc->block_size >= 16)
670 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
671 else
672 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000673 if (rekey_limit)
674 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000675}
676
Damien Miller5428f641999-11-25 11:54:57 +1100677/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000678 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000679 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000680 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
681 */
682static void
683packet_enable_delayed_compress(void)
684{
685 Comp *comp = NULL;
686 int mode;
687
688 /*
689 * Remember that we are past the authentication step, so rekeying
690 * with COMP_DELAYED will turn on compression immediately.
691 */
692 after_authentication = 1;
693 for (mode = 0; mode < MODE_MAX; mode++) {
694 comp = &newkeys[mode]->comp;
695 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000696 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000697 if (mode == MODE_OUT)
698 buffer_compress_init_send(6);
699 else
700 buffer_compress_init_recv();
701 comp->enabled = 1;
702 }
703 }
704}
705
706/*
Damien Miller33b13562000-04-04 14:38:59 +1000707 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
708 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000709static void
Damien Millera5539d22003-04-09 20:50:06 +1000710packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000711{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000712 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100713 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000714 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100715 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000716 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000717 Enc *enc = NULL;
718 Mac *mac = NULL;
719 Comp *comp = NULL;
720 int block_size;
721
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000722 if (newkeys[MODE_OUT] != NULL) {
723 enc = &newkeys[MODE_OUT]->enc;
724 mac = &newkeys[MODE_OUT]->mac;
725 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000726 }
Damien Miller963f6b22002-02-19 15:21:23 +1100727 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000728
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000729 cp = buffer_ptr(&outgoing_packet);
730 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000731
732#ifdef PACKET_DEBUG
733 fprintf(stderr, "plain: ");
734 buffer_dump(&outgoing_packet);
735#endif
736
737 if (comp && comp->enabled) {
738 len = buffer_len(&outgoing_packet);
739 /* skip header, compress only payload */
740 buffer_consume(&outgoing_packet, 5);
741 buffer_clear(&compression_buffer);
742 buffer_compress(&outgoing_packet, &compression_buffer);
743 buffer_clear(&outgoing_packet);
744 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
745 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
746 buffer_len(&compression_buffer));
747 DBG(debug("compression: raw %d compressed %d", len,
748 buffer_len(&outgoing_packet)));
749 }
750
751 /* sizeof (packet_len + pad_len + payload) */
752 len = buffer_len(&outgoing_packet);
753
754 /*
755 * calc size of padding, alloc space, get random data,
756 * minimum padding is 4 bytes
757 */
758 padlen = block_size - (len % block_size);
759 if (padlen < 4)
760 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100761 if (extra_pad) {
762 /* will wrap if extra_pad+padlen > 255 */
763 extra_pad = roundup(extra_pad, block_size);
764 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000765 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100766 pad, len, padlen, extra_pad);
767 padlen += pad;
768 extra_pad = 0;
769 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100770 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100771 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000772 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000773 for (i = 0; i < padlen; i++) {
774 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000775 rnd = arc4random();
776 cp[i] = rnd & 0xff;
777 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000778 }
Damien Millere247cc42000-05-07 12:03:14 +1000779 } else {
780 /* clear padding */
781 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000782 }
783 /* packet_length includes payload, padding and padding length field */
784 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000785 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100786 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000787 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000788 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
789
790 /* compute MAC over seqnr and packet(length fields, payload, padding) */
791 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000792 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100793 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000794 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000795 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000796 }
797 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100798 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100799 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000800 buffer_len(&outgoing_packet));
801 /* append unencrypted MAC */
802 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100803 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000804#ifdef PACKET_DEBUG
805 fprintf(stderr, "encrypted: ");
806 buffer_dump(&output);
807#endif
Damien Miller4af51302000-04-16 11:18:38 +1000808 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000809 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000810 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000811 if (++p_send.packets == 0)
812 if (!(datafellows & SSH_BUG_NOREKEY))
813 fatal("XXX too many packets with same key");
814 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000815 buffer_clear(&outgoing_packet);
816
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000817 if (type == SSH2_MSG_NEWKEYS)
818 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000819 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
820 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000821}
822
Damien Millera5539d22003-04-09 20:50:06 +1000823static void
824packet_send2(void)
825{
826 static int rekeying = 0;
827 struct packet *p;
828 u_char type, *cp;
829
830 cp = buffer_ptr(&outgoing_packet);
831 type = cp[5];
832
833 /* during rekeying we can only send key exchange messages */
834 if (rekeying) {
835 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
836 (type <= SSH2_MSG_TRANSPORT_MAX))) {
837 debug("enqueue packet: %u", type);
838 p = xmalloc(sizeof(*p));
839 p->type = type;
840 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
841 buffer_init(&outgoing_packet);
842 TAILQ_INSERT_TAIL(&outgoing, p, next);
843 return;
844 }
845 }
846
847 /* rekeying starts with sending KEXINIT */
848 if (type == SSH2_MSG_KEXINIT)
849 rekeying = 1;
850
851 packet_send2_wrapped();
852
853 /* after a NEWKEYS message we can send the complete queue */
854 if (type == SSH2_MSG_NEWKEYS) {
855 rekeying = 0;
856 while ((p = TAILQ_FIRST(&outgoing))) {
857 type = p->type;
858 debug("dequeue packet: %u", type);
859 buffer_free(&outgoing_packet);
860 memcpy(&outgoing_packet, &p->payload,
861 sizeof(Buffer));
862 TAILQ_REMOVE(&outgoing, p, next);
863 xfree(p);
864 packet_send2_wrapped();
865 }
866 }
867}
868
Damien Miller33b13562000-04-04 14:38:59 +1000869void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000870packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000871{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000872 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000873 packet_send2();
874 else
875 packet_send1();
876 DBG(debug("packet_send done"));
877}
878
Damien Miller33b13562000-04-04 14:38:59 +1000879/*
Damien Miller5428f641999-11-25 11:54:57 +1100880 * Waits until a packet has been received, and returns its type. Note that
881 * no other data is processed until this returns, so this function should not
882 * be used during the interactive session.
883 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884
885int
Damien Millerdff50992002-01-22 23:16:32 +1100886packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887{
Damien Miller95def091999-11-25 00:26:21 +1100888 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000889 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100890 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000891 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Damien Miller07d86be2006-03-26 14:19:21 +1100893 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000894 sizeof(fd_mask));
895
Damien Miller95def091999-11-25 00:26:21 +1100896 /* Since we are blocking, ensure that all written packets have been sent. */
897 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898
Damien Miller95def091999-11-25 00:26:21 +1100899 /* Stay in the loop until we have received a complete packet. */
900 for (;;) {
901 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100902 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000903 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000904 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100905 || type == SSH_SMSG_FAILURE
906 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000907 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100908 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100909 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000910 if (type != SSH_MSG_NONE) {
911 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100912 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000913 }
Damien Miller5428f641999-11-25 11:54:57 +1100914 /*
915 * Otherwise, wait for some data to arrive, add it to the
916 * buffer, and try again.
917 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000918 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
919 sizeof(fd_mask));
920 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100921
Damien Miller95def091999-11-25 00:26:21 +1100922 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000923 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000924 (errno == EAGAIN || errno == EINTR))
925 ;
Damien Miller5428f641999-11-25 11:54:57 +1100926
Damien Miller95def091999-11-25 00:26:21 +1100927 /* Read data from the socket. */
928 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100929 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000930 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000931 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100932 }
Damien Miller95def091999-11-25 00:26:21 +1100933 if (len < 0)
934 fatal("Read from socket failed: %.100s", strerror(errno));
935 /* Append it to the buffer. */
936 packet_process_incoming(buf, len);
937 }
938 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939}
940
Damien Miller278f9072001-12-21 15:00:19 +1100941int
Damien Millerdff50992002-01-22 23:16:32 +1100942packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100943{
Damien Millerdff50992002-01-22 23:16:32 +1100944 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100945}
946
Damien Miller5428f641999-11-25 11:54:57 +1100947/*
948 * Waits until a packet has been received, verifies that its type matches
949 * that given, and gives a fatal error and exits if there is a mismatch.
950 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
952void
Damien Millerdff50992002-01-22 23:16:32 +1100953packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954{
Damien Miller95def091999-11-25 00:26:21 +1100955 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956
Damien Millerdff50992002-01-22 23:16:32 +1100957 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100958 if (type != expected_type)
959 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000960 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961}
962
963/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100964 * packet_process_incoming. If so, reads the packet; otherwise returns
965 * SSH_MSG_NONE. This does not wait for data from the connection.
966 *
967 * SSH_MSG_DISCONNECT is handled specially here. Also,
968 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
969 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100970 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Ben Lindstrombba81212001-06-25 05:01:22 +0000972static int
Damien Millerdff50992002-01-22 23:16:32 +1100973packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000975 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000976 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000977 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979 /* Check if input size is less than minimum packet size. */
980 if (buffer_len(&input) < 4 + 8)
981 return SSH_MSG_NONE;
982 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000983 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100984 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100985 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000986 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100987 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988
Damien Miller95def091999-11-25 00:26:21 +1100989 /* Check if the packet has been entirely received. */
990 if (buffer_len(&input) < 4 + padded_len)
991 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992
Damien Miller95def091999-11-25 00:26:21 +1100993 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller95def091999-11-25 00:26:21 +1100995 /* Consume packet length. */
996 buffer_consume(&input, 4);
997
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000998 /*
999 * Cryptographic attack detector for ssh
1000 * (C)1998 CORE-SDI, Buenos Aires Argentina
1001 * Ariel Futoransky(futo@core-sdi.com)
1002 */
Damien Miller963f6b22002-02-19 15:21:23 +11001003 if (!receive_context.plaintext &&
Damien Miller7cd45792006-03-26 14:11:39 +11001004 detect_attack(buffer_ptr(&input), padded_len) == DEATTACK_DETECTED)
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001005 packet_disconnect("crc32 compensation attack: network attack detected");
1006
1007 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001008 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001009 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001010 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001011
Damien Miller95def091999-11-25 00:26:21 +11001012 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013
1014#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001015 fprintf(stderr, "read_poll plain: ");
1016 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018
Damien Miller95def091999-11-25 00:26:21 +11001019 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001020 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001021 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001022
Damien Miller95def091999-11-25 00:26:21 +11001023 /* Skip padding. */
1024 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001025
Damien Miller95def091999-11-25 00:26:21 +11001026 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001027 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001028 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001029 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001031 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001032 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001033 if (checksum != stored_checksum)
1034 packet_disconnect("Corrupted check bytes on input.");
1035 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036
Damien Miller95def091999-11-25 00:26:21 +11001037 if (packet_compression) {
1038 buffer_clear(&compression_buffer);
1039 buffer_uncompress(&incoming_packet, &compression_buffer);
1040 buffer_clear(&incoming_packet);
1041 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001042 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001043 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001044 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001045 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1046 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001047 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048}
Damien Miller95def091999-11-25 00:26:21 +11001049
Ben Lindstrombba81212001-06-25 05:01:22 +00001050static int
Damien Millerdff50992002-01-22 23:16:32 +11001051packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001052{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001053 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001054 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001055 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001056 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001057 Enc *enc = NULL;
1058 Mac *mac = NULL;
1059 Comp *comp = NULL;
1060
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001061 if (newkeys[MODE_IN] != NULL) {
1062 enc = &newkeys[MODE_IN]->enc;
1063 mac = &newkeys[MODE_IN]->mac;
1064 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001065 }
1066 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001067 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001068
1069 if (packet_length == 0) {
1070 /*
1071 * check if input size is less than the cipher block size,
1072 * decrypt first block and extract length of incoming packet
1073 */
1074 if (buffer_len(&input) < block_size)
1075 return SSH_MSG_NONE;
1076 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001077 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001078 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001079 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001080 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001081 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001082 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001083#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001084 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001085#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001086 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001087 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001088 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001089 buffer_consume(&input, block_size);
1090 }
1091 /* we have a partial packet of block_size bytes */
1092 need = 4 + packet_length - block_size;
1093 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1094 need, maclen));
1095 if (need % block_size != 0)
1096 fatal("padding error: need %d block %d mod %d",
1097 need, block_size, need % block_size);
1098 /*
1099 * check if the entire packet has been received and
1100 * decrypt into incoming_packet
1101 */
1102 if (buffer_len(&input) < need + maclen)
1103 return SSH_MSG_NONE;
1104#ifdef PACKET_DEBUG
1105 fprintf(stderr, "read_poll enc/full: ");
1106 buffer_dump(&input);
1107#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001108 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001109 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001110 buffer_consume(&input, need);
1111 /*
1112 * compute MAC over seqnr and packet,
1113 * increment sequence number for incoming packet
1114 */
Damien Miller4af51302000-04-16 11:18:38 +10001115 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001116 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001117 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001118 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001119 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001120 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001121 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001122 buffer_consume(&input, mac->mac_len);
1123 }
Damien Miller278f9072001-12-21 15:00:19 +11001124 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001125 *seqnr_p = p_read.seqnr;
1126 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001127 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001128 if (++p_read.packets == 0)
1129 if (!(datafellows & SSH_BUG_NOREKEY))
1130 fatal("XXX too many packets with same key");
1131 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001132
1133 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001134 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001135 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001136 DBG(debug("input: padlen %d", padlen));
1137 if (padlen < 4)
1138 packet_disconnect("Corrupted padlen %d on input.", padlen);
1139
1140 /* skip packet size + padlen, discard padding */
1141 buffer_consume(&incoming_packet, 4 + 1);
1142 buffer_consume_end(&incoming_packet, padlen);
1143
1144 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1145 if (comp && comp->enabled) {
1146 buffer_clear(&compression_buffer);
1147 buffer_uncompress(&incoming_packet, &compression_buffer);
1148 buffer_clear(&incoming_packet);
1149 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1150 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001151 DBG(debug("input: len after de-compress %d",
1152 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001153 }
1154 /*
1155 * get packet type, implies consume.
1156 * return length of payload (without type field)
1157 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001158 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001159 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1160 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001161 if (type == SSH2_MSG_NEWKEYS)
1162 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001163 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1164 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001165#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001166 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001167 buffer_dump(&incoming_packet);
1168#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001169 /* reset for next packet */
1170 packet_length = 0;
1171 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001172}
1173
1174int
Damien Millerdff50992002-01-22 23:16:32 +11001175packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001176{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001177 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001178 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001179 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001180
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001181 for (;;) {
1182 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001183 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001184 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001185 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001186 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001187 case SSH2_MSG_IGNORE:
1188 break;
1189 case SSH2_MSG_DEBUG:
1190 packet_get_char();
1191 msg = packet_get_string(NULL);
1192 debug("Remote: %.900s", msg);
1193 xfree(msg);
1194 msg = packet_get_string(NULL);
1195 xfree(msg);
1196 break;
1197 case SSH2_MSG_DISCONNECT:
1198 reason = packet_get_int();
1199 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001200 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001201 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001202 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001203 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001204 break;
Damien Miller659811f2002-01-22 23:23:11 +11001205 case SSH2_MSG_UNIMPLEMENTED:
1206 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001207 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1208 seqnr);
Damien Miller659811f2002-01-22 23:23:11 +11001209 break;
Damien Miller33b13562000-04-04 14:38:59 +10001210 default:
1211 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001212 }
Damien Miller33b13562000-04-04 14:38:59 +10001213 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001214 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001215 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001216 case SSH_MSG_IGNORE:
1217 break;
1218 case SSH_MSG_DEBUG:
1219 msg = packet_get_string(NULL);
1220 debug("Remote: %.900s", msg);
1221 xfree(msg);
1222 break;
1223 case SSH_MSG_DISCONNECT:
1224 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001225 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001226 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001227 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001228 xfree(msg);
1229 break;
1230 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001231 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001232 DBG(debug("received packet type %d", type));
1233 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001234 }
Damien Miller33b13562000-04-04 14:38:59 +10001235 }
1236 }
1237}
1238
Damien Miller278f9072001-12-21 15:00:19 +11001239int
Damien Millerdff50992002-01-22 23:16:32 +11001240packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001241{
Damien Millerdff50992002-01-22 23:16:32 +11001242 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001243}
1244
Damien Miller5428f641999-11-25 11:54:57 +11001245/*
1246 * Buffers the given amount of input characters. This is intended to be used
1247 * together with packet_read_poll.
1248 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249
1250void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001251packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252{
Damien Miller95def091999-11-25 00:26:21 +11001253 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254}
1255
1256/* Returns a character from the packet. */
1257
Ben Lindstrom46c16222000-12-22 01:43:59 +00001258u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001259packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260{
Damien Miller95def091999-11-25 00:26:21 +11001261 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001262
Damien Miller95def091999-11-25 00:26:21 +11001263 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001264 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001265}
1266
1267/* Returns an integer from the packet data. */
1268
Ben Lindstrom46c16222000-12-22 01:43:59 +00001269u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001270packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001271{
Damien Miller95def091999-11-25 00:26:21 +11001272 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001273}
1274
Damien Miller5428f641999-11-25 11:54:57 +11001275/*
1276 * Returns an arbitrary precision integer from the packet data. The integer
1277 * must have been initialized before this call.
1278 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001279
1280void
Damien Millerd432ccf2002-01-22 23:14:44 +11001281packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001282{
Damien Miller76e1e362002-01-22 23:15:57 +11001283 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001284}
1285
Damien Miller33b13562000-04-04 14:38:59 +10001286void
Damien Millerd432ccf2002-01-22 23:14:44 +11001287packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001288{
Damien Miller76e1e362002-01-22 23:15:57 +11001289 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001290}
1291
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001292void *
Damien Millereccb9de2005-06-17 12:59:34 +10001293packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001294{
Damien Millereccb9de2005-06-17 12:59:34 +10001295 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001296
Damien Miller33b13562000-04-04 14:38:59 +10001297 if (length_ptr != NULL)
1298 *length_ptr = bytes;
1299 return buffer_ptr(&incoming_packet);
1300}
1301
Damien Miller4af51302000-04-16 11:18:38 +10001302int
1303packet_remaining(void)
1304{
1305 return buffer_len(&incoming_packet);
1306}
1307
Damien Miller5428f641999-11-25 11:54:57 +11001308/*
1309 * Returns a string from the packet data. The string is allocated using
1310 * xmalloc; it is the responsibility of the calling program to free it when
1311 * no longer needed. The length_ptr argument may be NULL, or point to an
1312 * integer into which the length of the string is stored.
1313 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001314
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001315void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001316packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001317{
Damien Miller95def091999-11-25 00:26:21 +11001318 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319}
1320
Damien Miller5428f641999-11-25 11:54:57 +11001321/*
1322 * Sends a diagnostic message from the server to the client. This message
1323 * can be sent at any time (but not while constructing another message). The
1324 * message is printed immediately, but only if the client is being executed
1325 * in verbose mode. These messages are primarily intended to ease debugging
1326 * authentication problems. The length of the formatted message must not
1327 * exceed 1024 bytes. This will automatically call packet_write_wait.
1328 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001329
1330void
Damien Miller95def091999-11-25 00:26:21 +11001331packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001332{
Damien Miller95def091999-11-25 00:26:21 +11001333 char buf[1024];
1334 va_list args;
1335
Ben Lindstroma14ee472000-12-07 01:24:58 +00001336 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1337 return;
1338
Damien Miller95def091999-11-25 00:26:21 +11001339 va_start(args, fmt);
1340 vsnprintf(buf, sizeof(buf), fmt, args);
1341 va_end(args);
1342
Damien Miller7c8af4f2000-05-01 08:24:07 +10001343 if (compat20) {
1344 packet_start(SSH2_MSG_DEBUG);
1345 packet_put_char(0); /* bool: always display */
1346 packet_put_cstring(buf);
1347 packet_put_cstring("");
1348 } else {
1349 packet_start(SSH_MSG_DEBUG);
1350 packet_put_cstring(buf);
1351 }
Damien Miller95def091999-11-25 00:26:21 +11001352 packet_send();
1353 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001354}
1355
Damien Miller5428f641999-11-25 11:54:57 +11001356/*
1357 * Logs the error plus constructs and sends a disconnect packet, closes the
1358 * connection, and exits. This function never returns. The error message
1359 * should not contain a newline. The length of the formatted message must
1360 * not exceed 1024 bytes.
1361 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001362
1363void
Damien Miller95def091999-11-25 00:26:21 +11001364packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365{
Damien Miller95def091999-11-25 00:26:21 +11001366 char buf[1024];
1367 va_list args;
1368 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001369
Damien Miller95def091999-11-25 00:26:21 +11001370 if (disconnecting) /* Guard against recursive invocations. */
1371 fatal("packet_disconnect called recursively.");
1372 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001373
Damien Miller5428f641999-11-25 11:54:57 +11001374 /*
1375 * Format the message. Note that the caller must make sure the
1376 * message is of limited size.
1377 */
Damien Miller95def091999-11-25 00:26:21 +11001378 va_start(args, fmt);
1379 vsnprintf(buf, sizeof(buf), fmt, args);
1380 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001381
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001382 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001383 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001384
Damien Miller95def091999-11-25 00:26:21 +11001385 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001386 if (compat20) {
1387 packet_start(SSH2_MSG_DISCONNECT);
1388 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1389 packet_put_cstring(buf);
1390 packet_put_cstring("");
1391 } else {
1392 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001393 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001394 }
Damien Miller95def091999-11-25 00:26:21 +11001395 packet_send();
1396 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001397
Damien Miller95def091999-11-25 00:26:21 +11001398 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001399 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400
Damien Miller95def091999-11-25 00:26:21 +11001401 /* Close the connection. */
1402 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001403 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404}
1405
Damien Miller5428f641999-11-25 11:54:57 +11001406/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407
1408void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001409packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001410{
Damien Miller95def091999-11-25 00:26:21 +11001411 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001412
Damien Miller95def091999-11-25 00:26:21 +11001413 if (len > 0) {
1414 len = write(connection_out, buffer_ptr(&output), len);
1415 if (len <= 0) {
1416 if (errno == EAGAIN)
1417 return;
1418 else
1419 fatal("Write failed: %.100s", strerror(errno));
1420 }
1421 buffer_consume(&output, len);
1422 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423}
1424
Damien Miller5428f641999-11-25 11:54:57 +11001425/*
1426 * Calls packet_write_poll repeatedly until all pending output data has been
1427 * written.
1428 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429
1430void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001431packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001433 fd_set *setp;
1434
Damien Miller07d86be2006-03-26 14:19:21 +11001435 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001436 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001437 packet_write_poll();
1438 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001439 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1440 sizeof(fd_mask));
1441 FD_SET(connection_out, setp);
1442 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001443 (errno == EAGAIN || errno == EINTR))
1444 ;
Damien Miller95def091999-11-25 00:26:21 +11001445 packet_write_poll();
1446 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001447 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001448}
1449
1450/* Returns true if there is buffered data to write to the connection. */
1451
1452int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001453packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454{
Damien Miller95def091999-11-25 00:26:21 +11001455 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456}
1457
1458/* Returns true if there is not too much data to write to the connection. */
1459
1460int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001461packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462{
Damien Miller95def091999-11-25 00:26:21 +11001463 if (interactive_mode)
1464 return buffer_len(&output) < 16384;
1465 else
1466 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467}
1468
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001469
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001470static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001471packet_set_tos(int interactive)
1472{
Damien Miller5924ceb2003-11-22 15:02:42 +11001473#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001474 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1475
1476 if (!packet_connection_is_on_socket() ||
1477 !packet_connection_is_ipv4())
1478 return;
1479 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1480 sizeof(tos)) < 0)
1481 error("setsockopt IP_TOS %d: %.100s:",
1482 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001483#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001484}
Ben Lindstroma7433982002-12-23 02:41:41 +00001485
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001486/* Informs that the current session is interactive. Sets IP flags for that. */
1487
1488void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001489packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001491 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001493 if (called)
1494 return;
1495 called = 1;
1496
Damien Miller95def091999-11-25 00:26:21 +11001497 /* Record that we are in interactive mode. */
1498 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499
Damien Miller34132e52000-01-14 15:45:46 +11001500 /* Only set socket options if using a socket. */
1501 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001502 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001503 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001504 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001505}
1506
1507/* Returns true if the current connection is interactive. */
1508
1509int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001510packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001511{
Damien Miller95def091999-11-25 00:26:21 +11001512 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001513}
Damien Miller6162d121999-11-21 13:23:52 +11001514
Darren Tucker1f8311c2004-05-13 16:39:33 +10001515int
Darren Tucker502d3842003-06-28 12:38:01 +10001516packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001517{
Damien Miller95def091999-11-25 00:26:21 +11001518 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001519
Damien Miller95def091999-11-25 00:26:21 +11001520 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001521 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001522 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001523 return -1;
1524 }
1525 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001526 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001527 return -1;
1528 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001529 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001530 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001531 max_packet_size = s;
1532 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001533}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001534
Damien Miller9f643902001-11-12 11:02:52 +11001535/* roundup current message to pad bytes */
1536void
1537packet_add_padding(u_char pad)
1538{
1539 extra_pad = pad;
1540}
1541
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001542/*
1543 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001544 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001545 * byte SSH_MSG_IGNORE
1546 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001547 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001548 * All implementations MUST understand (and ignore) this message at any
1549 * time (after receiving the protocol version). No implementation is
1550 * required to send them. This message can be used as an additional
1551 * protection measure against advanced traffic analysis techniques.
1552 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001553void
1554packet_send_ignore(int nbytes)
1555{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001556 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001557 int i;
1558
1559 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001560 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001561 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001562 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001563 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001564 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001565 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001566 }
1567}
Damien Millera5539d22003-04-09 20:50:06 +10001568
Darren Tucker1f8311c2004-05-13 16:39:33 +10001569#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001570int
1571packet_need_rekeying(void)
1572{
1573 if (datafellows & SSH_BUG_NOREKEY)
1574 return 0;
1575 return
1576 (p_send.packets > MAX_PACKETS) ||
1577 (p_read.packets > MAX_PACKETS) ||
1578 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1579 (max_blocks_in && (p_read.blocks > max_blocks_in));
1580}
1581
1582void
1583packet_set_rekey_limit(u_int32_t bytes)
1584{
1585 rekey_limit = bytes;
1586}
Damien Miller9786e6e2005-07-26 21:54:56 +10001587
1588void
1589packet_set_server(void)
1590{
1591 server_side = 1;
1592}
1593
1594void
1595packet_set_authenticated(void)
1596{
1597 after_authentication = 1;
1598}