blob: 92997288d932afcd9196139841d3f528f51d80cd [file] [log] [blame]
Damien Miller5ed3d572008-02-10 22:27:47 +11001/* $OpenBSD: packet.c,v 1.150 2008/01/23 01:56:54 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
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 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;
Damien Millere45796f2007-06-11 14:01:42 +1000632 mac_clear(mac);
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;
Damien Millere45796f2007-06-11 14:01:42 +1000647 if (mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000648 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);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000654 memset(enc->key, 0, enc->key_len);
655 memset(mac->key, 0, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000656 if ((comp->type == COMP_ZLIB ||
657 (comp->type == COMP_DELAYED && after_authentication)) &&
658 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000659 packet_init_compression();
660 if (mode == MODE_OUT)
661 buffer_compress_init_send(6);
662 else
663 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000664 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000665 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000666 /*
667 * The 2^(blocksize*2) limit is too expensive for 3DES,
668 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
669 */
670 if (enc->block_size >= 16)
671 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
672 else
673 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000674 if (rekey_limit)
675 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000676}
677
Damien Miller5428f641999-11-25 11:54:57 +1100678/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000679 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000680 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000681 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
682 */
683static void
684packet_enable_delayed_compress(void)
685{
686 Comp *comp = NULL;
687 int mode;
688
689 /*
690 * Remember that we are past the authentication step, so rekeying
691 * with COMP_DELAYED will turn on compression immediately.
692 */
693 after_authentication = 1;
694 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000695 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
696 if (newkeys[mode] == NULL)
697 continue;
Damien Miller9786e6e2005-07-26 21:54:56 +1000698 comp = &newkeys[mode]->comp;
699 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000700 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000701 if (mode == MODE_OUT)
702 buffer_compress_init_send(6);
703 else
704 buffer_compress_init_recv();
705 comp->enabled = 1;
706 }
707 }
708}
709
710/*
Damien Miller33b13562000-04-04 14:38:59 +1000711 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
712 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000713static void
Damien Millera5539d22003-04-09 20:50:06 +1000714packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000715{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000716 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100717 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000718 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100719 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000720 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000721 Enc *enc = NULL;
722 Mac *mac = NULL;
723 Comp *comp = NULL;
724 int block_size;
725
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000726 if (newkeys[MODE_OUT] != NULL) {
727 enc = &newkeys[MODE_OUT]->enc;
728 mac = &newkeys[MODE_OUT]->mac;
729 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000730 }
Damien Miller963f6b22002-02-19 15:21:23 +1100731 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000732
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000733 cp = buffer_ptr(&outgoing_packet);
734 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000735
736#ifdef PACKET_DEBUG
737 fprintf(stderr, "plain: ");
738 buffer_dump(&outgoing_packet);
739#endif
740
741 if (comp && comp->enabled) {
742 len = buffer_len(&outgoing_packet);
743 /* skip header, compress only payload */
744 buffer_consume(&outgoing_packet, 5);
745 buffer_clear(&compression_buffer);
746 buffer_compress(&outgoing_packet, &compression_buffer);
747 buffer_clear(&outgoing_packet);
748 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
749 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
750 buffer_len(&compression_buffer));
751 DBG(debug("compression: raw %d compressed %d", len,
752 buffer_len(&outgoing_packet)));
753 }
754
755 /* sizeof (packet_len + pad_len + payload) */
756 len = buffer_len(&outgoing_packet);
757
758 /*
759 * calc size of padding, alloc space, get random data,
760 * minimum padding is 4 bytes
761 */
762 padlen = block_size - (len % block_size);
763 if (padlen < 4)
764 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100765 if (extra_pad) {
766 /* will wrap if extra_pad+padlen > 255 */
767 extra_pad = roundup(extra_pad, block_size);
768 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000769 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100770 pad, len, padlen, extra_pad);
771 padlen += pad;
772 extra_pad = 0;
773 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100774 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100775 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000776 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000777 for (i = 0; i < padlen; i++) {
778 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000779 rnd = arc4random();
780 cp[i] = rnd & 0xff;
781 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000782 }
Damien Millere247cc42000-05-07 12:03:14 +1000783 } else {
784 /* clear padding */
785 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000786 }
787 /* packet_length includes payload, padding and padding length field */
788 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000789 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100790 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000791 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000792 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
793
794 /* compute MAC over seqnr and packet(length fields, payload, padding) */
795 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000796 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100797 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000798 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000799 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000800 }
801 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100802 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100803 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000804 buffer_len(&outgoing_packet));
805 /* append unencrypted MAC */
806 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100807 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000808#ifdef PACKET_DEBUG
809 fprintf(stderr, "encrypted: ");
810 buffer_dump(&output);
811#endif
Damien Miller4af51302000-04-16 11:18:38 +1000812 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000813 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000814 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000815 if (++p_send.packets == 0)
816 if (!(datafellows & SSH_BUG_NOREKEY))
817 fatal("XXX too many packets with same key");
818 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000819 buffer_clear(&outgoing_packet);
820
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000821 if (type == SSH2_MSG_NEWKEYS)
822 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000823 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
824 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000825}
826
Damien Millera5539d22003-04-09 20:50:06 +1000827static void
828packet_send2(void)
829{
830 static int rekeying = 0;
831 struct packet *p;
832 u_char type, *cp;
833
834 cp = buffer_ptr(&outgoing_packet);
835 type = cp[5];
836
837 /* during rekeying we can only send key exchange messages */
838 if (rekeying) {
839 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
840 (type <= SSH2_MSG_TRANSPORT_MAX))) {
841 debug("enqueue packet: %u", type);
842 p = xmalloc(sizeof(*p));
843 p->type = type;
844 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
845 buffer_init(&outgoing_packet);
846 TAILQ_INSERT_TAIL(&outgoing, p, next);
847 return;
848 }
849 }
850
851 /* rekeying starts with sending KEXINIT */
852 if (type == SSH2_MSG_KEXINIT)
853 rekeying = 1;
854
855 packet_send2_wrapped();
856
857 /* after a NEWKEYS message we can send the complete queue */
858 if (type == SSH2_MSG_NEWKEYS) {
859 rekeying = 0;
860 while ((p = TAILQ_FIRST(&outgoing))) {
861 type = p->type;
862 debug("dequeue packet: %u", type);
863 buffer_free(&outgoing_packet);
864 memcpy(&outgoing_packet, &p->payload,
865 sizeof(Buffer));
866 TAILQ_REMOVE(&outgoing, p, next);
867 xfree(p);
868 packet_send2_wrapped();
869 }
870 }
871}
872
Damien Miller33b13562000-04-04 14:38:59 +1000873void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000874packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000875{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000876 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000877 packet_send2();
878 else
879 packet_send1();
880 DBG(debug("packet_send done"));
881}
882
Damien Miller33b13562000-04-04 14:38:59 +1000883/*
Damien Miller5428f641999-11-25 11:54:57 +1100884 * Waits until a packet has been received, and returns its type. Note that
885 * no other data is processed until this returns, so this function should not
886 * be used during the interactive session.
887 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888
889int
Damien Millerdff50992002-01-22 23:16:32 +1100890packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891{
Damien Miller95def091999-11-25 00:26:21 +1100892 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000893 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100894 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000895 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896
Damien Miller07d86be2006-03-26 14:19:21 +1100897 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000898 sizeof(fd_mask));
899
Damien Miller95def091999-11-25 00:26:21 +1100900 /* Since we are blocking, ensure that all written packets have been sent. */
901 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902
Damien Miller95def091999-11-25 00:26:21 +1100903 /* Stay in the loop until we have received a complete packet. */
904 for (;;) {
905 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100906 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000907 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000908 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100909 || type == SSH_SMSG_FAILURE
910 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000911 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100912 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100913 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000914 if (type != SSH_MSG_NONE) {
915 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100916 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000917 }
Damien Miller5428f641999-11-25 11:54:57 +1100918 /*
919 * Otherwise, wait for some data to arrive, add it to the
920 * buffer, and try again.
921 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000922 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
923 sizeof(fd_mask));
924 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100925
Damien Miller95def091999-11-25 00:26:21 +1100926 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000927 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000928 (errno == EAGAIN || errno == EINTR))
929 ;
Damien Miller5428f641999-11-25 11:54:57 +1100930
Damien Miller95def091999-11-25 00:26:21 +1100931 /* Read data from the socket. */
932 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100933 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000934 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000935 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100936 }
Damien Miller95def091999-11-25 00:26:21 +1100937 if (len < 0)
938 fatal("Read from socket failed: %.100s", strerror(errno));
939 /* Append it to the buffer. */
940 packet_process_incoming(buf, len);
941 }
942 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000943}
944
Damien Miller278f9072001-12-21 15:00:19 +1100945int
Damien Millerdff50992002-01-22 23:16:32 +1100946packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100947{
Damien Millerdff50992002-01-22 23:16:32 +1100948 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100949}
950
Damien Miller5428f641999-11-25 11:54:57 +1100951/*
952 * Waits until a packet has been received, verifies that its type matches
953 * that given, and gives a fatal error and exits if there is a mismatch.
954 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955
956void
Damien Millerdff50992002-01-22 23:16:32 +1100957packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958{
Damien Miller95def091999-11-25 00:26:21 +1100959 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960
Damien Millerdff50992002-01-22 23:16:32 +1100961 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100962 if (type != expected_type)
963 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000964 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965}
966
967/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100968 * packet_process_incoming. If so, reads the packet; otherwise returns
969 * SSH_MSG_NONE. This does not wait for data from the connection.
970 *
Damien Miller5ed3d572008-02-10 22:27:47 +1100971 * SSH_MSG_DISCONNECT is handled specially here. Also,
972 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
973 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100974 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975
Ben Lindstrombba81212001-06-25 05:01:22 +0000976static int
Damien Millerdff50992002-01-22 23:16:32 +1100977packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000979 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000980 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000981 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Damien Miller95def091999-11-25 00:26:21 +1100983 /* Check if input size is less than minimum packet size. */
984 if (buffer_len(&input) < 4 + 8)
985 return SSH_MSG_NONE;
986 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000987 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100988 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100989 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000990 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100991 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992
Damien Miller95def091999-11-25 00:26:21 +1100993 /* Check if the packet has been entirely received. */
994 if (buffer_len(&input) < 4 + padded_len)
995 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996
Damien Miller95def091999-11-25 00:26:21 +1100997 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998
Damien Miller95def091999-11-25 00:26:21 +1100999 /* Consume packet length. */
1000 buffer_consume(&input, 4);
1001
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001002 /*
1003 * Cryptographic attack detector for ssh
1004 * (C)1998 CORE-SDI, Buenos Aires Argentina
1005 * Ariel Futoransky(futo@core-sdi.com)
1006 */
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001007 if (!receive_context.plaintext) {
1008 switch (detect_attack(buffer_ptr(&input), padded_len)) {
1009 case DEATTACK_DETECTED:
1010 packet_disconnect("crc32 compensation attack: "
1011 "network attack detected");
1012 case DEATTACK_DOS_DETECTED:
1013 packet_disconnect("deattack denial of "
1014 "service detected");
1015 }
1016 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001017
1018 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001019 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001020 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001021 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001022
Damien Miller95def091999-11-25 00:26:21 +11001023 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024
1025#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001026 fprintf(stderr, "read_poll plain: ");
1027 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001029
Damien Miller95def091999-11-25 00:26:21 +11001030 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001031 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001032 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001033
Damien Miller95def091999-11-25 00:26:21 +11001034 /* Skip padding. */
1035 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001036
Damien Miller95def091999-11-25 00:26:21 +11001037 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001038 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001039 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001040 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001042 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001043 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001044 if (checksum != stored_checksum)
1045 packet_disconnect("Corrupted check bytes on input.");
1046 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047
Damien Miller95def091999-11-25 00:26:21 +11001048 if (packet_compression) {
1049 buffer_clear(&compression_buffer);
1050 buffer_uncompress(&incoming_packet, &compression_buffer);
1051 buffer_clear(&incoming_packet);
1052 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001053 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001054 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001055 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001056 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1057 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001058 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001059}
Damien Miller95def091999-11-25 00:26:21 +11001060
Ben Lindstrombba81212001-06-25 05:01:22 +00001061static int
Damien Millerdff50992002-01-22 23:16:32 +11001062packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001063{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001064 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001065 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001066 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001067 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001068 Enc *enc = NULL;
1069 Mac *mac = NULL;
1070 Comp *comp = NULL;
1071
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001072 if (newkeys[MODE_IN] != NULL) {
1073 enc = &newkeys[MODE_IN]->enc;
1074 mac = &newkeys[MODE_IN]->mac;
1075 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001076 }
1077 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001078 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001079
1080 if (packet_length == 0) {
1081 /*
1082 * check if input size is less than the cipher block size,
1083 * decrypt first block and extract length of incoming packet
1084 */
1085 if (buffer_len(&input) < block_size)
1086 return SSH_MSG_NONE;
1087 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001088 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001089 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001090 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001091 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001092 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001093 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001094#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001095 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001096#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001097 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001098 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001099 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001100 buffer_consume(&input, block_size);
1101 }
1102 /* we have a partial packet of block_size bytes */
1103 need = 4 + packet_length - block_size;
1104 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1105 need, maclen));
1106 if (need % block_size != 0)
1107 fatal("padding error: need %d block %d mod %d",
1108 need, block_size, need % block_size);
1109 /*
1110 * check if the entire packet has been received and
1111 * decrypt into incoming_packet
1112 */
1113 if (buffer_len(&input) < need + maclen)
1114 return SSH_MSG_NONE;
1115#ifdef PACKET_DEBUG
1116 fprintf(stderr, "read_poll enc/full: ");
1117 buffer_dump(&input);
1118#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001119 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001120 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001121 buffer_consume(&input, need);
1122 /*
1123 * compute MAC over seqnr and packet,
1124 * increment sequence number for incoming packet
1125 */
Damien Miller4af51302000-04-16 11:18:38 +10001126 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001127 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001128 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001129 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001130 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001131 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001132 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001133 buffer_consume(&input, mac->mac_len);
1134 }
Damien Miller278f9072001-12-21 15:00:19 +11001135 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001136 *seqnr_p = p_read.seqnr;
1137 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001138 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001139 if (++p_read.packets == 0)
1140 if (!(datafellows & SSH_BUG_NOREKEY))
1141 fatal("XXX too many packets with same key");
1142 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001143
1144 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001145 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001146 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001147 DBG(debug("input: padlen %d", padlen));
1148 if (padlen < 4)
1149 packet_disconnect("Corrupted padlen %d on input.", padlen);
1150
1151 /* skip packet size + padlen, discard padding */
1152 buffer_consume(&incoming_packet, 4 + 1);
1153 buffer_consume_end(&incoming_packet, padlen);
1154
1155 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1156 if (comp && comp->enabled) {
1157 buffer_clear(&compression_buffer);
1158 buffer_uncompress(&incoming_packet, &compression_buffer);
1159 buffer_clear(&incoming_packet);
1160 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1161 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001162 DBG(debug("input: len after de-compress %d",
1163 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001164 }
1165 /*
1166 * get packet type, implies consume.
1167 * return length of payload (without type field)
1168 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001169 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001170 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1171 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001172 if (type == SSH2_MSG_NEWKEYS)
1173 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001174 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1175 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001176#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001177 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001178 buffer_dump(&incoming_packet);
1179#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001180 /* reset for next packet */
1181 packet_length = 0;
1182 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001183}
1184
1185int
Damien Millerdff50992002-01-22 23:16:32 +11001186packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001187{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001188 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001189 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001190 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001191
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001192 for (;;) {
1193 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001194 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001195 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001196 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001197 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001198 case SSH2_MSG_IGNORE:
1199 break;
Damien Miller33b13562000-04-04 14:38:59 +10001200 case SSH2_MSG_DEBUG:
1201 packet_get_char();
1202 msg = packet_get_string(NULL);
1203 debug("Remote: %.900s", msg);
1204 xfree(msg);
1205 msg = packet_get_string(NULL);
1206 xfree(msg);
1207 break;
1208 case SSH2_MSG_DISCONNECT:
1209 reason = packet_get_int();
1210 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001211 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001212 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001213 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001214 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001215 break;
Damien Miller659811f2002-01-22 23:23:11 +11001216 case SSH2_MSG_UNIMPLEMENTED:
1217 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001218 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1219 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001220 break;
Damien Miller33b13562000-04-04 14:38:59 +10001221 default:
1222 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001223 }
Damien Miller33b13562000-04-04 14:38:59 +10001224 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001225 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001226 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001227 case SSH_MSG_IGNORE:
1228 break;
1229 case SSH_MSG_DEBUG:
1230 msg = packet_get_string(NULL);
1231 debug("Remote: %.900s", msg);
1232 xfree(msg);
1233 break;
1234 case SSH_MSG_DISCONNECT:
1235 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001236 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001237 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001238 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001239 break;
1240 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001241 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001242 DBG(debug("received packet type %d", type));
1243 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001244 }
Damien Miller33b13562000-04-04 14:38:59 +10001245 }
1246 }
1247}
1248
Damien Miller278f9072001-12-21 15:00:19 +11001249int
Damien Millerdff50992002-01-22 23:16:32 +11001250packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001251{
Damien Millerdff50992002-01-22 23:16:32 +11001252 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001253}
1254
Damien Miller5428f641999-11-25 11:54:57 +11001255/*
1256 * Buffers the given amount of input characters. This is intended to be used
1257 * together with packet_read_poll.
1258 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259
1260void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001261packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001262{
Damien Miller95def091999-11-25 00:26:21 +11001263 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264}
1265
1266/* Returns a character from the packet. */
1267
Ben Lindstrom46c16222000-12-22 01:43:59 +00001268u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001269packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270{
Damien Miller95def091999-11-25 00:26:21 +11001271 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001272
Damien Miller95def091999-11-25 00:26:21 +11001273 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001274 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001275}
1276
1277/* Returns an integer from the packet data. */
1278
Ben Lindstrom46c16222000-12-22 01:43:59 +00001279u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001280packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281{
Damien Miller95def091999-11-25 00:26:21 +11001282 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001283}
1284
Damien Miller5428f641999-11-25 11:54:57 +11001285/*
1286 * Returns an arbitrary precision integer from the packet data. The integer
1287 * must have been initialized before this call.
1288 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001289
1290void
Damien Millerd432ccf2002-01-22 23:14:44 +11001291packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001292{
Damien Miller76e1e362002-01-22 23:15:57 +11001293 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001294}
1295
Damien Miller33b13562000-04-04 14:38:59 +10001296void
Damien Millerd432ccf2002-01-22 23:14:44 +11001297packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001298{
Damien Miller76e1e362002-01-22 23:15:57 +11001299 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001300}
1301
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001302void *
Damien Millereccb9de2005-06-17 12:59:34 +10001303packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001304{
Damien Millereccb9de2005-06-17 12:59:34 +10001305 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001306
Damien Miller33b13562000-04-04 14:38:59 +10001307 if (length_ptr != NULL)
1308 *length_ptr = bytes;
1309 return buffer_ptr(&incoming_packet);
1310}
1311
Damien Miller4af51302000-04-16 11:18:38 +10001312int
1313packet_remaining(void)
1314{
1315 return buffer_len(&incoming_packet);
1316}
1317
Damien Miller5428f641999-11-25 11:54:57 +11001318/*
1319 * Returns a string from the packet data. The string is allocated using
1320 * xmalloc; it is the responsibility of the calling program to free it when
1321 * no longer needed. The length_ptr argument may be NULL, or point to an
1322 * integer into which the length of the string is stored.
1323 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001324
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001325void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001326packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001327{
Damien Miller95def091999-11-25 00:26:21 +11001328 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001329}
1330
Damien Miller5428f641999-11-25 11:54:57 +11001331/*
1332 * Sends a diagnostic message from the server to the client. This message
1333 * can be sent at any time (but not while constructing another message). The
1334 * message is printed immediately, but only if the client is being executed
1335 * in verbose mode. These messages are primarily intended to ease debugging
1336 * authentication problems. The length of the formatted message must not
1337 * exceed 1024 bytes. This will automatically call packet_write_wait.
1338 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001339
1340void
Damien Miller95def091999-11-25 00:26:21 +11001341packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001342{
Damien Miller95def091999-11-25 00:26:21 +11001343 char buf[1024];
1344 va_list args;
1345
Ben Lindstroma14ee472000-12-07 01:24:58 +00001346 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1347 return;
1348
Damien Miller95def091999-11-25 00:26:21 +11001349 va_start(args, fmt);
1350 vsnprintf(buf, sizeof(buf), fmt, args);
1351 va_end(args);
1352
Damien Miller7c8af4f2000-05-01 08:24:07 +10001353 if (compat20) {
1354 packet_start(SSH2_MSG_DEBUG);
1355 packet_put_char(0); /* bool: always display */
1356 packet_put_cstring(buf);
1357 packet_put_cstring("");
1358 } else {
1359 packet_start(SSH_MSG_DEBUG);
1360 packet_put_cstring(buf);
1361 }
Damien Miller95def091999-11-25 00:26:21 +11001362 packet_send();
1363 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364}
1365
Damien Miller5428f641999-11-25 11:54:57 +11001366/*
1367 * Logs the error plus constructs and sends a disconnect packet, closes the
1368 * connection, and exits. This function never returns. The error message
1369 * should not contain a newline. The length of the formatted message must
1370 * not exceed 1024 bytes.
1371 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001372
1373void
Damien Miller95def091999-11-25 00:26:21 +11001374packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375{
Damien Miller95def091999-11-25 00:26:21 +11001376 char buf[1024];
1377 va_list args;
1378 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001379
Damien Miller95def091999-11-25 00:26:21 +11001380 if (disconnecting) /* Guard against recursive invocations. */
1381 fatal("packet_disconnect called recursively.");
1382 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001383
Damien Miller5428f641999-11-25 11:54:57 +11001384 /*
1385 * Format the message. Note that the caller must make sure the
1386 * message is of limited size.
1387 */
Damien Miller95def091999-11-25 00:26:21 +11001388 va_start(args, fmt);
1389 vsnprintf(buf, sizeof(buf), fmt, args);
1390 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001392 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001393 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001394
Damien Miller95def091999-11-25 00:26:21 +11001395 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001396 if (compat20) {
1397 packet_start(SSH2_MSG_DISCONNECT);
1398 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1399 packet_put_cstring(buf);
1400 packet_put_cstring("");
1401 } else {
1402 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001403 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001404 }
Damien Miller95def091999-11-25 00:26:21 +11001405 packet_send();
1406 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407
Damien Miller95def091999-11-25 00:26:21 +11001408 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001409 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001410
Damien Miller95def091999-11-25 00:26:21 +11001411 /* Close the connection. */
1412 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001413 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414}
1415
Damien Miller5428f641999-11-25 11:54:57 +11001416/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417
1418void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001419packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420{
Damien Miller95def091999-11-25 00:26:21 +11001421 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001422
Damien Miller95def091999-11-25 00:26:21 +11001423 if (len > 0) {
1424 len = write(connection_out, buffer_ptr(&output), len);
1425 if (len <= 0) {
1426 if (errno == EAGAIN)
1427 return;
1428 else
1429 fatal("Write failed: %.100s", strerror(errno));
1430 }
1431 buffer_consume(&output, len);
1432 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001433}
1434
Damien Miller5428f641999-11-25 11:54:57 +11001435/*
1436 * Calls packet_write_poll repeatedly until all pending output data has been
1437 * written.
1438 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001439
1440void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001441packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001442{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001443 fd_set *setp;
1444
Damien Miller07d86be2006-03-26 14:19:21 +11001445 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001446 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001447 packet_write_poll();
1448 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001449 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1450 sizeof(fd_mask));
1451 FD_SET(connection_out, setp);
1452 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001453 (errno == EAGAIN || errno == EINTR))
1454 ;
Damien Miller95def091999-11-25 00:26:21 +11001455 packet_write_poll();
1456 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001457 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001458}
1459
1460/* Returns true if there is buffered data to write to the connection. */
1461
1462int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001463packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001464{
Damien Miller95def091999-11-25 00:26:21 +11001465 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466}
1467
1468/* Returns true if there is not too much data to write to the connection. */
1469
1470int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001471packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472{
Damien Miller95def091999-11-25 00:26:21 +11001473 if (interactive_mode)
1474 return buffer_len(&output) < 16384;
1475 else
1476 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001477}
1478
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001479
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001480static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001481packet_set_tos(int interactive)
1482{
Damien Miller5924ceb2003-11-22 15:02:42 +11001483#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001484 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1485
1486 if (!packet_connection_is_on_socket() ||
1487 !packet_connection_is_ipv4())
1488 return;
1489 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1490 sizeof(tos)) < 0)
1491 error("setsockopt IP_TOS %d: %.100s:",
1492 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001493#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001494}
Ben Lindstroma7433982002-12-23 02:41:41 +00001495
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001496/* Informs that the current session is interactive. Sets IP flags for that. */
1497
1498void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001499packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001501 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001502
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001503 if (called)
1504 return;
1505 called = 1;
1506
Damien Miller95def091999-11-25 00:26:21 +11001507 /* Record that we are in interactive mode. */
1508 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001509
Damien Miller34132e52000-01-14 15:45:46 +11001510 /* Only set socket options if using a socket. */
1511 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001512 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001513 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001514 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001515}
1516
1517/* Returns true if the current connection is interactive. */
1518
1519int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001520packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001521{
Damien Miller95def091999-11-25 00:26:21 +11001522 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001523}
Damien Miller6162d121999-11-21 13:23:52 +11001524
Darren Tucker1f8311c2004-05-13 16:39:33 +10001525int
Darren Tucker502d3842003-06-28 12:38:01 +10001526packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001527{
Damien Miller95def091999-11-25 00:26:21 +11001528 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001529
Damien Miller95def091999-11-25 00:26:21 +11001530 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001531 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001532 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001533 return -1;
1534 }
1535 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001536 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001537 return -1;
1538 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001539 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001540 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001541 max_packet_size = s;
1542 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001543}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001544
Damien Miller9f643902001-11-12 11:02:52 +11001545/* roundup current message to pad bytes */
1546void
1547packet_add_padding(u_char pad)
1548{
1549 extra_pad = pad;
1550}
1551
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001552/*
1553 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001554 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001555 * byte SSH_MSG_IGNORE
1556 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001557 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001558 * All implementations MUST understand (and ignore) this message at any
1559 * time (after receiving the protocol version). No implementation is
1560 * required to send them. This message can be used as an additional
1561 * protection measure against advanced traffic analysis techniques.
1562 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001563void
1564packet_send_ignore(int nbytes)
1565{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001566 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001567 int i;
1568
1569 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001570 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001571 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001572 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001573 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001574 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001575 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001576 }
1577}
Damien Millera5539d22003-04-09 20:50:06 +10001578
Darren Tucker1f8311c2004-05-13 16:39:33 +10001579#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001580int
1581packet_need_rekeying(void)
1582{
1583 if (datafellows & SSH_BUG_NOREKEY)
1584 return 0;
1585 return
1586 (p_send.packets > MAX_PACKETS) ||
1587 (p_read.packets > MAX_PACKETS) ||
1588 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1589 (max_blocks_in && (p_read.blocks > max_blocks_in));
1590}
1591
1592void
1593packet_set_rekey_limit(u_int32_t bytes)
1594{
1595 rekey_limit = bytes;
1596}
Damien Miller9786e6e2005-07-26 21:54:56 +10001597
1598void
1599packet_set_server(void)
1600{
1601 server_side = 1;
1602}
1603
1604void
1605packet_set_authenticated(void)
1606{
1607 after_authentication = 1;
1608}