blob: 90ad5ff6792bcf344b5dc3e634a004c246d6bd86 [file] [log] [blame]
Darren Tucker99bb7612008-06-13 22:02:50 +10001/* $OpenBSD: packet.c,v 1.155 2008/06/13 09:44:36 deraadt 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
Damien Miller8ec8c3e2006-07-10 20:35:38 +100050#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110051#include <netinet/ip.h>
Darren Tuckerdace2332006-09-22 19:22:17 +100052#include <arpa/inet.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 Miller58226f62008-03-07 18:33:30 +1100139int keep_alive_timeouts = 0;
140
Darren Tucker3fc464e2008-06-13 06:42:45 +1000141/* Set to the maximum time that we will wait to send or receive a packet */
142static int packet_timeout_ms = -1;
143
Damien Miller33b13562000-04-04 14:38:59 +1000144/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000145Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000146static struct packet_state {
147 u_int32_t seqnr;
148 u_int32_t packets;
149 u_int64_t blocks;
150} p_read, p_send;
151
152static u_int64_t max_blocks_in, max_blocks_out;
153static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000154
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000155/* Session key for protocol v1 */
156static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
157static u_int ssh1_keylen;
158
Damien Miller9f643902001-11-12 11:02:52 +1100159/* roundup current message to extra_pad bytes */
160static u_char extra_pad = 0;
161
Damien Millera5539d22003-04-09 20:50:06 +1000162struct packet {
163 TAILQ_ENTRY(packet) next;
164 u_char type;
165 Buffer payload;
166};
167TAILQ_HEAD(, packet) outgoing;
168
Damien Miller5428f641999-11-25 11:54:57 +1100169/*
170 * Sets the descriptors used for communication. Disables encryption until
171 * packet_set_encryption_key is called.
172 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173void
174packet_set_connection(int fd_in, int fd_out)
175{
Damien Miller874d77b2000-10-14 16:23:11 +1100176 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000177
Damien Miller874d77b2000-10-14 16:23:11 +1100178 if (none == NULL)
179 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100180 connection_in = fd_in;
181 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000182 cipher_init(&send_context, none, (const u_char *)"",
183 0, NULL, 0, CIPHER_ENCRYPT);
184 cipher_init(&receive_context, none, (const u_char *)"",
185 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000186 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100187 if (!initialized) {
188 initialized = 1;
189 buffer_init(&input);
190 buffer_init(&output);
191 buffer_init(&outgoing_packet);
192 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000193 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100194 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195}
196
Darren Tucker3fc464e2008-06-13 06:42:45 +1000197void
198packet_set_timeout(int timeout, int count)
199{
200 if (timeout == 0 || count == 0) {
201 packet_timeout_ms = -1;
202 return;
203 }
204 if ((INT_MAX / 1000) / count < timeout)
205 packet_timeout_ms = INT_MAX;
206 else
207 packet_timeout_ms = timeout * count * 1000;
208}
209
Damien Miller34132e52000-01-14 15:45:46 +1100210/* Returns 1 if remote host is connected via socket, 0 if not. */
211
212int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000213packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100214{
215 struct sockaddr_storage from, to;
216 socklen_t fromlen, tolen;
217
218 /* filedescriptors in and out are the same, so it's a socket */
219 if (connection_in == connection_out)
220 return 1;
221 fromlen = sizeof(from);
222 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100223 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100224 return 0;
225 tolen = sizeof(to);
226 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100227 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100228 return 0;
229 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
230 return 0;
231 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
232 return 0;
233 return 1;
234}
235
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000236/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000237 * Exports an IV from the CipherContext required to export the key
238 * state back from the unprivileged child to the privileged parent
239 * process.
240 */
241
242void
243packet_get_keyiv(int mode, u_char *iv, u_int len)
244{
245 CipherContext *cc;
246
247 if (mode == MODE_OUT)
248 cc = &send_context;
249 else
250 cc = &receive_context;
251
252 cipher_get_keyiv(cc, iv, len);
253}
254
255int
256packet_get_keycontext(int mode, u_char *dat)
257{
258 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000259
Ben Lindstromf6027d32002-03-22 01:42:04 +0000260 if (mode == MODE_OUT)
261 cc = &send_context;
262 else
263 cc = &receive_context;
264
265 return (cipher_get_keycontext(cc, dat));
266}
267
268void
269packet_set_keycontext(int mode, u_char *dat)
270{
271 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000272
Ben Lindstromf6027d32002-03-22 01:42:04 +0000273 if (mode == MODE_OUT)
274 cc = &send_context;
275 else
276 cc = &receive_context;
277
278 cipher_set_keycontext(cc, dat);
279}
280
281int
282packet_get_keyiv_len(int mode)
283{
284 CipherContext *cc;
285
286 if (mode == MODE_OUT)
287 cc = &send_context;
288 else
289 cc = &receive_context;
290
291 return (cipher_get_keyiv_len(cc));
292}
Damien Miller4f7becb2006-03-26 14:10:14 +1100293
Ben Lindstromf6027d32002-03-22 01:42:04 +0000294void
295packet_set_iv(int mode, u_char *dat)
296{
297 CipherContext *cc;
298
299 if (mode == MODE_OUT)
300 cc = &send_context;
301 else
302 cc = &receive_context;
303
304 cipher_set_keyiv(cc, dat);
305}
Damien Miller4f7becb2006-03-26 14:10:14 +1100306
Ben Lindstromf6027d32002-03-22 01:42:04 +0000307int
Damien Miller2b92d322003-06-11 22:05:06 +1000308packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000309{
310 return (cipher_get_number(receive_context.cipher));
311}
312
Damien Millera5539d22003-04-09 20:50:06 +1000313void
314packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000315{
Damien Millera5539d22003-04-09 20:50:06 +1000316 struct packet_state *state;
317
318 state = (mode == MODE_IN) ? &p_read : &p_send;
319 *seqnr = state->seqnr;
320 *blocks = state->blocks;
321 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000322}
323
324void
Damien Millera5539d22003-04-09 20:50:06 +1000325packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000326{
Damien Millera5539d22003-04-09 20:50:06 +1000327 struct packet_state *state;
328
329 state = (mode == MODE_IN) ? &p_read : &p_send;
330 state->seqnr = seqnr;
331 state->blocks = blocks;
332 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000333}
334
Damien Miller34132e52000-01-14 15:45:46 +1100335/* returns 1 if connection is via ipv4 */
336
337int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000338packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100339{
340 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100341 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100342
343 memset(&to, 0, sizeof(to));
344 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
345 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000346 if (to.ss_family == AF_INET)
347 return 1;
348#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100349 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000350 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
351 return 1;
352#endif
353 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100354}
355
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356/* Sets the connection into non-blocking mode. */
357
358void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000359packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360{
Damien Miller95def091999-11-25 00:26:21 +1100361 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000362 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
Damien Miller232711f2004-06-15 10:35:30 +1000364 if (connection_out != connection_in)
365 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366}
367
368/* Returns the socket used for reading. */
369
370int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000371packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372{
Damien Miller95def091999-11-25 00:26:21 +1100373 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374}
375
376/* Returns the descriptor used for writing. */
377
378int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000379packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380{
Damien Miller95def091999-11-25 00:26:21 +1100381 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382}
383
384/* Closes the connection and clears and frees internal data structures. */
385
386void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000387packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388{
Damien Miller95def091999-11-25 00:26:21 +1100389 if (!initialized)
390 return;
391 initialized = 0;
392 if (connection_in == connection_out) {
393 shutdown(connection_out, SHUT_RDWR);
394 close(connection_out);
395 } else {
396 close(connection_in);
397 close(connection_out);
398 }
399 buffer_free(&input);
400 buffer_free(&output);
401 buffer_free(&outgoing_packet);
402 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000403 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100404 buffer_free(&compression_buffer);
405 buffer_compress_uninit();
406 }
Damien Miller963f6b22002-02-19 15:21:23 +1100407 cipher_cleanup(&send_context);
408 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409}
410
411/* Sets remote side protocol flags. */
412
413void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000414packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415{
Damien Miller95def091999-11-25 00:26:21 +1100416 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417}
418
419/* Returns the remote protocol flags set earlier by the above function. */
420
Ben Lindstrom46c16222000-12-22 01:43:59 +0000421u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000422packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423{
Damien Miller95def091999-11-25 00:26:21 +1100424 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425}
426
Damien Miller5428f641999-11-25 11:54:57 +1100427/*
428 * Starts packet compression from the next packet on in both directions.
429 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
430 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Ben Lindstrombba81212001-06-25 05:01:22 +0000432static void
433packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000434{
435 if (compression_buffer_ready == 1)
436 return;
437 compression_buffer_ready = 1;
438 buffer_init(&compression_buffer);
439}
440
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441void
442packet_start_compression(int level)
443{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000444 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100445 fatal("Compression already enabled.");
446 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000447 packet_init_compression();
448 buffer_compress_init_send(level);
449 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450}
451
Damien Miller5428f641999-11-25 11:54:57 +1100452/*
Damien Miller5428f641999-11-25 11:54:57 +1100453 * Causes any further packets to be encrypted using the given key. The same
454 * key is used for both sending and reception. However, both directions are
455 * encrypted independently of each other.
456 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000457
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000459packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100460 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461{
Damien Miller874d77b2000-10-14 16:23:11 +1100462 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000463
Damien Miller874d77b2000-10-14 16:23:11 +1100464 if (cipher == NULL)
465 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000466 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100467 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000468 if (keylen > SSH_SESSION_KEY_LENGTH)
469 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
470 memcpy(ssh1_key, key, keylen);
471 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100472 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
473 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474}
475
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000476u_int
477packet_get_encryption_key(u_char *key)
478{
479 if (key == NULL)
480 return (ssh1_keylen);
481 memcpy(key, ssh1_key, ssh1_keylen);
482 return (ssh1_keylen);
483}
484
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000485/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000487packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000489 u_char buf[9];
490 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
Ben Lindstrom204e4882001-03-05 06:47:00 +0000492 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000493 len = compat20 ? 6 : 9;
494 memset(buf, 0, len - 1);
495 buf[len - 1] = type;
496 buffer_clear(&outgoing_packet);
497 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000498}
499
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000500/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501void
502packet_put_char(int value)
503{
Damien Miller95def091999-11-25 00:26:21 +1100504 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000505
Damien Miller95def091999-11-25 00:26:21 +1100506 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507}
Damien Miller4f7becb2006-03-26 14:10:14 +1100508
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000510packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511{
Damien Miller95def091999-11-25 00:26:21 +1100512 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513}
Damien Miller4f7becb2006-03-26 14:10:14 +1100514
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100516packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517{
Damien Miller95def091999-11-25 00:26:21 +1100518 buffer_put_string(&outgoing_packet, buf, len);
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_cstring(const char *str)
523{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000524 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000525}
Damien Miller4f7becb2006-03-26 14:10:14 +1100526
Damien Miller33b13562000-04-04 14:38:59 +1000527void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100528packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000529{
530 buffer_append(&outgoing_packet, buf, len);
531}
Damien Miller4f7becb2006-03-26 14:10:14 +1100532
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533void
Damien Miller95def091999-11-25 00:26:21 +1100534packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535{
Damien Miller95def091999-11-25 00:26:21 +1100536 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537}
Damien Miller4f7becb2006-03-26 14:10:14 +1100538
Damien Miller33b13562000-04-04 14:38:59 +1000539void
540packet_put_bignum2(BIGNUM * value)
541{
542 buffer_put_bignum2(&outgoing_packet, value);
543}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544
Damien Miller5428f641999-11-25 11:54:57 +1100545/*
546 * Finalizes and sends the packet. If the encryption key has been set,
547 * encrypts the packet before sending.
548 */
Damien Miller95def091999-11-25 00:26:21 +1100549
Ben Lindstrombba81212001-06-25 05:01:22 +0000550static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000551packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000553 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100554 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000555 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000556 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557
Damien Miller5428f641999-11-25 11:54:57 +1100558 /*
559 * If using packet compression, compress the payload of the outgoing
560 * packet.
561 */
Damien Miller95def091999-11-25 00:26:21 +1100562 if (packet_compression) {
563 buffer_clear(&compression_buffer);
564 /* Skip padding. */
565 buffer_consume(&outgoing_packet, 8);
566 /* padding */
567 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
568 buffer_compress(&outgoing_packet, &compression_buffer);
569 buffer_clear(&outgoing_packet);
570 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100571 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100572 }
573 /* Compute packet length without padding (add checksum, remove padding). */
574 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Damien Millere247cc42000-05-07 12:03:14 +1000576 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100577 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100578 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100579 cp = buffer_ptr(&outgoing_packet);
580 for (i = 0; i < padding; i++) {
581 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000582 rnd = arc4random();
583 cp[7 - i] = rnd & 0xff;
584 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100585 }
586 }
587 buffer_consume(&outgoing_packet, 8 - padding);
588
589 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100590 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000591 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100592 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100593 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594
595#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100596 fprintf(stderr, "packet_send plain: ");
597 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000598#endif
599
Damien Miller95def091999-11-25 00:26:21 +1100600 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100601 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100602 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100603 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100604 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100605 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100606
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100608 fprintf(stderr, "encrypted: ");
609 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610#endif
611
Damien Miller95def091999-11-25 00:26:21 +1100612 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Damien Miller5428f641999-11-25 11:54:57 +1100614 /*
Damien Miller788f2122005-11-05 15:14:59 +1100615 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100616 * actually sent until packet_write_wait or packet_write_poll is
617 * called.
618 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619}
620
Ben Lindstromf6027d32002-03-22 01:42:04 +0000621void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000622set_newkeys(int mode)
623{
624 Enc *enc;
625 Mac *mac;
626 Comp *comp;
627 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000628 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000629 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000630
Ben Lindstrom064496f2002-12-23 02:04:22 +0000631 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000632
Damien Miller963f6b22002-02-19 15:21:23 +1100633 if (mode == MODE_OUT) {
634 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000635 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000636 p_send.packets = p_send.blocks = 0;
637 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100638 } else {
639 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000640 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000641 p_read.packets = p_read.blocks = 0;
642 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100643 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000644 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000645 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100646 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000647 enc = &newkeys[mode]->enc;
648 mac = &newkeys[mode]->mac;
649 comp = &newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000650 mac_clear(mac);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000651 xfree(enc->name);
652 xfree(enc->iv);
653 xfree(enc->key);
654 xfree(mac->name);
655 xfree(mac->key);
656 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000657 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000658 }
659 newkeys[mode] = kex_get_newkeys(mode);
660 if (newkeys[mode] == NULL)
661 fatal("newkeys: no keys for mode %d", mode);
662 enc = &newkeys[mode]->enc;
663 mac = &newkeys[mode]->mac;
664 comp = &newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000665 if (mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000666 mac->enabled = 1;
667 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100668 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000669 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000670 /* Deleting the keys does not gain extra security */
671 /* memset(enc->iv, 0, enc->block_size);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000672 memset(enc->key, 0, enc->key_len);
673 memset(mac->key, 0, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000674 if ((comp->type == COMP_ZLIB ||
675 (comp->type == COMP_DELAYED && after_authentication)) &&
676 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000677 packet_init_compression();
678 if (mode == MODE_OUT)
679 buffer_compress_init_send(6);
680 else
681 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000682 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000683 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000684 /*
685 * The 2^(blocksize*2) limit is too expensive for 3DES,
686 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
687 */
688 if (enc->block_size >= 16)
689 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
690 else
691 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000692 if (rekey_limit)
693 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000694}
695
Damien Miller5428f641999-11-25 11:54:57 +1100696/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000697 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000698 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000699 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
700 */
701static void
702packet_enable_delayed_compress(void)
703{
704 Comp *comp = NULL;
705 int mode;
706
707 /*
708 * Remember that we are past the authentication step, so rekeying
709 * with COMP_DELAYED will turn on compression immediately.
710 */
711 after_authentication = 1;
712 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000713 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
714 if (newkeys[mode] == NULL)
715 continue;
Damien Miller9786e6e2005-07-26 21:54:56 +1000716 comp = &newkeys[mode]->comp;
717 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000718 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000719 if (mode == MODE_OUT)
720 buffer_compress_init_send(6);
721 else
722 buffer_compress_init_recv();
723 comp->enabled = 1;
724 }
725 }
726}
727
728/*
Damien Miller33b13562000-04-04 14:38:59 +1000729 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
730 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000731static void
Damien Millera5539d22003-04-09 20:50:06 +1000732packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000733{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000734 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100735 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000736 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100737 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000738 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000739 Enc *enc = NULL;
740 Mac *mac = NULL;
741 Comp *comp = NULL;
742 int block_size;
743
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000744 if (newkeys[MODE_OUT] != NULL) {
745 enc = &newkeys[MODE_OUT]->enc;
746 mac = &newkeys[MODE_OUT]->mac;
747 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000748 }
Damien Miller963f6b22002-02-19 15:21:23 +1100749 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000750
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000751 cp = buffer_ptr(&outgoing_packet);
752 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000753
754#ifdef PACKET_DEBUG
755 fprintf(stderr, "plain: ");
756 buffer_dump(&outgoing_packet);
757#endif
758
759 if (comp && comp->enabled) {
760 len = buffer_len(&outgoing_packet);
761 /* skip header, compress only payload */
762 buffer_consume(&outgoing_packet, 5);
763 buffer_clear(&compression_buffer);
764 buffer_compress(&outgoing_packet, &compression_buffer);
765 buffer_clear(&outgoing_packet);
766 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
767 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
768 buffer_len(&compression_buffer));
769 DBG(debug("compression: raw %d compressed %d", len,
770 buffer_len(&outgoing_packet)));
771 }
772
773 /* sizeof (packet_len + pad_len + payload) */
774 len = buffer_len(&outgoing_packet);
775
776 /*
777 * calc size of padding, alloc space, get random data,
778 * minimum padding is 4 bytes
779 */
780 padlen = block_size - (len % block_size);
781 if (padlen < 4)
782 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100783 if (extra_pad) {
784 /* will wrap if extra_pad+padlen > 255 */
785 extra_pad = roundup(extra_pad, block_size);
786 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000787 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100788 pad, len, padlen, extra_pad);
789 padlen += pad;
790 extra_pad = 0;
791 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100792 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100793 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000794 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000795 for (i = 0; i < padlen; i++) {
796 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000797 rnd = arc4random();
798 cp[i] = rnd & 0xff;
799 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000800 }
Damien Millere247cc42000-05-07 12:03:14 +1000801 } else {
802 /* clear padding */
803 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000804 }
805 /* packet_length includes payload, padding and padding length field */
806 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000807 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100808 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000809 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000810 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
811
812 /* compute MAC over seqnr and packet(length fields, payload, padding) */
813 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000814 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100815 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000816 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000817 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000818 }
819 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100820 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100821 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000822 buffer_len(&outgoing_packet));
823 /* append unencrypted MAC */
824 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100825 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000826#ifdef PACKET_DEBUG
827 fprintf(stderr, "encrypted: ");
828 buffer_dump(&output);
829#endif
Damien Miller4af51302000-04-16 11:18:38 +1000830 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000831 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000832 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000833 if (++p_send.packets == 0)
834 if (!(datafellows & SSH_BUG_NOREKEY))
835 fatal("XXX too many packets with same key");
836 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000837 buffer_clear(&outgoing_packet);
838
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000839 if (type == SSH2_MSG_NEWKEYS)
840 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000841 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
842 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000843}
844
Damien Millera5539d22003-04-09 20:50:06 +1000845static void
846packet_send2(void)
847{
848 static int rekeying = 0;
849 struct packet *p;
850 u_char type, *cp;
851
852 cp = buffer_ptr(&outgoing_packet);
853 type = cp[5];
854
855 /* during rekeying we can only send key exchange messages */
856 if (rekeying) {
857 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
858 (type <= SSH2_MSG_TRANSPORT_MAX))) {
859 debug("enqueue packet: %u", type);
860 p = xmalloc(sizeof(*p));
861 p->type = type;
862 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
863 buffer_init(&outgoing_packet);
864 TAILQ_INSERT_TAIL(&outgoing, p, next);
865 return;
866 }
867 }
868
869 /* rekeying starts with sending KEXINIT */
870 if (type == SSH2_MSG_KEXINIT)
871 rekeying = 1;
872
873 packet_send2_wrapped();
874
875 /* after a NEWKEYS message we can send the complete queue */
876 if (type == SSH2_MSG_NEWKEYS) {
877 rekeying = 0;
878 while ((p = TAILQ_FIRST(&outgoing))) {
879 type = p->type;
880 debug("dequeue packet: %u", type);
881 buffer_free(&outgoing_packet);
882 memcpy(&outgoing_packet, &p->payload,
883 sizeof(Buffer));
884 TAILQ_REMOVE(&outgoing, p, next);
885 xfree(p);
886 packet_send2_wrapped();
887 }
888 }
889}
890
Damien Miller33b13562000-04-04 14:38:59 +1000891void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000892packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000893{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000894 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000895 packet_send2();
896 else
897 packet_send1();
898 DBG(debug("packet_send done"));
899}
900
Damien Miller33b13562000-04-04 14:38:59 +1000901/*
Damien Miller5428f641999-11-25 11:54:57 +1100902 * Waits until a packet has been received, and returns its type. Note that
903 * no other data is processed until this returns, so this function should not
904 * be used during the interactive session.
905 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906
907int
Damien Millerdff50992002-01-22 23:16:32 +1100908packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000909{
Darren Tucker3fc464e2008-06-13 06:42:45 +1000910 int type, len, ret, ms_remain;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000911 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100912 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +1000913 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Darren Tucker99bb7612008-06-13 22:02:50 +1000915 DBG(debug("packet_read()"));
916
Damien Miller07d86be2006-03-26 14:19:21 +1100917 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000918 sizeof(fd_mask));
919
Damien Miller95def091999-11-25 00:26:21 +1100920 /* Since we are blocking, ensure that all written packets have been sent. */
921 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922
Damien Miller95def091999-11-25 00:26:21 +1100923 /* Stay in the loop until we have received a complete packet. */
924 for (;;) {
925 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100926 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000927 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000928 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100929 || type == SSH_SMSG_FAILURE
930 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000931 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100932 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100933 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000934 if (type != SSH_MSG_NONE) {
935 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100936 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000937 }
Damien Miller5428f641999-11-25 11:54:57 +1100938 /*
939 * Otherwise, wait for some data to arrive, add it to the
940 * buffer, and try again.
941 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000942 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
943 sizeof(fd_mask));
944 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100945
Darren Tucker3fc464e2008-06-13 06:42:45 +1000946 if (packet_timeout_ms > 0) {
947 ms_remain = packet_timeout_ms;
948 timeoutp = &timeout;
949 }
Damien Miller95def091999-11-25 00:26:21 +1100950 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +1000951 for (;;) {
952 if (packet_timeout_ms != -1) {
953 ms_to_timeval(&timeout, ms_remain);
954 gettimeofday(&start, NULL);
955 }
956 if ((ret = select(connection_in + 1, setp, NULL,
957 NULL, timeoutp)) >= 0)
958 break;
959 if (errno != EAGAIN && errno != EINTR)
960 break;
961 if (packet_timeout_ms == -1)
962 continue;
963 ms_subtract_diff(&start, &ms_remain);
964 if (ms_remain <= 0) {
965 ret = 0;
966 break;
967 }
968 }
969 if (ret == 0) {
970 logit("Connection to %.200s timed out while "
971 "waiting to read", get_remote_ipaddr());
972 cleanup_exit(255);
973 }
Damien Miller95def091999-11-25 00:26:21 +1100974 /* Read data from the socket. */
975 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100976 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000977 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000978 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100979 }
Damien Miller95def091999-11-25 00:26:21 +1100980 if (len < 0)
981 fatal("Read from socket failed: %.100s", strerror(errno));
982 /* Append it to the buffer. */
983 packet_process_incoming(buf, len);
984 }
985 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986}
987
Damien Miller278f9072001-12-21 15:00:19 +1100988int
Damien Millerdff50992002-01-22 23:16:32 +1100989packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100990{
Damien Millerdff50992002-01-22 23:16:32 +1100991 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100992}
993
Damien Miller5428f641999-11-25 11:54:57 +1100994/*
995 * Waits until a packet has been received, verifies that its type matches
996 * that given, and gives a fatal error and exits if there is a mismatch.
997 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998
999void
Damien Millerdff50992002-01-22 23:16:32 +11001000packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001{
Damien Miller95def091999-11-25 00:26:21 +11001002 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003
Damien Millerdff50992002-01-22 23:16:32 +11001004 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001005 if (type != expected_type)
1006 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001007 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008}
1009
1010/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001011 * packet_process_incoming. If so, reads the packet; otherwise returns
1012 * SSH_MSG_NONE. This does not wait for data from the connection.
1013 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001014 * SSH_MSG_DISCONNECT is handled specially here. Also,
1015 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1016 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001017 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018
Ben Lindstrombba81212001-06-25 05:01:22 +00001019static int
Damien Millerdff50992002-01-22 23:16:32 +11001020packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001021{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001022 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001023 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001024 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001025
Damien Miller95def091999-11-25 00:26:21 +11001026 /* Check if input size is less than minimum packet size. */
1027 if (buffer_len(&input) < 4 + 8)
1028 return SSH_MSG_NONE;
1029 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001030 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +11001031 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001032 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001033 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +11001034 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035
Damien Miller95def091999-11-25 00:26:21 +11001036 /* Check if the packet has been entirely received. */
1037 if (buffer_len(&input) < 4 + padded_len)
1038 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039
Damien Miller95def091999-11-25 00:26:21 +11001040 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041
Damien Miller95def091999-11-25 00:26:21 +11001042 /* Consume packet length. */
1043 buffer_consume(&input, 4);
1044
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001045 /*
1046 * Cryptographic attack detector for ssh
1047 * (C)1998 CORE-SDI, Buenos Aires Argentina
1048 * Ariel Futoransky(futo@core-sdi.com)
1049 */
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001050 if (!receive_context.plaintext) {
1051 switch (detect_attack(buffer_ptr(&input), padded_len)) {
1052 case DEATTACK_DETECTED:
1053 packet_disconnect("crc32 compensation attack: "
1054 "network attack detected");
1055 case DEATTACK_DOS_DETECTED:
1056 packet_disconnect("deattack denial of "
1057 "service detected");
1058 }
1059 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001060
1061 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001062 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001063 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001064 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001065
Damien Miller95def091999-11-25 00:26:21 +11001066 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
1068#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001069 fprintf(stderr, "read_poll plain: ");
1070 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001071#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072
Damien Miller95def091999-11-25 00:26:21 +11001073 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001074 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001075 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076
Damien Miller95def091999-11-25 00:26:21 +11001077 /* Skip padding. */
1078 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001079
Damien Miller95def091999-11-25 00:26:21 +11001080 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001081 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001082 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001083 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001084
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001085 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001086 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001087 if (checksum != stored_checksum)
1088 packet_disconnect("Corrupted check bytes on input.");
1089 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090
Damien Miller95def091999-11-25 00:26:21 +11001091 if (packet_compression) {
1092 buffer_clear(&compression_buffer);
1093 buffer_uncompress(&incoming_packet, &compression_buffer);
1094 buffer_clear(&incoming_packet);
1095 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001096 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001097 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001098 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001099 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1100 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001101 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102}
Damien Miller95def091999-11-25 00:26:21 +11001103
Ben Lindstrombba81212001-06-25 05:01:22 +00001104static int
Damien Millerdff50992002-01-22 23:16:32 +11001105packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001106{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001107 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001108 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001109 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001110 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001111 Enc *enc = NULL;
1112 Mac *mac = NULL;
1113 Comp *comp = NULL;
1114
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001115 if (newkeys[MODE_IN] != NULL) {
1116 enc = &newkeys[MODE_IN]->enc;
1117 mac = &newkeys[MODE_IN]->mac;
1118 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001119 }
1120 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001121 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001122
1123 if (packet_length == 0) {
1124 /*
1125 * check if input size is less than the cipher block size,
1126 * decrypt first block and extract length of incoming packet
1127 */
1128 if (buffer_len(&input) < block_size)
1129 return SSH_MSG_NONE;
1130 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001131 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001132 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001133 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001134 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001135 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001136 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001137#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001138 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001139#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001140 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001141 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001142 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001143 buffer_consume(&input, block_size);
1144 }
1145 /* we have a partial packet of block_size bytes */
1146 need = 4 + packet_length - block_size;
1147 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1148 need, maclen));
1149 if (need % block_size != 0)
1150 fatal("padding error: need %d block %d mod %d",
1151 need, block_size, need % block_size);
1152 /*
1153 * check if the entire packet has been received and
1154 * decrypt into incoming_packet
1155 */
1156 if (buffer_len(&input) < need + maclen)
1157 return SSH_MSG_NONE;
1158#ifdef PACKET_DEBUG
1159 fprintf(stderr, "read_poll enc/full: ");
1160 buffer_dump(&input);
1161#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001162 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001163 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001164 buffer_consume(&input, need);
1165 /*
1166 * compute MAC over seqnr and packet,
1167 * increment sequence number for incoming packet
1168 */
Damien Miller4af51302000-04-16 11:18:38 +10001169 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001170 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001171 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001172 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001173 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001174 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001175 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001176 buffer_consume(&input, mac->mac_len);
1177 }
Damien Miller278f9072001-12-21 15:00:19 +11001178 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001179 *seqnr_p = p_read.seqnr;
1180 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001181 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001182 if (++p_read.packets == 0)
1183 if (!(datafellows & SSH_BUG_NOREKEY))
1184 fatal("XXX too many packets with same key");
1185 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001186
1187 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001188 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001189 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001190 DBG(debug("input: padlen %d", padlen));
1191 if (padlen < 4)
1192 packet_disconnect("Corrupted padlen %d on input.", padlen);
1193
1194 /* skip packet size + padlen, discard padding */
1195 buffer_consume(&incoming_packet, 4 + 1);
1196 buffer_consume_end(&incoming_packet, padlen);
1197
1198 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1199 if (comp && comp->enabled) {
1200 buffer_clear(&compression_buffer);
1201 buffer_uncompress(&incoming_packet, &compression_buffer);
1202 buffer_clear(&incoming_packet);
1203 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1204 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001205 DBG(debug("input: len after de-compress %d",
1206 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001207 }
1208 /*
1209 * get packet type, implies consume.
1210 * return length of payload (without type field)
1211 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001212 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001213 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1214 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001215 if (type == SSH2_MSG_NEWKEYS)
1216 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001217 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1218 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001219#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001220 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001221 buffer_dump(&incoming_packet);
1222#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001223 /* reset for next packet */
1224 packet_length = 0;
1225 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001226}
1227
1228int
Damien Millerdff50992002-01-22 23:16:32 +11001229packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001230{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001231 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001232 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001233 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001234
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001235 for (;;) {
1236 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001237 type = packet_read_poll2(seqnr_p);
Darren Tucker136e56f2008-06-08 12:49:30 +10001238 if (type) {
1239 keep_alive_timeouts = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001240 DBG(debug("received packet type %d", type));
Darren Tucker136e56f2008-06-08 12:49:30 +10001241 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001242 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001243 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001244 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001245 break;
Damien Miller33b13562000-04-04 14:38:59 +10001246 case SSH2_MSG_DEBUG:
1247 packet_get_char();
1248 msg = packet_get_string(NULL);
1249 debug("Remote: %.900s", msg);
1250 xfree(msg);
1251 msg = packet_get_string(NULL);
1252 xfree(msg);
1253 break;
1254 case SSH2_MSG_DISCONNECT:
1255 reason = packet_get_int();
1256 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001257 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001258 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001259 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001260 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001261 break;
Damien Miller659811f2002-01-22 23:23:11 +11001262 case SSH2_MSG_UNIMPLEMENTED:
1263 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001264 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1265 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001266 break;
Damien Miller33b13562000-04-04 14:38:59 +10001267 default:
1268 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001269 }
Damien Miller33b13562000-04-04 14:38:59 +10001270 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001271 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001272 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001273 case SSH_MSG_IGNORE:
1274 break;
1275 case SSH_MSG_DEBUG:
1276 msg = packet_get_string(NULL);
1277 debug("Remote: %.900s", msg);
1278 xfree(msg);
1279 break;
1280 case SSH_MSG_DISCONNECT:
1281 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001282 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001283 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001284 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001285 break;
1286 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001287 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001288 DBG(debug("received packet type %d", type));
1289 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001290 }
Damien Miller33b13562000-04-04 14:38:59 +10001291 }
1292 }
1293}
1294
Damien Miller278f9072001-12-21 15:00:19 +11001295int
Damien Millerdff50992002-01-22 23:16:32 +11001296packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001297{
Damien Millerdff50992002-01-22 23:16:32 +11001298 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001299}
1300
Damien Miller5428f641999-11-25 11:54:57 +11001301/*
1302 * Buffers the given amount of input characters. This is intended to be used
1303 * together with packet_read_poll.
1304 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001305
1306void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001307packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001308{
Damien Miller95def091999-11-25 00:26:21 +11001309 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310}
1311
1312/* Returns a character from the packet. */
1313
Ben Lindstrom46c16222000-12-22 01:43:59 +00001314u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001315packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316{
Damien Miller95def091999-11-25 00:26:21 +11001317 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001318
Damien Miller95def091999-11-25 00:26:21 +11001319 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001320 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001321}
1322
1323/* Returns an integer from the packet data. */
1324
Ben Lindstrom46c16222000-12-22 01:43:59 +00001325u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001326packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001327{
Damien Miller95def091999-11-25 00:26:21 +11001328 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001329}
1330
Damien Miller5428f641999-11-25 11:54:57 +11001331/*
1332 * Returns an arbitrary precision integer from the packet data. The integer
1333 * must have been initialized before this call.
1334 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001335
1336void
Damien Millerd432ccf2002-01-22 23:14:44 +11001337packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338{
Damien Miller76e1e362002-01-22 23:15:57 +11001339 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340}
1341
Damien Miller33b13562000-04-04 14:38:59 +10001342void
Damien Millerd432ccf2002-01-22 23:14:44 +11001343packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001344{
Damien Miller76e1e362002-01-22 23:15:57 +11001345 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001346}
1347
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001348void *
Damien Millereccb9de2005-06-17 12:59:34 +10001349packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001350{
Damien Millereccb9de2005-06-17 12:59:34 +10001351 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001352
Damien Miller33b13562000-04-04 14:38:59 +10001353 if (length_ptr != NULL)
1354 *length_ptr = bytes;
1355 return buffer_ptr(&incoming_packet);
1356}
1357
Damien Miller4af51302000-04-16 11:18:38 +10001358int
1359packet_remaining(void)
1360{
1361 return buffer_len(&incoming_packet);
1362}
1363
Damien Miller5428f641999-11-25 11:54:57 +11001364/*
1365 * Returns a string from the packet data. The string is allocated using
1366 * xmalloc; it is the responsibility of the calling program to free it when
1367 * no longer needed. The length_ptr argument may be NULL, or point to an
1368 * integer into which the length of the string is stored.
1369 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001370
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001371void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001372packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001373{
Damien Miller95def091999-11-25 00:26:21 +11001374 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375}
1376
Damien Millerdb255ca2008-05-19 14:59:37 +10001377void *
1378packet_get_string_ptr(u_int *length_ptr)
1379{
1380 return buffer_get_string_ptr(&incoming_packet, length_ptr);
1381}
1382
Damien Miller5428f641999-11-25 11:54:57 +11001383/*
1384 * Sends a diagnostic message from the server to the client. This message
1385 * can be sent at any time (but not while constructing another message). The
1386 * message is printed immediately, but only if the client is being executed
1387 * in verbose mode. These messages are primarily intended to ease debugging
1388 * authentication problems. The length of the formatted message must not
1389 * exceed 1024 bytes. This will automatically call packet_write_wait.
1390 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391
1392void
Damien Miller95def091999-11-25 00:26:21 +11001393packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394{
Damien Miller95def091999-11-25 00:26:21 +11001395 char buf[1024];
1396 va_list args;
1397
Ben Lindstroma14ee472000-12-07 01:24:58 +00001398 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1399 return;
1400
Damien Miller95def091999-11-25 00:26:21 +11001401 va_start(args, fmt);
1402 vsnprintf(buf, sizeof(buf), fmt, args);
1403 va_end(args);
1404
Damien Miller7c8af4f2000-05-01 08:24:07 +10001405 if (compat20) {
1406 packet_start(SSH2_MSG_DEBUG);
1407 packet_put_char(0); /* bool: always display */
1408 packet_put_cstring(buf);
1409 packet_put_cstring("");
1410 } else {
1411 packet_start(SSH_MSG_DEBUG);
1412 packet_put_cstring(buf);
1413 }
Damien Miller95def091999-11-25 00:26:21 +11001414 packet_send();
1415 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416}
1417
Damien Miller5428f641999-11-25 11:54:57 +11001418/*
1419 * Logs the error plus constructs and sends a disconnect packet, closes the
1420 * connection, and exits. This function never returns. The error message
1421 * should not contain a newline. The length of the formatted message must
1422 * not exceed 1024 bytes.
1423 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001424
1425void
Damien Miller95def091999-11-25 00:26:21 +11001426packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001427{
Damien Miller95def091999-11-25 00:26:21 +11001428 char buf[1024];
1429 va_list args;
1430 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001431
Damien Miller95def091999-11-25 00:26:21 +11001432 if (disconnecting) /* Guard against recursive invocations. */
1433 fatal("packet_disconnect called recursively.");
1434 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435
Damien Miller5428f641999-11-25 11:54:57 +11001436 /*
1437 * Format the message. Note that the caller must make sure the
1438 * message is of limited size.
1439 */
Damien Miller95def091999-11-25 00:26:21 +11001440 va_start(args, fmt);
1441 vsnprintf(buf, sizeof(buf), fmt, args);
1442 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001444 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001445 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001446
Damien Miller95def091999-11-25 00:26:21 +11001447 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001448 if (compat20) {
1449 packet_start(SSH2_MSG_DISCONNECT);
1450 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1451 packet_put_cstring(buf);
1452 packet_put_cstring("");
1453 } else {
1454 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001455 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001456 }
Damien Miller95def091999-11-25 00:26:21 +11001457 packet_send();
1458 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001459
Damien Miller95def091999-11-25 00:26:21 +11001460 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001461 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462
Damien Miller95def091999-11-25 00:26:21 +11001463 /* Close the connection. */
1464 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001465 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466}
1467
Damien Miller5428f641999-11-25 11:54:57 +11001468/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469
1470void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001471packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472{
Damien Miller95def091999-11-25 00:26:21 +11001473 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001474
Damien Miller95def091999-11-25 00:26:21 +11001475 if (len > 0) {
1476 len = write(connection_out, buffer_ptr(&output), len);
1477 if (len <= 0) {
1478 if (errno == EAGAIN)
1479 return;
1480 else
1481 fatal("Write failed: %.100s", strerror(errno));
1482 }
1483 buffer_consume(&output, len);
1484 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485}
1486
Damien Miller5428f641999-11-25 11:54:57 +11001487/*
1488 * Calls packet_write_poll repeatedly until all pending output data has been
1489 * written.
1490 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491
1492void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001493packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001495 fd_set *setp;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001496 int ret, ms_remain;
1497 struct timeval start, timeout, *timeoutp = NULL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001498
Damien Miller07d86be2006-03-26 14:19:21 +11001499 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001500 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001501 packet_write_poll();
1502 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001503 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1504 sizeof(fd_mask));
1505 FD_SET(connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001506
1507 if (packet_timeout_ms > 0) {
1508 ms_remain = packet_timeout_ms;
1509 timeoutp = &timeout;
1510 }
1511 for (;;) {
1512 if (packet_timeout_ms != -1) {
1513 ms_to_timeval(&timeout, ms_remain);
1514 gettimeofday(&start, NULL);
1515 }
1516 if ((ret = select(connection_out + 1, NULL, setp,
1517 NULL, timeoutp)) >= 0)
1518 break;
1519 if (errno != EAGAIN && errno != EINTR)
1520 break;
1521 if (packet_timeout_ms == -1)
1522 continue;
1523 ms_subtract_diff(&start, &ms_remain);
1524 if (ms_remain <= 0) {
1525 ret = 0;
1526 break;
1527 }
1528 }
1529 if (ret == 0) {
1530 logit("Connection to %.200s timed out while "
1531 "waiting to write", get_remote_ipaddr());
1532 cleanup_exit(255);
1533 }
Damien Miller95def091999-11-25 00:26:21 +11001534 packet_write_poll();
1535 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001536 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001537}
1538
1539/* Returns true if there is buffered data to write to the connection. */
1540
1541int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001542packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001543{
Damien Miller95def091999-11-25 00:26:21 +11001544 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001545}
1546
1547/* Returns true if there is not too much data to write to the connection. */
1548
1549int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001550packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001551{
Damien Miller95def091999-11-25 00:26:21 +11001552 if (interactive_mode)
1553 return buffer_len(&output) < 16384;
1554 else
1555 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001556}
1557
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001558
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001559static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001560packet_set_tos(int interactive)
1561{
Damien Miller5924ceb2003-11-22 15:02:42 +11001562#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001563 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1564
1565 if (!packet_connection_is_on_socket() ||
1566 !packet_connection_is_ipv4())
1567 return;
1568 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1569 sizeof(tos)) < 0)
1570 error("setsockopt IP_TOS %d: %.100s:",
1571 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001572#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001573}
Ben Lindstroma7433982002-12-23 02:41:41 +00001574
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001575/* Informs that the current session is interactive. Sets IP flags for that. */
1576
1577void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001578packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001579{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001580 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001581
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001582 if (called)
1583 return;
1584 called = 1;
1585
Damien Miller95def091999-11-25 00:26:21 +11001586 /* Record that we are in interactive mode. */
1587 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001588
Damien Miller34132e52000-01-14 15:45:46 +11001589 /* Only set socket options if using a socket. */
1590 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001591 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001592 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001593 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001594}
1595
1596/* Returns true if the current connection is interactive. */
1597
1598int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001599packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001600{
Damien Miller95def091999-11-25 00:26:21 +11001601 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001602}
Damien Miller6162d121999-11-21 13:23:52 +11001603
Darren Tucker1f8311c2004-05-13 16:39:33 +10001604int
Darren Tucker502d3842003-06-28 12:38:01 +10001605packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001606{
Damien Miller95def091999-11-25 00:26:21 +11001607 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001608
Damien Miller95def091999-11-25 00:26:21 +11001609 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001610 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001611 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001612 return -1;
1613 }
1614 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001615 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001616 return -1;
1617 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001618 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001619 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001620 max_packet_size = s;
1621 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001622}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001623
Damien Miller9f643902001-11-12 11:02:52 +11001624/* roundup current message to pad bytes */
1625void
1626packet_add_padding(u_char pad)
1627{
1628 extra_pad = pad;
1629}
1630
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001631/*
1632 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001633 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001634 * byte SSH_MSG_IGNORE
1635 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001636 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001637 * All implementations MUST understand (and ignore) this message at any
1638 * time (after receiving the protocol version). No implementation is
1639 * required to send them. This message can be used as an additional
1640 * protection measure against advanced traffic analysis techniques.
1641 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001642void
1643packet_send_ignore(int nbytes)
1644{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001645 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001646 int i;
1647
1648 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001649 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001650 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001651 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001652 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001653 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001654 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001655 }
1656}
Damien Millera5539d22003-04-09 20:50:06 +10001657
Darren Tucker1f8311c2004-05-13 16:39:33 +10001658#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001659int
1660packet_need_rekeying(void)
1661{
1662 if (datafellows & SSH_BUG_NOREKEY)
1663 return 0;
1664 return
1665 (p_send.packets > MAX_PACKETS) ||
1666 (p_read.packets > MAX_PACKETS) ||
1667 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1668 (max_blocks_in && (p_read.blocks > max_blocks_in));
1669}
1670
1671void
1672packet_set_rekey_limit(u_int32_t bytes)
1673{
1674 rekey_limit = bytes;
1675}
Damien Miller9786e6e2005-07-26 21:54:56 +10001676
1677void
1678packet_set_server(void)
1679{
1680 server_side = 1;
1681}
1682
1683void
1684packet_set_authenticated(void)
1685{
1686 after_authentication = 1;
1687}