blob: 6afe24b9fe4fa7680e374ba9e8d232444d2ab448 [file] [log] [blame]
Damien Miller58226f62008-03-07 18:33:30 +11001/* $OpenBSD: packet.c,v 1.151 2008/02/22 20:44:02 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 Miller58226f62008-03-07 18:33:30 +1100139int keep_alive_timeouts = 0;
140
Damien Miller33b13562000-04-04 14:38:59 +1000141/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000142Newkeys *newkeys[MODE_MAX];
Damien Millera5539d22003-04-09 20:50:06 +1000143static struct packet_state {
144 u_int32_t seqnr;
145 u_int32_t packets;
146 u_int64_t blocks;
147} p_read, p_send;
148
149static u_int64_t max_blocks_in, max_blocks_out;
150static u_int32_t rekey_limit;
Damien Miller33b13562000-04-04 14:38:59 +1000151
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000152/* Session key for protocol v1 */
153static u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
154static u_int ssh1_keylen;
155
Damien Miller9f643902001-11-12 11:02:52 +1100156/* roundup current message to extra_pad bytes */
157static u_char extra_pad = 0;
158
Damien Millera5539d22003-04-09 20:50:06 +1000159struct packet {
160 TAILQ_ENTRY(packet) next;
161 u_char type;
162 Buffer payload;
163};
164TAILQ_HEAD(, packet) outgoing;
165
Damien Miller5428f641999-11-25 11:54:57 +1100166/*
167 * Sets the descriptors used for communication. Disables encryption until
168 * packet_set_encryption_key is called.
169 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170void
171packet_set_connection(int fd_in, int fd_out)
172{
Damien Miller874d77b2000-10-14 16:23:11 +1100173 Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000174
Damien Miller874d77b2000-10-14 16:23:11 +1100175 if (none == NULL)
176 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100177 connection_in = fd_in;
178 connection_out = fd_out;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000179 cipher_init(&send_context, none, (const u_char *)"",
180 0, NULL, 0, CIPHER_ENCRYPT);
181 cipher_init(&receive_context, none, (const u_char *)"",
182 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000183 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100184 if (!initialized) {
185 initialized = 1;
186 buffer_init(&input);
187 buffer_init(&output);
188 buffer_init(&outgoing_packet);
189 buffer_init(&incoming_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000190 TAILQ_INIT(&outgoing);
Damien Miller95def091999-11-25 00:26:21 +1100191 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192}
193
Damien Miller34132e52000-01-14 15:45:46 +1100194/* Returns 1 if remote host is connected via socket, 0 if not. */
195
196int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000197packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100198{
199 struct sockaddr_storage from, to;
200 socklen_t fromlen, tolen;
201
202 /* filedescriptors in and out are the same, so it's a socket */
203 if (connection_in == connection_out)
204 return 1;
205 fromlen = sizeof(from);
206 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100207 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100208 return 0;
209 tolen = sizeof(to);
210 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100211 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100212 return 0;
213 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
214 return 0;
215 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
216 return 0;
217 return 1;
218}
219
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000220/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000221 * Exports an IV from the CipherContext required to export the key
222 * state back from the unprivileged child to the privileged parent
223 * process.
224 */
225
226void
227packet_get_keyiv(int mode, u_char *iv, u_int len)
228{
229 CipherContext *cc;
230
231 if (mode == MODE_OUT)
232 cc = &send_context;
233 else
234 cc = &receive_context;
235
236 cipher_get_keyiv(cc, iv, len);
237}
238
239int
240packet_get_keycontext(int mode, u_char *dat)
241{
242 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000243
Ben Lindstromf6027d32002-03-22 01:42:04 +0000244 if (mode == MODE_OUT)
245 cc = &send_context;
246 else
247 cc = &receive_context;
248
249 return (cipher_get_keycontext(cc, dat));
250}
251
252void
253packet_set_keycontext(int mode, u_char *dat)
254{
255 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000256
Ben Lindstromf6027d32002-03-22 01:42:04 +0000257 if (mode == MODE_OUT)
258 cc = &send_context;
259 else
260 cc = &receive_context;
261
262 cipher_set_keycontext(cc, dat);
263}
264
265int
266packet_get_keyiv_len(int mode)
267{
268 CipherContext *cc;
269
270 if (mode == MODE_OUT)
271 cc = &send_context;
272 else
273 cc = &receive_context;
274
275 return (cipher_get_keyiv_len(cc));
276}
Damien Miller4f7becb2006-03-26 14:10:14 +1100277
Ben Lindstromf6027d32002-03-22 01:42:04 +0000278void
279packet_set_iv(int mode, u_char *dat)
280{
281 CipherContext *cc;
282
283 if (mode == MODE_OUT)
284 cc = &send_context;
285 else
286 cc = &receive_context;
287
288 cipher_set_keyiv(cc, dat);
289}
Damien Miller4f7becb2006-03-26 14:10:14 +1100290
Ben Lindstromf6027d32002-03-22 01:42:04 +0000291int
Damien Miller2b92d322003-06-11 22:05:06 +1000292packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000293{
294 return (cipher_get_number(receive_context.cipher));
295}
296
Damien Millera5539d22003-04-09 20:50:06 +1000297void
298packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000299{
Damien Millera5539d22003-04-09 20:50:06 +1000300 struct packet_state *state;
301
302 state = (mode == MODE_IN) ? &p_read : &p_send;
303 *seqnr = state->seqnr;
304 *blocks = state->blocks;
305 *packets = state->packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000306}
307
308void
Damien Millera5539d22003-04-09 20:50:06 +1000309packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000310{
Damien Millera5539d22003-04-09 20:50:06 +1000311 struct packet_state *state;
312
313 state = (mode == MODE_IN) ? &p_read : &p_send;
314 state->seqnr = seqnr;
315 state->blocks = blocks;
316 state->packets = packets;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000317}
318
Damien Miller34132e52000-01-14 15:45:46 +1100319/* returns 1 if connection is via ipv4 */
320
321int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000322packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100323{
324 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100325 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100326
327 memset(&to, 0, sizeof(to));
328 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
329 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000330 if (to.ss_family == AF_INET)
331 return 1;
332#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100333 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000334 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
335 return 1;
336#endif
337 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100338}
339
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340/* Sets the connection into non-blocking mode. */
341
342void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000343packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344{
Damien Miller95def091999-11-25 00:26:21 +1100345 /* Set the socket into non-blocking mode. */
Damien Miller232711f2004-06-15 10:35:30 +1000346 set_nonblock(connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347
Damien Miller232711f2004-06-15 10:35:30 +1000348 if (connection_out != connection_in)
349 set_nonblock(connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350}
351
352/* Returns the socket used for reading. */
353
354int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000355packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356{
Damien Miller95def091999-11-25 00:26:21 +1100357 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358}
359
360/* Returns the descriptor used for writing. */
361
362int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000363packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364{
Damien Miller95def091999-11-25 00:26:21 +1100365 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366}
367
368/* Closes the connection and clears and frees internal data structures. */
369
370void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000371packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372{
Damien Miller95def091999-11-25 00:26:21 +1100373 if (!initialized)
374 return;
375 initialized = 0;
376 if (connection_in == connection_out) {
377 shutdown(connection_out, SHUT_RDWR);
378 close(connection_out);
379 } else {
380 close(connection_in);
381 close(connection_out);
382 }
383 buffer_free(&input);
384 buffer_free(&output);
385 buffer_free(&outgoing_packet);
386 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000387 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100388 buffer_free(&compression_buffer);
389 buffer_compress_uninit();
390 }
Damien Miller963f6b22002-02-19 15:21:23 +1100391 cipher_cleanup(&send_context);
392 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393}
394
395/* Sets remote side protocol flags. */
396
397void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000398packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399{
Damien Miller95def091999-11-25 00:26:21 +1100400 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401}
402
403/* Returns the remote protocol flags set earlier by the above function. */
404
Ben Lindstrom46c16222000-12-22 01:43:59 +0000405u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000406packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407{
Damien Miller95def091999-11-25 00:26:21 +1100408 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409}
410
Damien Miller5428f641999-11-25 11:54:57 +1100411/*
412 * Starts packet compression from the next packet on in both directions.
413 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
414 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Ben Lindstrombba81212001-06-25 05:01:22 +0000416static void
417packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000418{
419 if (compression_buffer_ready == 1)
420 return;
421 compression_buffer_ready = 1;
422 buffer_init(&compression_buffer);
423}
424
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425void
426packet_start_compression(int level)
427{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000428 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100429 fatal("Compression already enabled.");
430 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000431 packet_init_compression();
432 buffer_compress_init_send(level);
433 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434}
435
Damien Miller5428f641999-11-25 11:54:57 +1100436/*
Damien Miller5428f641999-11-25 11:54:57 +1100437 * Causes any further packets to be encrypted using the given key. The same
438 * key is used for both sending and reception. However, both directions are
439 * encrypted independently of each other.
440 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000441
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000443packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100444 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445{
Damien Miller874d77b2000-10-14 16:23:11 +1100446 Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000447
Damien Miller874d77b2000-10-14 16:23:11 +1100448 if (cipher == NULL)
449 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000450 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100451 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000452 if (keylen > SSH_SESSION_KEY_LENGTH)
453 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
454 memcpy(ssh1_key, key, keylen);
455 ssh1_keylen = keylen;
Damien Miller963f6b22002-02-19 15:21:23 +1100456 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
457 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458}
459
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000460u_int
461packet_get_encryption_key(u_char *key)
462{
463 if (key == NULL)
464 return (ssh1_keylen);
465 memcpy(key, ssh1_key, ssh1_keylen);
466 return (ssh1_keylen);
467}
468
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000469/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000471packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000473 u_char buf[9];
474 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475
Ben Lindstrom204e4882001-03-05 06:47:00 +0000476 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000477 len = compat20 ? 6 : 9;
478 memset(buf, 0, len - 1);
479 buf[len - 1] = type;
480 buffer_clear(&outgoing_packet);
481 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000482}
483
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000484/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485void
486packet_put_char(int value)
487{
Damien Miller95def091999-11-25 00:26:21 +1100488 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000489
Damien Miller95def091999-11-25 00:26:21 +1100490 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491}
Damien Miller4f7becb2006-03-26 14:10:14 +1100492
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000494packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495{
Damien Miller95def091999-11-25 00:26:21 +1100496 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497}
Damien Miller4f7becb2006-03-26 14:10:14 +1100498
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100500packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501{
Damien Miller95def091999-11-25 00:26:21 +1100502 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503}
Damien Miller4f7becb2006-03-26 14:10:14 +1100504
Damien Miller33b13562000-04-04 14:38:59 +1000505void
506packet_put_cstring(const char *str)
507{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000508 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000509}
Damien Miller4f7becb2006-03-26 14:10:14 +1100510
Damien Miller33b13562000-04-04 14:38:59 +1000511void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100512packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000513{
514 buffer_append(&outgoing_packet, buf, len);
515}
Damien Miller4f7becb2006-03-26 14:10:14 +1100516
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517void
Damien Miller95def091999-11-25 00:26:21 +1100518packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519{
Damien Miller95def091999-11-25 00:26:21 +1100520 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521}
Damien Miller4f7becb2006-03-26 14:10:14 +1100522
Damien Miller33b13562000-04-04 14:38:59 +1000523void
524packet_put_bignum2(BIGNUM * value)
525{
526 buffer_put_bignum2(&outgoing_packet, value);
527}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Damien Miller5428f641999-11-25 11:54:57 +1100529/*
530 * Finalizes and sends the packet. If the encryption key has been set,
531 * encrypts the packet before sending.
532 */
Damien Miller95def091999-11-25 00:26:21 +1100533
Ben Lindstrombba81212001-06-25 05:01:22 +0000534static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000535packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000537 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100538 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000539 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000540 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541
Damien Miller5428f641999-11-25 11:54:57 +1100542 /*
543 * If using packet compression, compress the payload of the outgoing
544 * packet.
545 */
Damien Miller95def091999-11-25 00:26:21 +1100546 if (packet_compression) {
547 buffer_clear(&compression_buffer);
548 /* Skip padding. */
549 buffer_consume(&outgoing_packet, 8);
550 /* padding */
551 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
552 buffer_compress(&outgoing_packet, &compression_buffer);
553 buffer_clear(&outgoing_packet);
554 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100555 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100556 }
557 /* Compute packet length without padding (add checksum, remove padding). */
558 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559
Damien Millere247cc42000-05-07 12:03:14 +1000560 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100561 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100562 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100563 cp = buffer_ptr(&outgoing_packet);
564 for (i = 0; i < padding; i++) {
565 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000566 rnd = arc4random();
567 cp[7 - i] = rnd & 0xff;
568 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100569 }
570 }
571 buffer_consume(&outgoing_packet, 8 - padding);
572
573 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100574 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000575 buffer_len(&outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100576 put_u32(buf, checksum);
Damien Miller95def091999-11-25 00:26:21 +1100577 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
579#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100580 fprintf(stderr, "packet_send plain: ");
581 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582#endif
583
Damien Miller95def091999-11-25 00:26:21 +1100584 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100585 put_u32(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100586 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100587 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100588 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100589 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100590
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100592 fprintf(stderr, "encrypted: ");
593 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594#endif
595
Damien Miller95def091999-11-25 00:26:21 +1100596 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597
Damien Miller5428f641999-11-25 11:54:57 +1100598 /*
Damien Miller788f2122005-11-05 15:14:59 +1100599 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100600 * actually sent until packet_write_wait or packet_write_poll is
601 * called.
602 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603}
604
Ben Lindstromf6027d32002-03-22 01:42:04 +0000605void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000606set_newkeys(int mode)
607{
608 Enc *enc;
609 Mac *mac;
610 Comp *comp;
611 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000612 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000613 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000614
Ben Lindstrom064496f2002-12-23 02:04:22 +0000615 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000616
Damien Miller963f6b22002-02-19 15:21:23 +1100617 if (mode == MODE_OUT) {
618 cc = &send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000619 crypt_type = CIPHER_ENCRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000620 p_send.packets = p_send.blocks = 0;
621 max_blocks = &max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100622 } else {
623 cc = &receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000624 crypt_type = CIPHER_DECRYPT;
Damien Millera5539d22003-04-09 20:50:06 +1000625 p_read.packets = p_read.blocks = 0;
626 max_blocks = &max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100627 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000628 if (newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000629 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100630 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000631 enc = &newkeys[mode]->enc;
632 mac = &newkeys[mode]->mac;
633 comp = &newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000634 mac_clear(mac);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000635 xfree(enc->name);
636 xfree(enc->iv);
637 xfree(enc->key);
638 xfree(mac->name);
639 xfree(mac->key);
640 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000641 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000642 }
643 newkeys[mode] = kex_get_newkeys(mode);
644 if (newkeys[mode] == NULL)
645 fatal("newkeys: no keys for mode %d", mode);
646 enc = &newkeys[mode]->enc;
647 mac = &newkeys[mode]->mac;
648 comp = &newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000649 if (mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000650 mac->enabled = 1;
651 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100652 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000653 enc->iv, enc->block_size, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000654 /* Deleting the keys does not gain extra security */
655 /* memset(enc->iv, 0, enc->block_size);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000656 memset(enc->key, 0, enc->key_len);
657 memset(mac->key, 0, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000658 if ((comp->type == COMP_ZLIB ||
659 (comp->type == COMP_DELAYED && after_authentication)) &&
660 comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000661 packet_init_compression();
662 if (mode == MODE_OUT)
663 buffer_compress_init_send(6);
664 else
665 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000666 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000667 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000668 /*
669 * The 2^(blocksize*2) limit is too expensive for 3DES,
670 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
671 */
672 if (enc->block_size >= 16)
673 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
674 else
675 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Damien Millera5539d22003-04-09 20:50:06 +1000676 if (rekey_limit)
677 *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000678}
679
Damien Miller5428f641999-11-25 11:54:57 +1100680/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000681 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000682 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000683 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
684 */
685static void
686packet_enable_delayed_compress(void)
687{
688 Comp *comp = NULL;
689 int mode;
690
691 /*
692 * Remember that we are past the authentication step, so rekeying
693 * with COMP_DELAYED will turn on compression immediately.
694 */
695 after_authentication = 1;
696 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000697 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
698 if (newkeys[mode] == NULL)
699 continue;
Damien Miller9786e6e2005-07-26 21:54:56 +1000700 comp = &newkeys[mode]->comp;
701 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000702 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000703 if (mode == MODE_OUT)
704 buffer_compress_init_send(6);
705 else
706 buffer_compress_init_recv();
707 comp->enabled = 1;
708 }
709 }
710}
711
712/*
Damien Miller33b13562000-04-04 14:38:59 +1000713 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
714 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000715static void
Damien Millera5539d22003-04-09 20:50:06 +1000716packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000717{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000718 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100719 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000720 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100721 u_int i, len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000722 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000723 Enc *enc = NULL;
724 Mac *mac = NULL;
725 Comp *comp = NULL;
726 int block_size;
727
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000728 if (newkeys[MODE_OUT] != NULL) {
729 enc = &newkeys[MODE_OUT]->enc;
730 mac = &newkeys[MODE_OUT]->mac;
731 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000732 }
Damien Miller963f6b22002-02-19 15:21:23 +1100733 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000734
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000735 cp = buffer_ptr(&outgoing_packet);
736 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000737
738#ifdef PACKET_DEBUG
739 fprintf(stderr, "plain: ");
740 buffer_dump(&outgoing_packet);
741#endif
742
743 if (comp && comp->enabled) {
744 len = buffer_len(&outgoing_packet);
745 /* skip header, compress only payload */
746 buffer_consume(&outgoing_packet, 5);
747 buffer_clear(&compression_buffer);
748 buffer_compress(&outgoing_packet, &compression_buffer);
749 buffer_clear(&outgoing_packet);
750 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
751 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
752 buffer_len(&compression_buffer));
753 DBG(debug("compression: raw %d compressed %d", len,
754 buffer_len(&outgoing_packet)));
755 }
756
757 /* sizeof (packet_len + pad_len + payload) */
758 len = buffer_len(&outgoing_packet);
759
760 /*
761 * calc size of padding, alloc space, get random data,
762 * minimum padding is 4 bytes
763 */
764 padlen = block_size - (len % block_size);
765 if (padlen < 4)
766 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100767 if (extra_pad) {
768 /* will wrap if extra_pad+padlen > 255 */
769 extra_pad = roundup(extra_pad, block_size);
770 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000771 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100772 pad, len, padlen, extra_pad);
773 padlen += pad;
774 extra_pad = 0;
775 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100776 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100777 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000778 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000779 for (i = 0; i < padlen; i++) {
780 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000781 rnd = arc4random();
782 cp[i] = rnd & 0xff;
783 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000784 }
Damien Millere247cc42000-05-07 12:03:14 +1000785 } else {
786 /* clear padding */
787 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000788 }
789 /* packet_length includes payload, padding and padding length field */
790 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000791 cp = buffer_ptr(&outgoing_packet);
Damien Miller3f941882006-03-31 23:13:02 +1100792 put_u32(cp, packet_length);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000793 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000794 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
795
796 /* compute MAC over seqnr and packet(length fields, payload, padding) */
797 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +1000798 macbuf = mac_compute(mac, p_send.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100799 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000800 buffer_len(&outgoing_packet));
Damien Millera5539d22003-04-09 20:50:06 +1000801 DBG(debug("done calc MAC out #%d", p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000802 }
803 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100804 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100805 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000806 buffer_len(&outgoing_packet));
807 /* append unencrypted MAC */
808 if (mac && mac->enabled)
Damien Millera0fdce92006-03-26 14:28:50 +1100809 buffer_append(&output, macbuf, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +1000810#ifdef PACKET_DEBUG
811 fprintf(stderr, "encrypted: ");
812 buffer_dump(&output);
813#endif
Damien Miller4af51302000-04-16 11:18:38 +1000814 /* increment sequence number for outgoing packets */
Damien Millera5539d22003-04-09 20:50:06 +1000815 if (++p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000816 logit("outgoing seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +1000817 if (++p_send.packets == 0)
818 if (!(datafellows & SSH_BUG_NOREKEY))
819 fatal("XXX too many packets with same key");
820 p_send.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +1000821 buffer_clear(&outgoing_packet);
822
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000823 if (type == SSH2_MSG_NEWKEYS)
824 set_newkeys(MODE_OUT);
Damien Miller9786e6e2005-07-26 21:54:56 +1000825 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side)
826 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000827}
828
Damien Millera5539d22003-04-09 20:50:06 +1000829static void
830packet_send2(void)
831{
832 static int rekeying = 0;
833 struct packet *p;
834 u_char type, *cp;
835
836 cp = buffer_ptr(&outgoing_packet);
837 type = cp[5];
838
839 /* during rekeying we can only send key exchange messages */
840 if (rekeying) {
841 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
842 (type <= SSH2_MSG_TRANSPORT_MAX))) {
843 debug("enqueue packet: %u", type);
844 p = xmalloc(sizeof(*p));
845 p->type = type;
846 memcpy(&p->payload, &outgoing_packet, sizeof(Buffer));
847 buffer_init(&outgoing_packet);
848 TAILQ_INSERT_TAIL(&outgoing, p, next);
849 return;
850 }
851 }
852
853 /* rekeying starts with sending KEXINIT */
854 if (type == SSH2_MSG_KEXINIT)
855 rekeying = 1;
856
857 packet_send2_wrapped();
858
859 /* after a NEWKEYS message we can send the complete queue */
860 if (type == SSH2_MSG_NEWKEYS) {
861 rekeying = 0;
862 while ((p = TAILQ_FIRST(&outgoing))) {
863 type = p->type;
864 debug("dequeue packet: %u", type);
865 buffer_free(&outgoing_packet);
866 memcpy(&outgoing_packet, &p->payload,
867 sizeof(Buffer));
868 TAILQ_REMOVE(&outgoing, p, next);
869 xfree(p);
870 packet_send2_wrapped();
871 }
872 }
873}
874
Damien Miller33b13562000-04-04 14:38:59 +1000875void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000876packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000877{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000878 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000879 packet_send2();
880 else
881 packet_send1();
882 DBG(debug("packet_send done"));
883}
884
Damien Miller33b13562000-04-04 14:38:59 +1000885/*
Damien Miller5428f641999-11-25 11:54:57 +1100886 * Waits until a packet has been received, and returns its type. Note that
887 * no other data is processed until this returns, so this function should not
888 * be used during the interactive session.
889 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890
891int
Damien Millerdff50992002-01-22 23:16:32 +1100892packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893{
Damien Miller95def091999-11-25 00:26:21 +1100894 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000895 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100896 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000897 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898
Damien Miller07d86be2006-03-26 14:19:21 +1100899 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000900 sizeof(fd_mask));
901
Damien Miller95def091999-11-25 00:26:21 +1100902 /* Since we are blocking, ensure that all written packets have been sent. */
903 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904
Damien Miller95def091999-11-25 00:26:21 +1100905 /* Stay in the loop until we have received a complete packet. */
906 for (;;) {
907 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100908 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000909 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000910 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100911 || type == SSH_SMSG_FAILURE
912 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000913 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100914 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100915 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000916 if (type != SSH_MSG_NONE) {
917 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100918 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000919 }
Damien Miller5428f641999-11-25 11:54:57 +1100920 /*
921 * Otherwise, wait for some data to arrive, add it to the
922 * buffer, and try again.
923 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000924 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
925 sizeof(fd_mask));
926 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100927
Damien Miller95def091999-11-25 00:26:21 +1100928 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000929 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000930 (errno == EAGAIN || errno == EINTR))
931 ;
Damien Miller5428f641999-11-25 11:54:57 +1100932
Damien Miller95def091999-11-25 00:26:21 +1100933 /* Read data from the socket. */
934 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100935 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000936 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +1000937 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +1100938 }
Damien Miller95def091999-11-25 00:26:21 +1100939 if (len < 0)
940 fatal("Read from socket failed: %.100s", strerror(errno));
941 /* Append it to the buffer. */
942 packet_process_incoming(buf, len);
943 }
944 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945}
946
Damien Miller278f9072001-12-21 15:00:19 +1100947int
Damien Millerdff50992002-01-22 23:16:32 +1100948packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100949{
Damien Millerdff50992002-01-22 23:16:32 +1100950 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100951}
952
Damien Miller5428f641999-11-25 11:54:57 +1100953/*
954 * Waits until a packet has been received, verifies that its type matches
955 * that given, and gives a fatal error and exits if there is a mismatch.
956 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
958void
Damien Millerdff50992002-01-22 23:16:32 +1100959packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960{
Damien Miller95def091999-11-25 00:26:21 +1100961 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962
Damien Millerdff50992002-01-22 23:16:32 +1100963 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100964 if (type != expected_type)
965 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000966 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967}
968
969/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100970 * packet_process_incoming. If so, reads the packet; otherwise returns
971 * SSH_MSG_NONE. This does not wait for data from the connection.
972 *
Damien Miller5ed3d572008-02-10 22:27:47 +1100973 * SSH_MSG_DISCONNECT is handled specially here. Also,
974 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
975 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100976 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000977
Ben Lindstrombba81212001-06-25 05:01:22 +0000978static int
Damien Millerdff50992002-01-22 23:16:32 +1100979packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000981 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000982 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000983 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000984
Damien Miller95def091999-11-25 00:26:21 +1100985 /* Check if input size is less than minimum packet size. */
986 if (buffer_len(&input) < 4 + 8)
987 return SSH_MSG_NONE;
988 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000989 cp = buffer_ptr(&input);
Damien Miller3f941882006-03-31 23:13:02 +1100990 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100991 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +0000992 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +1100993 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller95def091999-11-25 00:26:21 +1100995 /* Check if the packet has been entirely received. */
996 if (buffer_len(&input) < 4 + padded_len)
997 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998
Damien Miller95def091999-11-25 00:26:21 +1100999 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
Damien Miller95def091999-11-25 00:26:21 +11001001 /* Consume packet length. */
1002 buffer_consume(&input, 4);
1003
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001004 /*
1005 * Cryptographic attack detector for ssh
1006 * (C)1998 CORE-SDI, Buenos Aires Argentina
1007 * Ariel Futoransky(futo@core-sdi.com)
1008 */
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001009 if (!receive_context.plaintext) {
1010 switch (detect_attack(buffer_ptr(&input), padded_len)) {
1011 case DEATTACK_DETECTED:
1012 packet_disconnect("crc32 compensation attack: "
1013 "network attack detected");
1014 case DEATTACK_DOS_DETECTED:
1015 packet_disconnect("deattack denial of "
1016 "service detected");
1017 }
1018 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001019
1020 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +11001021 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001022 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +11001023 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001024
Damien Miller95def091999-11-25 00:26:21 +11001025 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001026
1027#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001028 fprintf(stderr, "read_poll plain: ");
1029 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031
Damien Miller95def091999-11-25 00:26:21 +11001032 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +11001033 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +10001034 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035
Damien Miller95def091999-11-25 00:26:21 +11001036 /* Skip padding. */
1037 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001038
Damien Miller95def091999-11-25 00:26:21 +11001039 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +11001040 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001041 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +10001042 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001044 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001045 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001046 if (checksum != stored_checksum)
1047 packet_disconnect("Corrupted check bytes on input.");
1048 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049
Damien Miller95def091999-11-25 00:26:21 +11001050 if (packet_compression) {
1051 buffer_clear(&compression_buffer);
1052 buffer_uncompress(&incoming_packet, &compression_buffer);
1053 buffer_clear(&incoming_packet);
1054 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +10001055 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001056 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001057 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001058 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1059 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001060 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001061}
Damien Miller95def091999-11-25 00:26:21 +11001062
Ben Lindstrombba81212001-06-25 05:01:22 +00001063static int
Damien Millerdff50992002-01-22 23:16:32 +11001064packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001065{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001066 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001067 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001068 u_char *macbuf, *cp, type;
Damien Millereccb9de2005-06-17 12:59:34 +10001069 u_int maclen, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001070 Enc *enc = NULL;
1071 Mac *mac = NULL;
1072 Comp *comp = NULL;
1073
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001074 if (newkeys[MODE_IN] != NULL) {
1075 enc = &newkeys[MODE_IN]->enc;
1076 mac = &newkeys[MODE_IN]->mac;
1077 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +10001078 }
1079 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001080 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +10001081
1082 if (packet_length == 0) {
1083 /*
1084 * check if input size is less than the cipher block size,
1085 * decrypt first block and extract length of incoming packet
1086 */
1087 if (buffer_len(&input) < block_size)
1088 return SSH_MSG_NONE;
1089 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001090 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +11001091 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +10001092 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001093 cp = buffer_ptr(&incoming_packet);
Damien Miller3f941882006-03-31 23:13:02 +11001094 packet_length = get_u32(cp);
Damien Miller33b13562000-04-04 14:38:59 +10001095 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001096#ifdef PACKET_DEBUG
Damien Miller33b13562000-04-04 14:38:59 +10001097 buffer_dump(&incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001098#endif
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001099 packet_disconnect("Bad packet length %u.", packet_length);
Damien Miller33b13562000-04-04 14:38:59 +10001100 }
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001101 DBG(debug("input: packet len %u", packet_length+4));
Damien Miller33b13562000-04-04 14:38:59 +10001102 buffer_consume(&input, block_size);
1103 }
1104 /* we have a partial packet of block_size bytes */
1105 need = 4 + packet_length - block_size;
1106 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1107 need, maclen));
1108 if (need % block_size != 0)
1109 fatal("padding error: need %d block %d mod %d",
1110 need, block_size, need % block_size);
1111 /*
1112 * check if the entire packet has been received and
1113 * decrypt into incoming_packet
1114 */
1115 if (buffer_len(&input) < need + maclen)
1116 return SSH_MSG_NONE;
1117#ifdef PACKET_DEBUG
1118 fprintf(stderr, "read_poll enc/full: ");
1119 buffer_dump(&input);
1120#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001121 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +11001122 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +10001123 buffer_consume(&input, need);
1124 /*
1125 * compute MAC over seqnr and packet,
1126 * increment sequence number for incoming packet
1127 */
Damien Miller4af51302000-04-16 11:18:38 +10001128 if (mac && mac->enabled) {
Damien Millera5539d22003-04-09 20:50:06 +10001129 macbuf = mac_compute(mac, p_read.seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +11001130 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001131 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +10001132 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +11001133 packet_disconnect("Corrupted MAC on input.");
Damien Millera5539d22003-04-09 20:50:06 +10001134 DBG(debug("MAC #%d ok", p_read.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +10001135 buffer_consume(&input, mac->mac_len);
1136 }
Damien Miller278f9072001-12-21 15:00:19 +11001137 if (seqnr_p != NULL)
Damien Millera5539d22003-04-09 20:50:06 +10001138 *seqnr_p = p_read.seqnr;
1139 if (++p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001140 logit("incoming seqnr wraps around");
Damien Millera5539d22003-04-09 20:50:06 +10001141 if (++p_read.packets == 0)
1142 if (!(datafellows & SSH_BUG_NOREKEY))
1143 fatal("XXX too many packets with same key");
1144 p_read.blocks += (packet_length + 4) / block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001145
1146 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001147 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001148 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001149 DBG(debug("input: padlen %d", padlen));
1150 if (padlen < 4)
1151 packet_disconnect("Corrupted padlen %d on input.", padlen);
1152
1153 /* skip packet size + padlen, discard padding */
1154 buffer_consume(&incoming_packet, 4 + 1);
1155 buffer_consume_end(&incoming_packet, padlen);
1156
1157 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
1158 if (comp && comp->enabled) {
1159 buffer_clear(&compression_buffer);
1160 buffer_uncompress(&incoming_packet, &compression_buffer);
1161 buffer_clear(&incoming_packet);
1162 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1163 buffer_len(&compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001164 DBG(debug("input: len after de-compress %d",
1165 buffer_len(&incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001166 }
1167 /*
1168 * get packet type, implies consume.
1169 * return length of payload (without type field)
1170 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001171 type = buffer_get_char(&incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001172 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1173 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001174 if (type == SSH2_MSG_NEWKEYS)
1175 set_newkeys(MODE_IN);
Damien Miller9786e6e2005-07-26 21:54:56 +10001176 else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side)
1177 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001178#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001179 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +10001180 buffer_dump(&incoming_packet);
1181#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001182 /* reset for next packet */
1183 packet_length = 0;
1184 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001185}
1186
1187int
Damien Millerdff50992002-01-22 23:16:32 +11001188packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001189{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001190 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001191 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001192 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001193
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001194 for (;;) {
1195 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001196 type = packet_read_poll2(seqnr_p);
Damien Miller58226f62008-03-07 18:33:30 +11001197 keep_alive_timeouts = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001198 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001199 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001200 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001201 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001202 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001203 break;
Damien Miller33b13562000-04-04 14:38:59 +10001204 case SSH2_MSG_DEBUG:
1205 packet_get_char();
1206 msg = packet_get_string(NULL);
1207 debug("Remote: %.900s", msg);
1208 xfree(msg);
1209 msg = packet_get_string(NULL);
1210 xfree(msg);
1211 break;
1212 case SSH2_MSG_DISCONNECT:
1213 reason = packet_get_int();
1214 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001215 logit("Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001216 get_remote_ipaddr(), reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001217 xfree(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001218 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001219 break;
Damien Miller659811f2002-01-22 23:23:11 +11001220 case SSH2_MSG_UNIMPLEMENTED:
1221 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001222 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1223 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001224 break;
Damien Miller33b13562000-04-04 14:38:59 +10001225 default:
1226 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001227 }
Damien Miller33b13562000-04-04 14:38:59 +10001228 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001229 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001230 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001231 case SSH_MSG_IGNORE:
1232 break;
1233 case SSH_MSG_DEBUG:
1234 msg = packet_get_string(NULL);
1235 debug("Remote: %.900s", msg);
1236 xfree(msg);
1237 break;
1238 case SSH_MSG_DISCONNECT:
1239 msg = packet_get_string(NULL);
Damien Miller996acd22003-04-09 20:59:48 +10001240 logit("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001241 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001242 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001243 break;
1244 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001245 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001246 DBG(debug("received packet type %d", type));
1247 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001248 }
Damien Miller33b13562000-04-04 14:38:59 +10001249 }
1250 }
1251}
1252
Damien Miller278f9072001-12-21 15:00:19 +11001253int
Damien Millerdff50992002-01-22 23:16:32 +11001254packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001255{
Damien Millerdff50992002-01-22 23:16:32 +11001256 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001257}
1258
Damien Miller5428f641999-11-25 11:54:57 +11001259/*
1260 * Buffers the given amount of input characters. This is intended to be used
1261 * together with packet_read_poll.
1262 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001263
1264void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001265packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266{
Damien Miller95def091999-11-25 00:26:21 +11001267 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268}
1269
1270/* Returns a character from the packet. */
1271
Ben Lindstrom46c16222000-12-22 01:43:59 +00001272u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001273packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001274{
Damien Miller95def091999-11-25 00:26:21 +11001275 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001276
Damien Miller95def091999-11-25 00:26:21 +11001277 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001278 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001279}
1280
1281/* Returns an integer from the packet data. */
1282
Ben Lindstrom46c16222000-12-22 01:43:59 +00001283u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001284packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285{
Damien Miller95def091999-11-25 00:26:21 +11001286 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001287}
1288
Damien Miller5428f641999-11-25 11:54:57 +11001289/*
1290 * Returns an arbitrary precision integer from the packet data. The integer
1291 * must have been initialized before this call.
1292 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293
1294void
Damien Millerd432ccf2002-01-22 23:14:44 +11001295packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296{
Damien Miller76e1e362002-01-22 23:15:57 +11001297 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001298}
1299
Damien Miller33b13562000-04-04 14:38:59 +10001300void
Damien Millerd432ccf2002-01-22 23:14:44 +11001301packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001302{
Damien Miller76e1e362002-01-22 23:15:57 +11001303 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001304}
1305
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001306void *
Damien Millereccb9de2005-06-17 12:59:34 +10001307packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001308{
Damien Millereccb9de2005-06-17 12:59:34 +10001309 u_int bytes = buffer_len(&incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001310
Damien Miller33b13562000-04-04 14:38:59 +10001311 if (length_ptr != NULL)
1312 *length_ptr = bytes;
1313 return buffer_ptr(&incoming_packet);
1314}
1315
Damien Miller4af51302000-04-16 11:18:38 +10001316int
1317packet_remaining(void)
1318{
1319 return buffer_len(&incoming_packet);
1320}
1321
Damien Miller5428f641999-11-25 11:54:57 +11001322/*
1323 * Returns a string from the packet data. The string is allocated using
1324 * xmalloc; it is the responsibility of the calling program to free it when
1325 * no longer needed. The length_ptr argument may be NULL, or point to an
1326 * integer into which the length of the string is stored.
1327 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001328
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001329void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001330packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331{
Damien Miller95def091999-11-25 00:26:21 +11001332 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001333}
1334
Damien Miller5428f641999-11-25 11:54:57 +11001335/*
1336 * Sends a diagnostic message from the server to the client. This message
1337 * can be sent at any time (but not while constructing another message). The
1338 * message is printed immediately, but only if the client is being executed
1339 * in verbose mode. These messages are primarily intended to ease debugging
1340 * authentication problems. The length of the formatted message must not
1341 * exceed 1024 bytes. This will automatically call packet_write_wait.
1342 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343
1344void
Damien Miller95def091999-11-25 00:26:21 +11001345packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346{
Damien Miller95def091999-11-25 00:26:21 +11001347 char buf[1024];
1348 va_list args;
1349
Ben Lindstroma14ee472000-12-07 01:24:58 +00001350 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1351 return;
1352
Damien Miller95def091999-11-25 00:26:21 +11001353 va_start(args, fmt);
1354 vsnprintf(buf, sizeof(buf), fmt, args);
1355 va_end(args);
1356
Damien Miller7c8af4f2000-05-01 08:24:07 +10001357 if (compat20) {
1358 packet_start(SSH2_MSG_DEBUG);
1359 packet_put_char(0); /* bool: always display */
1360 packet_put_cstring(buf);
1361 packet_put_cstring("");
1362 } else {
1363 packet_start(SSH_MSG_DEBUG);
1364 packet_put_cstring(buf);
1365 }
Damien Miller95def091999-11-25 00:26:21 +11001366 packet_send();
1367 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368}
1369
Damien Miller5428f641999-11-25 11:54:57 +11001370/*
1371 * Logs the error plus constructs and sends a disconnect packet, closes the
1372 * connection, and exits. This function never returns. The error message
1373 * should not contain a newline. The length of the formatted message must
1374 * not exceed 1024 bytes.
1375 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001376
1377void
Damien Miller95def091999-11-25 00:26:21 +11001378packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379{
Damien Miller95def091999-11-25 00:26:21 +11001380 char buf[1024];
1381 va_list args;
1382 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001383
Damien Miller95def091999-11-25 00:26:21 +11001384 if (disconnecting) /* Guard against recursive invocations. */
1385 fatal("packet_disconnect called recursively.");
1386 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
Damien Miller5428f641999-11-25 11:54:57 +11001388 /*
1389 * Format the message. Note that the caller must make sure the
1390 * message is of limited size.
1391 */
Damien Miller95def091999-11-25 00:26:21 +11001392 va_start(args, fmt);
1393 vsnprintf(buf, sizeof(buf), fmt, args);
1394 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001395
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001396 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001397 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001398
Damien Miller95def091999-11-25 00:26:21 +11001399 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001400 if (compat20) {
1401 packet_start(SSH2_MSG_DISCONNECT);
1402 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1403 packet_put_cstring(buf);
1404 packet_put_cstring("");
1405 } else {
1406 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001407 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001408 }
Damien Miller95def091999-11-25 00:26:21 +11001409 packet_send();
1410 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001411
Damien Miller95def091999-11-25 00:26:21 +11001412 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001413 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414
Damien Miller95def091999-11-25 00:26:21 +11001415 /* Close the connection. */
1416 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001417 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418}
1419
Damien Miller5428f641999-11-25 11:54:57 +11001420/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001421
1422void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001423packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001424{
Damien Miller95def091999-11-25 00:26:21 +11001425 int len = buffer_len(&output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001426
Damien Miller95def091999-11-25 00:26:21 +11001427 if (len > 0) {
1428 len = write(connection_out, buffer_ptr(&output), len);
1429 if (len <= 0) {
1430 if (errno == EAGAIN)
1431 return;
1432 else
1433 fatal("Write failed: %.100s", strerror(errno));
1434 }
1435 buffer_consume(&output, len);
1436 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437}
1438
Damien Miller5428f641999-11-25 11:54:57 +11001439/*
1440 * Calls packet_write_poll repeatedly until all pending output data has been
1441 * written.
1442 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443
1444void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001445packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001447 fd_set *setp;
1448
Damien Miller07d86be2006-03-26 14:19:21 +11001449 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001450 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001451 packet_write_poll();
1452 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001453 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1454 sizeof(fd_mask));
1455 FD_SET(connection_out, setp);
1456 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001457 (errno == EAGAIN || errno == EINTR))
1458 ;
Damien Miller95def091999-11-25 00:26:21 +11001459 packet_write_poll();
1460 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001461 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462}
1463
1464/* Returns true if there is buffered data to write to the connection. */
1465
1466int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001467packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468{
Damien Miller95def091999-11-25 00:26:21 +11001469 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470}
1471
1472/* Returns true if there is not too much data to write to the connection. */
1473
1474int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001475packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476{
Damien Miller95def091999-11-25 00:26:21 +11001477 if (interactive_mode)
1478 return buffer_len(&output) < 16384;
1479 else
1480 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481}
1482
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001483
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001484static void
Ben Lindstroma7433982002-12-23 02:41:41 +00001485packet_set_tos(int interactive)
1486{
Damien Miller5924ceb2003-11-22 15:02:42 +11001487#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstroma7433982002-12-23 02:41:41 +00001488 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1489
1490 if (!packet_connection_is_on_socket() ||
1491 !packet_connection_is_ipv4())
1492 return;
1493 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos,
1494 sizeof(tos)) < 0)
1495 error("setsockopt IP_TOS %d: %.100s:",
1496 tos, strerror(errno));
Ben Lindstromc8a49d72003-04-02 15:18:22 +00001497#endif
Damien Miller5924ceb2003-11-22 15:02:42 +11001498}
Ben Lindstroma7433982002-12-23 02:41:41 +00001499
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500/* Informs that the current session is interactive. Sets IP flags for that. */
1501
1502void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001503packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001505 static int called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001507 if (called)
1508 return;
1509 called = 1;
1510
Damien Miller95def091999-11-25 00:26:21 +11001511 /* Record that we are in interactive mode. */
1512 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001513
Damien Miller34132e52000-01-14 15:45:46 +11001514 /* Only set socket options if using a socket. */
1515 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001516 return;
Damien Miller314dd4b2006-03-15 12:05:22 +11001517 set_nodelay(connection_in);
Ben Lindstroma7433982002-12-23 02:41:41 +00001518 packet_set_tos(interactive);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001519}
1520
1521/* Returns true if the current connection is interactive. */
1522
1523int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001524packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001525{
Damien Miller95def091999-11-25 00:26:21 +11001526 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001527}
Damien Miller6162d121999-11-21 13:23:52 +11001528
Darren Tucker1f8311c2004-05-13 16:39:33 +10001529int
Darren Tucker502d3842003-06-28 12:38:01 +10001530packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001531{
Damien Miller95def091999-11-25 00:26:21 +11001532 static int called = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001533
Damien Miller95def091999-11-25 00:26:21 +11001534 if (called) {
Damien Miller996acd22003-04-09 20:59:48 +10001535 logit("packet_set_maxsize: called twice: old %d new %d",
Damien Miller33b13562000-04-04 14:38:59 +10001536 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001537 return -1;
1538 }
1539 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001540 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001541 return -1;
1542 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001543 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001544 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001545 max_packet_size = s;
1546 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001547}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001548
Damien Miller9f643902001-11-12 11:02:52 +11001549/* roundup current message to pad bytes */
1550void
1551packet_add_padding(u_char pad)
1552{
1553 extra_pad = pad;
1554}
1555
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001556/*
1557 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001558 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001559 * byte SSH_MSG_IGNORE
1560 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001561 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001562 * All implementations MUST understand (and ignore) this message at any
1563 * time (after receiving the protocol version). No implementation is
1564 * required to send them. This message can be used as an additional
1565 * protection measure against advanced traffic analysis techniques.
1566 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001567void
1568packet_send_ignore(int nbytes)
1569{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001570 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001571 int i;
1572
1573 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001574 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001575 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001576 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001577 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001578 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001579 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001580 }
1581}
Damien Millera5539d22003-04-09 20:50:06 +10001582
Darren Tucker1f8311c2004-05-13 16:39:33 +10001583#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001584int
1585packet_need_rekeying(void)
1586{
1587 if (datafellows & SSH_BUG_NOREKEY)
1588 return 0;
1589 return
1590 (p_send.packets > MAX_PACKETS) ||
1591 (p_read.packets > MAX_PACKETS) ||
1592 (max_blocks_out && (p_send.blocks > max_blocks_out)) ||
1593 (max_blocks_in && (p_read.blocks > max_blocks_in));
1594}
1595
1596void
1597packet_set_rekey_limit(u_int32_t bytes)
1598{
1599 rekey_limit = bytes;
1600}
Damien Miller9786e6e2005-07-26 21:54:56 +10001601
1602void
1603packet_set_server(void)
1604{
1605 server_side = 1;
1606}
1607
1608void
1609packet_set_authenticated(void)
1610{
1611 after_authentication = 1;
1612}