blob: bf8fa549ef87ceb15f454d922b91ca457b971b0f [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains code implementing the packet protocol and communication
6 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100013 *
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * SSH2 packet format added by Markus Friedl.
16 * Copyright (c) 2000 Markus Friedl. All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Ben Lindstrom80c6d772001-06-05 21:09:18 +000040RCSID("$OpenBSD: packet.c,v 1.62 2001/05/28 23:58:35 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "xmalloc.h"
43#include "buffer.h"
44#include "packet.h"
45#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "getput.h"
48
49#include "compress.h"
50#include "deattack.h"
Damien Millerb38eff82000-04-01 11:09:21 +100051#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Miller33b13562000-04-04 14:38:59 +100053#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100055#include "ssh2.h"
56
Damien Miller874d77b2000-10-14 16:23:11 +110057#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100058#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000059#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "log.h"
61#include "canohost.h"
Damien Miller33b13562000-04-04 14:38:59 +100062
63#ifdef PACKET_DEBUG
64#define DBG(x) x
65#else
66#define DBG(x)
67#endif
68
Damien Miller5428f641999-11-25 11:54:57 +110069/*
70 * This variable contains the file descriptors used for communicating with
71 * the other side. connection_in is used for reading; connection_out for
72 * writing. These can be the same descriptor, in which case it is assumed to
73 * be a socket.
74 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075static int connection_in = -1;
76static int connection_out = -1;
77
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000079static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
81/* Encryption context for receiving data. This is only used for decryption. */
82static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110083
84/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085static CipherContext send_context;
86
87/* Buffer for raw input data from the socket. */
88static Buffer input;
89
90/* Buffer for raw output data going to the socket. */
91static Buffer output;
92
93/* Buffer for the partial outgoing packet being constructed. */
94static Buffer outgoing_packet;
95
96/* Buffer for the incoming packet currently being processed. */
97static Buffer incoming_packet;
98
99/* Scratch buffer for packet compression/decompression. */
100static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000101static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
103/* Flag indicating whether packet compression/decompression is enabled. */
104static int packet_compression = 0;
105
Damien Miller6162d121999-11-21 13:23:52 +1100106/* default maximum packet size */
107int max_packet_size = 32768;
108
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109/* Flag indicating whether this module has been initialized. */
110static int initialized = 0;
111
112/* Set to true if the connection is interactive. */
113static int interactive_mode = 0;
114
Damien Miller33b13562000-04-04 14:38:59 +1000115/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000116Newkeys *newkeys[MODE_MAX];
Damien Miller33b13562000-04-04 14:38:59 +1000117
Damien Miller5428f641999-11-25 11:54:57 +1100118/*
119 * Sets the descriptors used for communication. Disables encryption until
120 * packet_set_encryption_key is called.
121 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122void
123packet_set_connection(int fd_in, int fd_out)
124{
Damien Miller874d77b2000-10-14 16:23:11 +1100125 Cipher *none = cipher_by_name("none");
126 if (none == NULL)
127 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100128 connection_in = fd_in;
129 connection_out = fd_out;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000130 cipher_init(&send_context, none, (u_char *) "", 0, NULL, 0);
131 cipher_init(&receive_context, none, (u_char *) "", 0, NULL, 0);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000132 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100133 if (!initialized) {
134 initialized = 1;
135 buffer_init(&input);
136 buffer_init(&output);
137 buffer_init(&outgoing_packet);
138 buffer_init(&incoming_packet);
139 }
140 /* Kludge: arrange the close function to be called from fatal(). */
141 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142}
143
Damien Miller34132e52000-01-14 15:45:46 +1100144/* Returns 1 if remote host is connected via socket, 0 if not. */
145
146int
147packet_connection_is_on_socket()
148{
149 struct sockaddr_storage from, to;
150 socklen_t fromlen, tolen;
151
152 /* filedescriptors in and out are the same, so it's a socket */
153 if (connection_in == connection_out)
154 return 1;
155 fromlen = sizeof(from);
156 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100157 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100158 return 0;
159 tolen = sizeof(to);
160 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100161 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100162 return 0;
163 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
164 return 0;
165 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
166 return 0;
167 return 1;
168}
169
170/* returns 1 if connection is via ipv4 */
171
172int
173packet_connection_is_ipv4()
174{
175 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100176 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100177
178 memset(&to, 0, sizeof(to));
179 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
180 return 0;
181 if (to.ss_family != AF_INET)
182 return 0;
183 return 1;
184}
185
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186/* Sets the connection into non-blocking mode. */
187
188void
189packet_set_nonblocking()
190{
Damien Miller95def091999-11-25 00:26:21 +1100191 /* Set the socket into non-blocking mode. */
192 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
193 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Damien Miller95def091999-11-25 00:26:21 +1100195 if (connection_out != connection_in) {
196 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
197 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
198 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199}
200
201/* Returns the socket used for reading. */
202
203int
204packet_get_connection_in()
205{
Damien Miller95def091999-11-25 00:26:21 +1100206 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207}
208
209/* Returns the descriptor used for writing. */
210
211int
212packet_get_connection_out()
213{
Damien Miller95def091999-11-25 00:26:21 +1100214 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215}
216
217/* Closes the connection and clears and frees internal data structures. */
218
219void
220packet_close()
221{
Damien Miller95def091999-11-25 00:26:21 +1100222 if (!initialized)
223 return;
224 initialized = 0;
225 if (connection_in == connection_out) {
226 shutdown(connection_out, SHUT_RDWR);
227 close(connection_out);
228 } else {
229 close(connection_in);
230 close(connection_out);
231 }
232 buffer_free(&input);
233 buffer_free(&output);
234 buffer_free(&outgoing_packet);
235 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000236 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100237 buffer_free(&compression_buffer);
238 buffer_compress_uninit();
239 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
242/* Sets remote side protocol flags. */
243
244void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000245packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246{
Damien Miller95def091999-11-25 00:26:21 +1100247 remote_protocol_flags = protocol_flags;
248 channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249}
250
251/* Returns the remote protocol flags set earlier by the above function. */
252
Ben Lindstrom46c16222000-12-22 01:43:59 +0000253u_int
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254packet_get_protocol_flags()
255{
Damien Miller95def091999-11-25 00:26:21 +1100256 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257}
258
Damien Miller5428f641999-11-25 11:54:57 +1100259/*
260 * Starts packet compression from the next packet on in both directions.
261 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
262 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000264void
265packet_init_compression()
266{
267 if (compression_buffer_ready == 1)
268 return;
269 compression_buffer_ready = 1;
270 buffer_init(&compression_buffer);
271}
272
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273void
274packet_start_compression(int level)
275{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000276 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100277 fatal("Compression already enabled.");
278 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000279 packet_init_compression();
280 buffer_compress_init_send(level);
281 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282}
283
Damien Miller5428f641999-11-25 11:54:57 +1100284/*
Damien Miller5428f641999-11-25 11:54:57 +1100285 * Causes any further packets to be encrypted using the given key. The same
286 * key is used for both sending and reception. However, both directions are
287 * encrypted independently of each other.
288 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000290packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100291 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292{
Damien Miller874d77b2000-10-14 16:23:11 +1100293 Cipher *cipher = cipher_by_number(number);
294 if (cipher == NULL)
295 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000296 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100297 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
298 cipher_init(&receive_context, cipher, key, keylen, NULL, 0);
299 cipher_init(&send_context, cipher, key, keylen, NULL, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300}
301
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000302/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000304packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000306 u_char buf[9];
307 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Ben Lindstrom204e4882001-03-05 06:47:00 +0000309 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000310 len = compat20 ? 6 : 9;
311 memset(buf, 0, len - 1);
312 buf[len - 1] = type;
313 buffer_clear(&outgoing_packet);
314 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000315}
316
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000317/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318void
319packet_put_char(int value)
320{
Damien Miller95def091999-11-25 00:26:21 +1100321 char ch = value;
322 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000325packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326{
Damien Miller95def091999-11-25 00:26:21 +1100327 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000330packet_put_string(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331{
Damien Miller95def091999-11-25 00:26:21 +1100332 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333}
Damien Miller33b13562000-04-04 14:38:59 +1000334void
335packet_put_cstring(const char *str)
336{
337 buffer_put_string(&outgoing_packet, str, strlen(str));
338}
Damien Miller33b13562000-04-04 14:38:59 +1000339void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000340packet_put_raw(const char *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000341{
342 buffer_append(&outgoing_packet, buf, len);
343}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344void
Damien Miller95def091999-11-25 00:26:21 +1100345packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346{
Damien Miller95def091999-11-25 00:26:21 +1100347 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348}
Damien Miller33b13562000-04-04 14:38:59 +1000349void
350packet_put_bignum2(BIGNUM * value)
351{
352 buffer_put_bignum2(&outgoing_packet, value);
353}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Miller5428f641999-11-25 11:54:57 +1100355/*
356 * Finalizes and sends the packet. If the encryption key has been set,
357 * encrypts the packet before sending.
358 */
Damien Miller95def091999-11-25 00:26:21 +1100359
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000361packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362{
Damien Miller95def091999-11-25 00:26:21 +1100363 char buf[8], *cp;
364 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000365 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100366 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367
Damien Miller5428f641999-11-25 11:54:57 +1100368 /*
369 * If using packet compression, compress the payload of the outgoing
370 * packet.
371 */
Damien Miller95def091999-11-25 00:26:21 +1100372 if (packet_compression) {
373 buffer_clear(&compression_buffer);
374 /* Skip padding. */
375 buffer_consume(&outgoing_packet, 8);
376 /* padding */
377 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
378 buffer_compress(&outgoing_packet, &compression_buffer);
379 buffer_clear(&outgoing_packet);
380 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
381 buffer_len(&compression_buffer));
382 }
383 /* Compute packet length without padding (add checksum, remove padding). */
384 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385
Damien Millere247cc42000-05-07 12:03:14 +1000386 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100387 padding = 8 - len % 8;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000388 if (send_context.cipher->number != SSH_CIPHER_NONE) {
Damien Miller95def091999-11-25 00:26:21 +1100389 cp = buffer_ptr(&outgoing_packet);
390 for (i = 0; i < padding; i++) {
391 if (i % 4 == 0)
392 rand = arc4random();
393 cp[7 - i] = rand & 0xff;
394 rand >>= 8;
395 }
396 }
397 buffer_consume(&outgoing_packet, 8 - padding);
398
399 /* Add check bytes. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000400 checksum = ssh_crc32((u_char *) buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000401 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100402 PUT_32BIT(buf, checksum);
403 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404
405#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100406 fprintf(stderr, "packet_send plain: ");
407 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408#endif
409
Damien Miller95def091999-11-25 00:26:21 +1100410 /* Append to output. */
411 PUT_32BIT(buf, len);
412 buffer_append(&output, buf, 4);
413 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000414 cipher_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller95def091999-11-25 00:26:21 +1100415 buffer_len(&outgoing_packet));
416
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100418 fprintf(stderr, "encrypted: ");
419 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420#endif
421
Damien Miller95def091999-11-25 00:26:21 +1100422 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Damien Miller5428f641999-11-25 11:54:57 +1100424 /*
425 * Note that the packet is now only buffered in output. It won\'t be
426 * actually sent until packet_write_wait or packet_write_poll is
427 * called.
428 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429}
430
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000431void
432set_newkeys(int mode)
433{
434 Enc *enc;
435 Mac *mac;
436 Comp *comp;
437 CipherContext *cc;
438
439 debug("newkeys: mode %d", mode);
440
441 cc = (mode == MODE_OUT) ? &send_context : &receive_context;
442 if (newkeys[mode] != NULL) {
443 debug("newkeys: rekeying");
Ben Lindstrom238abf62001-04-04 17:52:53 +0000444 /* todo: free old keys, reset compression/cipher-ctxt; */
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000445 memset(cc, 0, sizeof(*cc));
446 enc = &newkeys[mode]->enc;
447 mac = &newkeys[mode]->mac;
448 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000449 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000450 xfree(enc->name);
451 xfree(enc->iv);
452 xfree(enc->key);
453 xfree(mac->name);
454 xfree(mac->key);
455 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000456 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000457 }
458 newkeys[mode] = kex_get_newkeys(mode);
459 if (newkeys[mode] == NULL)
460 fatal("newkeys: no keys for mode %d", mode);
461 enc = &newkeys[mode]->enc;
462 mac = &newkeys[mode]->mac;
463 comp = &newkeys[mode]->comp;
464 if (mac->md != NULL)
465 mac->enabled = 1;
466 DBG(debug("cipher_init_context: %d", mode));
467 cipher_init(cc, enc->cipher, enc->key, enc->cipher->key_len,
468 enc->iv, enc->cipher->block_size);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000469 memset(enc->iv, 0, enc->cipher->block_size);
470 memset(enc->key, 0, enc->cipher->key_len);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000471 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000472 packet_init_compression();
473 if (mode == MODE_OUT)
474 buffer_compress_init_send(6);
475 else
476 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000477 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000478 }
479}
480
Damien Miller5428f641999-11-25 11:54:57 +1100481/*
Damien Miller33b13562000-04-04 14:38:59 +1000482 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
483 */
484void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000485packet_send2(void)
Damien Miller33b13562000-04-04 14:38:59 +1000486{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000487 static u_int32_t seqnr = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000488 u_char type, *ucp, *macbuf = NULL;
Damien Miller33b13562000-04-04 14:38:59 +1000489 char *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000490 u_int packet_length = 0;
491 u_int i, padlen, len;
Damien Miller33b13562000-04-04 14:38:59 +1000492 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000493 Enc *enc = NULL;
494 Mac *mac = NULL;
495 Comp *comp = NULL;
496 int block_size;
497
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000498 if (newkeys[MODE_OUT] != NULL) {
499 enc = &newkeys[MODE_OUT]->enc;
500 mac = &newkeys[MODE_OUT]->mac;
501 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000502 }
Damien Miller874d77b2000-10-14 16:23:11 +1100503 block_size = enc ? enc->cipher->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000504
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000505 ucp = (u_char *) buffer_ptr(&outgoing_packet);
506 type = ucp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000507
508#ifdef PACKET_DEBUG
509 fprintf(stderr, "plain: ");
510 buffer_dump(&outgoing_packet);
511#endif
512
513 if (comp && comp->enabled) {
514 len = buffer_len(&outgoing_packet);
515 /* skip header, compress only payload */
516 buffer_consume(&outgoing_packet, 5);
517 buffer_clear(&compression_buffer);
518 buffer_compress(&outgoing_packet, &compression_buffer);
519 buffer_clear(&outgoing_packet);
520 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
521 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
522 buffer_len(&compression_buffer));
523 DBG(debug("compression: raw %d compressed %d", len,
524 buffer_len(&outgoing_packet)));
525 }
526
527 /* sizeof (packet_len + pad_len + payload) */
528 len = buffer_len(&outgoing_packet);
529
530 /*
531 * calc size of padding, alloc space, get random data,
532 * minimum padding is 4 bytes
533 */
534 padlen = block_size - (len % block_size);
535 if (padlen < 4)
536 padlen += block_size;
537 buffer_append_space(&outgoing_packet, &cp, padlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100538 if (enc && enc->cipher->number != SSH_CIPHER_NONE) {
Damien Millere247cc42000-05-07 12:03:14 +1000539 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000540 for (i = 0; i < padlen; i++) {
541 if (i % 4 == 0)
542 rand = arc4random();
543 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000544 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000545 }
Damien Millere247cc42000-05-07 12:03:14 +1000546 } else {
547 /* clear padding */
548 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000549 }
550 /* packet_length includes payload, padding and padding length field */
551 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000552 ucp = (u_char *)buffer_ptr(&outgoing_packet);
553 PUT_32BIT(ucp, packet_length);
554 ucp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000555 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
556
557 /* compute MAC over seqnr and packet(length fields, payload, padding) */
558 if (mac && mac->enabled) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000559 macbuf = mac_compute(mac, seqnr,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000560 (u_char *) buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000561 buffer_len(&outgoing_packet));
Damien Miller874d77b2000-10-14 16:23:11 +1100562 DBG(debug("done calc MAC out #%d", seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000563 }
564 /* encrypt packet and append to output buffer. */
565 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000566 cipher_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000567 buffer_len(&outgoing_packet));
568 /* append unencrypted MAC */
569 if (mac && mac->enabled)
570 buffer_append(&output, (char *)macbuf, mac->mac_len);
571#ifdef PACKET_DEBUG
572 fprintf(stderr, "encrypted: ");
573 buffer_dump(&output);
574#endif
Damien Miller4af51302000-04-16 11:18:38 +1000575 /* increment sequence number for outgoing packets */
576 if (++seqnr == 0)
577 log("outgoing seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000578 buffer_clear(&outgoing_packet);
579
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000580 if (type == SSH2_MSG_NEWKEYS)
581 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000582}
583
584void
585packet_send()
586{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000587 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000588 packet_send2();
589 else
590 packet_send1();
591 DBG(debug("packet_send done"));
592}
593
Damien Miller33b13562000-04-04 14:38:59 +1000594/*
Damien Miller5428f641999-11-25 11:54:57 +1100595 * Waits until a packet has been received, and returns its type. Note that
596 * no other data is processed until this returns, so this function should not
597 * be used during the interactive session.
598 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599
600int
601packet_read(int *payload_len_ptr)
602{
Damien Miller95def091999-11-25 00:26:21 +1100603 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000604 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100605 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000606 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000608 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
609 sizeof(fd_mask));
610
Damien Miller95def091999-11-25 00:26:21 +1100611 /* Since we are blocking, ensure that all written packets have been sent. */
612 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Damien Miller95def091999-11-25 00:26:21 +1100614 /* Stay in the loop until we have received a complete packet. */
615 for (;;) {
616 /* Try to read a packet from the buffer. */
617 type = packet_read_poll(payload_len_ptr);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000618 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000619 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100620 || type == SSH_SMSG_FAILURE
621 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000622 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller95def091999-11-25 00:26:21 +1100623 packet_integrity_check(*payload_len_ptr, 0, type);
624 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000625 if (type != SSH_MSG_NONE) {
626 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100627 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000628 }
Damien Miller5428f641999-11-25 11:54:57 +1100629 /*
630 * Otherwise, wait for some data to arrive, add it to the
631 * buffer, and try again.
632 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000633 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
634 sizeof(fd_mask));
635 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100636
Damien Miller95def091999-11-25 00:26:21 +1100637 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000638 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000639 (errno == EAGAIN || errno == EINTR))
640 ;
Damien Miller5428f641999-11-25 11:54:57 +1100641
Damien Miller95def091999-11-25 00:26:21 +1100642 /* Read data from the socket. */
643 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100644 if (len == 0) {
645 log("Connection closed by %.200s", get_remote_ipaddr());
646 fatal_cleanup();
647 }
Damien Miller95def091999-11-25 00:26:21 +1100648 if (len < 0)
649 fatal("Read from socket failed: %.100s", strerror(errno));
650 /* Append it to the buffer. */
651 packet_process_incoming(buf, len);
652 }
653 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654}
655
Damien Miller5428f641999-11-25 11:54:57 +1100656/*
657 * Waits until a packet has been received, verifies that its type matches
658 * that given, and gives a fatal error and exits if there is a mismatch.
659 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660
661void
662packet_read_expect(int *payload_len_ptr, int expected_type)
663{
Damien Miller95def091999-11-25 00:26:21 +1100664 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665
Damien Miller95def091999-11-25 00:26:21 +1100666 type = packet_read(payload_len_ptr);
667 if (type != expected_type)
668 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000669 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670}
671
672/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100673 * packet_process_incoming. If so, reads the packet; otherwise returns
674 * SSH_MSG_NONE. This does not wait for data from the connection.
675 *
676 * SSH_MSG_DISCONNECT is handled specially here. Also,
677 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
678 * to higher levels.
679 *
680 * The returned payload_len does include space consumed by:
681 * Packet length
682 * Padding
683 * Packet type
684 * Check bytes
685 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686
687int
Damien Miller33b13562000-04-04 14:38:59 +1000688packet_read_poll1(int *payload_len_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000690 u_int len, padded_len;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000691 u_char *ucp, type;
692 char *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000693 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694
Damien Miller95def091999-11-25 00:26:21 +1100695 /* Check if input size is less than minimum packet size. */
696 if (buffer_len(&input) < 4 + 8)
697 return SSH_MSG_NONE;
698 /* Get length of incoming packet. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000699 ucp = (u_char *) buffer_ptr(&input);
Damien Miller95def091999-11-25 00:26:21 +1100700 len = GET_32BIT(ucp);
701 if (len < 1 + 2 + 2 || len > 256 * 1024)
702 packet_disconnect("Bad packet length %d.", len);
703 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704
Damien Miller95def091999-11-25 00:26:21 +1100705 /* Check if the packet has been entirely received. */
706 if (buffer_len(&input) < 4 + padded_len)
707 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708
Damien Miller95def091999-11-25 00:26:21 +1100709 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710
Damien Miller95def091999-11-25 00:26:21 +1100711 /* Consume packet length. */
712 buffer_consume(&input, 4);
713
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000714 /*
715 * Cryptographic attack detector for ssh
716 * (C)1998 CORE-SDI, Buenos Aires Argentina
717 * Ariel Futoransky(futo@core-sdi.com)
718 */
719 if (receive_context.cipher->number != SSH_CIPHER_NONE &&
720 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
721 packet_disconnect("crc32 compensation attack: network attack detected");
722
723 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100724 buffer_clear(&incoming_packet);
725 buffer_append_space(&incoming_packet, &cp, padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000726 cipher_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len);
727
Damien Miller95def091999-11-25 00:26:21 +1100728 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729
730#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100731 fprintf(stderr, "read_poll plain: ");
732 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734
Damien Miller95def091999-11-25 00:26:21 +1100735 /* Compute packet checksum. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000736 checksum = ssh_crc32((u_char *) buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000737 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000738
Damien Miller95def091999-11-25 00:26:21 +1100739 /* Skip padding. */
740 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100741
Damien Miller95def091999-11-25 00:26:21 +1100742 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100743 if (len != buffer_len(&incoming_packet))
744 packet_disconnect("packet_read_poll: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000745 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746
Ben Lindstrom46c16222000-12-22 01:43:59 +0000747 ucp = (u_char *) buffer_ptr(&incoming_packet) + len - 4;
Damien Miller95def091999-11-25 00:26:21 +1100748 stored_checksum = GET_32BIT(ucp);
749 if (checksum != stored_checksum)
750 packet_disconnect("Corrupted check bytes on input.");
751 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752
Damien Miller95def091999-11-25 00:26:21 +1100753 if (packet_compression) {
754 buffer_clear(&compression_buffer);
755 buffer_uncompress(&incoming_packet, &compression_buffer);
756 buffer_clear(&incoming_packet);
757 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000758 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100759 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000760 type = buffer_get_char(&incoming_packet);
Damien Miller95def091999-11-25 00:26:21 +1100761 *payload_len_ptr = buffer_len(&incoming_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000762 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000763}
Damien Miller95def091999-11-25 00:26:21 +1100764
Damien Miller33b13562000-04-04 14:38:59 +1000765int
766packet_read_poll2(int *payload_len_ptr)
767{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000768 static u_int32_t seqnr = 0;
769 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000770 u_int padlen, need;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000771 u_char *macbuf, *ucp, type;
Damien Miller33b13562000-04-04 14:38:59 +1000772 char *cp;
Damien Miller33b13562000-04-04 14:38:59 +1000773 int maclen, block_size;
774 Enc *enc = NULL;
775 Mac *mac = NULL;
776 Comp *comp = NULL;
777
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000778 if (newkeys[MODE_IN] != NULL) {
779 enc = &newkeys[MODE_IN]->enc;
780 mac = &newkeys[MODE_IN]->mac;
781 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000782 }
783 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100784 block_size = enc ? enc->cipher->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000785
786 if (packet_length == 0) {
787 /*
788 * check if input size is less than the cipher block size,
789 * decrypt first block and extract length of incoming packet
790 */
791 if (buffer_len(&input) < block_size)
792 return SSH_MSG_NONE;
793 buffer_clear(&incoming_packet);
794 buffer_append_space(&incoming_packet, &cp, block_size);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000795 cipher_decrypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +1000796 block_size);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000797 ucp = (u_char *) buffer_ptr(&incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000798 packet_length = GET_32BIT(ucp);
799 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
800 buffer_dump(&incoming_packet);
801 packet_disconnect("Bad packet length %d.", packet_length);
802 }
803 DBG(debug("input: packet len %d", packet_length+4));
804 buffer_consume(&input, block_size);
805 }
806 /* we have a partial packet of block_size bytes */
807 need = 4 + packet_length - block_size;
808 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
809 need, maclen));
810 if (need % block_size != 0)
811 fatal("padding error: need %d block %d mod %d",
812 need, block_size, need % block_size);
813 /*
814 * check if the entire packet has been received and
815 * decrypt into incoming_packet
816 */
817 if (buffer_len(&input) < need + maclen)
818 return SSH_MSG_NONE;
819#ifdef PACKET_DEBUG
820 fprintf(stderr, "read_poll enc/full: ");
821 buffer_dump(&input);
822#endif
823 buffer_append_space(&incoming_packet, &cp, need);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000824 cipher_decrypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +1000825 buffer_consume(&input, need);
826 /*
827 * compute MAC over seqnr and packet,
828 * increment sequence number for incoming packet
829 */
Damien Miller4af51302000-04-16 11:18:38 +1000830 if (mac && mac->enabled) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000831 macbuf = mac_compute(mac, seqnr,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000832 (u_char *) buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000833 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000834 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100835 packet_disconnect("Corrupted MAC on input.");
836 DBG(debug("MAC #%d ok", seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000837 buffer_consume(&input, mac->mac_len);
838 }
Damien Miller4af51302000-04-16 11:18:38 +1000839 if (++seqnr == 0)
840 log("incoming seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000841
842 /* get padlen */
843 cp = buffer_ptr(&incoming_packet) + 4;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000844 padlen = (u_char) *cp;
Damien Miller33b13562000-04-04 14:38:59 +1000845 DBG(debug("input: padlen %d", padlen));
846 if (padlen < 4)
847 packet_disconnect("Corrupted padlen %d on input.", padlen);
848
849 /* skip packet size + padlen, discard padding */
850 buffer_consume(&incoming_packet, 4 + 1);
851 buffer_consume_end(&incoming_packet, padlen);
852
853 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
854 if (comp && comp->enabled) {
855 buffer_clear(&compression_buffer);
856 buffer_uncompress(&incoming_packet, &compression_buffer);
857 buffer_clear(&incoming_packet);
858 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
859 buffer_len(&compression_buffer));
860 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
861 }
862 /*
863 * get packet type, implies consume.
864 * return length of payload (without type field)
865 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000866 type = buffer_get_char(&incoming_packet);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000867 if (type == SSH2_MSG_NEWKEYS)
868 set_newkeys(MODE_IN);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000869 *payload_len_ptr = buffer_len(&incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000870#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +0000871 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +1000872 buffer_dump(&incoming_packet);
873#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000874 /* reset for next packet */
875 packet_length = 0;
876 return type;
Damien Miller33b13562000-04-04 14:38:59 +1000877}
878
879int
880packet_read_poll(int *payload_len_ptr)
881{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000882 int reason;
883 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +1000884 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +1000885
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000886 for (;;) {
887 if (compat20) {
888 type = packet_read_poll2(payload_len_ptr);
889 if (type)
Damien Miller33b13562000-04-04 14:38:59 +1000890 DBG(debug("received packet type %d", type));
891 switch(type) {
892 case SSH2_MSG_IGNORE:
893 break;
894 case SSH2_MSG_DEBUG:
895 packet_get_char();
896 msg = packet_get_string(NULL);
897 debug("Remote: %.900s", msg);
898 xfree(msg);
899 msg = packet_get_string(NULL);
900 xfree(msg);
901 break;
902 case SSH2_MSG_DISCONNECT:
903 reason = packet_get_int();
904 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000905 log("Received disconnect from %s: %d: %.400s", get_remote_ipaddr(),
906 reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +1000907 xfree(msg);
908 fatal_cleanup();
909 break;
910 default:
911 return type;
912 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000913 }
Damien Miller33b13562000-04-04 14:38:59 +1000914 } else {
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000915 type = packet_read_poll1(payload_len_ptr);
Damien Miller33b13562000-04-04 14:38:59 +1000916 switch(type) {
917 case SSH_MSG_IGNORE:
918 break;
919 case SSH_MSG_DEBUG:
920 msg = packet_get_string(NULL);
921 debug("Remote: %.900s", msg);
922 xfree(msg);
923 break;
924 case SSH_MSG_DISCONNECT:
925 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000926 log("Received disconnect from %s: %.400s", get_remote_ipaddr(),
927 msg);
Damien Miller33b13562000-04-04 14:38:59 +1000928 fatal_cleanup();
929 xfree(msg);
930 break;
931 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000932 if (type)
Damien Miller33b13562000-04-04 14:38:59 +1000933 DBG(debug("received packet type %d", type));
934 return type;
935 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000936 }
Damien Miller33b13562000-04-04 14:38:59 +1000937 }
938 }
939}
940
Damien Miller5428f641999-11-25 11:54:57 +1100941/*
942 * Buffers the given amount of input characters. This is intended to be used
943 * together with packet_read_poll.
944 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945
946void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000947packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948{
Damien Miller95def091999-11-25 00:26:21 +1100949 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950}
951
952/* Returns a character from the packet. */
953
Ben Lindstrom46c16222000-12-22 01:43:59 +0000954u_int
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955packet_get_char()
956{
Damien Miller95def091999-11-25 00:26:21 +1100957 char ch;
958 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000959 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960}
961
962/* Returns an integer from the packet data. */
963
Ben Lindstrom46c16222000-12-22 01:43:59 +0000964u_int
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965packet_get_int()
966{
Damien Miller95def091999-11-25 00:26:21 +1100967 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968}
969
Damien Miller5428f641999-11-25 11:54:57 +1100970/*
971 * Returns an arbitrary precision integer from the packet data. The integer
972 * must have been initialized before this call.
973 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974
975void
Damien Miller95def091999-11-25 00:26:21 +1100976packet_get_bignum(BIGNUM * value, int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000977{
Damien Miller95def091999-11-25 00:26:21 +1100978 *length_ptr = buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979}
980
Damien Miller33b13562000-04-04 14:38:59 +1000981void
982packet_get_bignum2(BIGNUM * value, int *length_ptr)
983{
984 *length_ptr = buffer_get_bignum2(&incoming_packet, value);
985}
986
987char *
988packet_get_raw(int *length_ptr)
989{
990 int bytes = buffer_len(&incoming_packet);
991 if (length_ptr != NULL)
992 *length_ptr = bytes;
993 return buffer_ptr(&incoming_packet);
994}
995
Damien Miller4af51302000-04-16 11:18:38 +1000996int
997packet_remaining(void)
998{
999 return buffer_len(&incoming_packet);
1000}
1001
Damien Miller5428f641999-11-25 11:54:57 +11001002/*
1003 * Returns a string from the packet data. The string is allocated using
1004 * xmalloc; it is the responsibility of the calling program to free it when
1005 * no longer needed. The length_ptr argument may be NULL, or point to an
1006 * integer into which the length of the string is stored.
1007 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Damien Miller5428f641999-11-25 11:54:57 +11001009char *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001010packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011{
Damien Miller95def091999-11-25 00:26:21 +11001012 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013}
1014
Damien Miller5428f641999-11-25 11:54:57 +11001015/*
1016 * Sends a diagnostic message from the server to the client. This message
1017 * can be sent at any time (but not while constructing another message). The
1018 * message is printed immediately, but only if the client is being executed
1019 * in verbose mode. These messages are primarily intended to ease debugging
1020 * authentication problems. The length of the formatted message must not
1021 * exceed 1024 bytes. This will automatically call packet_write_wait.
1022 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
1024void
Damien Miller95def091999-11-25 00:26:21 +11001025packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001026{
Damien Miller95def091999-11-25 00:26:21 +11001027 char buf[1024];
1028 va_list args;
1029
Ben Lindstroma14ee472000-12-07 01:24:58 +00001030 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1031 return;
1032
Damien Miller95def091999-11-25 00:26:21 +11001033 va_start(args, fmt);
1034 vsnprintf(buf, sizeof(buf), fmt, args);
1035 va_end(args);
1036
Damien Miller7c8af4f2000-05-01 08:24:07 +10001037 if (compat20) {
1038 packet_start(SSH2_MSG_DEBUG);
1039 packet_put_char(0); /* bool: always display */
1040 packet_put_cstring(buf);
1041 packet_put_cstring("");
1042 } else {
1043 packet_start(SSH_MSG_DEBUG);
1044 packet_put_cstring(buf);
1045 }
Damien Miller95def091999-11-25 00:26:21 +11001046 packet_send();
1047 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048}
1049
Damien Miller5428f641999-11-25 11:54:57 +11001050/*
1051 * Logs the error plus constructs and sends a disconnect packet, closes the
1052 * connection, and exits. This function never returns. The error message
1053 * should not contain a newline. The length of the formatted message must
1054 * not exceed 1024 bytes.
1055 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001056
1057void
Damien Miller95def091999-11-25 00:26:21 +11001058packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001059{
Damien Miller95def091999-11-25 00:26:21 +11001060 char buf[1024];
1061 va_list args;
1062 static int disconnecting = 0;
1063 if (disconnecting) /* Guard against recursive invocations. */
1064 fatal("packet_disconnect called recursively.");
1065 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001066
Damien Miller5428f641999-11-25 11:54:57 +11001067 /*
1068 * Format the message. Note that the caller must make sure the
1069 * message is of limited size.
1070 */
Damien Miller95def091999-11-25 00:26:21 +11001071 va_start(args, fmt);
1072 vsnprintf(buf, sizeof(buf), fmt, args);
1073 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074
Damien Miller95def091999-11-25 00:26:21 +11001075 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001076 if (compat20) {
1077 packet_start(SSH2_MSG_DISCONNECT);
1078 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1079 packet_put_cstring(buf);
1080 packet_put_cstring("");
1081 } else {
1082 packet_start(SSH_MSG_DISCONNECT);
1083 packet_put_string(buf, strlen(buf));
1084 }
Damien Miller95def091999-11-25 00:26:21 +11001085 packet_send();
1086 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087
Damien Miller95def091999-11-25 00:26:21 +11001088 /* Stop listening for connections. */
1089 channel_stop_listening();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090
Damien Miller95def091999-11-25 00:26:21 +11001091 /* Close the connection. */
1092 packet_close();
1093
1094 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001095 log("Disconnecting: %.100s", buf);
1096 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097}
1098
Damien Miller5428f641999-11-25 11:54:57 +11001099/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001100
1101void
1102packet_write_poll()
1103{
Damien Miller95def091999-11-25 00:26:21 +11001104 int len = buffer_len(&output);
1105 if (len > 0) {
1106 len = write(connection_out, buffer_ptr(&output), len);
1107 if (len <= 0) {
1108 if (errno == EAGAIN)
1109 return;
1110 else
1111 fatal("Write failed: %.100s", strerror(errno));
1112 }
1113 buffer_consume(&output, len);
1114 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115}
1116
Damien Miller5428f641999-11-25 11:54:57 +11001117/*
1118 * Calls packet_write_poll repeatedly until all pending output data has been
1119 * written.
1120 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121
1122void
1123packet_write_wait()
1124{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001125 fd_set *setp;
1126
1127 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1128 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001129 packet_write_poll();
1130 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001131 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1132 sizeof(fd_mask));
1133 FD_SET(connection_out, setp);
1134 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001135 (errno == EAGAIN || errno == EINTR))
1136 ;
Damien Miller95def091999-11-25 00:26:21 +11001137 packet_write_poll();
1138 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001139 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140}
1141
1142/* Returns true if there is buffered data to write to the connection. */
1143
1144int
1145packet_have_data_to_write()
1146{
Damien Miller95def091999-11-25 00:26:21 +11001147 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001148}
1149
1150/* Returns true if there is not too much data to write to the connection. */
1151
1152int
1153packet_not_very_much_data_to_write()
1154{
Damien Miller95def091999-11-25 00:26:21 +11001155 if (interactive_mode)
1156 return buffer_len(&output) < 16384;
1157 else
1158 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001159}
1160
1161/* Informs that the current session is interactive. Sets IP flags for that. */
1162
1163void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001164packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001166 static int called = 0;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001167#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001168 int lowdelay = IPTOS_LOWDELAY;
1169 int throughput = IPTOS_THROUGHPUT;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001170#endif
Damien Miller95def091999-11-25 00:26:21 +11001171 int on = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001172
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001173 if (called)
1174 return;
1175 called = 1;
1176
Damien Miller95def091999-11-25 00:26:21 +11001177 /* Record that we are in interactive mode. */
1178 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001179
Damien Miller34132e52000-01-14 15:45:46 +11001180 /* Only set socket options if using a socket. */
1181 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001182 return;
Damien Miller34132e52000-01-14 15:45:46 +11001183 /*
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001184 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
Damien Miller34132e52000-01-14 15:45:46 +11001185 */
Damien Miller95def091999-11-25 00:26:21 +11001186 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001187 /*
1188 * Set IP options for an interactive connection. Use
1189 * IPTOS_LOWDELAY and TCP_NODELAY.
1190 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001191#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001192 if (packet_connection_is_ipv4()) {
Kevin Steves0c696152001-01-24 13:47:43 +00001193 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001194 (void *) &lowdelay, sizeof(lowdelay)) < 0)
Kevin Steves0c696152001-01-24 13:47:43 +00001195 error("setsockopt IPTOS_LOWDELAY: %.100s",
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001196 strerror(errno));
1197 }
Damien Miller615f9392000-05-17 22:53:33 +10001198#endif
Damien Miller95def091999-11-25 00:26:21 +11001199 if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on,
Damien Miller34132e52000-01-14 15:45:46 +11001200 sizeof(on)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001201 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001202 } else if (packet_connection_is_ipv4()) {
Damien Miller5428f641999-11-25 11:54:57 +11001203 /*
1204 * Set IP options for a non-interactive connection. Use
1205 * IPTOS_THROUGHPUT.
1206 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001207#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Damien Miller95def091999-11-25 00:26:21 +11001208 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001209 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001210 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001211#endif
Damien Miller95def091999-11-25 00:26:21 +11001212 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213}
1214
1215/* Returns true if the current connection is interactive. */
1216
1217int
1218packet_is_interactive()
1219{
Damien Miller95def091999-11-25 00:26:21 +11001220 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001221}
Damien Miller6162d121999-11-21 13:23:52 +11001222
1223int
1224packet_set_maxsize(int s)
1225{
Damien Miller95def091999-11-25 00:26:21 +11001226 static int called = 0;
1227 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001228 log("packet_set_maxsize: called twice: old %d new %d",
1229 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001230 return -1;
1231 }
1232 if (s < 4 * 1024 || s > 1024 * 1024) {
1233 log("packet_set_maxsize: bad size %d", s);
1234 return -1;
1235 }
1236 log("packet_set_maxsize: setting to %d", s);
1237 max_packet_size = s;
1238 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001239}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001240
1241/*
1242 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001243 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001244 * byte SSH_MSG_IGNORE
1245 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001246 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001247 * All implementations MUST understand (and ignore) this message at any
1248 * time (after receiving the protocol version). No implementation is
1249 * required to send them. This message can be used as an additional
1250 * protection measure against advanced traffic analysis techniques.
1251 */
1252/* size of current + ignore message should be n*sumlen bytes (w/o mac) */
1253void
1254packet_inject_ignore(int sumlen)
1255{
Ben Lindstrome229b252001-03-05 06:28:06 +00001256 int blocksize, padlen, have, need, nb, mini, nbytes;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001257 Enc *enc = NULL;
1258
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001259 if (compat20 == 0)
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001260 return;
1261
1262 have = buffer_len(&outgoing_packet);
1263 debug2("packet_inject_ignore: current %d", have);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001264 if (newkeys[MODE_OUT] != NULL)
1265 enc = &newkeys[MODE_OUT]->enc;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001266 blocksize = enc ? enc->cipher->block_size : 8;
1267 padlen = blocksize - (have % blocksize);
1268 if (padlen < 4)
1269 padlen += blocksize;
1270 have += padlen;
1271 have /= blocksize; /* # of blocks for current message */
1272
1273 nb = roundup(sumlen, blocksize) / blocksize; /* blocks for both */
1274 mini = roundup(5+1+4+4, blocksize) / blocksize; /* minsize ignore msg */
1275 need = nb - (have % nb); /* blocks for ignore */
1276 if (need <= mini)
1277 need += nb;
1278 nbytes = (need - mini) * blocksize; /* size of ignore payload */
1279 debug2("packet_inject_ignore: block %d have %d nb %d mini %d need %d",
1280 blocksize, have, nb, mini, need);
1281
1282 /* enqueue current message and append a ignore message */
1283 packet_send();
Ben Lindstrome229b252001-03-05 06:28:06 +00001284 packet_send_ignore(nbytes);
1285}
1286
1287void
1288packet_send_ignore(int nbytes)
1289{
1290 u_int32_t rand = 0;
1291 int i;
1292
1293 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001294 packet_put_int(nbytes);
1295 for(i = 0; i < nbytes; i++) {
1296 if (i % 4 == 0)
1297 rand = arc4random();
1298 packet_put_char(rand & 0xff);
1299 rand >>= 8;
1300 }
1301}